-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
884 additions
and
226 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { ImportFile } from 'api/csv/importFile'; | ||
import { WithId } from 'api/odm'; | ||
import thesauri from 'api/thesauri'; | ||
import { PropertySchema } from 'shared/types/commonTypes'; | ||
import { TemplateSchema } from 'shared/types/templateType'; | ||
import { ThesaurusSchema } from 'shared/types/thesaurusType'; | ||
|
||
import csv, { CSVRow } from './csv'; | ||
import { splitMultiselectLabels } from './typeParsers/multiselect'; | ||
import { normalizeThesaurusLabel } from './typeParsers/select'; | ||
|
||
const filterJSObject = (input: { [k: string]: any }, keys: string[]): { [k: string]: any } => { | ||
const result: { [k: string]: any } = {}; | ||
keys.forEach(k => { | ||
if (input.hasOwnProperty(k)) { | ||
result[k] = input[k]; | ||
} | ||
}); | ||
return result; | ||
}; | ||
|
||
class ArrangeThesauriError extends Error { | ||
source: Error; | ||
row: CSVRow; | ||
index: number; | ||
|
||
constructor(source: Error, row: CSVRow, index: number) { | ||
super(source.message); | ||
this.source = source; | ||
this.row = row; | ||
this.index = index; | ||
} | ||
} | ||
|
||
const createNameToIdMap = ( | ||
thesauriRelatedProperties: PropertySchema[] | undefined, | ||
languages?: string[] | ||
): { [k: string]: string } => { | ||
const nameToThesauriId: { [k: string]: string } = {}; | ||
|
||
thesauriRelatedProperties?.forEach(p => { | ||
if (p.content && p.type) { | ||
const thesarusID = p.content.toString(); | ||
nameToThesauriId[p.name] = thesarusID; | ||
languages?.forEach(suffix => { | ||
nameToThesauriId[`${p.name}__${suffix}`] = thesarusID; | ||
}); | ||
} | ||
}); | ||
|
||
return nameToThesauriId; | ||
}; | ||
|
||
type ThesauriValueData = { | ||
thesauriIdToExistingValues: Map<string, Set<string>>; | ||
thesauriIdToNewValues: Map<string, Set<string>>; | ||
thesauriIdToNormalizedNewValues: Map<string, Set<string>>; | ||
}; | ||
|
||
const setupIdValueMaps = (allRelatedThesauri: WithId<ThesaurusSchema>[]): ThesauriValueData => { | ||
const thesauriIdToExistingValues = new Map(); | ||
const thesauriIdToNewValues = new Map(); | ||
const thesauriIdToNormalizedNewValues = new Map(); | ||
|
||
allRelatedThesauri.forEach(t => { | ||
const id = t._id.toString(); | ||
thesauriIdToExistingValues.set( | ||
id, | ||
new Set(t.values?.map(v => normalizeThesaurusLabel(v.label))) | ||
); | ||
thesauriIdToNewValues.set(id, new Set()); | ||
thesauriIdToNormalizedNewValues.set(id, new Set()); | ||
}); | ||
|
||
return { thesauriIdToExistingValues, thesauriIdToNewValues, thesauriIdToNormalizedNewValues }; | ||
}; | ||
|
||
const syncSaveThesauri = async ( | ||
allRelatedThesauri: WithId<ThesaurusSchema>[], | ||
thesauriIdToNewValues: Map<string, Set<string>> | ||
) => { | ||
const thesauriWithNewValues = allRelatedThesauri.filter( | ||
t => (thesauriIdToNewValues.get(t._id.toString()) || new Set()).size > 0 | ||
); | ||
for (let i = 0; i < thesauriWithNewValues.length; i += 1) { | ||
const thesaurus = allRelatedThesauri[i]; | ||
const newValues = Array.from( | ||
thesauriIdToNewValues.get(thesaurus._id.toString()) || [] | ||
).map(tval => ({ label: tval })); | ||
const thesaurusValues = thesaurus.values || []; | ||
// eslint-disable-next-line no-await-in-loop | ||
await thesauri.save({ | ||
...thesaurus, | ||
values: thesaurusValues.concat(newValues), | ||
}); | ||
} | ||
}; | ||
|
||
const arrangeThesauri = async ( | ||
file: ImportFile, | ||
template: TemplateSchema, | ||
languages?: string[], | ||
stopOnError: boolean = true | ||
) => { | ||
const thesauriRelatedProperties = template.properties?.filter(p => | ||
['select', 'multiselect'].includes(p.type) | ||
); | ||
|
||
const nameToThesauriId = createNameToIdMap(thesauriRelatedProperties, languages); | ||
|
||
const allRelatedThesauri = await thesauri.get({ | ||
$in: Array.from( | ||
new Set(thesauriRelatedProperties?.map(p => p.content?.toString()).filter(t => t)) | ||
), | ||
}); | ||
|
||
const thesauriValueData = setupIdValueMaps(allRelatedThesauri); | ||
|
||
await csv(await file.readStream(), stopOnError) | ||
.onRow(async (row: CSVRow) => { | ||
Object.entries(filterJSObject(nameToThesauriId, Object.keys(row))).forEach(([name, id]) => { | ||
const labels = splitMultiselectLabels(row[name]); | ||
Object.entries(labels).forEach(([normalizedLabel, originalLabel]) => { | ||
if ( | ||
!thesauriValueData.thesauriIdToExistingValues.get(id)?.has(normalizedLabel) && | ||
!thesauriValueData.thesauriIdToNormalizedNewValues.get(id)?.has(normalizedLabel) | ||
) { | ||
thesauriValueData.thesauriIdToNewValues.get(id)?.add(originalLabel); | ||
thesauriValueData.thesauriIdToNormalizedNewValues.get(id)?.add(normalizedLabel); | ||
} | ||
}); | ||
}); | ||
}) | ||
.onError(async (e: Error, row: CSVRow, index: number) => { | ||
throw new ArrangeThesauriError(e, row, index); | ||
}) | ||
.read(); | ||
|
||
await syncSaveThesauri(allRelatedThesauri, thesauriValueData.thesauriIdToNewValues); | ||
}; | ||
|
||
export { arrangeThesauri, ArrangeThesauriError }; |
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,17 @@ | ||
title,unrelated_property,select_property__en, select_property__es,multiselect_property__en, multiselect_property__es | ||
select_1,unrelated_text,B,Bes,A,Aes | ||
select_2,unrelated_text,C,Ces,A,Aes | ||
select_3,unrelated_text,b,bes,A,Aes | ||
select_4,unrelated_text,B,Bes,A,Aes | ||
select_5,unrelated_text,d,des,A,Aes | ||
select_6,unrelated_text,D,Des,A,Aes | ||
select_7,unrelated_text, b,bes,A,Aes | ||
select_8,unrelated_text, , ,A,Aes | ||
select_8,unrelated_text, , ,A,Aes | ||
multiselect_1,unrelated_text,A,Aes,B,Bes | ||
multiselect_2,unrelated_text,A,Aes,c,ces | ||
multiselect_3,unrelated_text,A,Aes,A|b,Aes|bes | ||
multiselect_4,unrelated_text,A,Aes,a|B|C,aes|Bes|Ces | ||
multiselect_5,unrelated_text,A,Aes, a| b | , aes| bes | | ||
multiselect_6,unrelated_text,A,Aes, | | , | | | ||
multiselect_7, unrelated_text,A,Aes,A|B|C|D| |E| e| g ,Aes|Bes|Ces|Des| |Ees| ees| ges |
Oops, something went wrong.