Skip to content

Commit

Permalink
feat(import): We can import language multiple times and added an opti…
Browse files Browse the repository at this point in the history
…on to allow replacing values
  • Loading branch information
kwiky committed Jan 11, 2024
1 parent ec1b9c7 commit aaf9ca2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/projects/dto/create-language.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
18 changes: 11 additions & 7 deletions src/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/languages/languages.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit aaf9ca2

Please sign in to comment.