Skip to content

Commit

Permalink
[TRA-14702] Correction du poids d'une annexe 1 après la révision du p…
Browse files Browse the repository at this point in the history
…oids d'un BSD enfant (#3631)

* fix: fixed back & front to take weight change into account

* fix: fixed sample number as well

* fix: small frontend fix
  • Loading branch information
GaelFerrand authored Oct 4, 2024
1 parent 6c0b483 commit 6957b77
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 2 deletions.
8 changes: 8 additions & 0 deletions back/src/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ export const dateToXMonthAtHHMM = (date: Date = new Date()): string => {
export const isDefined = (obj: any) => {
return obj !== null && obj !== undefined;
};

/**
* Tests if a list of objects are ALL defined. 0 will be considered as defined
*/
export const areDefined = (...obj: any[]) => {
if (obj.some(o => !isDefined(o))) return false;
return true;
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { distinct } from "../../../common/arrays";
import { ForbiddenError } from "../../../common/errors";
import { isFinalOperationCode } from "../../../common/operationCodes";
import { operationHook } from "../../operationHook";
import { areDefined } from "../../../common/helpers";

export type AcceptRevisionRequestApprovalFn = (
revisionRequestApprovalId: string,
Expand Down Expand Up @@ -176,6 +177,8 @@ async function getUpdateFromFormRevisionRequest(
...(revisionRequest.wasteDetailsPop !== null && {
wasteDetailsPop: revisionRequest.wasteDetailsPop
}),
wasteDetailsQuantity: revisionRequest.wasteDetailsQuantity,
wasteDetailsSampleNumber: revisionRequest.wasteDetailsSampleNumber,
...(revisionRequest.wasteDetailsPackagingInfos && {
wasteDetailsPackagingInfos: revisionRequest.wasteDetailsPackagingInfos
}),
Expand Down Expand Up @@ -363,6 +366,46 @@ export async function approveAndApplyRevisionRequest(
}
}

// Revision targeted an ANNEXE_1 (child). We need to update its parent
if (updatedBsdd.emitterType === EmitterType.APPENDIX1_PRODUCER) {
// Quantity changed. We need to fix parent's quantity as well
if (
areDefined(
bsddBeforeRevision.wasteDetailsQuantity,
revisionRequest.wasteDetailsQuantity
)
) {
// Calculate revision diff
const diff = bsddBeforeRevision
.wasteDetailsQuantity!.minus(revisionRequest.wasteDetailsQuantity!)
.toDecimalPlaces(6);
const { nextForm: parent } = await prisma.formGroupement.findFirstOrThrow(
{
where: { initialFormId: bsddBeforeRevision.id },
include: {
nextForm: true
}
}
);

// Calculate parent new quantity using diff
const parentNewQuantity = parent.wasteDetailsQuantity
?.minus(diff)
.toDecimalPlaces(6);

// Update & re-index parent
await prisma.form.update({
where: { id: parent.id },
data: {
wasteDetailsQuantity: parentNewQuantity
}
});
prisma.addAfterCommitCallback?.(() => {
enqueueUpdatedBsdToIndex(parent.readableId);
});
}
}

if (updatedBsdd.emitterType === EmitterType.APPENDIX1) {
const { wasteDetailsCode, wasteDetailsName, wasteDetailsPop } =
revisionRequest;
Expand Down
Loading

0 comments on commit 6957b77

Please sign in to comment.