Skip to content

Commit

Permalink
Correctifs révisions bsdasris
Browse files Browse the repository at this point in the history
  • Loading branch information
providenz committed May 30, 2024
1 parent 2cf185e commit 33addbd
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ async function checkIfUserCanRequestRevisionOnBsdasri(
"Impossible de créer une révision sur un bordereau de synthèse ou de groupement."
);
}
if (bsdasri.groupedInId || bsdasri.synthesizedInId) {
throw new ForbiddenError(
"Impossible de créer une révision sur un bordereau inclus dans une synthèse ou un groupement."
);
}
if (bsdasri.status === BsdasriStatus.INITIAL) {
throw new ForbiddenError(
"Impossible de créer une révision sur ce bordereau. Vous pouvez le modifier directement, aucune signature bloquante n'a encore été apposée."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const mapRevision = (
} ${review?.[bsdName]?.emitter?.pickupSite?.city} ${
review?.[bsdName]?.emitter?.pickupSite?.infos ?? ""
}`
: "",
: "Non renseigné",
dataNewValue: review?.content?.emitter?.pickupSite
? `${review?.content?.emitter?.pickupSite?.address}, ${
review?.content?.emitter?.pickupSite?.postalCode
Expand All @@ -166,11 +166,13 @@ export const mapRevision = (
dataOldValue: review?.[bsdName]?.wasteDetails?.code,
dataNewValue: review?.content?.wasteDetails?.code
},

{
dataName: DataNameEnum.WASTE_CODE,
dataOldValue: review?.bsda?.waste?.code,
dataOldValue: review?.[bsdName]?.waste?.code,
dataNewValue: review?.content?.waste?.code
},

{
dataName: DataNameEnum.POP,
dataOldValue: review?.bsda?.waste?.pop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ type Props = {
readonly hint?: string;
// The variable path: eg; `destination.operation.weight`
readonly path: string;
// The value to display
readonly value: string | number | React.ReactNode;
// Value coming from revised bsd, allowing reset when component is closed
readonly defaultValue: any;
// Optional value to initialize children field value, useful for booleans
readonly initialValue?: any;
readonly value: string | number | React.ReactNode;

// is the field disabled
readonly disabled?: boolean;

Expand All @@ -24,7 +26,6 @@ type Props = {
const LabelContent = ({
labelText,
suffix,

value
}: {
labelText: string;
Expand All @@ -46,11 +47,11 @@ const LabelContent = ({
export function RhfReviewableField({
title,
suffix,
defaultValue,
value,
defaultValue,
initialValue,
path,
hint,
initialValue,

children,
disabled = false
Expand All @@ -61,7 +62,6 @@ export function RhfReviewableField({
function handleIsEditingChange() {
if (isEditing) {
// When toggling visibility to off, set children value to pre-existing value

setValue(path, defaultValue);
} else {
// When toggling visibility to on, set children value to optional initialValue (to tell apart empty strings from boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,42 @@ type Props = {
readonly bsdasri: Bsdasri;
};

const bsdasriPackagingSchema = z.object({
type: z.enum(
[
"BOITE_CARTON",
"FUT",
"BOITE_PERFORANTS",
"GRAND_EMBALLAGE",
"GRV",
"AUTRE"
],
{
required_error: "Ce champ est requis",
invalid_type_error: "Ce champ est requis"
const bsdasriPackagingSchema = z
.object({
type: z.enum(
[
"BOITE_CARTON",
"FUT",
"BOITE_PERFORANTS",
"GRAND_EMBALLAGE",
"GRV",
"AUTRE"
],
{
required_error: "Ce champ est requis",
invalid_type_error: "Ce champ est requis"
}
),
other: z.string(),
volume: z.coerce
.number()
.positive("Ce champ est requis est doit être supérieur à 0"),

quantity: z.coerce
.number()
.positive("Ce champ est requis est doit être supérieur à 0")
})
.superRefine((values, context) => {
if (values.type === "AUTRE" && !values.other) {
context.addIssue({
code: z.ZodIssueCode.custom,

message: "Veuillez préciser le conditionnement",

path: ["other"]
});
}
),
other: z.string(),
volume: z.coerce.number().positive().nullish(),

quantity: z.coerce.number().positive()
});
});

const getSchema = () =>
z.object({
Expand Down Expand Up @@ -87,7 +103,9 @@ const getSchema = () =>
.nullish(),
waste: z.object({ code: z.string().nullish() }),
isCanceled: z.boolean().nullish(),
comment: z.string().min(3)
comment: z
.string()
.min(3, "Le commentaire doit faire au moins 3 caractères")
});

const revisionRules = {
Expand Down Expand Up @@ -130,28 +148,6 @@ export function BsdasriRequestRevision({ bsdasri }: Props) {
MutationCreateBsdasriRevisionRequestArgs
>(CREATE_BSDASRI_REVISION_REQUEST);

// const defaultValues = {
// emitter: {
// pickupSite: {
// name: "",
// address: "",
// city: "",
// postalCode: "",
// infos: ""
// }
// },
// destination: {
// reception: { packagings: [] },
// operation: {
// weight: null,
// code: null,
// mode: null
// }
// },
// waste: { code: null },
// isCanceled: false
// };

const initialReview = {
emitter: {
pickupSite: {
Expand Down Expand Up @@ -236,13 +232,17 @@ export function BsdasriRequestRevision({ bsdasri }: Props) {
bsdasri?.destination?.reception?.packagings || []
);

const pickuSiteSummary = [
bsdasri.emitter?.pickupSite?.address,
bsdasri.emitter?.pickupSite?.postalCode,
bsdasri.emitter?.pickupSite?.city
]
.filter(Boolean)
.join(" ");
const pickuSiteSummary =
[
bsdasri.emitter?.pickupSite?.address,
bsdasri.emitter?.pickupSite?.postalCode,
bsdasri.emitter?.pickupSite?.city
]
.filter(Boolean)
.join(" ") || "Non renseigné";

const status = bsdasri["bsdasriStatus"];

return (
<div className={styles.container}>
<h2 className={styles.title}>
Expand All @@ -260,10 +260,7 @@ export function BsdasriRequestRevision({ bsdasri }: Props) {
path="emitter.pickupSite"
value={pickuSiteSummary}
defaultValue={initialReview?.emitter?.pickupSite}
disabled={isDisabled(
"emitter.pickupSite",
bsdasri["bsdasriStatus"]
)}
disabled={isDisabled("emitter.pickupSite", status)}
>
<Input
label="Nom du site d'enlèvement"
Expand Down Expand Up @@ -304,10 +301,8 @@ export function BsdasriRequestRevision({ bsdasri }: Props) {
path="destination.reception.packagings"
value={packagingsSummary}
defaultValue={initialReview?.destination?.reception?.packagings}
disabled={isDisabled(
"destination.reception.packagings",
bsdasri["bsdasriStatus"]
)}
initialValue={bsdasri?.destination?.reception?.packagings}
disabled={isDisabled("destination.reception.packagings", status)}
>
<BsdasriPackagings />
</RhfReviewableField>
Expand All @@ -318,7 +313,7 @@ export function BsdasriRequestRevision({ bsdasri }: Props) {
value={bsdasri?.waste?.code}
defaultValue={initialReview?.waste?.code}
initialValue={null}
disabled={isDisabled("waste.code", bsdasri["bsdasriStatus"])}
disabled={isDisabled("waste.code", status)}
>
<RadioButtons
options={[
Expand Down Expand Up @@ -348,10 +343,7 @@ export function BsdasriRequestRevision({ bsdasri }: Props) {
path="destination.operation.weight"
value={bsdasri?.destination?.operation?.weight?.value}
defaultValue={initialReview?.destination?.operation?.weight}
disabled={isDisabled(
"destination.operation.weight",
bsdasri["bsdasriStatus"]
)}
disabled={isDisabled("destination.operation.weight", status)}
>
<Input
label="Poids en kilos"
Expand Down Expand Up @@ -382,10 +374,7 @@ export function BsdasriRequestRevision({ bsdasri }: Props) {
path="destination.operation.code"
value={bsdasri?.destination?.operation?.code}
defaultValue={initialReview?.destination?.operation?.code}
disabled={isDisabled(
"destination.operation.code",
bsdasri["bsdasriStatus"]
)}
disabled={isDisabled("destination.operation.code", status)}
>
<Select
label="Code de l'opération"
Expand Down
45 changes: 35 additions & 10 deletions front/src/form/bsdasri/components/packagings/RhfPackagings.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from "react";
import React, { useEffect } from "react";
import { Input } from "@codegouvfr/react-dsfr/Input";

import { Button } from "@codegouvfr/react-dsfr/Button";
import { Select } from "@codegouvfr/react-dsfr/Select";

import { BsdasriPackagingType } from "@td/codegen-ui";
import { useFormContext, useFieldArray } from "react-hook-form";
import { PACKAGINGS_NAMES } from "../../utils/packagings";

Expand All @@ -16,22 +16,39 @@ export const emptyPackaging = {

const path = "destination.reception.packagings";

const PaohPackaging = ({ idx, remove, disabled }) => {
const { register, getFieldState } = useFormContext();
const BsdasriPackaging = ({ idx, remove, disabled }) => {
const { register, getFieldState, getValues, setValue } = useFormContext();
const name = `${path}.${idx}`;
const packagingType = getValues(`${name}.type`);

useEffect(() => {
// reset `other` detail field when packaging type is not `Autre`
if (packagingType !== BsdasriPackagingType.Autre) {
setValue(`${name}.other`, "");
}
}, [packagingType, setValue, name]);

// can't manage to retrieve typesafe state through formState
const { error: quantityError } = getFieldState(`${name}.quantity`);
const { error: typeError } = getFieldState(`${name}.type`);
const { error: volumeError } = getFieldState(`${name}.volume`);
const { error: otherError } = getFieldState(`${name}.other`);

return (
<div>
{idx > 0 && <hr />}
<div className="fr-grid-row fr-grid-row--gutters fr-grid-row--bottom fr-mb-1v">
<div className="fr-grid-row fr-grid-row--gutters fr-grid-row--top fr-mb-1v">
<div className="fr-col-12 fr-col-md-2">
<Input
label="Nombre de coli(s)"
state={quantityError && "error"}
stateRelatedMessage={(quantityError?.message as string) ?? ""}
nativeInputProps={{
defaultValue: 1,
...register(`${name}.quantity`)
...register(`${name}.quantity`),
inputMode: "numeric",
pattern: "[0-9]*",
type: "number"
}}
/>
</div>
Expand All @@ -43,8 +60,6 @@ const PaohPackaging = ({ idx, remove, disabled }) => {
state={typeError && "error"}
stateRelatedMessage={(typeError?.message as string) ?? ""}
>
<option value=""></option>

{(
Object.entries(PACKAGINGS_NAMES) as Array<
[keyof typeof PACKAGINGS_NAMES, string]
Expand All @@ -59,6 +74,12 @@ const PaohPackaging = ({ idx, remove, disabled }) => {
<div className="fr-col-12 fr-col-md-2">
<Input
label="Précisez"
disabled={
disabled ||
getValues(`${name}.type`) !== BsdasriPackagingType.Autre
}
state={otherError && "error"}
stateRelatedMessage={(otherError?.message as string) ?? ""}
nativeInputProps={{
defaultValue: "",
...register(`${name}.other`)
Expand All @@ -69,13 +90,15 @@ const PaohPackaging = ({ idx, remove, disabled }) => {
<Input
label="Volume unitaire (l)"
disabled={disabled}
state={volumeError && "error"}
stateRelatedMessage={(volumeError?.message as string) ?? ""}
nativeInputProps={{
...register(`${name}.volume`),
inputMode: "numeric",
pattern: "[0-9]*",
type: "number"
}}
></Input>
/>
</div>

<div className="fr-col-12 fr-col-md-1">
Expand All @@ -95,14 +118,16 @@ const PaohPackaging = ({ idx, remove, disabled }) => {
};

export const BsdasriPackagings = ({ disabled = false }) => {
const { control } = useFormContext(); // retrieve control for initial values
const { fields, append, remove } = useFieldArray({
control,
name: path
});

return (
<div>
{fields.map((packaging, index) => (
<PaohPackaging
<BsdasriPackaging
idx={index}
key={packaging.id}
remove={remove}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@
href="https://faq.trackdechets.fr/amiante/informations-generales/le-bsda-simple-collecte-sur-chantier/modifier-ou-supprimer-un-bsda#modifier-certains-champs-du-bsda-grace-a-la-revision"
>➡️&nbsp;Modifier certains champs du BSDA grâce à la révision</a
>
<br />
<a
href="https://faq.trackdechets.fr/dasri/reglementation/le-parcours-du-bsdasri-simple/modifier-ou-supprimer-un-bsdasri#comment-modifier-certains-champs-du-bsda-grace-a-la-revision"
>➡️&nbsp;Modifier certains champs du DASRI grâce à la révision</a
>
</p>

0 comments on commit 33addbd

Please sign in to comment.