parent
f8781e4d5f
commit
712266664e
1 changed files with 15 additions and 9 deletions
24
lib/slots.ts
24
lib/slots.ts
|
@ -1,10 +1,12 @@
|
||||||
import dayjs, { Dayjs } from "dayjs";
|
import dayjs, { Dayjs } from "dayjs";
|
||||||
import isBetween from "dayjs/plugin/isBetween";
|
import isBetween from "dayjs/plugin/isBetween";
|
||||||
|
import isToday from "dayjs/plugin/isToday";
|
||||||
import utc from "dayjs/plugin/utc";
|
import utc from "dayjs/plugin/utc";
|
||||||
|
|
||||||
import { getWorkingHours } from "./availability";
|
import { getWorkingHours } from "./availability";
|
||||||
import { WorkingHours } from "./types/schedule";
|
import { WorkingHours } from "./types/schedule";
|
||||||
|
|
||||||
|
dayjs.extend(isToday);
|
||||||
dayjs.extend(utc);
|
dayjs.extend(utc);
|
||||||
dayjs.extend(isBetween);
|
dayjs.extend(isBetween);
|
||||||
|
|
||||||
|
@ -17,37 +19,41 @@ export type GetSlots = {
|
||||||
|
|
||||||
const getMinuteOffset = (date: Dayjs, step: number) => {
|
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
|
// Diffs the current time with the given date and iff same day; (handled by 1440) - return difference; otherwise 0
|
||||||
const minuteOffset = Math.min(date.diff(dayjs().startOf("day"), "minutes"), 1440) % 1440;
|
const minuteOffset = Math.min(date.diff(dayjs().startOf("day"), "minute"), 1440) % 1440;
|
||||||
// round down to nearest step
|
// round down to nearest step
|
||||||
return Math.floor(minuteOffset / step) * step;
|
return Math.ceil(minuteOffset / step) * step;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours }: GetSlots) => {
|
const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours }: GetSlots) => {
|
||||||
// current date in invitee tz
|
// current date in invitee tz
|
||||||
const startDate = dayjs(inviteeDate).add(minimumBookingNotice, "minutes"); // + minimum notice period
|
let startDate = dayjs(inviteeDate).add(minimumBookingNotice, "minute");
|
||||||
// checks if the start date is in the past
|
// checks if the start date is in the past
|
||||||
if (startDate.isBefore(dayjs(), "day")) {
|
if (startDate.isBefore(dayjs(), "day")) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
// Add the current time to the startDate if the day is today
|
||||||
|
if (startDate.isToday()) {
|
||||||
|
startDate = startDate.add(dayjs().diff(startDate, "minute"), "minute");
|
||||||
|
}
|
||||||
|
|
||||||
const localWorkingHours = getWorkingHours(
|
const localWorkingHours = getWorkingHours(
|
||||||
{ utcOffset: -inviteeDate.utcOffset() },
|
{ utcOffset: -inviteeDate.utcOffset() },
|
||||||
workingHours.map((schedule) => ({
|
workingHours.map((schedule) => ({
|
||||||
days: schedule.days,
|
days: schedule.days,
|
||||||
startTime: dayjs.utc().startOf("day").add(schedule.startTime, "minutes"),
|
startTime: dayjs.utc().startOf("day").add(schedule.startTime, "minute"),
|
||||||
endTime: dayjs.utc().startOf("day").add(schedule.endTime, "minutes"),
|
endTime: dayjs.utc().startOf("day").add(schedule.endTime, "minute"),
|
||||||
}))
|
}))
|
||||||
).filter((hours) => hours.days.includes(inviteeDate.day()));
|
).filter((hours) => hours.days.includes(inviteeDate.day()));
|
||||||
|
|
||||||
const slots: Dayjs[] = [];
|
const slots: Dayjs[] = [];
|
||||||
for (let minutes = getMinuteOffset(inviteeDate, frequency); minutes < 1440; minutes += frequency) {
|
for (let minutes = getMinuteOffset(startDate, frequency); minutes < 1440; minutes += frequency) {
|
||||||
const slot = inviteeDate.startOf("day").add(minutes, "minutes");
|
const slot = startDate.startOf("day").add(minutes, "minute");
|
||||||
// add slots to available slots if it is found to be between the start and end time of the checked working hours.
|
// add slots to available slots if it is found to be between the start and end time of the checked working hours.
|
||||||
if (
|
if (
|
||||||
localWorkingHours.some((hours) =>
|
localWorkingHours.some((hours) =>
|
||||||
slot.isBetween(
|
slot.isBetween(
|
||||||
inviteeDate.startOf("day").add(hours.startTime, "minutes"),
|
startDate.startOf("day").add(hours.startTime, "minute"),
|
||||||
inviteeDate.startOf("day").add(hours.endTime, "minutes"),
|
startDate.startOf("day").add(hours.endTime, "minute"),
|
||||||
null,
|
null,
|
||||||
"[)"
|
"[)"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue