Skip to content

Commit

Permalink
Fixed data import cannot select multiple attributes in nested entities (
Browse files Browse the repository at this point in the history
#3208)

* data import: show multiple attributes in nested entities

* data import / export all templates include multiple attributes

---------

Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Dec 20, 2023
1 parent 00b08d3 commit 496d75d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 31 deletions.
26 changes: 13 additions & 13 deletions server/modules/dataImport/service/dataImportTemplateService.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ const valuesByNodeDefType = {
},
}

const extractDataImportTemplate = async ({ survey, cycle, entityDefUuid }) => {
const entityDef = Survey.getNodeDefByUuid(entityDefUuid)(survey)
const extractDataImportTemplate = async ({ survey, cycle, nodeDefUuid }) => {
const nodeDef = Survey.getNodeDefByUuid(nodeDefUuid)(survey)
const exportModel = new CsvDataExportModel({
survey,
cycle,
nodeDefContext: entityDef,
nodeDefContext: nodeDef,
options: {
includeAnalysis: false,
includeCategoryItemsLabels: false,
Expand All @@ -56,18 +56,18 @@ const extractDataImportTemplate = async ({ survey, cycle, entityDefUuid }) => {

return {
template,
entityDef,
nodeDef,
}
}

const exportDataImportTemplate = async ({ surveyId, cycle, entityDefUuid, res }) => {
const exportDataImportTemplate = async ({ surveyId, cycle, nodeDefUuid, res }) => {
const survey = await SurveyManager.fetchSurveyAndNodeDefsBySurveyId({ surveyId, cycle })

const { template, entityDef } = await extractDataImportTemplate({ survey, cycle, entityDefUuid })
const { template, nodeDef } = await extractDataImportTemplate({ survey, cycle, nodeDefUuid })

setContentTypeFile({
res,
fileName: `data_import_template_${NodeDef.getName(entityDef)}.csv`,
fileName: `data_import_template_${NodeDef.getName(nodeDef)}.csv`,
contentType: contentTypes.csv,
})

Expand All @@ -85,21 +85,21 @@ const exportAllDataImportTemplates = async ({ surveyId, cycle, res }) => {
contentType: contentTypes.zip,
})

const multipleEntityDefUuids = []
const multipleNodeDefUuids = []

Survey.visitDescendantsAndSelf({
visitorFn: (nodeDef) => {
if (NodeDef.isRoot(nodeDef) || NodeDef.isMultipleEntity(nodeDef)) {
multipleEntityDefUuids.push(NodeDef.getUuid(nodeDef))
if (NodeDef.isRoot(nodeDef) || NodeDef.isMultiple(nodeDef)) {
multipleNodeDefUuids.push(NodeDef.getUuid(nodeDef))
}
},
})(survey)

const tempFilePaths = []

await Promises.each(multipleEntityDefUuids, async (entityDefUuid) => {
const { template, entityDef } = await extractDataImportTemplate({ survey, cycle, entityDefUuid })
const zipEntryName = `data_import_template_${NodeDef.getName(entityDef)}.csv`
await Promises.each(multipleNodeDefUuids, async (nodeDefUuid) => {
const { template, nodeDef } = await extractDataImportTemplate({ survey, cycle, nodeDefUuid })
const zipEntryName = `data_import_template_${NodeDef.getName(nodeDef)}.csv`
const tempFilePath = FileUtils.newTempFilePath()

await CSVWriter.writeItemsToStream({ outputStream: FileUtils.createWriteStream(tempFilePath), items: [template] })
Expand Down
15 changes: 3 additions & 12 deletions server/modules/record/api/recordApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,9 @@ export const init = (app) => {

app.get('/survey/:surveyId/record/importfromcsv/template', requireRecordCreatePermission, async (req, res, next) => {
try {
const { surveyId, entityDefUuid, cycle } = Request.getParams(req)
const { surveyId, nodeDefUuid, cycle } = Request.getParams(req)

await DataImportTemplateService.exportDataImportTemplate({
surveyId,
cycle,
entityDefUuid,
res,
})
await DataImportTemplateService.exportDataImportTemplate({ surveyId, cycle, nodeDefUuid, res })
} catch (error) {
next(error)
}
Expand All @@ -324,11 +319,7 @@ export const init = (app) => {
try {
const { surveyId, cycle } = Request.getParams(req)

await DataImportTemplateService.exportAllDataImportTemplates({
surveyId,
cycle,
res,
})
await DataImportTemplateService.exportAllDataImportTemplates({ surveyId, cycle, res })
} catch (error) {
next(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const EntitySelectorTreeNode = (props) => {
nodeDef={nodeDefChild}
nodeDefLabelType={nodeDefLabelType}
nodeDefUuidActive={nodeDefUuidActive}
onlyEntities={onlyEntities}
onlyPages={onlyPages}
onSelect={onSelect}
/>
Expand Down
7 changes: 2 additions & 5 deletions webapp/service/api/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ export const startDataImportFromArenaJob = async ({ surveyId, cycle, file, onUpl
return job
}

export const getDataImportFromCsvTemplateUrl = ({ surveyId, entityDefUuid, cycle }) => {
const params = new URLSearchParams({
entityDefUuid,
cycle,
})
export const getDataImportFromCsvTemplateUrl = ({ surveyId, nodeDefUuid, cycle }) => {
const params = new URLSearchParams({ nodeDefUuid, cycle })
return `/api/survey/${surveyId}/record/importfromcsv/template?${params.toString()}`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export const DataImportCsvView = () => {
<>
<ButtonDownload
className="download-template-btn"
href={API.getDataImportFromCsvTemplateUrl({ surveyId, cycle, entityDefUuid: selectedNodeDefUuid })}
href={API.getDataImportFromCsvTemplateUrl({ surveyId, cycle, nodeDefUuid: selectedNodeDefUuid })}
label="dataImportView.downloadTemplate"
disabled={!selectedNodeDefUuid}
/>
Expand Down

0 comments on commit 496d75d

Please sign in to comment.