Skip to content

Commit 08547c8

Browse files
committed
#3951 get team type budget double counting fix
1 parent cf7775d commit 08547c8

1 file changed

Lines changed: 68 additions & 24 deletions

File tree

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

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -611,33 +611,77 @@ export default class FinanceServices {
611611

612612
if (!division) throw new NotFoundException('Team Type', teamTypeId);
613613

614-
const results: ReimbursementRequestData = {
615-
totalBudget: 0,
616-
pendingFinance: 0,
617-
pendingLeadership: 0,
618-
submittedToSabo: 0,
619-
reimbursed: 0,
620-
available: 0
621-
};
622-
614+
const projectsById = new Map<string, { budget: number }>();
623615
for (const team of division.teams) {
624-
const data: ReimbursementRequestData = await this.getReimbursementRequestTeamData(
625-
organization,
626-
team.teamId,
627-
startDate,
628-
endDate,
629-
carNumber
630-
);
631-
632-
results.totalBudget += data.totalBudget;
633-
results.pendingFinance += data.pendingFinance;
634-
results.pendingLeadership += data.pendingLeadership;
635-
results.submittedToSabo += data.submittedToSabo;
636-
results.reimbursed += data.reimbursed;
637-
results.available += data.available;
616+
for (const project of team.projects) {
617+
if (!projectsById.has(project.projectId)) {
618+
projectsById.set(project.projectId, { budget: project.budget });
619+
}
620+
}
638621
}
622+
const totalBudget = [...projectsById.values()].reduce((acc, p) => acc + p.budget, 0);
623+
const divisionProjectIds = [...projectsById.keys()];
624+
625+
const reimbursementRequests = await prisma.reimbursement_Request.findMany({
626+
where: {
627+
reimbursementProducts: {
628+
some: {
629+
reimbursementProductReason: {
630+
wbsElement: {
631+
project: {
632+
projectId: { in: divisionProjectIds }
633+
}
634+
}
635+
}
636+
}
637+
},
638+
reimbursementStatuses: {
639+
none: {
640+
type: Reimbursement_Status_Type.DENIED
641+
}
642+
},
643+
...getReimbursementRequestWhereInput(startDate, endDate, carNumber)
644+
},
645+
select: {
646+
reimbursementStatuses: true,
647+
totalCost: true,
648+
reimbursementProducts: {
649+
where: {
650+
dateDeleted: null,
651+
reimbursementProductReason: {
652+
wbsElement: {
653+
project: {
654+
projectId: { in: divisionProjectIds }
655+
}
656+
}
657+
}
658+
},
659+
select: {
660+
cost: true
661+
}
662+
}
663+
}
664+
});
665+
666+
const { pendingFinance, pendingLeadership, submittedToSabo, reimbursed } = computeRRTotals(reimbursementRequests);
639667

640-
return results;
668+
const totalBalance =
669+
reimbursementRequests.reduce((acc, curr) => {
670+
const teamProductsCost = curr.reimbursementProducts.reduce((prodAcc, prod) => prodAcc + prod.cost, 0);
671+
return acc + teamProductsCost;
672+
}, 0) / 100;
673+
674+
const available = totalBudget - totalBalance;
675+
676+
const data: ReimbursementRequestData = {
677+
totalBudget,
678+
pendingFinance,
679+
pendingLeadership,
680+
submittedToSabo,
681+
reimbursed,
682+
available
683+
};
684+
return data;
641685
}
642686

643687
static async getSpendingBarTeamData(

0 commit comments

Comments
 (0)