-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* survey labels import (WIP) * labels import job * fixed labels import job server side * added FileUploadDialog * code cleanup * reset survey defs on job complete * reset survey defs on job complete * fixing tests * survey labels export --------- Co-authored-by: Stefano Ricci <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
db01e00
commit ac4c464
Showing
30 changed files
with
541 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as Survey from '@core/survey/survey' | ||
import * as NodeDef from '@core/survey/nodeDef' | ||
|
||
import * as CSVWriter from '@server/utils/file/csvWriter' | ||
import * as SurveyManager from '@server/modules/survey/manager/surveyManager' | ||
|
||
const exportLabels = async ({ surveyId, outputStream }) => { | ||
const survey = await SurveyManager.fetchSurveyAndNodeDefsBySurveyId({ surveyId, draft: true, includeAnalysis: false }) | ||
const languages = Survey.getLanguages(Survey.getSurveyInfo(survey)) | ||
const items = [] | ||
Survey.visitDescendantsAndSelf({ | ||
nodeDef: Survey.getNodeDefRoot(survey), | ||
visitorFn: (nodeDef) => { | ||
items.push({ | ||
name: NodeDef.getName(nodeDef), | ||
// labels | ||
...languages.reduce( | ||
(labelsAcc, lang) => ({ ...labelsAcc, [`label_${lang}`]: NodeDef.getLabel(nodeDef, lang) }), | ||
{} | ||
), | ||
// description | ||
...languages.reduce( | ||
(labelsAcc, lang) => ({ ...labelsAcc, [`description_${lang}`]: NodeDef.getDescription(lang)(nodeDef) }), | ||
{} | ||
), | ||
}) | ||
}, | ||
})(survey) | ||
|
||
await CSVWriter.writeItemsToStream({ outputStream, items, options: { removeNewLines: false } }) | ||
} | ||
|
||
export const SurveyLabelsExport = { | ||
exportLabels, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const labelColumnPrefix = 'label_' | ||
const descriptionColumnPrefix = 'description_' | ||
const getLabelColumn = (langCode) => `${labelColumnPrefix}${langCode}` | ||
const getDescriptionColumn = (langCode) => `${descriptionColumnPrefix}${langCode}` | ||
|
||
export const SurveyLabelsExportModel = { | ||
labelColumnPrefix, | ||
descriptionColumnPrefix, | ||
getLabelColumn, | ||
getDescriptionColumn, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import * as A from '@core/arena' | ||
import * as Survey from '@core/survey/survey' | ||
import * as NodeDef from '@core/survey/nodeDef' | ||
import * as StringUtils from '@core/stringUtils' | ||
|
||
import Job from '@server/job/job' | ||
import * as CSVReader from '@server/utils/file/csvReader' | ||
import * as SurveyManager from '@server/modules/survey/manager/surveyManager' | ||
import * as NodeDefManager from '@server/modules/nodeDef/manager/nodeDefManager' | ||
import { SurveyLabelsExportModel } from './surveyLabelsExportModel' | ||
|
||
const errorPrefix = `validationErrors.surveyLabelsImport.` | ||
|
||
const extractLabels = ({ row, langCodes, columnPrefix }) => | ||
langCodes.reduce((acc, langCode) => { | ||
const value = row[`${columnPrefix}${langCode}`] | ||
const label = StringUtils.trim(value) | ||
if (StringUtils.isNotBlank(label)) { | ||
acc[langCode] = label | ||
} | ||
return acc | ||
}, {}) | ||
|
||
export default class SurveyLabelsImportJob extends Job { | ||
constructor(params) { | ||
super(SurveyLabelsImportJob.type, params) | ||
this.validateHeaders = this.validateHeaders.bind(this) | ||
} | ||
|
||
async execute() { | ||
const { context, tx } = this | ||
const { filePath, surveyId } = context | ||
const survey = await SurveyManager.fetchSurveyAndNodeDefsBySurveyId({ surveyId, draft: true }, tx) | ||
this.setContext({ survey }) | ||
const langCodes = Survey.getLanguages(Survey.getSurveyInfo(survey)) | ||
|
||
const nodeDefsUpdated = [] | ||
|
||
this.csvReader = CSVReader.createReaderFromFile( | ||
filePath, | ||
this.validateHeaders, | ||
async (row) => { | ||
const nodeDef = await this.getNodeDef({ survey, row }) | ||
if (!nodeDef) return | ||
|
||
const labels = extractLabels({ langCodes, row, columnPrefix: SurveyLabelsExportModel.labelColumnPrefix }) | ||
const descriptions = extractLabels({ | ||
langCodes, | ||
row, | ||
columnPrefix: SurveyLabelsExportModel.descriptionColumnPrefix, | ||
}) | ||
|
||
const nodeDefUpdated = A.pipe(NodeDef.assocLabels(labels), NodeDef.assocDescriptions(descriptions))(nodeDef) | ||
nodeDefsUpdated.push(nodeDefUpdated) | ||
|
||
this.incrementProcessedItems() | ||
}, | ||
(total) => (this.total = total) | ||
) | ||
|
||
await this.csvReader.start() | ||
|
||
if (nodeDefsUpdated.length > 0) { | ||
await NodeDefManager.updateNodeDefPropsInBatch( | ||
{ | ||
surveyId, | ||
nodeDefs: nodeDefsUpdated.map((nodeDefUpdated) => ({ | ||
nodeDefUuid: NodeDef.getUuid(nodeDefUpdated), | ||
props: NodeDef.getProps(nodeDefUpdated), | ||
})), | ||
}, | ||
tx | ||
) | ||
} | ||
} | ||
|
||
async validateHeaders(headers) { | ||
const { context } = this | ||
const { survey } = context | ||
const langCodes = Survey.getLanguages(Survey.getSurveyInfo(survey)) | ||
|
||
const fixedHeaders = ['name'] | ||
const dynamicHeaders = [] | ||
langCodes.forEach((langCode) => { | ||
dynamicHeaders.push( | ||
SurveyLabelsExportModel.getLabelColumn(langCode), | ||
SurveyLabelsExportModel.getDescriptionColumn(langCode) | ||
) | ||
}) | ||
const validHeaders = [...fixedHeaders, ...dynamicHeaders] | ||
const invalidHeaders = headers.filter((header) => !validHeaders.includes(header)).join(', ') | ||
if (invalidHeaders) { | ||
await this.addErrorAndStopCsvReader('invalidHeaders', { invalidHeaders }) | ||
} | ||
} | ||
|
||
async getNodeDef({ row }) { | ||
const { context } = this | ||
const { survey } = context | ||
const { name } = row | ||
let nodeDef = null | ||
if (name) { | ||
nodeDef = Survey.getNodeDefByName(name)(survey) | ||
} | ||
if (!nodeDef) { | ||
await this.addErrorAndStopCsvReader('cannotFindNodeDef', { name }) | ||
} | ||
return nodeDef | ||
} | ||
|
||
async addErrorAndStopCsvReader(key, params) { | ||
this.addError({ | ||
error: { | ||
valid: false, | ||
errors: [{ key: `${errorPrefix}${key}`, params }], | ||
}, | ||
}) | ||
this.csvReader.cancel() | ||
await this.setStatusFailed() | ||
} | ||
} | ||
|
||
SurveyLabelsImportJob.type = 'SurveyLabelsImportJob' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.