From 4e9babe1456990902255672a2b625f8c69629c9f Mon Sep 17 00:00:00 2001 From: Matthieu Fesselier Date: Tue, 10 Dec 2024 14:46:17 +0100 Subject: [PATCH] fix(server): :bug: remove administrationName from added sections if null --- apps/server/src/typegoose/Traductions.test.ts | 28 +++++++++++++++++++ apps/server/src/typegoose/Traductions.ts | 4 ++- .../1733826190794_RemoveStuckedToReview.ts | 22 +++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 migrations/1733826190794_RemoveStuckedToReview.ts diff --git a/apps/server/src/typegoose/Traductions.test.ts b/apps/server/src/typegoose/Traductions.test.ts index 08d3bc2ff0..318f4e1ebc 100644 --- a/apps/server/src/typegoose/Traductions.test.ts +++ b/apps/server/src/typegoose/Traductions.test.ts @@ -120,6 +120,25 @@ const trad_avancement: RecursivePartial = { validatorId: new ObjectId("656076dbaf8df7a3f7bceeb4"), }; +const trad_added_adminName: TranslationContent = { + //@ts-ignore + content: { + titreInformatif: "abc", + titreMarque: "def", + abstract: "tyui", + what: "WHAT", + how: { "my-uuid-v4-key": { title: "title", text: "text" } }, + next: { + "my-uuid-v4-key": { title: "title", text: "text" }, + "my-uuid-v4-key-2": { title: "title", text: "text" }, + "my-uuid-v4-key-3": { title: "title", text: "text" }, + }, + }, + + created_at: new Date(), + validatorId: new ObjectId("656076dbaf8df7a3f7bceeb4"), +}; + describe("Traductions", () => { describe("diff", () => { it("should return empty array", () => { @@ -153,6 +172,15 @@ describe("Traductions", () => { removed: ["content.next.my-uuid-v4-key.title", "content.next.my-uuid-v4-key.text"], }); }); + it("should not return administrationName ", () => { + const newTradAdded = JSON.parse(JSON.stringify(trad_added_adminName)); + newTradAdded.content = { ...newTradAdded.content, administrationName: null }; + expect(Traductions.diff(trad_added_adminName, newTradAdded)).toEqual({ + modified: [], + added: [], + removed: [], + }); + }); }); describe("computeFinished", () => { diff --git a/apps/server/src/typegoose/Traductions.ts b/apps/server/src/typegoose/Traductions.ts index 4e50cb1b1a..21b0813714 100644 --- a/apps/server/src/typegoose/Traductions.ts +++ b/apps/server/src/typegoose/Traductions.ts @@ -122,7 +122,9 @@ export class Traductions extends Base { const originKeys = keys(origin); const compareToKeys = keys(compareTo); // ces champs devront être traduits impérativement => to review - const added = difference(compareToKeys, originKeys); + const added = difference(compareToKeys, originKeys).filter( + (key: keyof TranslationContent) => get(compareTo, key) !== null, + ); // les champs supprimés peuvent être traités automatiquement sans re-traduction const removed = difference(originKeys, compareToKeys); diff --git a/migrations/1733826190794_RemoveStuckedToReview.ts b/migrations/1733826190794_RemoveStuckedToReview.ts new file mode 100644 index 0000000000..5d526a9ab8 --- /dev/null +++ b/migrations/1733826190794_RemoveStuckedToReview.ts @@ -0,0 +1,22 @@ +import { MigrationInterface } from "mongo-migrate-ts"; +import { Db } from "mongodb"; + +export class Migration1733826190794 implements MigrationInterface { + public async up(db: Db): Promise { + const trads = await db.collection("traductions").find({ toReview: "content.administrationName" }).toArray(); + for (const trad of trads) { + const dispositif = await db.collection("dispositifs").findOne({ _id: trad.dispositifId }); + if (dispositif && dispositif.translations.fr.content.administrationName === null) { + //@ts-ignore + await db + .collection("traductions") + .updateOne( + { _id: trad._id }, + { $pull: { toReview: "content.administrationName", toReviewCache: "content.administrationName" } }, + ); + } + } + } + + public async down(db: Db): Promise {} +}