diff --git a/src/projects/dto/create-language.dto.ts b/src/projects/dto/create-language.dto.ts index 3a354f3..acd97cb 100644 --- a/src/projects/dto/create-language.dto.ts +++ b/src/projects/dto/create-language.dto.ts @@ -11,9 +11,14 @@ export default class CreateLanguageDto extends BaseDto { groups: Joi .array() + .optional(), + + replaceExistingKeys: Joi + .boolean() .optional() }); public name: string; public groups: CreateGroupDto[]; + public replaceExistingKeys: boolean; } diff --git a/src/projects/projects.service.ts b/src/projects/projects.service.ts index 496c84a..4cae77d 100644 --- a/src/projects/projects.service.ts +++ b/src/projects/projects.service.ts @@ -192,13 +192,16 @@ export default class ProjectsService { const language = new Language(); language.name = createLanguageDto.name; language.project = project; - const createdLanguage = await this.languagesRepository.save(language); + + let createdLanguage = await this.languagesRepository.findOneBy({name: language.name, projectId: project.id}); + if (!createdLanguage) { + createdLanguage = await this.languagesRepository.save(language); + } + const allLanguages = await this.languagesRepository.findBy({ projectId: projectId }); - const createdKeyIds = []; - // Find all translation keys of the project in order to create the translation values for the new language const projectKeys: TranslationKey[] = await this.keyRepository.findBy({projectId: projectId}); @@ -228,7 +231,6 @@ export default class ProjectsService { key.name = keyToCreate.name; key.isPlural = keyToCreate.isPlural; key = await this.keyRepository.save(key); - createdKeyIds.push(key.id); } // We check if the key has values @@ -239,7 +241,9 @@ export default class ProjectsService { const values = await this.valueRepository.findBy({keyId: key.id}); allLanguages.forEach(language => { const valueExists = values.find(value => value.languageId === language.id); - if (!valueExists) { + // If this is the language we are importing and replaceExistingKeys is true, we replace the value + const shouldReplaceValue = language.id === createdLanguage.id && createLanguageDto.replaceExistingKeys; + if (!valueExists || shouldReplaceValue) { 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 @@ -267,14 +271,14 @@ export default class ProjectsService { value.name = ""; value.keyId = key.id; value.quantityString = quantity; - value.languageId = language.id; + value.languageId = createdLanguage.id; return await this.valueRepository.save(value); })); } else { const value = new TranslationValue(); value.name = ""; value.keyId = key.id; - value.languageId = language.id; + value.languageId = createdLanguage.id; return await this.valueRepository.save(value); } diff --git a/tests/languages/languages.e2e-spec.ts b/tests/languages/languages.e2e-spec.ts index 7edaed0..914a928 100644 --- a/tests/languages/languages.e2e-spec.ts +++ b/tests/languages/languages.e2e-spec.ts @@ -273,7 +273,10 @@ describe("Languages of a project E2E", () => { .auth("mocked.jwt", {type: "bearer"}) .set("mocked_user_id", TestsHelpers.MOCKED_USER_ID_1) .send({name: "language"}); - expect(duplicatedLanguageResp.status).toEqual(422); + expect(duplicatedLanguageResp.status).toEqual(201); + + // The language should be the same as the first one + expect(duplicatedLanguageResp.body.id).toEqual(createLanguageResp.body.id); }); it("Creating a project from the API", async () => {