diff --git a/frontend/modules/transport/composables/__tests__/useCarrierForm.test.ts b/frontend/modules/transport/composables/__tests__/useCarrierForm.test.ts index 1a0d32a..d77bda9 100644 --- a/frontend/modules/transport/composables/__tests__/useCarrierForm.test.ts +++ b/frontend/modules/transport/composables/__tests__/useCarrierForm.test.ts @@ -404,6 +404,23 @@ describe('useCarrierForm — copie QUALIMAT (ERP-166)', () => { ) }) + it('après création : PATCH en échec → pas de copie locale (rollback) et retour false', async () => { + mockPost.mockResolvedValueOnce({ id: 9, name: 'X', certificationType: 'GMP_PLUS' }) + mockPatch.mockRejectedValueOnce({ response: { status: 500, _data: {} } }) + const form = useCarrierForm() + form.main.name = 'X' + form.main.certificationType = 'GMP_PLUS' + await form.submitMain() + + const ok = await form.applyQualimatSelection(QUALIMAT_ROW) + + // Échec serveur : l'UI ne doit pas refléter une intégration QUALIMAT non persistée. + expect(ok).toBe(false) + expect(form.main.name).toBe('X') + expect(form.main.certificationType).toBe('GMP_PLUS') + expect(form.main.qualimatCarrierIri).toBeNull() + }) + it('buildMainPayload inclut qualimatCarrier + certificationType QUALIMAT après intégration', async () => { const form = useCarrierForm() form.main.name = 'Acme' diff --git a/frontend/modules/transport/composables/useCarrierForm.ts b/frontend/modules/transport/composables/useCarrierForm.ts index a35bc3c..ae6b331 100644 --- a/frontend/modules/transport/composables/useCarrierForm.ts +++ b/frontend/modules/transport/composables/useCarrierForm.ts @@ -260,10 +260,27 @@ export function useCarrierForm() { * § 2.5) : copie le nom, force la certification à « QUALIMAT » (lecture seule), * pose la FK `qualimatCarrier` (IRI) et copie l'adresse (pour l'onglet Adresses). * Si le transporteur existe déjà (post-POST, cas nominal de l'onglet), persiste - * la copie via un PATCH partiel `carrier:write:main`. La copie locale a lieu - * dans tous les cas. Retourne true si l'intégration a abouti. + * d'abord la copie via un PATCH partiel `carrier:write:main` : la copie locale + * (nom, certification figée « QUALIMAT », FK, adresse) n'est appliquée qu'en cas + * de succès, pour ne pas laisser l'UI dans un état QUALIMAT non sauvegardé si le + * PATCH échoue. Retourne true si l'intégration a abouti. */ async function applyQualimatSelection(row: QualimatCarrierRow): Promise { + // Transporteur déjà créé : on persiste avant de refléter localement. + if (carrierId.value !== null) { + try { + await patchCarrier({ + qualimatCarrier: row['@id'], + name: row.name, + certificationType: 'QUALIMAT', + }) + } + catch (error) { + mainErrors.handleApiError(error, { fallbackMessage: t('transport.carriers.toast.error') }) + return false + } + } + main.name = row.name ?? '' main.certificationType = 'QUALIMAT' main.qualimatCarrierIri = row['@id'] @@ -273,23 +290,7 @@ export function useCarrierForm() { city: row.city ?? '', street: row.address ?? '', } - - if (carrierId.value === null) { - return true - } - - try { - await patchCarrier({ - qualimatCarrier: row['@id'], - name: row.name, - certificationType: 'QUALIMAT', - }) - return true - } - catch (error) { - mainErrors.handleApiError(error, { fallbackMessage: t('transport.carriers.toast.error') }) - return false - } + return true } /**