From 73be4e41190b0b60aa0fe0645fe35ca00323e7d2 Mon Sep 17 00:00:00 2001 From: Olivia Guyot Date: Tue, 9 Apr 2024 17:25:46 +0200 Subject: [PATCH] feat(md-converter): improve keywords grouping --- .../src/lib/iso19139/write-parts.spec.ts | 87 +++++++++++++++++++ .../src/lib/iso19139/write-parts.ts | 7 +- 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/libs/api/metadata-converter/src/lib/iso19139/write-parts.spec.ts b/libs/api/metadata-converter/src/lib/iso19139/write-parts.spec.ts index 9755306808..12b4cd1009 100644 --- a/libs/api/metadata-converter/src/lib/iso19139/write-parts.spec.ts +++ b/libs/api/metadata-converter/src/lib/iso19139/write-parts.spec.ts @@ -422,6 +422,93 @@ describe('write parts', () => { +`) + }) + + it('correctly adds a thesaurus to an existing keyword', () => { + // add some distributions first + const sample = parseXmlString(` + + + + + + + Usage des sols + + + Agriculture + + + + + + + + +`) + rootEl = getRootElement(sample) + writeKeywords( + { + ...datasetRecord, + keywords: [ + { + label: 'Usage des sols', + type: 'theme', + }, + { + label: 'Agriculture', + type: 'theme', + thesaurus: { + id: 'abcd', + url: new URL('http://abcd.com'), + name: 'A thesaurus', + }, + }, + ], + }, + rootEl + ) + expect(rootAsString()).toEqual(` + + + + + + + + + Usage des sols + + + + + + + + + + + + A thesaurus + + + + + abcd + + + + + + + Agriculture + + + + + `) }) }) diff --git a/libs/api/metadata-converter/src/lib/iso19139/write-parts.ts b/libs/api/metadata-converter/src/lib/iso19139/write-parts.ts index 2d629eab25..f9f40ba562 100644 --- a/libs/api/metadata-converter/src/lib/iso19139/write-parts.ts +++ b/libs/api/metadata-converter/src/lib/iso19139/write-parts.ts @@ -398,13 +398,14 @@ export function createThesaurus(thesaurus: KeywordThesaurus) { } export function appendKeywords(keywords: Keyword[]) { + // keywords are grouped by thesaurus if they have one, otherwise by type const keywordsByThesaurus: Keyword[][] = keywords.reduce((acc, keyword) => { const thesaurusId = keyword.thesaurus?.id const type = keyword.type let existingGroup = acc.find((group) => - group[0].thesaurus - ? group[0].thesaurus.id === thesaurusId - : group[0].type === type + thesaurusId + ? group[0].thesaurus?.id === thesaurusId + : group[0].type === type && !group[0].thesaurus ) if (!existingGroup) { existingGroup = []