Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRA-15269] La quantité refusée (champ quantityRefused) est désormais obligatoire sur les BSDD #3823

Open
wants to merge 21 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ describe("Query.bsds workflow", () => {
receivedAt: new Date().toISOString() as any,
receivedBy: recipient.user.name,
quantityReceived: 1,
quantityRefused: 0,
signedAt: new Date().toISOString() as any,
wasteAcceptationStatus: "ACCEPTED"
}
Expand Down Expand Up @@ -859,7 +860,8 @@ describe("Query.bsds workflow", () => {
receivedAt: new Date().toISOString() as any,
signedAt: new Date().toISOString() as any,
receivedBy: "Destination",
quantityReceived: 1
quantityReceived: 1,
quantityRefused: 0
}
}
});
Expand Down
157 changes: 115 additions & 42 deletions back/src/forms/__tests__/validation.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ describe("receivedInfosSchema", () => {
const receivedInfo: ReceivedFormInput = {
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 12.5,
quantityRefused: 0,
wasteRefusalReason: "",
receivedBy: "Jim",
receivedAt: new Date("2020-01-17T10:12:00+0100"),
Expand All @@ -1137,7 +1138,8 @@ describe("receivedInfosSchema", () => {
describe("waste is refused", () => {
const receivedInfo: ReceivedFormInput = {
wasteAcceptationStatus: "REFUSED",
quantityReceived: 0,
quantityReceived: 10,
quantityRefused: 10,
wasteRefusalReason: "non conformity",
receivedBy: "Joe",
receivedAt: new Date("2020-01-17T10:12:00+0100"),
Expand Down Expand Up @@ -1165,6 +1167,7 @@ describe("receivedInfosSchema", () => {
const receivedInfo: ReceivedFormInput = {
wasteAcceptationStatus: "PARTIALLY_REFUSED",
quantityReceived: 11,
quantityRefused: 6,
wasteRefusalReason: "mixed waste",
receivedBy: "Bill",
receivedAt: new Date("2020-01-17T10:12:00+0100"),
Expand All @@ -1187,23 +1190,44 @@ describe("receivedInfosSchema", () => {
);
});

it("should be invalid when quantity received is 0", async () => {
const validateFn = () =>
receivedInfoSchema.validate({ ...receivedInfo, quantityReceived: 0 });
await expect(validateFn()).rejects.toThrow(
"Réception : le poids doit être supérieur à 0 lorsque le déchet est accepté ou accepté partiellement"
);
});
// TODO: can no longer be tested because error on quantityRefused pops first
// it("should be invalid when quantity received is 0", async () => {
// const validateFn = () =>
// receivedInfoSchema.validate({
// ...receivedInfo,
// quantityReceived: 0,
// quantityRefused: 0
// });
// await expect(validateFn()).rejects.toThrow(
// "Réception : le poids doit être supérieur à 0 lorsque le déchet est accepté ou accepté partiellement"
// );
// });
});

describe("quantityRefused", () => {
const form = {
receivedBy: "Bill",
createdAt: new Date("2025-03-20T10:12:00+0100"),
receivedAt: new Date("2020-01-17T10:12:00+0100"),
signedAt: new Date("2020-01-17T10:12:00+0100")
};

it("quantityRefused is not mandatory", async () => {
it("quantityRefused is not required if reception only", async () => {
// Given
const update = {
...form,
quantityReceived: 10
// No acceptation data, just plain reception
};

// When
const isValid = await receivedInfoSchema.validate(update);

// Then
expect(isValid).toBeTruthy();
});

it("quantityRefused is required if wasteAcceptationStatus + quantityReceived (PARTIALLY_REFUSED)", async () => {
// Given
const update = {
...form,
Expand All @@ -1212,6 +1236,60 @@ describe("receivedInfosSchema", () => {
quantityReceived: 10
};

// When
const validateFn = () => receivedInfoSchema.validate(update);

// Then
await expect(validateFn()).rejects.toThrow(
"La quantité refusée (quantityRefused) est requise"
);
});

it("quantityRefused is required if wasteAcceptationStatus + quantityReceived (ACCEPTED)", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 10
};

// When
const validateFn = () => receivedInfoSchema.validate(update);

// Then
await expect(validateFn()).rejects.toThrow(
"La quantité refusée (quantityRefused) est requise"
);
});

it("quantityRefused is required if wasteAcceptationStatus + quantityReceived (REFUSED)", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Because",
quantityReceived: 10
};

// When
const validateFn = () => receivedInfoSchema.validate(update);

// Then
await expect(validateFn()).rejects.toThrow(
"La quantité refusée (quantityRefused) est requise"
);
});

it("quantityRefused can be 0 if waste is REFUSED and quantityReceived = 0", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Because",
quantityReceived: 0,
quantityRefused: 0
};

// When
const isValid = await receivedInfoSchema.validate(update);

Expand All @@ -1235,24 +1313,21 @@ describe("receivedInfosSchema", () => {
);
});

it.each([null, undefined, 0])(
"waste is ACCEPTED > quantityReceived = 10 > quantityRefused can be %p",
async quantityRefused => {
// Given
const update = {
...form,
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 10,
quantityRefused
};
it("waste is ACCEPTED > quantityReceived = 10 > quantityRefused must equal 0", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 10,
quantityRefused: 0
};

// When
const isValid = await receivedInfoSchema.validate(update);
// When
const isValid = await receivedInfoSchema.validate(update);

// Then
expect(isValid).toBeTruthy();
}
);
// Then
expect(isValid).toBeTruthy();
});

it.each([5, 10, 15])(
"waste is ACCEPTED > quantityReceived = 10 > quantityRefused can NOT be %p",
Expand All @@ -1275,25 +1350,22 @@ describe("receivedInfosSchema", () => {
}
);

it.each([null, undefined, 10])(
"waste is REFUSED > quantityReceived = 10 > quantityRefused can be %p",
async quantityRefused => {
// Given
const update = {
...form,
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Reason",
quantityReceived: 10,
quantityRefused
};
it("waste is REFUSED > quantityReceived = 10 > quantityRefused must be quantityReceived", async () => {
// Given
const update = {
...form,
wasteAcceptationStatus: "REFUSED",
wasteRefusalReason: "Reason",
quantityReceived: 10,
quantityRefused: 10
};

// When
const isValid = await receivedInfoSchema.validate(update);
// When
const isValid = await receivedInfoSchema.validate(update);

// Then
expect(isValid).toBeTruthy();
}
);
// Then
expect(isValid).toBeTruthy();
});

it.each([0, 3, 15])(
"waste is REFUSED > quantityReceived = 10 > quantityRefused can NOT be %p",
Expand Down Expand Up @@ -2340,6 +2412,7 @@ describe("processedInfoSchema", () => {
const receivedInfo: ReceivedFormInput = {
wasteAcceptationStatus: "ACCEPTED",
quantityReceived: 12.5,
quantityRefused: 0,
wasteRefusalReason: "",
receivedBy: "Jim",
receivedAt: new Date("2020-01-17T10:12:00+0100"),
Expand Down
5 changes: 3 additions & 2 deletions back/src/forms/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,9 @@ export function flattenBsddRevisionRequestInput(
wasteDetailsPackagingInfos: prismaJsonNoNull(
chain(reviewContent, c => chain(c.wasteDetails, w => w.packagingInfos))
),
wasteAcceptationStatus: chain(reviewContent, c => c.wasteAcceptationStatus),
wasteRefusalReason: chain(reviewContent, c => c.wasteRefusalReason),
// Retirés jusqu'à nouvel ordre!
// wasteAcceptationStatus: chain(reviewContent, c => c.wasteAcceptationStatus),
// wasteRefusalReason: chain(reviewContent, c => c.wasteRefusalReason),
wasteDetailsSampleNumber: chain(reviewContent, c =>
chain(c.wasteDetails, w => w.sampleNumber)
),
Expand Down
4 changes: 3 additions & 1 deletion back/src/forms/examples/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ const receivedInfoInput = {
receivedBy: "Antoine Derieux",
receivedAt: "2020-04-05T11:18:00",
signedAt: "2020-04-05T12:00:00",
quantityReceived: 1
quantityReceived: 1,
quantityRefused: 0
};

const processedInfoInput = {
Expand Down Expand Up @@ -168,6 +169,7 @@ const tempStoredInfosInput = {
receivedAt: "2020-05-03T09:00:00",
signedAt: "2020-05-03T09:00:00",
quantityReceived: 1,
quantityRefused: 0,
quantityType: "REAL"
};

Expand Down
Loading
Loading