Skip to content

Commit

Permalink
fix(sms): fix Send Date
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed May 10, 2024
1 parent 993094e commit 9bc2333
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Structure } from "@domifa/common";
import { generateScheduleSendDate } from "./generateScheduleSendDate";
import { utcToZonedTime } from "date-fns-tz";

const structure: Pick<Structure, "sms"> = {
sms: {
Expand All @@ -9,26 +10,50 @@ const structure: Pick<Structure, "sms"> = {
senderName: "",
schedule: {
monday: false,
tuesday: true,
tuesday: false,
wednesday: false,
thursday: true,
thursday: false,
friday: false,
},
},
};

describe("Generate SMS Schedule Date", () => {
it("Should set scheduled date for the next tuesday, because it's monday", () => {
const monday = new Date("2021-10-25T10:00:00.000Z");
const tuesdayRef = new Date("2021-10-26T19:00:00.000Z");
expect(generateScheduleSendDate(structure, monday)).toEqual(tuesdayRef);
const monday = new Date("2024-05-06T10:00:00.000Z");
const tuesdayRef = new Date("2024-05-07T19:00:00.000Z");
structure.sms.schedule.tuesday = true;

const expectedDate = utcToZonedTime(
generateScheduleSendDate(structure, monday),
"Europe/Paris"
);

expect(expectedDate).toEqual(tuesdayRef);
});

it("Should set scheduled date for the next friday, because it's monday", () => {
const monday = new Date("2021-10-25T10:00:00.000Z");
it("Receive & Send on same day: friday 10 may 2024, before 19:00", () => {
const friday = new Date("2024-05-10T18:00:00.000Z");
const fridayRef = new Date("2024-05-10T19:00:00.000Z");
structure.sms.schedule.tuesday = false;
structure.sms.schedule.friday = true;
const friday = new Date("2021-10-28T19:00:00.000Z");
expect(generateScheduleSendDate(structure, monday)).toEqual(friday);
const expectedDate = utcToZonedTime(
generateScheduleSendDate(structure, friday),
"Europe/Paris"
);

expect(expectedDate).toEqual(fridayRef);
});
it("Receive after 19:00, send next week", () => {
const friday = new Date("2024-05-10T21:05:00.000Z");
const fridayRef = new Date("2024-05-17T19:00:00.000Z");
structure.sms.schedule.tuesday = false;
structure.sms.schedule.friday = true;
const expectedDate = utcToZonedTime(
generateScheduleSendDate(structure, friday),
"Europe/Paris"
);

expect(expectedDate).toEqual(fridayRef);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ export const generateScheduleSendDate = (

const nextDayId = getNextClosestDay(days, dateReference);

const nextDate = nextDay(dateReference, nextDayId as Day);
nextDate.setHours(19, 0, 0);
const nextDate =
nextDayId === dateReference.getDay() && dateReference.getUTCHours() < 19
? dateReference
: nextDay(dateReference, nextDayId as Day);

nextDate.setUTCHours(19, 0, 0);
return nextDate;
};

function getNextClosestDay(days: string[], dateReference: Date): number {
const todayIndex = dateReference.getDay();
const dayIndices = days.map((day) => weekDays.indexOf(day.toLowerCase()));

dayIndices.sort((a, b) => a - b);

for (const dayIndex of dayIndices) {
if (dayIndex >= todayIndex) {
return dayIndex;
Expand Down

0 comments on commit 9bc2333

Please sign in to comment.