Skip to content

Commit

Permalink
fix(expired): fix approximation on time diff for expired rules (#2676)
Browse files Browse the repository at this point in the history
  • Loading branch information
P3rceval authored Nov 6, 2024
1 parent 5012526 commit d623684
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
9 changes: 9 additions & 0 deletions api/src/lib/date/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
2 changes: 1 addition & 1 deletion api/src/lib/date/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
],
],
);
});
});

0 comments on commit d623684

Please sign in to comment.