2021-06-29 01:45:58 +00:00
|
|
|
import dayjs, { Dayjs } from "dayjs";
|
2021-11-18 01:03:19 +00:00
|
|
|
import isBetween from "dayjs/plugin/isBetween";
|
2021-11-24 22:53:41 +00:00
|
|
|
import isToday from "dayjs/plugin/isToday";
|
2021-09-22 19:52:38 +00:00
|
|
|
import utc from "dayjs/plugin/utc";
|
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
import { getWorkingHours } from "./availability";
|
|
|
|
import { WorkingHours } from "./types/schedule";
|
2021-04-17 00:08:35 +00:00
|
|
|
|
2021-11-24 22:53:41 +00:00
|
|
|
dayjs.extend(isToday);
|
2021-11-18 01:03:19 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(isBetween);
|
2021-04-17 00:08:35 +00:00
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
export type GetSlots = {
|
2021-06-24 22:15:18 +00:00
|
|
|
inviteeDate: Dayjs;
|
|
|
|
frequency: number;
|
2021-11-18 01:03:19 +00:00
|
|
|
workingHours: WorkingHours[];
|
|
|
|
minimumBookingNotice: number;
|
2021-06-29 22:35:13 +00:00
|
|
|
};
|
2021-04-17 00:08:35 +00:00
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
const getMinuteOffset = (date: Dayjs, step: number) => {
|
|
|
|
// Diffs the current time with the given date and iff same day; (handled by 1440) - return difference; otherwise 0
|
2021-11-24 22:53:41 +00:00
|
|
|
const minuteOffset = Math.min(date.diff(dayjs().startOf("day"), "minute"), 1440) % 1440;
|
2021-11-18 01:03:19 +00:00
|
|
|
// round down to nearest step
|
2021-11-24 22:53:41 +00:00
|
|
|
return Math.ceil(minuteOffset / step) * step;
|
2021-06-29 22:35:13 +00:00
|
|
|
};
|
2021-04-17 00:08:35 +00:00
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours }: GetSlots) => {
|
|
|
|
// current date in invitee tz
|
2021-11-24 22:53:41 +00:00
|
|
|
let startDate = dayjs(inviteeDate).add(minimumBookingNotice, "minute");
|
2021-11-18 01:03:19 +00:00
|
|
|
// checks if the start date is in the past
|
|
|
|
if (startDate.isBefore(dayjs(), "day")) {
|
|
|
|
return [];
|
2021-04-17 00:08:35 +00:00
|
|
|
}
|
2021-11-24 22:53:41 +00:00
|
|
|
// Add the current time to the startDate if the day is today
|
|
|
|
if (startDate.isToday()) {
|
|
|
|
startDate = startDate.add(dayjs().diff(startDate, "minute"), "minute");
|
|
|
|
}
|
2021-07-08 21:14:29 +00:00
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
const localWorkingHours = getWorkingHours(
|
|
|
|
{ utcOffset: -inviteeDate.utcOffset() },
|
|
|
|
workingHours.map((schedule) => ({
|
|
|
|
days: schedule.days,
|
2021-11-24 22:53:41 +00:00
|
|
|
startTime: dayjs.utc().startOf("day").add(schedule.startTime, "minute"),
|
|
|
|
endTime: dayjs.utc().startOf("day").add(schedule.endTime, "minute"),
|
2021-11-18 01:03:19 +00:00
|
|
|
}))
|
|
|
|
).filter((hours) => hours.days.includes(inviteeDate.day()));
|
2021-04-17 00:08:35 +00:00
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
const slots: Dayjs[] = [];
|
2021-11-24 22:53:41 +00:00
|
|
|
for (let minutes = getMinuteOffset(startDate, frequency); minutes < 1440; minutes += frequency) {
|
|
|
|
const slot = startDate.startOf("day").add(minutes, "minute");
|
2021-11-18 01:03:19 +00:00
|
|
|
// add slots to available slots if it is found to be between the start and end time of the checked working hours.
|
|
|
|
if (
|
|
|
|
localWorkingHours.some((hours) =>
|
|
|
|
slot.isBetween(
|
2021-11-24 22:53:41 +00:00
|
|
|
startDate.startOf("day").add(hours.startTime, "minute"),
|
|
|
|
startDate.startOf("day").add(hours.endTime, "minute"),
|
2021-11-18 01:03:19 +00:00
|
|
|
null,
|
|
|
|
"[)"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
slots.push(slot);
|
|
|
|
}
|
2021-04-17 00:08:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
return slots;
|
2021-06-29 01:45:58 +00:00
|
|
|
};
|
2021-04-17 00:08:35 +00:00
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
export default getSlots;
|