Skip to content

Commit ea15cad

Browse files
authored
Merge pull request #3833 from Northeastern-Electric-Racing/#3832--maintenance-multi-project-RRs-deduct-total-cost
#3832: fixed backend over-calculating budget deductions from RRs that a project has an item on
2 parents ca6acc3 + bb4d004 commit ea15cad

2 files changed

Lines changed: 67 additions & 8 deletions

File tree

src/backend/src/services/finance.services.ts

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,13 +434,31 @@ export default class FinanceServices {
434434
},
435435
select: {
436436
reimbursementStatuses: true,
437-
totalCost: true
437+
totalCost: true,
438+
reimbursementProducts: {
439+
where: {
440+
reimbursementProductReason: {
441+
wbsElement: {
442+
project: {
443+
projectId
444+
}
445+
}
446+
}
447+
},
448+
select: {
449+
cost: true
450+
}
451+
}
438452
}
439453
});
440454

441455
const { pendingFinance, pendingLeadership, submittedToSabo, reimbursed } = computeRRTotals(reimbursementRequests);
442456

443-
const totalBalance = reimbursementRequests.reduce((acc, curr) => acc + curr.totalCost, 0) / 100;
457+
const totalBalance =
458+
reimbursementRequests.reduce((acc, curr) => {
459+
const projectProductsCost = curr.reimbursementProducts.reduce((prodAcc, prod) => prodAcc + prod.cost, 0);
460+
return acc + projectProductsCost;
461+
}, 0) / 100;
444462

445463
const available = project.budget - totalBalance;
446464

@@ -507,15 +525,37 @@ export default class FinanceServices {
507525
},
508526
select: {
509527
reimbursementStatuses: true,
510-
totalCost: true
528+
totalCost: true,
529+
reimbursementProducts: {
530+
where: {
531+
reimbursementProductReason: {
532+
wbsElement: {
533+
project: {
534+
teams: {
535+
some: {
536+
teamId
537+
}
538+
}
539+
}
540+
}
541+
}
542+
},
543+
select: {
544+
cost: true
545+
}
546+
}
511547
}
512548
});
513549

514550
const totalBudget = team.projects.reduce((acc, curr) => acc + curr.budget, 0);
515551

516552
const { pendingFinance, pendingLeadership, submittedToSabo, reimbursed } = computeRRTotals(reimbursementRequests);
517553

518-
const totalBalance = reimbursementRequests.reduce((acc, curr) => acc + curr.totalCost, 0) / 100;
554+
const totalBalance =
555+
reimbursementRequests.reduce((acc, curr) => {
556+
const teamProductsCost = curr.reimbursementProducts.reduce((prodAcc, prod) => prodAcc + prod.cost, 0);
557+
return acc + teamProductsCost;
558+
}, 0) / 100;
519559

520560
const available = totalBudget - totalBalance;
521561

@@ -910,7 +950,17 @@ export default class FinanceServices {
910950
},
911951
select: {
912952
reimbursementStatuses: true,
913-
totalCost: true
953+
totalCost: true,
954+
reimbursementProducts: {
955+
where: {
956+
reimbursementProductReason: {
957+
otherReasonId
958+
}
959+
},
960+
select: {
961+
cost: true
962+
}
963+
}
914964
}
915965
});
916966

@@ -935,7 +985,8 @@ export default class FinanceServices {
935985
const lastStatus = req.reimbursementStatuses.at(-1)?.type;
936986

937987
if (lastStatus && totals[lastStatus] !== undefined) {
938-
totals[lastStatus] += req.totalCost;
988+
const categoryProductsCost = req.reimbursementProducts.reduce((prodAcc, prod) => prodAcc + prod.cost, 0);
989+
totals[lastStatus] += categoryProductsCost;
939990
}
940991
});
941992

@@ -944,7 +995,10 @@ export default class FinanceServices {
944995
const submittedToSabo = totals[Reimbursement_Status_Type.SABO_SUBMITTED] ?? 0;
945996
const reimbursed = totals[Reimbursement_Status_Type.REIMBURSED] ?? 0;
946997

947-
const totalBalance = reimbursementRequests.reduce((acc, curr) => acc + curr.totalCost, 0);
998+
const totalBalance = reimbursementRequests.reduce((acc, curr) => {
999+
const categoryProductsCost = curr.reimbursementProducts.reduce((prodAcc, prod) => prodAcc + prod.cost, 0);
1000+
return acc + categoryProductsCost;
1001+
}, 0);
9481002

9491003
const available = totalBudget - totalBalance;
9501004

src/backend/src/utils/finance.utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export const computeRRTotals = (
114114
reimbursementStatusId: string;
115115
userId: string;
116116
}[];
117+
reimbursementProducts?: { cost: number }[];
117118
}[]
118119
): {
119120
pendingFinance: number;
@@ -132,7 +133,11 @@ export const computeRRTotals = (
132133
const lastStatus = req.reimbursementStatuses.at(-1)?.type;
133134

134135
if (lastStatus && totals[lastStatus] !== undefined) {
135-
totals[lastStatus] += req.totalCost;
136+
// If reimbursementProducts are provided, sum their costs; otherwise use totalCost
137+
const costToAdd = req.reimbursementProducts
138+
? req.reimbursementProducts.reduce((acc, prod) => acc + prod.cost, 0)
139+
: req.totalCost;
140+
totals[lastStatus] += costToAdd;
136141
}
137142
});
138143

0 commit comments

Comments
 (0)