From a340d8139acbb0792ac9d2d6d539aed84fc65d44 Mon Sep 17 00:00:00 2001 From: tristan Date: Fri, 12 Jun 2026 08:45:38 +0000 Subject: [PATCH] =?UTF-8?q?feat(commercial)=20:=20am=C3=A9lioration=20et?= =?UTF-8?q?=20validation=20stricte=20des=20champs=20date=20(ERP-148)=20(#9?= =?UTF-8?q?2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Contexte ERP-148 — mise à jour @malio/layer-ui et amélioration des champs date (onglet Information, Client & Fournisseur). ## Changements - **MalioDate v1.7.10** : le composant expose désormais son état de validité (`@update:valid`) et la saisie brute invalide (`@update:rawValue`). - **Validation back-autoritaire du format** : `foundedAt` n'accepte plus que l'ISO strict `Y-m-d` (`#[Context]` DateTimeNormalizer) + `collectDenormalizationErrors` sur `Client` et `Supplier`. Toute saisie non-ISO renvoie un **422 porté sur le champ**. - Corrige un cas piège : `12/25/2026` (invalide en JJ/MM/AAAA côté front) était auparavant accepté par PHP en M/J/AAAA → 25 décembre. Désormais rejeté. - **Front** : la saisie invalide est transmise au back ; le message technique de type-error est surchargé par une clé i18n via le **code de violation** (`resolveViolationMessage` / `VIOLATION_MESSAGE_I18N`), affiché inline par `useFormErrors`. - Réorganisation des utils de formulaire sous `utils/forms/`. ## Tests - Back : `ClientFoundedAtFormatTest` / `SupplierFoundedAtFormatTest` (dont le cas piège `12/25/2026`). - Front : résolveur i18n (`api.test.ts`, `useFormErrors.test.ts`) + payloads (`clientEdit`/`supplierEdit` specs). - Suite Commercial verte ; vérifié bout-en-bout en navigateur (PATCH → 422, erreur inline, submit bloqué). ## Note Échecs JWT aléatoires connus du hook pre-commit (401/500 sur tests d'auth sans rapport) ; tous verts en isolation. Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Starseed/pulls/92 Co-authored-by: tristan Co-committed-by: tristan --- .claude/rules/frontend.md | 1 + frontend/i18n/locales/fr.json | 5 +- .../components/ClientAddressBlock.vue | 2 +- .../commercial/composables/useClient.ts | 2 +- .../commercial/composables/useSupplier.ts | 2 +- .../commercial/pages/clients/[id]/edit.vue | 7 +- .../commercial/pages/clients/[id]/index.vue | 4 +- .../modules/commercial/pages/clients/new.vue | 10 ++- .../commercial/pages/suppliers/[id]/edit.vue | 7 +- .../commercial/pages/suppliers/[id]/index.vue | 4 +- .../commercial/pages/suppliers/new.vue | 7 +- .../__tests__/clientConsultation.spec.ts | 0 .../{ => forms}/__tests__/clientEdit.spec.ts | 11 +++ .../__tests__/clientFormRules.spec.ts | 0 .../__tests__/supplierConsultation.spec.ts | 0 .../__tests__/supplierEdit.spec.ts | 13 +++- .../__tests__/supplierFormRules.spec.ts | 0 .../utils/{ => forms}/clientConsultation.ts | 0 .../utils/{ => forms}/clientEdit.ts | 17 ++++- .../utils/{ => forms}/clientFormRules.ts | 0 .../utils/{ => forms}/supplierConsultation.ts | 0 .../utils/{ => forms}/supplierEdit.ts | 17 ++++- .../utils/{ => forms}/supplierFormRules.ts | 0 frontend/package-lock.json | 8 +-- frontend/package.json | 2 +- .../__tests__/useFormErrors.test.ts | 16 +++++ frontend/shared/composables/useFormErrors.ts | 17 +++-- frontend/shared/utils/__tests__/api.test.ts | 29 +++++++- frontend/shared/utils/api.ts | 46 +++++++++++- .../Commercial/Domain/Entity/Client.php | 19 +++++ .../Commercial/Domain/Entity/Supplier.php | 17 +++++ .../Api/ClientFoundedAtFormatTest.php | 71 +++++++++++++++++++ .../Api/SupplierFoundedAtFormatTest.php | 71 +++++++++++++++++++ 33 files changed, 364 insertions(+), 41 deletions(-) rename frontend/modules/commercial/utils/{ => forms}/__tests__/clientConsultation.spec.ts (100%) rename frontend/modules/commercial/utils/{ => forms}/__tests__/clientEdit.spec.ts (95%) rename frontend/modules/commercial/utils/{ => forms}/__tests__/clientFormRules.spec.ts (100%) rename frontend/modules/commercial/utils/{ => forms}/__tests__/supplierConsultation.spec.ts (100%) rename frontend/modules/commercial/utils/{ => forms}/__tests__/supplierEdit.spec.ts (93%) rename frontend/modules/commercial/utils/{ => forms}/__tests__/supplierFormRules.spec.ts (100%) rename frontend/modules/commercial/utils/{ => forms}/clientConsultation.ts (100%) rename frontend/modules/commercial/utils/{ => forms}/clientEdit.ts (93%) rename frontend/modules/commercial/utils/{ => forms}/clientFormRules.ts (100%) rename frontend/modules/commercial/utils/{ => forms}/supplierConsultation.ts (100%) rename frontend/modules/commercial/utils/{ => forms}/supplierEdit.ts (92%) rename frontend/modules/commercial/utils/{ => forms}/supplierFormRules.ts (100%) create mode 100644 tests/Module/Commercial/Api/ClientFoundedAtFormatTest.php create mode 100644 tests/Module/Commercial/Api/SupplierFoundedAtFormatTest.php diff --git a/.claude/rules/frontend.md b/.claude/rules/frontend.md index f5714c3..0580119 100644 --- a/.claude/rules/frontend.md +++ b/.claude/rules/frontend.md @@ -79,6 +79,7 @@ Regles : - **Toujours `{ toast: false }`** sur l'appel API qui veut un mapping inline (sinon le toast natif d'`useApi` masque le fin). - **Cas metier specifique** (ex: 409 doublon) : `setError('champ', message)` + toast explicite **avant** de deleguer le reste a `handleApiError`. Cf. `useCategoryForm` (doublon RG-1.07). - **Collections** (listes de sous-entites sauvees par un appel par ligne) : une erreur PAR LIGNE via un tableau `ref[]>` aligne sur l'index, peuple par `mapViolationsToRecord(error.response._data)` (util pur de `shared/utils/api.ts`). Le composant de ligne expose une prop `:errors` (`Record`) bindee sur le `:error` de chaque champ. Cf. `ClientContactBlock` / `ClientAddressBlock` et les submits de `clients/new.vue` / `clients/[id]/edit.vue`. +- **Message back technique → surcharge i18n par code** : la plupart des contraintes back portent un message FR explicite (affiche tel quel). Mais une 422 peut porter un message TECHNIQUE non montrable (ex. erreur de type API Platform sur une date non parsable : « Cette valeur doit être de type DateTimeImmutable|null. », voire en anglais selon la negociation). On le surcharge **cote front** via le `code` de violation (UUID Symfony fige, robuste — pas un match sur le texte) : table `VIOLATION_MESSAGE_I18N` + `resolveViolationMessage` dans `shared/utils/api.ts`, appliquee par `useFormErrors`. Ajouter un cas = une entree `code -> cle i18n`. Cas reference : date invalide (MalioDate forwarde la saisie brute via `@update:rawValue`, le back renvoie 422 sur `foundedAt` grace a `collectDenormalizationErrors`, le front affiche `errors.validation.invalidDate`). **Interdit** : se contenter d'un toast global sur une 422 quand le back identifie les champs fautifs (`propertyPath`). Reimplementer un mapping `if/else` par champ a la main au lieu d'`useFormErrors` / `mapViolationsToRecord`. diff --git a/frontend/i18n/locales/fr.json b/frontend/i18n/locales/fr.json index d3ea681..cd6a8ad 100644 --- a/frontend/i18n/locales/fr.json +++ b/frontend/i18n/locales/fr.json @@ -386,7 +386,10 @@ }, "title": "Erreur", "generic": "Une erreur est survenue.", - "unknown": "Erreur inconnue." + "unknown": "Erreur inconnue.", + "validation": { + "invalidDate": "Date invalide" + } }, "sites": { "selector": { diff --git a/frontend/modules/commercial/components/ClientAddressBlock.vue b/frontend/modules/commercial/components/ClientAddressBlock.vue index 3533a7d..ff2c574 100644 --- a/frontend/modules/commercial/components/ClientAddressBlock.vue +++ b/frontend/modules/commercial/components/ClientAddressBlock.vue @@ -187,7 +187,7 @@ import { addressTypeFromFlags, isBillingEmailRequired, type AddressType, -} from '~/modules/commercial/utils/clientFormRules' +} from '~/modules/commercial/utils/forms/clientFormRules' import { useAddressAutocomplete, type AddressSuggestion } from '~/shared/composables/useAddressAutocomplete' import type { CategoryOption, RefOption } from '~/modules/commercial/composables/useClientReferentials' import type { AddressFormDraft } from '~/modules/commercial/types/clientForm' diff --git a/frontend/modules/commercial/composables/useClient.ts b/frontend/modules/commercial/composables/useClient.ts index eff43cc..a91396c 100644 --- a/frontend/modules/commercial/composables/useClient.ts +++ b/frontend/modules/commercial/composables/useClient.ts @@ -1,5 +1,5 @@ import { ref } from 'vue' -import type { ClientDetail } from '~/modules/commercial/utils/clientConsultation' +import type { ClientDetail } from '~/modules/commercial/utils/forms/clientConsultation' /** * Chargement et actions d'archivage d'un client unique (ecran « Consultation diff --git a/frontend/modules/commercial/composables/useSupplier.ts b/frontend/modules/commercial/composables/useSupplier.ts index 47d05a1..c77f25e 100644 --- a/frontend/modules/commercial/composables/useSupplier.ts +++ b/frontend/modules/commercial/composables/useSupplier.ts @@ -1,5 +1,5 @@ import { ref } from 'vue' -import type { SupplierDetail } from '~/modules/commercial/utils/supplierConsultation' +import type { SupplierDetail } from '~/modules/commercial/utils/forms/supplierConsultation' /** * Chargement et actions d'archivage d'un fournisseur unique (ecran « Consultation diff --git a/frontend/modules/commercial/pages/clients/[id]/edit.vue b/frontend/modules/commercial/pages/clients/[id]/edit.vue index 86604b4..75c477a 100644 --- a/frontend/modules/commercial/pages/clients/[id]/edit.vue +++ b/frontend/modules/commercial/pages/clients/[id]/edit.vue @@ -116,6 +116,7 @@ :readonly="businessReadonly" :editable="true" :error="informationErrors.errors.foundedAt" + @update:raw-value="(v: string) => information.foundedAtRaw = v" /> import { computed, onMounted, ref } from 'vue' import { useClient } from '~/modules/commercial/composables/useClient' -import { buildClientFormTabKeys, isRibRequiredForPaymentType } from '~/modules/commercial/utils/clientFormRules' +import { buildClientFormTabKeys, isRibRequiredForPaymentType } from '~/modules/commercial/utils/forms/clientFormRules' import { readHistoryTab } from '~/shared/utils/historyTab' import { canEditClient, @@ -297,7 +297,7 @@ import { showRestoreAction, type ClientDetail, type SelectOption, -} from '~/modules/commercial/utils/clientConsultation' +} from '~/modules/commercial/utils/forms/clientConsultation' import { emptyAddress, emptyContact } from '~/modules/commercial/types/clientForm' // Masque d'affichage (purement visuel, la donnee reste celle du serveur). diff --git a/frontend/modules/commercial/pages/clients/new.vue b/frontend/modules/commercial/pages/clients/new.vue index 7fde08f..a3e9617 100644 --- a/frontend/modules/commercial/pages/clients/new.vue +++ b/frontend/modules/commercial/pages/clients/new.vue @@ -111,6 +111,7 @@ :readonly="isValidated('information')" :editable="true" :error="informationErrors.errors.foundedAt" + @update:raw-value="(v: string) => information.foundedAtRaw = v" /> { await api.patch(`/clients/${clientId.value}`, { description: information.description || null, competitors: information.competitors || null, - foundedAt: information.foundedAt || null, + // Saisie invalide prioritaire -> 422 back sur foundedAt (cf. foundedAtRaw). + foundedAt: information.foundedAtRaw || information.foundedAt || null, employeesCount: information.employeesCount ? Number(information.employeesCount) : null, revenueAmount: information.revenueAmount || null, profitAmount: information.profitAmount || null, diff --git a/frontend/modules/commercial/pages/suppliers/[id]/edit.vue b/frontend/modules/commercial/pages/suppliers/[id]/edit.vue index 39ab978..8695ca2 100644 --- a/frontend/modules/commercial/pages/suppliers/[id]/edit.vue +++ b/frontend/modules/commercial/pages/suppliers/[id]/edit.vue @@ -77,6 +77,7 @@ :readonly="businessReadonly" :editable="true" :error="informationErrors.errors.foundedAt" + @update:raw-value="(v: string) => information.foundedAtRaw = v" /> import { computed, onMounted, ref } from 'vue' import { useSupplier } from '~/modules/commercial/composables/useSupplier' -import { buildSupplierFormTabKeys, isRibRequiredForPaymentType } from '~/modules/commercial/utils/supplierFormRules' +import { buildSupplierFormTabKeys, isRibRequiredForPaymentType } from '~/modules/commercial/utils/forms/supplierFormRules' import { readHistoryTab } from '~/shared/utils/historyTab' import { canEditSupplier, @@ -280,7 +280,7 @@ import { showRestoreAction, type SelectOption, type SupplierDetail, -} from '~/modules/commercial/utils/supplierConsultation' +} from '~/modules/commercial/utils/forms/supplierConsultation' import { emptyContact } from '~/modules/commercial/types/supplierForm' // Masque d'affichage (purement visuel, la donnee reste celle du serveur). diff --git a/frontend/modules/commercial/pages/suppliers/new.vue b/frontend/modules/commercial/pages/suppliers/new.vue index eaa8602..e30c026 100644 --- a/frontend/modules/commercial/pages/suppliers/new.vue +++ b/frontend/modules/commercial/pages/suppliers/new.vue @@ -71,6 +71,7 @@ :readonly="isValidated('information')" :editable="true" :error="informationErrors.errors.foundedAt" + @update:raw-value="(v: string) => information.foundedAtRaw = v" /> = {}): Inform description: 'desc', competitors: 'concurrents', foundedAt: '2010-05-01', + foundedAtRaw: '', employeesCount: '42', revenueAmount: '1000000', profitAmount: '50000', @@ -140,6 +141,16 @@ describe('buildInformationPayload — scoping strict groupe client:write:informa expect(payload.description).toBeNull() expect(payload.directorName).toBeNull() }) + + it('envoie la saisie invalide (foundedAtRaw) en priorite -> le back tranchera (422)', () => { + // Saisie malformee : on transmet le texte brut tel quel pour declencher la + // 422 back sur foundedAt (validation autoritaire du format, MUI-44). + expect(buildInformationPayload(informationDraft({ foundedAt: null, foundedAtRaw: '32/13/2026' })).foundedAt) + .toBe('32/13/2026') + // Saisie valide : foundedAtRaw vide -> on envoie la date ISO. + expect(buildInformationPayload(informationDraft({ foundedAt: '2010-05-01', foundedAtRaw: '' })).foundedAt) + .toBe('2010-05-01') + }) }) describe('buildAccountingPayload — scoping strict groupe client:write:accounting', () => { diff --git a/frontend/modules/commercial/utils/__tests__/clientFormRules.spec.ts b/frontend/modules/commercial/utils/forms/__tests__/clientFormRules.spec.ts similarity index 100% rename from frontend/modules/commercial/utils/__tests__/clientFormRules.spec.ts rename to frontend/modules/commercial/utils/forms/__tests__/clientFormRules.spec.ts diff --git a/frontend/modules/commercial/utils/__tests__/supplierConsultation.spec.ts b/frontend/modules/commercial/utils/forms/__tests__/supplierConsultation.spec.ts similarity index 100% rename from frontend/modules/commercial/utils/__tests__/supplierConsultation.spec.ts rename to frontend/modules/commercial/utils/forms/__tests__/supplierConsultation.spec.ts diff --git a/frontend/modules/commercial/utils/__tests__/supplierEdit.spec.ts b/frontend/modules/commercial/utils/forms/__tests__/supplierEdit.spec.ts similarity index 93% rename from frontend/modules/commercial/utils/__tests__/supplierEdit.spec.ts rename to frontend/modules/commercial/utils/forms/__tests__/supplierEdit.spec.ts index 8d9f3d6..341280f 100644 --- a/frontend/modules/commercial/utils/__tests__/supplierEdit.spec.ts +++ b/frontend/modules/commercial/utils/forms/__tests__/supplierEdit.spec.ts @@ -11,7 +11,7 @@ import { mapMainDraft, resolveTabEditability, } from '../supplierEdit' -import type { SupplierDetail } from '~/modules/commercial/utils/supplierConsultation' +import type { SupplierDetail } from '~/modules/commercial/utils/forms/supplierConsultation' import { emptyAddress, emptyContact, emptyRib } from '~/modules/commercial/types/supplierForm' describe('buildMainPayload (groupe supplier:write:main)', () => { @@ -37,7 +37,7 @@ describe('buildMainPayload (groupe supplier:write:main)', () => { describe('buildInformationPayload (groupe supplier:write:information)', () => { const base = { - description: null, competitors: null, foundedAt: null, employeesCount: null, + description: null, competitors: null, foundedAt: null, foundedAtRaw: '', employeesCount: null, revenueAmount: null, profitAmount: null, directorName: null, volumeForecast: null, } @@ -48,6 +48,15 @@ describe('buildInformationPayload (groupe supplier:write:information)', () => { }) expect(buildInformationPayload(base)).toMatchObject({ employeesCount: null, volumeForecast: null }) }) + + it('envoie la saisie invalide (foundedAtRaw) en priorite -> le back tranchera (422)', () => { + // Saisie malformee transmise telle quelle pour declencher la 422 back (MUI-44). + expect(buildInformationPayload({ ...base, foundedAt: null, foundedAtRaw: '32/13/2026' }).foundedAt) + .toBe('32/13/2026') + // Saisie valide : foundedAtRaw vide -> on envoie la date ISO. + expect(buildInformationPayload({ ...base, foundedAt: '2008-04-01', foundedAtRaw: '' }).foundedAt) + .toBe('2008-04-01') + }) }) describe('buildContactPayload (sous-ressource supplier_contact)', () => { diff --git a/frontend/modules/commercial/utils/__tests__/supplierFormRules.spec.ts b/frontend/modules/commercial/utils/forms/__tests__/supplierFormRules.spec.ts similarity index 100% rename from frontend/modules/commercial/utils/__tests__/supplierFormRules.spec.ts rename to frontend/modules/commercial/utils/forms/__tests__/supplierFormRules.spec.ts diff --git a/frontend/modules/commercial/utils/clientConsultation.ts b/frontend/modules/commercial/utils/forms/clientConsultation.ts similarity index 100% rename from frontend/modules/commercial/utils/clientConsultation.ts rename to frontend/modules/commercial/utils/forms/clientConsultation.ts diff --git a/frontend/modules/commercial/utils/clientEdit.ts b/frontend/modules/commercial/utils/forms/clientEdit.ts similarity index 93% rename from frontend/modules/commercial/utils/clientEdit.ts rename to frontend/modules/commercial/utils/forms/clientEdit.ts index a0d4dff..bfadb0e 100644 --- a/frontend/modules/commercial/utils/clientEdit.ts +++ b/frontend/modules/commercial/utils/forms/clientEdit.ts @@ -20,14 +20,14 @@ import { iriOf, relationOf, type ClientDetail, -} from '~/modules/commercial/utils/clientConsultation' +} from '~/modules/commercial/utils/forms/clientConsultation' import { ADDRESS_REQUIRED_NON_NULLABLE_KEYS, blankEmptyRequired, MAIN_REQUIRED_NON_NULLABLE_KEYS, omitEmptyRequired, RIB_REQUIRED_NON_NULLABLE_KEYS, -} from '~/modules/commercial/utils/clientFormRules' +} from '~/modules/commercial/utils/forms/clientFormRules' import type { AddressFormDraft, ContactFormDraft, RibFormDraft } from '~/modules/commercial/types/clientForm' /** @@ -53,6 +53,13 @@ export interface InformationFormDraft { competitors: string | null /** Date de creation de l'entreprise au format YYYY-MM-DD (MalioDate). */ foundedAt: string | null + /** + * Saisie brute invalide remontee par MalioDate (`@update:rawValue`) : '' tant + * que la saisie est valide/vide, sinon le texte tel que tape. On l'envoie au + * back en priorite sur `foundedAt` pour que la 422 (validation autoritaire du + * format, ERP-101) porte sur le champ et s'affiche inline. Cf. MUI-44. + */ + foundedAtRaw: string /** Nombre de salaries en chaine (saisie masquee), converti en number au PATCH. */ employeesCount: string | null revenueAmount: string | null @@ -118,6 +125,8 @@ export function mapInformationDraft(client: ClientDetail): InformationFormDraft competitors: client.competitors ?? null, // MalioDate attend strictement YYYY-MM-DD : on tronque l'ISO datetime. foundedAt: client.foundedAt ? client.foundedAt.slice(0, 10) : null, + // Aucune saisie brute invalide au chargement (la valeur stockee est valide). + foundedAtRaw: '', employeesCount: client.employeesCount != null ? String(client.employeesCount) : null, revenueAmount: client.revenueAmount ?? null, profitAmount: client.profitAmount ?? null, @@ -191,7 +200,9 @@ export function buildInformationPayload(information: InformationFormDraft): Reco return { description: information.description || null, competitors: information.competitors || null, - foundedAt: information.foundedAt || null, + // Saisie invalide (foundedAtRaw) prioritaire : on l'envoie telle quelle + // pour que le back renvoie une 422 sur foundedAt (cf. foundedAtRaw). + foundedAt: information.foundedAtRaw || information.foundedAt || null, employeesCount: information.employeesCount ? Number(information.employeesCount) : null, revenueAmount: information.revenueAmount || null, profitAmount: information.profitAmount || null, diff --git a/frontend/modules/commercial/utils/clientFormRules.ts b/frontend/modules/commercial/utils/forms/clientFormRules.ts similarity index 100% rename from frontend/modules/commercial/utils/clientFormRules.ts rename to frontend/modules/commercial/utils/forms/clientFormRules.ts diff --git a/frontend/modules/commercial/utils/supplierConsultation.ts b/frontend/modules/commercial/utils/forms/supplierConsultation.ts similarity index 100% rename from frontend/modules/commercial/utils/supplierConsultation.ts rename to frontend/modules/commercial/utils/forms/supplierConsultation.ts diff --git a/frontend/modules/commercial/utils/supplierEdit.ts b/frontend/modules/commercial/utils/forms/supplierEdit.ts similarity index 92% rename from frontend/modules/commercial/utils/supplierEdit.ts rename to frontend/modules/commercial/utils/forms/supplierEdit.ts index 2f8a5e5..cd2ffff 100644 --- a/frontend/modules/commercial/utils/supplierEdit.ts +++ b/frontend/modules/commercial/utils/forms/supplierEdit.ts @@ -17,8 +17,8 @@ import { MAIN_REQUIRED_NON_NULLABLE_KEYS, omitEmptyRequired, RIB_REQUIRED_NON_NULLABLE_KEYS, -} from '~/modules/commercial/utils/supplierFormRules' -import { iriOf, type SupplierDetail } from '~/modules/commercial/utils/supplierConsultation' +} from '~/modules/commercial/utils/forms/supplierFormRules' +import { iriOf, type SupplierDetail } from '~/modules/commercial/utils/forms/supplierConsultation' import type { SupplierAddressFormDraft, SupplierContactFormDraft, @@ -38,6 +38,13 @@ export interface InformationFormDraft { competitors: string | null /** Date de creation de l'entreprise au format YYYY-MM-DD (MalioDate). */ foundedAt: string | null + /** + * Saisie brute invalide remontee par MalioDate (`@update:rawValue`) : '' tant + * que la saisie est valide/vide, sinon le texte tel que tape. On l'envoie au + * back en priorite sur `foundedAt` pour que la 422 (validation autoritaire du + * format, ERP-101) porte sur le champ et s'affiche inline. Cf. MUI-44. + */ + foundedAtRaw: string /** Nombre de salaries en chaine (saisie masquee), converti en number au PATCH. */ employeesCount: string | null revenueAmount: string | null @@ -95,6 +102,8 @@ export function mapInformationDraft(supplier: SupplierDetail): InformationFormDr competitors: supplier.competitors ?? null, // MalioDate attend strictement YYYY-MM-DD : on tronque l'ISO datetime. foundedAt: supplier.foundedAt ? supplier.foundedAt.slice(0, 10) : null, + // Aucune saisie brute invalide au chargement (la valeur stockee est valide). + foundedAtRaw: '', employeesCount: supplier.employeesCount != null ? String(supplier.employeesCount) : null, revenueAmount: supplier.revenueAmount ?? null, profitAmount: supplier.profitAmount ?? null, @@ -177,7 +186,9 @@ export function buildInformationPayload(information: InformationFormDraft): Reco return { description: information.description || null, competitors: information.competitors || null, - foundedAt: information.foundedAt || null, + // Saisie invalide (foundedAtRaw) prioritaire : on l'envoie telle quelle + // pour que le back renvoie une 422 sur foundedAt (cf. foundedAtRaw). + foundedAt: information.foundedAtRaw || information.foundedAt || null, employeesCount: information.employeesCount ? Number(information.employeesCount) : null, revenueAmount: information.revenueAmount || null, profitAmount: information.profitAmount || null, diff --git a/frontend/modules/commercial/utils/supplierFormRules.ts b/frontend/modules/commercial/utils/forms/supplierFormRules.ts similarity index 100% rename from frontend/modules/commercial/utils/supplierFormRules.ts rename to frontend/modules/commercial/utils/forms/supplierFormRules.ts diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 43f4fc0..2344110 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -7,7 +7,7 @@ "name": "starseed-frontend", "hasInstallScript": true, "dependencies": { - "@malio/layer-ui": "^1.7.8", + "@malio/layer-ui": "^1.7.10", "@nuxt/icon": "^2.2.1", "@nuxtjs/i18n": "^10.2.3", "@nuxtjs/tailwindcss": "^6.14.0", @@ -1866,9 +1866,9 @@ "license": "MIT" }, "node_modules/@malio/layer-ui": { - "version": "1.7.8", - "resolved": "https://gitea.malio.fr/api/packages/MALIO-DEV/npm/%40malio%2Flayer-ui/-/1.7.8/layer-ui-1.7.8.tgz", - "integrity": "sha512-gUMAZzBsPCfQUF3OQSjN/OFzjONvQZYfwqH0u5VUbxaqwBdX1hUGtjD4ym6RvZkyNsKulrxkncFZYTWCS+IdGA==", + "version": "1.7.10", + "resolved": "https://gitea.malio.fr/api/packages/MALIO-DEV/npm/%40malio%2Flayer-ui/-/1.7.10/layer-ui-1.7.10.tgz", + "integrity": "sha512-ZWYaKvl+VpGAqeTE+4xdyKOmuRd4zwjlUYVppeIBZwGeNAK16kZnrztR+4eQmnzUqPZVybBhEBdKP9weqWHSUg==", "dependencies": { "@nuxt/icon": "^2.2.1", "@nuxtjs/tailwindcss": "^6.14.0", diff --git a/frontend/package.json b/frontend/package.json index a60e49b..b4cf9b0 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -17,7 +17,7 @@ "test:e2e:ui": "playwright test --ui" }, "dependencies": { - "@malio/layer-ui": "^1.7.8", + "@malio/layer-ui": "^1.7.10", "@nuxt/icon": "^2.2.1", "@nuxtjs/i18n": "^10.2.3", "@nuxtjs/tailwindcss": "^6.14.0", diff --git a/frontend/shared/composables/__tests__/useFormErrors.test.ts b/frontend/shared/composables/__tests__/useFormErrors.test.ts index 71ed149..b24f289 100644 --- a/frontend/shared/composables/__tests__/useFormErrors.test.ts +++ b/frontend/shared/composables/__tests__/useFormErrors.test.ts @@ -41,6 +41,22 @@ describe('useFormErrors', () => { expect(hasErrors.value).toBe(true) }) + it('setServerErrors surcharge un message technique (erreur de type) par la cle i18n', () => { + const { errors, setServerErrors } = useFormErrors() + const mapped = setServerErrors({ + violations: [ + // Code Symfony Type::INVALID_TYPE_ERROR (date non parsable) : surcharge. + { propertyPath: 'foundedAt', message: 'Cette valeur doit être de type DateTimeImmutable|null.', code: 'ba785a8c-82cb-4283-967c-3cf342181b40' }, + // Violation metier classique : message back conserve. + { propertyPath: 'companyName', message: 'Obligatoire.', code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3' }, + ], + }) + expect(mapped).toBe(true) + // Stub i18n -> renvoie la cle telle quelle. + expect(errors.foundedAt).toBe('errors.validation.invalidDate') + expect(errors.companyName).toBe('Obligatoire.') + }) + it('setServerErrors retourne false et ne touche rien sans violation', () => { const { errors, setServerErrors } = useFormErrors() expect(setServerErrors({})).toBe(false) diff --git a/frontend/shared/composables/useFormErrors.ts b/frontend/shared/composables/useFormErrors.ts index 0749569..5fda0b9 100644 --- a/frontend/shared/composables/useFormErrors.ts +++ b/frontend/shared/composables/useFormErrors.ts @@ -17,7 +17,7 @@ * appel par ligne), utiliser directement `mapViolationsToRecord` par ligne. */ import { computed, reactive } from 'vue' -import { extractApiErrorMessage, mapViolationsToRecord } from '~/shared/utils/api' +import { extractApiErrorMessage, extractApiViolations, resolveViolationMessage } from '~/shared/utils/api' /** * Erreur HTTP capturee par ofetch. On n'expose que les champs lus ici (status @@ -69,13 +69,16 @@ export function useFormErrors() { * violation exploitable). */ function setServerErrors(data: unknown): boolean { - const mapped = mapViolationsToRecord(data) - const keys = Object.keys(mapped) - if (keys.length === 0) return false - for (const key of keys) { - errors[key] = mapped[key] + const violations = extractApiViolations(data) + let mapped = false + for (const v of violations) { + if (!v.propertyPath) continue + // Message back tel quel, sauf code surcharge par une cle i18n (ex. + // erreur de type sur une date non parsable -> « Date invalide »). + errors[v.propertyPath] = resolveViolationMessage(v, t) + mapped = true } - return true + return mapped } /** diff --git a/frontend/shared/utils/__tests__/api.test.ts b/frontend/shared/utils/__tests__/api.test.ts index 7d39644..9b10f1d 100644 --- a/frontend/shared/utils/__tests__/api.test.ts +++ b/frontend/shared/utils/__tests__/api.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest' -import { mapViolationsToRecord } from '../api' +import { mapViolationsToRecord, resolveViolationMessage } from '../api' /** * Tests de `mapViolationsToRecord` — fondation du mapping erreur→champ des @@ -56,3 +56,30 @@ describe('mapViolationsToRecord', () => { expect(mapViolationsToRecord(data)).toEqual({ name: 'Second message.' }) }) }) + +/** + * Tests de `resolveViolationMessage` — surcharge i18n d'un message back par code + * de violation. Le back peut renvoyer un message technique (erreur de type sur + * une date non parsable) : on le remplace via le `code` Symfony (stable) par une + * cle i18n, sans toucher au back. Le `t` ici renvoie la cle telle quelle. + */ +describe('resolveViolationMessage', () => { + const t = (key: string) => key + // Code Symfony Constraints\Type::INVALID_TYPE_ERROR (fige). + const TYPE_ERROR = 'ba785a8c-82cb-4283-967c-3cf342181b40' + + it('surcharge le message technique d\'une erreur de type par la cle i18n', () => { + const v = { propertyPath: 'foundedAt', message: 'Cette valeur doit être de type DateTimeImmutable|null.', code: TYPE_ERROR } + expect(resolveViolationMessage(v, t)).toBe('errors.validation.invalidDate') + }) + + it('renvoie le message back tel quel quand le code n\'est pas surcharge', () => { + const v = { propertyPath: 'companyName', message: 'Le nom est obligatoire.', code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3' } + expect(resolveViolationMessage(v, t)).toBe('Le nom est obligatoire.') + }) + + it('renvoie le message back tel quel quand il n\'y a pas de code', () => { + const v = { propertyPath: 'siren', message: 'SIREN deja utilise.', code: '' } + expect(resolveViolationMessage(v, t)).toBe('SIREN deja utilise.') + }) +}) diff --git a/frontend/shared/utils/api.ts b/frontend/shared/utils/api.ts index 0de396a..bf1081a 100644 --- a/frontend/shared/utils/api.ts +++ b/frontend/shared/utils/api.ts @@ -34,11 +34,15 @@ export function extractHydraMembers(collection: HydraCollection): T[] { /** * Une violation de contrainte API Platform (reponse 422). Le `propertyPath` - * pointe le champ concerne, `message` est le libelle a afficher. + * pointe le champ concerne, `message` est le libelle a afficher, `code` est le + * code de contrainte Symfony (UUID stable, independant de la langue) — il sert + * a surcharger un message back technique par une cle i18n (cf. + * `VIOLATION_MESSAGE_I18N` / `resolveViolationMessage`). */ export interface ApiViolation { propertyPath: string message: string + code: string } /** @@ -61,6 +65,7 @@ export function extractApiViolations(data: unknown): ApiViolation[] { out.push({ propertyPath: String(obj.propertyPath ?? ''), message: String(obj.message ?? ''), + code: String(obj.code ?? ''), }) } return out @@ -85,6 +90,45 @@ export function mapViolationsToRecord(data: unknown): Record { return out } +/** + * Surcharge i18n d'un message back par CODE de violation. + * + * La plupart des contraintes back portent deja un message FR explicite (ex. + * `#[Assert\NotBlank(message: '...')]`) : on l'affiche tel quel. Mais certaines + * 422 portent un message TECHNIQUE non montrable a l'utilisateur — typiquement + * l'erreur de TYPE renvoyee par API Platform quand le back ne peut pas + * denormaliser la valeur (date non parsable envoyee sur un champ + * `DateTimeImmutable` : « Cette valeur doit être de type DateTimeImmutable|null. », + * voire en anglais selon la negociation de langue). + * + * Plutot que de traduire/maquiller cote back, on surcharge ces messages cote + * front via leur `code` de violation. Ce code est un UUID Symfony FIGE (contrat + * de compatibilite : il ne change pas entre versions), donc bien plus robuste + * qu'un match sur le texte du message (qui depend de la langue). La table + * associe un code -> une cle i18n ; `resolveViolationMessage` l'applique. + * + * Limite a connaitre : le code de type-error est GENERIQUE (toute valeur de + * mauvais type). Dans nos formulaires, seul un champ date saisi en texte libre + * (MalioDate, qui forwarde la saisie brute invalide) le declenche, d'ou le + * libelle « Date invalide ». Si un autre champ typé en saisie libre apparait, + * affiner la resolution via `propertyPath` plutot que par code seul. + */ +export const VIOLATION_MESSAGE_I18N: Record = { + // Symfony `Constraints\Type::INVALID_TYPE_ERROR` — valeur de mauvais type. + 'ba785a8c-82cb-4283-967c-3cf342181b40': 'errors.validation.invalidDate', +} + +/** + * Resout le message a afficher pour une violation : si son `code` est surcharge + * par `VIOLATION_MESSAGE_I18N`, renvoie la traduction de la cle associee ; + * sinon, le message back tel quel (cas nominal). `t` est passe par l'appelant + * (les utils sont purs, sans acces a useI18n). + */ +export function resolveViolationMessage(v: ApiViolation, t: (key: string) => string): string { + const i18nKey = VIOLATION_MESSAGE_I18N[v.code] + return i18nKey ? t(i18nKey) : v.message +} + /** * Extrait un message d'erreur lisible depuis un payload Hydra / JSON * d'erreur API Platform. Essaie les champs courants dans l'ordre : diff --git a/src/Module/Commercial/Domain/Entity/Client.php b/src/Module/Commercial/Domain/Entity/Client.php index c8a033d..f3f2c5c 100644 --- a/src/Module/Commercial/Domain/Entity/Client.php +++ b/src/Module/Commercial/Domain/Entity/Client.php @@ -22,8 +22,10 @@ use DateTimeImmutable; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Attribute\Context; use Symfony\Component\Serializer\Attribute\Groups; use Symfony\Component\Serializer\Attribute\SerializedName; +use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Context\ExecutionContextInterface; @@ -94,6 +96,12 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; security: "is_granted('commercial.clients.manage')", normalizationContext: ['groups' => ['client:read', 'default:read', 'category:read', 'site:read']], denormalizationContext: ['groups' => ['client:write:main']], + // Une valeur de mauvais type (ex. date non parsable sur foundedAt) + // doit produire un 422 porte sur le champ (violations[].propertyPath, + // mappable inline par useFormErrors) plutot qu'un 400 generique non + // exploitable. Le front (MalioDate, MUI-44) forwarde la saisie brute + // invalide : le back reste la couche autoritaire du format (ERP-101). + collectDenormalizationErrors: true, processor: ClientProcessor::class, ), new Patch( @@ -117,6 +125,10 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; 'client:write:accounting', 'client:write:archive', ]], + // Cf. Post : date non parsable (foundedAt) -> 422 porte sur le champ + // au lieu d'un 400 generique. Indispensable au mapping inline du + // front (MalioDate MUI-44 forwarde la saisie brute invalide). + collectDenormalizationErrors: true, provider: ClientProvider::class, processor: ClientProcessor::class, ), @@ -206,6 +218,13 @@ class Client implements TimestampableInterface, BlamableInterface #[ORM\Column(type: 'date_immutable', nullable: true)] #[Groups(['client:read', 'client:write:information'])] + // Format d'ENTREE strict ISO `Y-m-d` (le `!` remet l'heure a 00:00:00). Sans + // ce format, PHP DateTime accepte des formes ambigues : « 12/25/2026 » (que + // le front MalioDate juge invalide en JJ/MM/AAAA) serait sinon interprete en + // M/J/AAAA -> 25 decembre 2026, et accepte a tort. Avec le format, toute + // saisie brute non-ISO (forwardee par MalioDate sur date invalide) echoue la + // denormalisation -> 422 sur foundedAt (cf. collectDenormalizationErrors). + #[Context(denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => '!Y-m-d'])] private ?DateTimeImmutable $foundedAt = null; #[ORM\Column(nullable: true)] diff --git a/src/Module/Commercial/Domain/Entity/Supplier.php b/src/Module/Commercial/Domain/Entity/Supplier.php index 30709a1..c182e62 100644 --- a/src/Module/Commercial/Domain/Entity/Supplier.php +++ b/src/Module/Commercial/Domain/Entity/Supplier.php @@ -22,8 +22,10 @@ use DateTimeImmutable; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Attribute\Context; use Symfony\Component\Serializer\Attribute\Groups; use Symfony\Component\Serializer\Attribute\SerializedName; +use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Context\ExecutionContextInterface; @@ -94,6 +96,12 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; security: "is_granted('commercial.suppliers.manage')", normalizationContext: ['groups' => ['supplier:read', 'default:read', 'category:read', 'site:read']], denormalizationContext: ['groups' => ['supplier:write:main']], + // Une valeur de mauvais type (ex. date non parsable sur foundedAt) + // doit produire un 422 porte sur le champ (violations[].propertyPath, + // mappable inline par useFormErrors) plutot qu'un 400 generique. Le + // front (MalioDate, MUI-44) forwarde la saisie brute invalide : le + // back reste la couche autoritaire du format (ERP-101). Cf. Client. + collectDenormalizationErrors: true, processor: SupplierProcessor::class, ), new Patch( @@ -113,6 +121,10 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; 'supplier:write:accounting', 'supplier:write:archive', ]], + // Cf. Post : date non parsable (foundedAt) -> 422 porte sur le champ + // au lieu d'un 400 generique. Indispensable au mapping inline du + // front (MalioDate MUI-44 forwarde la saisie brute invalide). + collectDenormalizationErrors: true, provider: SupplierProvider::class, processor: SupplierProcessor::class, ), @@ -187,6 +199,11 @@ class Supplier implements TimestampableInterface, BlamableInterface #[ORM\Column(type: 'date_immutable', nullable: true)] #[Groups(['supplier:read', 'supplier:write:information'])] + // Format d'ENTREE strict ISO `Y-m-d` : sans lui, PHP DateTime accepte des + // formes ambigues (« 12/25/2026 », jugee invalide par MalioDate en JJ/MM/AAAA, + // serait lue en M/J -> 25 decembre et acceptee a tort). Avec le format, toute + // saisie brute non-ISO echoue -> 422 sur foundedAt. Cf. Client. + #[Context(denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => '!Y-m-d'])] private ?DateTimeImmutable $foundedAt = null; #[ORM\Column(nullable: true)] diff --git a/tests/Module/Commercial/Api/ClientFoundedAtFormatTest.php b/tests/Module/Commercial/Api/ClientFoundedAtFormatTest.php new file mode 100644 index 0000000..6cb6c3e --- /dev/null +++ b/tests/Module/Commercial/Api/ClientFoundedAtFormatTest.php @@ -0,0 +1,71 @@ + 422 porte sur foundedAt (et pas un 400 generique). */ + public function testFoundedAtNonParsableEst422(): void + { + $client = $this->createAdminClient(); + $seed = $this->seedClient('Founded Format SARL'); + + $body = $client->request('PATCH', '/api/clients/'.$seed->getId(), [ + 'headers' => ['Content-Type' => self::MERGE], + 'json' => ['foundedAt' => '32/13/2026'], + ])->toArray(false); + + self::assertResponseStatusCodeSame(422); + self::assertArrayHasKey('foundedAt', $this->violationsByPath($body)); + } + + /** + * Cas piege : « 12/25/2026 » est invalide cote front (JJ/MM/AAAA -> mois 25) + * mais PHP DateTime l'accepterait en M/J/AAAA (25 decembre). Le format d'entree + * strict ISO `Y-m-d` (Context sur foundedAt) doit le rejeter -> 422. + */ + public function testFoundedAtFormatAmbiguUsEst422(): void + { + $client = $this->createAdminClient(); + $seed = $this->seedClient('Founded Ambigu SARL'); + + $body = $client->request('PATCH', '/api/clients/'.$seed->getId(), [ + 'headers' => ['Content-Type' => self::MERGE], + 'json' => ['foundedAt' => '12/25/2026'], + ])->toArray(false); + + self::assertResponseStatusCodeSame(422); + self::assertArrayHasKey('foundedAt', $this->violationsByPath($body)); + } + + /** Non-regression : une date ISO valide reste acceptee (200). */ + public function testFoundedAtIsoValideEst200(): void + { + $client = $this->createAdminClient(); + $seed = $this->seedClient('Founded Ok SARL'); + + $data = $client->request('PATCH', '/api/clients/'.$seed->getId(), [ + 'headers' => ['Content-Type' => self::MERGE], + 'json' => ['foundedAt' => '2010-05-01'], + ])->toArray(); + + self::assertResponseStatusCodeSame(200); + self::assertStringStartsWith('2010-05-01', $data['foundedAt']); + } +} diff --git a/tests/Module/Commercial/Api/SupplierFoundedAtFormatTest.php b/tests/Module/Commercial/Api/SupplierFoundedAtFormatTest.php new file mode 100644 index 0000000..2451688 --- /dev/null +++ b/tests/Module/Commercial/Api/SupplierFoundedAtFormatTest.php @@ -0,0 +1,71 @@ + 422 porte sur foundedAt (et pas un 400 generique). */ + public function testFoundedAtNonParsableEst422(): void + { + $seed = $this->seedSupplier('Founded Format Negoce'); + $credentials = $this->createUserWithPermission('commercial.suppliers.manage'); + $client = $this->authenticatedClient($credentials['username'], $credentials['password']); + + $body = $client->request('PATCH', '/api/suppliers/'.$seed->getId(), [ + 'headers' => ['Content-Type' => self::MERGE], + 'json' => ['foundedAt' => '32/13/2026'], + ])->toArray(false); + + self::assertResponseStatusCodeSame(422); + self::assertArrayHasKey('foundedAt', $this->violationsByPath($body)); + } + + /** + * Cas piege : « 12/25/2026 » est invalide cote front (JJ/MM/AAAA -> mois 25) + * mais PHP DateTime l'accepterait en M/J/AAAA. Le format d'entree strict ISO + * `Y-m-d` (Context sur foundedAt) doit le rejeter -> 422. + */ + public function testFoundedAtFormatAmbiguUsEst422(): void + { + $seed = $this->seedSupplier('Founded Ambigu Negoce'); + $credentials = $this->createUserWithPermission('commercial.suppliers.manage'); + $client = $this->authenticatedClient($credentials['username'], $credentials['password']); + + $body = $client->request('PATCH', '/api/suppliers/'.$seed->getId(), [ + 'headers' => ['Content-Type' => self::MERGE], + 'json' => ['foundedAt' => '12/25/2026'], + ])->toArray(false); + + self::assertResponseStatusCodeSame(422); + self::assertArrayHasKey('foundedAt', $this->violationsByPath($body)); + } + + /** Non-regression : une date ISO valide reste acceptee (200). */ + public function testFoundedAtIsoValideEst200(): void + { + $seed = $this->seedSupplier('Founded Ok Negoce'); + $credentials = $this->createUserWithPermission('commercial.suppliers.manage'); + $client = $this->authenticatedClient($credentials['username'], $credentials['password']); + + $data = $client->request('PATCH', '/api/suppliers/'.$seed->getId(), [ + 'headers' => ['Content-Type' => self::MERGE], + 'json' => ['foundedAt' => '2010-05-01'], + ])->toArray(); + + self::assertResponseStatusCodeSame(200); + self::assertStringStartsWith('2010-05-01', $data['foundedAt']); + } +}