Skip to content

Commit

Permalink
Merge pull request #3230 from MTES-MCT/fix/decimal-bug
Browse files Browse the repository at this point in the history
Correction d'un effet de bord du passage à Decimal
  • Loading branch information
benoitguigal authored Apr 5, 2024
2 parents f334273 + 1055260 commit 507086e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,37 @@ describe("Mutation.updateForm", () => {
expect(errors).toBeUndefined();
});

it("should be possible to resend same Decimal value when emitter has signed", async () => {
const emitter = await userWithCompanyFactory("ADMIN");
const destination = await userWithCompanyFactory("ADMIN");
const intermediary1 = await companyFactory();
const form = await formFactory({
ownerId: emitter.user.id,
opt: {
status: "SIGNED_BY_PRODUCER",
emitterCompanySiret: emitter.company.siret,
recipientCompanySiret: destination.company.siret,
emittedAt: new Date(),
intermediaries: { create: toIntermediaryCompany(intermediary1) }
}
});
const { mutate } = makeClient(destination.user);

const { errors } = await mutate<
Pick<Mutation, "updateForm">,
MutationUpdateFormArgs
>(UPDATE_FORM, {
variables: {
updateFormInput: {
id: form.id,
wasteDetails: { quantity: form.wasteDetailsQuantity?.toNumber() }
}
}
});

expect(errors).toBeUndefined();
});

it.each(["emitter", "trader", "broker", "recipient", "transporter"])(
"should allow %p to update a draft form",
async role => {
Expand Down
24 changes: 24 additions & 0 deletions back/src/forms/workflow/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PrismaFormWithForwardedInAndTransporters,
expandFormFromDb
} from "../converter";
import Decimal from "decimal.js";

export function isArray(obj) {
return {}.toString.apply(obj) === "[object Array]";
Expand All @@ -16,6 +17,11 @@ export function isString(obj) {
return {}.toString.call(obj) === "[object String]";
}

export function isNumber(obj) {
const objectType = {}.toString.apply(obj);
return objectType === "[object Decimal]" || objectType === "[object Number]";
}

export function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
Expand Down Expand Up @@ -48,6 +54,16 @@ export function stringEqual(
return (s1 ?? "") === (s2 ?? "");
}

export function numberEqual(
n1: number | Decimal | null | undefined,
n2: number | Decimal | null | undefined
) {
if (n1 && n2) {
return new Decimal(n1).equals(new Decimal(n2));
}
return n1 === n2;
}

export function objectDiff(o1, o2) {
return Object.keys(o2).reduce((diff, key) => {
if (isObject(o2[key])) {
Expand Down Expand Up @@ -84,6 +100,14 @@ export function objectDiff(o1, o2) {
[key]: o2[key]
};
}

if (isNumber(o2[key])) {
if (numberEqual(o1[key], o2[key])) {
return diff;
}
return { ...diff, [key]: o2[key] };
}

if (o1[key] === o2[key] || !(key in o1)) return diff;
return {
...diff,
Expand Down

0 comments on commit 507086e

Please sign in to comment.