Skip to content

Commit

Permalink
Merge pull request #3720 from MTES-MCT/tra-15240/mark-as-received-qua…
Browse files Browse the repository at this point in the history
…ntity

TRA-15240 La présence d'une quantité reçue est requise pour passer du statut SENT à ACCEPTED via la mutation markAsReceived
  • Loading branch information
providenz authored Nov 11, 2024
2 parents e427ba7 + 17b34e8 commit cc7ed49
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ et le projet suit un schéma de versionning inspiré de [Calendar Versioning](ht
#### :boom: Breaking changes

- Le champ "Numéro de notification" est obligatoire lorsque la destination ultérieure renseignée est étrangère [PR 3719](https://github.com/MTES-MCT/trackdechets/pull/3719)
- La présence d'une quantité reçue est requise pour passer du statut SENT à ACCEPTED via la mutation markAsReceived [PR 3720](https://github.com/MTES-MCT/trackdechets/pull/3720)

# [2024.10.1] 22/10/2024

Expand Down
10 changes: 7 additions & 3 deletions back/src/common/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ export const weightConditions: WeightConditions = {
WasteAcceptationStatus.PARTIALLY_REFUSED
].includes(status)
) {
return weight.positive(
"${path} : le poids doit être supérieur à 0 lorsque le déchet est accepté ou accepté partiellement"
);
return weight
.required(
"${path} : le poids est requis lorsque le déchet est accepté ou accepté partiellement."
)
.positive(
"${path} : le poids doit être supérieur à 0 lorsque le déchet est accepté ou accepté partiellement"
);
}
return weight;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import makeClient from "../../../../__tests__/testClient";
import { prepareDB, prepareRedis } from "../../../__tests__/helpers";
import { allowedFormats } from "../../../../common/dates";
import { ErrorCode } from "../../../../common/errors";
import {
Mutation,
MutationMarkAsReceivedArgs
Expand Down Expand Up @@ -191,6 +192,111 @@ describe("Test Form reception", () => {
expect(logs[0].status).toBe("ACCEPTED");
});

it("it should not mark a sent form as accepted if wasteAcceptationStatus is ACCEPTED but quantityReceived is 0", async () => {
const {
emitterCompany,
recipient,
recipientCompany,
form: initialForm
} = await prepareDB();
const form = await prisma.form.update({
where: { id: initialForm.id },
data: { currentTransporterOrgId: siretify(3) }
});
await prepareRedis({
emitterCompany,
recipientCompany
});
const frm1 = await prisma.form.findUniqueOrThrow({
where: { id: form.id }
});

expect(frm1.quantityReceivedType).toBeNull();
const { mutate } = makeClient(recipient);
const { errors } = await mutate(MARK_AS_RECEIVED, {
variables: {
id: form.id,
receivedInfo: {
receivedBy: "Bill",
receivedAt: "2019-01-17T10:22:00+0100",
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 0
}
}
});

expect(errors).toEqual([
expect.objectContaining({
message:
"Réception : le poids doit être supérieur à 0 lorsque le déchet est accepté ou accepté partiellement",
extensions: expect.objectContaining({
code: ErrorCode.BAD_USER_INPUT
})
})
]);
const frm = await prisma.form.findUniqueOrThrow({
where: { id: form.id }
});
// form was not accepted, still sent
expect(frm.status).toBe("SENT");
expect(frm.wasteAcceptationStatus).toBe(null);
expect(frm.receivedBy).toBe(null);
expect(frm.quantityReceived).toBe(null);
});

it("it should not mark a sent form as accepted if wasteAcceptationStatus is ACCEPTED but quantityReceived is missing", async () => {
const {
emitterCompany,
recipient,
recipientCompany,
form: initialForm
} = await prepareDB();
const form = await prisma.form.update({
where: { id: initialForm.id },
data: { currentTransporterOrgId: siretify(3) }
});
await prepareRedis({
emitterCompany,
recipientCompany
});
const frm1 = await prisma.form.findUniqueOrThrow({
where: { id: form.id }
});

expect(frm1.quantityReceivedType).toBeNull();
const { mutate } = makeClient(recipient);
const { errors } = await mutate(MARK_AS_RECEIVED, {
variables: {
id: form.id,
receivedInfo: {
receivedBy: "Bill",
receivedAt: "2019-01-17T10:22:00+0100",
signedAt: "2019-01-17T10:22:00+0100",
wasteAcceptationStatus: "ACCEPTED"
}
}
});

expect(errors).toEqual([
expect.objectContaining({
message:
"Réception : le poids est requis lorsque le déchet est accepté ou accepté partiellement.",
extensions: expect.objectContaining({
code: ErrorCode.BAD_USER_INPUT
})
})
]);
const frm = await prisma.form.findUniqueOrThrow({
where: { id: form.id }
});
// form was not accepted, still sent
expect(frm.status).toBe("SENT");
expect(frm.wasteAcceptationStatus).toBe(null);
expect(frm.receivedBy).toBe(null);
expect(frm.quantityReceived).toBe(null);
});

it("should not accept negative values", async () => {
const { emitterCompany, recipient, recipientCompany, form } =
await prepareDB();
Expand Down Expand Up @@ -1365,4 +1471,57 @@ describe("Test Form reception", () => {
expect(sendMail as jest.Mock).toHaveBeenCalledTimes(0);
expect(data.markAsReceived.status).toBe("ACCEPTED");
});

it("quantityReceived is required for a final destination to accept a BSD", async () => {
const emitter = await userWithCompanyFactory("MEMBER");
const tempStorer = await userWithCompanyFactory("MEMBER");
const destination = await userWithCompanyFactory("MEMBER");
const form = await formWithTempStorageFactory({
ownerId: emitter.user.id,
opt: {
emitterCompanySiret: emitter.company.siret,
recipientCompanySiret: tempStorer.company.siret,
status: "RESENT",
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 10,
quantityRefused: 0
},
forwardedInOpts: {
sentAt: new Date(),
emitterCompanySiret: tempStorer.company.siret,
recipientCompanySiret: destination.company.siret,
emittedAt: new Date()
}
});

const { mutate } = makeClient(destination.user);
const { errors } = await mutate<
Pick<Mutation, "markAsReceived">,
MutationMarkAsReceivedArgs
>(MARK_AS_RECEIVED, {
variables: {
id: form.id,
receivedInfo: {
receivedAt: new Date("2022-01-01").toISOString() as any,
receivedBy: "John",

wasteAcceptationStatus: "ACCEPTED"
// quantityReceived not provided,
}
}
});

expect(errors).toEqual([
expect.objectContaining({
message:
"Réception : le poids est requis lorsque le déchet est accepté ou accepté partiellement."
})
]);

const frm = await prisma.form.findUniqueOrThrow({
where: { id: form.id }
});
// form was not accepted, still resent
expect(frm.status).toBe("RESENT");
});
});
2 changes: 2 additions & 0 deletions back/src/forms/typeDefs/bsdd.inputs.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ input ReceivedFormInput {
Doit être égale à 0 lorsque le déchet est refusé.
Doit être inférieure à 40T en cas de transport routier et inférieure à 50 000 T tout type de transport confondu.
Le champ est requis pour passer du statut `SENT` à `ACCEPTED` via `markAsReceived`.
"""
quantityReceived: Float

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"scalingo-postbuild": "bash scripts/build/scalingo.sh",
"nx": "nx",
"dev": "npx nx run-many -t serve --parallel=7 --projects=front,api,tag:backend:background",
"bg:integration": "npx nx run-many -t serve --configuration=integration --projects=tag:backend:queues --parallel=5",
"bg:integration": "npx nx run-many -t serve --configuration=integration --projects=tag:backend:queues --parallel=6",
"afterpull": "npm i ; npx prisma migrate dev ; npx nx run back:codegen --skip-nx-cache ; npx nx run @td/codegen-ui:build --skip-nx-cache"
},
"engines": {
Expand Down

0 comments on commit cc7ed49

Please sign in to comment.