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";
|
2022-06-15 20:54:31 +00:00
|
|
|
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;
|
2022-03-12 06:52:27 +00:00
|
|
|
eventLength: number;
|
2021-06-29 22:35:13 +00:00
|
|
|
};
|
2022-02-27 22:51:41 +00:00
|
|
|
export type WorkingHoursTimeFrame = { startTime: number; endTime: number };
|
2021-04-17 00:08:35 +00:00
|
|
|
|
2022-02-27 22:51:41 +00:00
|
|
|
const splitAvailableTime = (
|
|
|
|
startTimeMinutes: number,
|
|
|
|
endTimeMinutes: number,
|
2022-03-12 06:52:27 +00:00
|
|
|
frequency: number,
|
|
|
|
eventLength: number
|
2022-02-27 22:51:41 +00:00
|
|
|
): Array<WorkingHoursTimeFrame> => {
|
|
|
|
let initialTime = startTimeMinutes;
|
|
|
|
const finalizationTime = endTimeMinutes;
|
|
|
|
const result = [] as Array<WorkingHoursTimeFrame>;
|
|
|
|
while (initialTime < finalizationTime) {
|
|
|
|
const periodTime = initialTime + frequency;
|
2022-03-12 06:52:27 +00:00
|
|
|
const slotEndTime = initialTime + eventLength;
|
|
|
|
/*
|
2022-05-16 19:03:33 +00:00
|
|
|
check if the slot end time surpasses availability end time of the user
|
2022-03-12 06:52:27 +00:00
|
|
|
1 minute is added to round up the hour mark so that end of the slot is considered in the check instead of x9
|
|
|
|
eg: if finalization time is 11:59, slotEndTime is 12:00, we ideally want the slot to be available
|
|
|
|
*/
|
|
|
|
if (slotEndTime <= finalizationTime + 1) result.push({ startTime: initialTime, endTime: periodTime });
|
2022-02-27 22:51:41 +00:00
|
|
|
initialTime += frequency;
|
|
|
|
}
|
|
|
|
return result;
|
2021-06-29 22:35:13 +00:00
|
|
|
};
|
2021-04-17 00:08:35 +00:00
|
|
|
|
2022-06-15 20:54:31 +00:00
|
|
|
const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours, eventLength }: GetSlots) => {
|
2021-11-18 01:03:19 +00:00
|
|
|
// current date in invitee tz
|
2021-12-16 15:20:38 +00:00
|
|
|
const startDate = dayjs().add(minimumBookingNotice, "minute");
|
2022-02-27 00:19:50 +00:00
|
|
|
const startOfDay = dayjs.utc().startOf("day");
|
|
|
|
const startOfInviteeDay = inviteeDate.startOf("day");
|
2021-11-18 01:03:19 +00:00
|
|
|
// checks if the start date is in the past
|
2021-12-16 15:20:38 +00:00
|
|
|
if (inviteeDate.isBefore(startDate, "day")) {
|
2021-11-18 01:03:19 +00:00
|
|
|
return [];
|
2021-04-17 00:08:35 +00:00
|
|
|
}
|
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,
|
2022-02-27 00:19:50 +00:00
|
|
|
startTime: startOfDay.add(schedule.startTime, "minute"),
|
|
|
|
endTime: startOfDay.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[] = [];
|
2022-02-27 22:51:41 +00:00
|
|
|
|
|
|
|
const slotsTimeFrameAvailable = [] as Array<WorkingHoursTimeFrame>;
|
2022-05-16 19:03:33 +00:00
|
|
|
// Here we split working hour in chunks for every frequency available that can fit in whole working hours
|
|
|
|
const computedLocalWorkingHours: WorkingHoursTimeFrame[] = [];
|
|
|
|
let tempComputeTimeFrame: WorkingHoursTimeFrame | undefined;
|
|
|
|
const computeLength = localWorkingHours.length - 1;
|
|
|
|
const makeTimeFrame = (item: typeof localWorkingHours[0]): WorkingHoursTimeFrame => ({
|
|
|
|
startTime: item.startTime,
|
|
|
|
endTime: item.endTime,
|
|
|
|
});
|
2022-02-27 22:51:41 +00:00
|
|
|
localWorkingHours.forEach((item, index) => {
|
2022-05-16 19:03:33 +00:00
|
|
|
if (!tempComputeTimeFrame) {
|
|
|
|
tempComputeTimeFrame = makeTimeFrame(item);
|
|
|
|
} else {
|
|
|
|
// please check the comment in splitAvailableTime func for the added 1 minute
|
|
|
|
if (tempComputeTimeFrame.endTime + 1 === item.startTime) {
|
|
|
|
// to deal with time that across the day, e.g. from 11:59 to to 12:01
|
|
|
|
tempComputeTimeFrame.endTime = item.endTime;
|
|
|
|
} else {
|
|
|
|
computedLocalWorkingHours.push(tempComputeTimeFrame);
|
|
|
|
tempComputeTimeFrame = makeTimeFrame(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index == computeLength) {
|
|
|
|
computedLocalWorkingHours.push(tempComputeTimeFrame);
|
|
|
|
}
|
|
|
|
});
|
2022-05-17 20:43:27 +00:00
|
|
|
computedLocalWorkingHours.forEach((item) => {
|
2022-03-12 06:52:27 +00:00
|
|
|
slotsTimeFrameAvailable.push(...splitAvailableTime(item.startTime, item.endTime, frequency, eventLength));
|
2022-02-27 22:51:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
slotsTimeFrameAvailable.forEach((item) => {
|
|
|
|
const slot = startOfInviteeDay.add(item.startTime, "minute");
|
|
|
|
// Validating slot its not on the past
|
|
|
|
if (!slot.isBefore(startDate)) {
|
2022-01-27 15:16:38 +00:00
|
|
|
slots.push(slot);
|
2021-11-18 01:03:19 +00:00
|
|
|
}
|
2022-02-27 22:51:41 +00:00
|
|
|
});
|
2022-06-16 16:03:51 +00:00
|
|
|
|
|
|
|
const uniq = (a: Dayjs[]) => {
|
|
|
|
const seen: Record<string, boolean> = {};
|
|
|
|
return a.filter((item) => {
|
|
|
|
return seen.hasOwnProperty(item.format()) ? false : (seen[item.format()] = true);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return uniq(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;
|