diff --git a/api/src/lib/date/index.spec.ts b/api/src/lib/date/index.spec.ts new file mode 100644 index 0000000000..9d466b4404 --- /dev/null +++ b/api/src/lib/date/index.spec.ts @@ -0,0 +1,9 @@ +import { assertEquals } from "@/dev_deps.ts"; +import { differenceInHours } from "@/lib/date/index.ts"; + +Deno.test("should return difference in hours", () => { + const created_at = new Date("2024-10-24 06:37:09"); + const start_datetime = new Date("2024-10-23 05:00:47"); + const result = differenceInHours(created_at, start_datetime); + assertEquals(result, 25.60611111111111); +}); diff --git a/api/src/lib/date/index.ts b/api/src/lib/date/index.ts index 804d9047c7..163ac59c81 100644 --- a/api/src/lib/date/index.ts +++ b/api/src/lib/date/index.ts @@ -1,7 +1,7 @@ import { difference } from "@/deps.ts"; export function differenceInHours(d1: Date, d2: Date): number { - return difference(d1, d2, { units: ["hours"] }).hours || 0; + return (difference(d1, d2).milliseconds || 0) / 3600000; } export function addMinutes(d1: Date, nb: number): Date { diff --git a/api/src/pdc/providers/carpool/providers/CarpoolAcquisitionService.integration.spec.ts b/api/src/pdc/providers/carpool/providers/CarpoolAcquisitionService.integration.spec.ts index 8a34197a29..e708c4760e 100644 --- a/api/src/pdc/providers/carpool/providers/CarpoolAcquisitionService.integration.spec.ts +++ b/api/src/pdc/providers/carpool/providers/CarpoolAcquisitionService.integration.spec.ts @@ -212,7 +212,7 @@ describe("CarpoolAcquistionService", () => { assertEquals(result.rows, []); }); - it("Should raise error if terms is violated", async () => { + it("Should raise error if distance too short terms is violated", async () => { const carpoolL = sinon.spy(lookupRepository); const service = getService({ CarpoolLookupRepository: carpoolL, @@ -269,4 +269,67 @@ describe("CarpoolAcquistionService", () => { ], ); }); + + it("Should raise error if expired terms is violated", async () => { + const carpoolL = sinon.spy(lookupRepository); + const service = getService({ + CarpoolLookupRepository: carpoolL, + }); + + const data = { + operator_id: 1, + created_at: new Date("2024-10-24 06:37:58.000Z"), + start_datetime: new Date("2024-10-23 05:00:00.000Z"), + distance: 4_000, + driver_identity_key: "key_driver", + passenger_identity_key: "key_passenger", + end_datetime: new Date("2024-10-23 07:20:00.000Z"), + operator_trip_id: "operator_trip_id", + }; + + const errors = await service.verifyTermsViolation( + data, + ); + assertEquals(errors, ["expired"]); + assertEquals( + carpoolL.countJourneyBy.getCalls().map((c: any) => c.args), + [ + [ + { + identity_key: [ + "key_driver", + "key_passenger", + ], + identity_key_or: true, + operator_id: 1, + start_date: { + max: new Date("2024-10-23T21:59:59.999Z"), + min: new Date("2024-10-22T22:00:00.000Z"), + }, + }, + undefined, + ], + [ + { + end_date: { + max: new Date("2024-10-23T07:20:00.000Z"), + min: new Date("2024-10-23T04:30:00.000Z"), + }, + identity_key: [ + "key_driver", + "key_passenger", + ], + identity_key_or: false, + operator_id: 1, + operator_trip_id: "operator_trip_id", + start_date: { + max: new Date("2024-10-23T07:50:00.000Z"), + min: new Date("2024-10-23T05:00:00.000Z"), + }, + }, + undefined, + ], + ], + ); + }); });