From e09e042000456edcc3f3c24ac160ac3d23200363 Mon Sep 17 00:00:00 2001 From: Laurent Paoletti Date: Mon, 6 Jan 2025 11:57:11 +0100 Subject: [PATCH] Fix vhu identification number validation --- Changelog.md | 1 + .../__tests__/validation.integration.ts | 46 +++++++++++++++++++ back/src/bsvhu/validation/refinements.ts | 2 + back/src/bsvhu/validation/rules.ts | 13 ++++-- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/Changelog.md b/Changelog.md index 972bbefd09..06cbca4d42 100644 --- a/Changelog.md +++ b/Changelog.md @@ -33,6 +33,7 @@ et le projet suit un schéma de versionning inspiré de [Calendar Versioning](ht - Le champ "Modifié le" des BSDD dans le dashboard affiche la valeur du updatedAt, pas du lastActionOn [PR 3834](https://github.com/MTES-MCT/trackdechets/pull/3834) - Seul l'émetteur peut supprimer un BSVHU s'il l'a signé (SIGNED_BY_PRODUCER) [PR 3837](https://github.com/MTES-MCT/trackdechets/pull/3837) - Correction pour les cas particuliers pour le mail sur le changement de CAP: ajout ou suppression de la nextDestination [PR 3858](https://github.com/MTES-MCT/trackdechets/pull/3858) +- Les numéros d'identification du bsvhus ne devraient pas être obligatoires pour les vhus créés avant l'ajout de la règle [PR 3877](https://github.com/MTES-MCT/trackdechets/pull/3877) #### :boom: Breaking changes diff --git a/back/src/bsvhu/validation/__tests__/validation.integration.ts b/back/src/bsvhu/validation/__tests__/validation.integration.ts index 0fc78cae3e..1b6f29de64 100644 --- a/back/src/bsvhu/validation/__tests__/validation.integration.ts +++ b/back/src/bsvhu/validation/__tests__/validation.integration.ts @@ -291,6 +291,26 @@ describe("BSVHU validation", () => { expect(parsed).toBeDefined(); }); + test("when identification numbers are not provided on a bsvhu created before v20241001", async () => { + const data: ZodBsvhu = { + ...bsvhu, + createdAt: new Date("2024-10-21T00:00:00.000"), // before v20241001 + + identificationNumbers: [], + + transporterTransportMode: "ROAD", + + transporterTransportPlates: ["XY-23-TR"] + }; + + const parsed = await parseBsvhuAsync(data, { + ...context, + currentSignatureType: "TRANSPORT" + }); + + expect(parsed).toBeDefined(); + }); + test.each([ TransportMode.RAIL, TransportMode.AIR, @@ -887,6 +907,32 @@ describe("BSVHU validation", () => { } ); + test("when identification numbers are not provided on a bsvhu created after v20241001", async () => { + const data: ZodBsvhu = { + ...bsvhu, + + identificationNumbers: [], + + transporterTransportMode: "ROAD", + + transporterTransportPlates: ["XY-23-TR"] + }; + + expect.assertions(1); + + try { + await parseBsvhuAsync(data, { + ...context, + currentSignatureType: "TRANSPORT" + }); + } catch (err) { + expect((err as ZodError).issues).toEqual([ + expect.objectContaining({ + message: "Les numéros d'identification est un champ requis." + }) + ]); + } + }); describe("Emitter transports own waste", () => { it("allowed if exemption", async () => { const data: ZodBsvhu = { diff --git a/back/src/bsvhu/validation/refinements.ts b/back/src/bsvhu/validation/refinements.ts index e038361c34..19a4dc81a8 100644 --- a/back/src/bsvhu/validation/refinements.ts +++ b/back/src/bsvhu/validation/refinements.ts @@ -162,6 +162,8 @@ export const checkEmitterSituation: Refinement = ( }); } }; +// Date de la MAJ 2024.10.1 qui rend obligatoire la complétion des numéros d'identification +export const v20241001 = new Date("2024-10-23T00:00:00.000"); // Date de la MAJ 2024.12.1 qui modifie les règles de validation de BsvhuInput.packaging et identification.type export const v20241201 = new Date( diff --git a/back/src/bsvhu/validation/rules.ts b/back/src/bsvhu/validation/rules.ts index b005df4bc5..272067670c 100644 --- a/back/src/bsvhu/validation/rules.ts +++ b/back/src/bsvhu/validation/rules.ts @@ -13,7 +13,7 @@ import { import { capitalize } from "../../common/strings"; import { SealedFieldError } from "../../common/errors"; import { Leaves } from "../../types"; -import { v20250101 } from "./refinements"; +import { v20250101, v20241001 } from "./refinements"; // Liste des champs éditables sur l'objet Bsvhu export type BsvhuEditableFields = Required< @@ -386,8 +386,15 @@ export const bsvhuEditionRules: BsvhuEditionRules = { path: ["packaging"] }, identificationNumbers: { - sealed: { from: sealedFromEmissionExceptForEmitter }, - required: { from: "EMISSION" }, + sealed: { + from: sealedFromEmissionExceptForEmitter + }, + required: { + from: "EMISSION", + when: bsvhu => { + return (bsvhu.createdAt || new Date()).getTime() >= v20241001.getTime(); + } + }, readableFieldName: "Les numéros d'identification", path: ["identification", "numbers"] },