Skip to content

Commit

Permalink
Correctifs sur les DASRI pour le nouveau mode de transport obligatoire (
Browse files Browse the repository at this point in the history
#3596)

* fix: fixed simple DASRI

* fix: not requiring transport mode for synthesis dasris

* fix: PR review fix

* fix: lint
  • Loading branch information
GaelFerrand authored Sep 23, 2024
1 parent be51fcb commit 90d8809
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { resetDatabase } from "../../../../../integration-tests/helper";
import {
companyFactory,
transporterReceiptFactory,
userWithCompanyFactory
} from "../../../../__tests__/factories";
import makeClient from "../../../../__tests__/testClient";
import {
BsdasriStatus,
BsdasriType,
TransportMode,
WasteAcceptationStatus
} from "@prisma/client";
Expand All @@ -32,7 +34,7 @@ const UPDATE_DASRI = gql`
`;

describe("Mutation.signBsdasri transport", () => {
afterEach(resetDatabase);
afterAll(resetDatabase);

it("should put transport signature on a SIGNED_BY_PRODUCER dasri", async () => {
const { company: emitterCompany } = await userWithCompanyFactory("MEMBER");
Expand Down Expand Up @@ -219,14 +221,19 @@ describe("Mutation.signBsdasri transport", () => {
// Update ?
const { mutate } = makeClient(transporter);
if (updateOpt) {
await mutate<Pick<Mutation, "updateBsdasri">>(UPDATE_DASRI, {
variables: {
id: dasri.id,
input: {
...updateOpt
const { errors } = await mutate<Pick<Mutation, "updateBsdasri">>(
UPDATE_DASRI,
{
variables: {
id: dasri.id,
input: {
...updateOpt
}
}
}
});
);

expect(errors).toBeUndefined();
}

// Sign transport
Expand Down Expand Up @@ -304,5 +311,50 @@ describe("Mutation.signBsdasri transport", () => {
expect(errors).not.toBeUndefined();
expect(errors[0].message).toBe("Le mode de transport est obligatoire.");
});

it("transport mode is not required for synthesis DASRI", async () => {
// Given
const { company: initialCompany } = await userWithCompanyFactory(
"MEMBER"
);
const { user: transporter, company: transporterCompany } =
await userWithCompanyFactory("MEMBER");
await transporterReceiptFactory({ company: transporterCompany });
const { company: destinationCompany } = await userWithCompanyFactory(
"MEMBER"
);
const mainCompany = await companyFactory();
const initialBsdasri = await bsdasriFactory({
opt: {
...initialData(initialCompany)
}
});
const synthesisBsdasri = await bsdasriFactory({
opt: {
type: BsdasriType.SYNTHESIS,
...initialData(mainCompany),
...readyToPublishData(destinationCompany),
...readyToTakeOverData(transporterCompany),
status: BsdasriStatus.SIGNED_BY_PRODUCER,
synthesizing: { connect: [{ id: initialBsdasri.id }] },
transporterTransportMode: null
}
});

// When
const { mutate } = makeClient(transporter);
const { errors } = await mutate<Pick<Mutation, "signBsdasri">>(
SIGN_DASRI,
{
variables: {
id: synthesisBsdasri.id,
input: { type: "TRANSPORT", author: "Jimmy" }
}
}
);

// Then
expect(errors).toBeUndefined();
});
});
});
18 changes: 15 additions & 3 deletions back/src/bsdasris/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
transporterRecepisseSchema
} from "../common/validation";
import { destinationOperationModeValidation } from "../common/validation/operationMode";
import { isDefined } from "../common/helpers";

const wasteCodes = DASRI_WASTE_CODES.map(el => el.code);

Expand Down Expand Up @@ -484,9 +485,20 @@ export const transportSchema: FactorySchemaOf<
transporterTransportMode: yup
.mixed<TransportMode>()
.nullable()
.requiredIf(
context.transportSignature,
"Le mode de transport est obligatoire."
.test(
"transport-mode",
"Le mode de transport est obligatoire.",
function (value) {
// Required only at transport signature
if (!context.transportSignature) return true;

// Not required for synthesis DASRI
if (this.parent.type === BsdasriType.SYNTHESIS) {
return true;
}

return isDefined(value);
}
)
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
QueryBsdasriArgs,
BsdasriSignatureType,
MutationUpdateBsdasriArgs,
Bsdasri
Bsdasri,
TransportMode
} from "@td/codegen-ui";
import {
NotificationError,
Expand Down Expand Up @@ -165,6 +166,13 @@ export function RouteSignBsdasri({
getComputedState(getInitialState(), bsdasri)
);

// DASRI can be created via API with a null transport mode.
// getComputedState() will not fix it because it doesn't override null values,
// so fix it here manually
if (!formState.transporter.transport.mode) {
formState.transporter.transport.mode = TransportMode.Road;
}

const TODAY = new Date();

return (
Expand Down

0 comments on commit 90d8809

Please sign in to comment.