Skip to content

Commit

Permalink
fix(api): handle undefined exercise 🙄
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-telescoop committed Nov 7, 2024
1 parent 3e338b1 commit b621fc1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/api/src/modules/grant/grant.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export default class GrantAdapter {
// application part
exercice:
grant?.application?.annee_demande?.value ??
paymentService.getPaymentExercise((grant?.payments as Payment[])[0]),
paymentService.getPaymentExercise((grant?.payments as Payment[])?.[0]) ??
0,
action: getValue(grant?.application?.actions_proposee?.[0]?.intitule),
askedAmount: getValue(grant?.application?.montants?.demande),
grantedAmount: getValue(grant?.application?.montants?.accorde),
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/modules/grant/grant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,10 @@ export class GrantService {
splitGrantByExercise(grant: Grant): Grant[] {
const { application, payments } = grant;
const byYear: Record<number, Grant> = {};
const NO_YEAR = "unknown";
const NO_YEAR = 0;

if (application) byYear[subventionsService.getSubventionExercise(application) ?? NO_YEAR] = { application };
if (application)
byYear[subventionsService.getSubventionExercise(application) ?? NO_YEAR] = { application, payments: null };

let year: number;
for (const payment of payments ?? []) {
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/modules/payments/payments.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ describe("PaymentsService", () => {
const actual = paymentsService.getPaymentExercise(PAYMENT);
expect(actual).toBe(expected);
});

it("returns undefined if payment is undefined", () => {
const expected = undefined;
const actual = paymentsService.getPaymentExercise(undefined);
expect(actual).toBe(expected);
});
});
});
5 changes: 3 additions & 2 deletions packages/api/src/modules/payments/payments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export class PaymentsService {
});
}

getPaymentExercise(payment: Payment) {
return payment.dateOperation.value.getFullYear();
getPaymentExercise(payment: Payment | undefined) {
if (!payment) return undefined;
return payment?.dateOperation?.value?.getFullYear();
}
}

Expand Down

0 comments on commit b621fc1

Please sign in to comment.