Skip to content

Commit

Permalink
fix(import): Also create empty values for other languages of imported…
Browse files Browse the repository at this point in the history
… keys
  • Loading branch information
kwiky committed Jan 3, 2024
1 parent ae41b8f commit 9436223
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
24 changes: 15 additions & 9 deletions src/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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);
}
});
}));
}
}));
Expand Down
41 changes: 25 additions & 16 deletions tests/languages/languages.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -172,25 +173,29 @@ 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"
})
],
}),
new CreateKeyDto({
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"
})
],
})
Expand All @@ -204,25 +209,29 @@ 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"
})
],
}),
new CreateKeyDto({
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"
})
],
})
Expand Down

0 comments on commit 9436223

Please sign in to comment.