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 03e37fb
Show file tree
Hide file tree
Showing 5 changed files with 24 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
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Object {
"value": "f84f45a69fca47a7f8a36665b94db7719208abdf",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down Expand Up @@ -148,6 +149,7 @@ Object {
"value": "Accordé",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down Expand Up @@ -224,6 +226,7 @@ Object {
"value": "74959357aff2a17c3a876310f5d90bb037a80427",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down Expand Up @@ -448,6 +451,7 @@ Object {
"value": "2103814945",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down Expand Up @@ -573,6 +577,7 @@ Object {
"value": "EJ00001",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down Expand Up @@ -942,6 +947,7 @@ Object {
"value": "f84f45a69fca47a7f8a36665b94db7719208abdf",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down Expand Up @@ -1012,6 +1018,7 @@ Object {
"value": "Accordé",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down Expand Up @@ -1088,6 +1095,7 @@ Object {
"value": "74959357aff2a17c3a876310f5d90bb037a80427",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down Expand Up @@ -1312,6 +1320,7 @@ Object {
"value": "2103814945",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down Expand Up @@ -1437,6 +1446,7 @@ Object {
"value": "EJ00001",
},
},
"payments": null,
},
Object {
"application": Object {
Expand Down

0 comments on commit 03e37fb

Please sign in to comment.