From 94362235e05fa51790e7fecf56ec06f42effb977 Mon Sep 17 00:00:00 2001 From: Steve Grosbois Date: Wed, 3 Jan 2024 16:57:16 +0100 Subject: [PATCH] fix(import): Also create empty values for other languages of imported keys --- src/projects/projects.service.ts | 24 ++++++++++------ tests/languages/languages.e2e-spec.ts | 41 ++++++++++++++++----------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/projects/projects.service.ts b/src/projects/projects.service.ts index b0cdebb..1929fc4 100644 --- a/src/projects/projects.service.ts +++ b/src/projects/projects.service.ts @@ -193,6 +193,9 @@ export default class ProjectsService { language.name = createLanguageDto.name; language.project = project; const createdLanguage = await this.languagesRepository.save(language); + const allLanguages = await this.languagesRepository.findBy({ + projectId: projectId + }); const createdKeyIds = []; @@ -231,17 +234,20 @@ export default class ProjectsService { // We check if the key has values if (keyToCreate.values && keyToCreate.values.length > 0) { await Promise.all(keyToCreate.values.map(async (valueToCreate) => { + if (valueToCreate.languageName != createdLanguage.name) return; // We check if values already exists const values = await this.valueRepository.findBy({keyId: key.id}); - // If not, we create it - if (!values || values.length === 0) { - const value = new TranslationValue(); - value.keyId = key.id; - value.name = valueToCreate.name; - value.languageId = createdLanguage.id; - value.quantityString = valueToCreate.quantityString; - await this.valueRepository.save(value); - } + allLanguages.forEach(language => { + const valueExists = values.find(value => value.languageId === language.id); + if (!valueExists) { + const value = new TranslationValue(); + value.keyId = key.id; + value.name = language.id === createdLanguage.id ? valueToCreate.name : ""; // Empty if the language is not the one we are creating + value.languageId = language.id; + value.quantityString = valueToCreate.quantityString; + this.valueRepository.save(value); + } + }); })); } })); diff --git a/tests/languages/languages.e2e-spec.ts b/tests/languages/languages.e2e-spec.ts index f809143..7edaed0 100644 --- a/tests/languages/languages.e2e-spec.ts +++ b/tests/languages/languages.e2e-spec.ts @@ -18,6 +18,7 @@ import TranslationKey from "../../src/translation/translation_key.entity"; import Group, {DefaultGroupName} from "../../src/groups/group.entity"; import CreateGroupDto from "../../src/groups/dto/create-group.dto"; import CreateKeyDto from "../../src/translation/dto/create-key.dto"; +import CreateValueDto from "../../src/translation/dto/create-value.dto"; describe("Languages of a project E2E", () => { let app: INestApplication; @@ -172,9 +173,10 @@ describe("Languages of a project E2E", () => { name: "singular_key", isPlural: false, values: [ - new CreateGroupDto({ + new CreateValueDto({ name: "singular_key_fr", - quantityString: null + quantityString: null, + languageName: "fr" }) ], }), @@ -182,15 +184,18 @@ describe("Languages of a project E2E", () => { name: "plural_key", isPlural: true, values: [ - new CreateGroupDto({ + new CreateValueDto({ name: "plural_key_fr_zero", - quantityString: "zero" - }), new CreateGroupDto({ + quantityString: "zero", + languageName: "fr" + }), new CreateValueDto({ name: "plural_key_fr_one", - quantityString: "one" - }), new CreateGroupDto({ + quantityString: "one", + languageName: "fr" + }), new CreateValueDto({ name: "plural_key_fr_other", - quantityString: "other" + quantityString: "other", + languageName: "fr" }) ], }) @@ -204,9 +209,10 @@ describe("Languages of a project E2E", () => { name: "singular_key", isPlural: false, values: [ - new CreateGroupDto({ + new CreateValueDto({ name: "singular_key_fr", - quantityString: null + quantityString: null, + languageName: "fr" }) ], }), @@ -214,15 +220,18 @@ describe("Languages of a project E2E", () => { name: "plural_key", isPlural: true, values: [ - new CreateGroupDto({ + new CreateValueDto({ name: "plural_key_fr_zero", - quantityString: "zero" - }), new CreateGroupDto({ + quantityString: "zero", + languageName: "fr" + }), new CreateValueDto({ name: "plural_key_fr_one", - quantityString: "one" - }), new CreateGroupDto({ + quantityString: "one", + languageName: "fr" + }), new CreateValueDto({ name: "plural_key_fr_other", - quantityString: "other" + quantityString: "other", + languageName: "fr" }) ], })