Skip to content

Commit

Permalink
Display bsvhu plate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
providenz committed Jan 12, 2025
1 parent 0908262 commit b3e0101
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 35 deletions.
3 changes: 0 additions & 3 deletions back/src/bsvhu/validation/__tests__/validation.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,6 @@ describe("BSVHU validation", () => {
currentSignatureType: "TRANSPORT"
});
} catch (err) {
console.log(err);
expect((err as ZodError).issues).toEqual([
expect.objectContaining({
message:
Expand All @@ -764,7 +763,6 @@ describe("BSVHU validation", () => {
currentSignatureType: "TRANSPORT"
});
} catch (err) {
console.log(err);
expect((err as ZodError).issues).toEqual([
expect.objectContaining({
message:
Expand All @@ -791,7 +789,6 @@ describe("BSVHU validation", () => {
currentSignatureType: "TRANSPORT"
});
} catch (err) {
console.log(err);
expect((err as ZodError).issues).toEqual([
expect.objectContaining({
message: "Le numéro de plaque fourni est incorrect"
Expand Down
6 changes: 3 additions & 3 deletions back/src/bsvhu/validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { BsvhuValidationContext, PrismaBsvhuForParsing } from "./types";
* par signature.
*/
export async function mergeInputAndParseBsvhuAsync(
// BSFF déjà stockée en base de données.
// Bsvhu déjà stocké en base de données.
persisted: PrismaBsvhuForParsing,
// Données entrantes provenant de la couche GraphQL.
input: BsvhuInput,
Expand Down Expand Up @@ -65,7 +65,7 @@ export async function mergeInputAndParseBsvhuAsync(
}

// Calcule la signature courante à partir des données si elle n'est
// pas fourni via le contexte
// pas fournie via le contexte
const currentSignatureType =
context.currentSignatureType ?? getCurrentSignatureType(zodPersisted);

Expand All @@ -75,7 +75,7 @@ export async function mergeInputAndParseBsvhuAsync(
};

// Vérifie que l'on n'est pas en train de modifier des données
// vérrouillées par signature.
// verrouillées par signature.
const updatedFields = await checkBsvhuSealedFields(
zodPersisted,
bsvhu,
Expand Down
40 changes: 40 additions & 0 deletions back/src/bsvhu/validation/refinements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,46 @@ export const checkTransportModeAndReceptionWeight: Refinement<
);
};

const onlyWhiteSpace = (str: string) => !str.trim().length; // check whitespaces, tabs, newlines and invisible chars

export const checkTransportPlates: Refinement<ParsedZodBsvhu> = (
bsvhu,
ctx
) => {
const { transporterTransportPlates } = bsvhu;
const path = ["transporter", "transport", "plates"];
if (transporterTransportPlates.length > 2) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path,
message: "Un maximum de 2 plaques d'immatriculation est accepté"
});
}

if (transporterTransportPlates.some(plate => plate.length > 12)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path,
message: "Un numéro d'immatriculation doit faire 12 caractères au maximum"
});
}

if (transporterTransportPlates.some(plate => plate.length < 4)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path,
message: "Un numéro d'immatriculation doit faire 4 caractères au minimum"
});
}
if (transporterTransportPlates.some(plate => onlyWhiteSpace(plate))) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path,
message: "Le numéro de plaque fourni est incorrect"
});
}
};

export const checkRequiredFields: (
validationContext: BsvhuValidationContext
) => Refinement<ParsedZodBsvhu> = validationContext => {
Expand Down
47 changes: 24 additions & 23 deletions back/src/bsvhu/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
checkEmitterSituation,
checkPackagingAndIdentificationType,
checkTransportModeAndWeight,
checkTransportModeAndReceptionWeight
checkTransportModeAndReceptionWeight,
checkTransportPlates
} from "./refinements";
import { BsvhuValidationContext } from "./types";
import { weightSchema } from "../../common/validation/weight";
Expand Down Expand Up @@ -71,8 +72,6 @@ export const ZodOperationEnum = z

export type ZodOperationEnum = z.infer<typeof ZodOperationEnum>;

const notOnlyWhiteSpace = (str: string) => str.trim(); // check whitespaces, tabs, newlines and invisible chars

const rawBsvhuSchema = z.object({
id: z.string().default(() => getReadableId(ReadableIdPrefix.VHU)),
// on ajoute `createdAt` au schéma de validation pour appliquer certaines
Expand Down Expand Up @@ -192,24 +191,25 @@ const rawBsvhuSchema = z.object({
transporterTransportTakenOverAt: z.coerce.date().nullish(),
transporterCustomInfo: z.string().nullish(),
transporterTransportMode: z.nativeEnum(TransportMode).nullish(),
transporterTransportPlates: z
.array(
z
.string()
.min(4, {
message:
"Un numéro d'immatriculation doit faire 4 caractères au minimum"
})
.max(12, {
message:
"Un numéro d'immatriculation doit faire 12 caractères au maximum"
})
.refine(notOnlyWhiteSpace, {
message: "Le numéro de plaque fourni est incorrect"
})
)
.max(2, "Un maximum de 2 plaques d'immatriculation est accepté")
.default([]),
// transporterTransportPlates: z
// .array(
// z
// .string()
// .min(4, {
// message:
// "Un numéro d'immatriculation doit faire 4 caractères au minimum"
// })
// .max(12, {
// message:
// "Un numéro d'immatriculation doit faire 12 caractères au maximum"
// })
// )
// .max(2, "Un maximum de 2 plaques d'immatriculation est accepté")

// .default([]),

transporterTransportPlates: z.array(z.string()).default([]),

ecoOrganismeName: z.string().nullish(),
ecoOrganismeSiret: siretSchema(CompanyRole.EcoOrganisme).nullish(),
brokerCompanyName: z.string().nullish(),
Expand Down Expand Up @@ -255,7 +255,8 @@ const refinedBsvhuSchema = rawBsvhuSchema
.superRefine(checkEmitterSituation)
.superRefine(checkPackagingAndIdentificationType)
.superRefine(checkTransportModeAndWeight)
.superRefine(checkTransportModeAndReceptionWeight);
.superRefine(checkTransportModeAndReceptionWeight)
.superRefine(checkTransportPlates);

// Transformations synchrones qui sont toujours
// joués même si `enableCompletionTransformers=false`
Expand Down Expand Up @@ -284,7 +285,7 @@ export const contextualBsvhuSchemaAsync = (context: BsvhuValidationContext) => {
.transform((bsvhu: ParsedZodBsvhu) => runTransformers(bsvhu, context))
.superRefine(
// run le check sur les champs requis après les transformations
// au cas où des transformations auto-complète certains champs
// au cas où des transformations auto-complètent certains champs
checkRequiredFields(context)
);
};
16 changes: 10 additions & 6 deletions front/src/Apps/Dashboard/Creation/bsvhu/steps/Transporter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ const TransporterBsvhu = ({ errors }) => {
setError
);
}
if (transporter?.transport?.plates) {
setFieldError(
errors,
`${actor}.transport.plates`,
formState.errors?.[actor]?.["transport"]?.plates,
setError
);
}
}
}, [
errors,
Expand All @@ -104,7 +112,8 @@ const TransporterBsvhu = ({ errors }) => {
transporter?.company?.mail,
transporter?.company?.phone,
transporter?.company?.siret,
transporter?.company?.vatNumber
transporter?.company?.vatNumber,
transporter?.transport?.plates
]);

const orgId = useMemo(
Expand Down Expand Up @@ -220,11 +229,6 @@ const TransporterBsvhu = ({ errors }) => {
fieldName={`transporter.transport.plates`}
hintText="2 max : Véhicule, remorque"
/>
{formState.errors?.transporter?.["transport"]?.plates && (
<p className="fr-text--sm fr-error-text fr-mb-4v">
{formState.errors?.transporter?.["transport"]?.plates?.message}
</p>
)}
</div>
</div>
</>
Expand Down
5 changes: 5 additions & 0 deletions front/src/Apps/Dashboard/Creation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ const pathPrefixToTab = {
if (pathPrefix === "ecoOrganisme") {
return TabId.other;
}
if (pathPrefix.startsWith("transporter")) {
// dirty solution to handle TransporterFooBar paths
return TabId.transporter;
}
if (Object.values(TabId).includes(pathPrefix as TabId)) {
return TabId[pathPrefix];
}
Expand Down Expand Up @@ -147,6 +151,7 @@ export const getPublishErrorMessages = (
apiErrors?: NormalizedError[]
): TabError[] => {
// return an array of messages with tabId, name (path) and the related message

const publishErrorMessages =
apiErrors
?.map(apiError => {
Expand Down

0 comments on commit b3e0101

Please sign in to comment.