2021-11-18 01:03:19 +00:00
|
|
|
import { SchedulingType } from "@prisma/client";
|
2021-09-23 17:18:29 +00:00
|
|
|
import { stringify } from "querystring";
|
|
|
|
import { useEffect, useState } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-06-15 20:54:31 +00:00
|
|
|
import type { CurrentSeats } from "@calcom/core/getUserAvailability";
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs, { Dayjs } from "@calcom/dayjs";
|
2022-06-15 20:54:31 +00:00
|
|
|
|
2021-09-22 19:52:38 +00:00
|
|
|
import getSlots from "@lib/slots";
|
2022-06-15 20:54:31 +00:00
|
|
|
import type { TimeRange, WorkingHours } from "@lib/types/schedule";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
type AvailabilityUserResponse = {
|
2021-11-18 01:03:19 +00:00
|
|
|
busy: TimeRange[];
|
2021-09-23 17:18:29 +00:00
|
|
|
timeZone: string;
|
2021-11-18 01:03:19 +00:00
|
|
|
workingHours: WorkingHours[];
|
2022-06-15 20:54:31 +00:00
|
|
|
currentSeats?: CurrentSeats;
|
2021-09-22 18:36:13 +00:00
|
|
|
};
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
type Slot = {
|
|
|
|
time: Dayjs;
|
|
|
|
users?: string[];
|
2022-05-24 13:19:12 +00:00
|
|
|
bookingUid?: string;
|
|
|
|
attendees?: number;
|
2021-09-14 08:45:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type UseSlotsProps = {
|
2021-12-19 12:11:31 +00:00
|
|
|
slotInterval: number | null;
|
2021-09-14 08:45:28 +00:00
|
|
|
eventLength: number;
|
2021-09-23 17:18:29 +00:00
|
|
|
eventTypeId: number;
|
2021-09-14 08:45:28 +00:00
|
|
|
minimumBookingNotice?: number;
|
|
|
|
date: Dayjs;
|
2021-09-23 17:18:29 +00:00
|
|
|
users: { username: string | null }[];
|
|
|
|
schedulingType: SchedulingType | null;
|
2022-03-04 10:19:03 +00:00
|
|
|
beforeBufferTime?: number;
|
|
|
|
afterBufferTime?: number;
|
2021-09-14 08:45:28 +00:00
|
|
|
};
|
|
|
|
|
2022-03-12 06:52:27 +00:00
|
|
|
type getFilteredTimesProps = {
|
|
|
|
times: dayjs.Dayjs[];
|
|
|
|
busy: TimeRange[];
|
|
|
|
eventLength: number;
|
|
|
|
beforeBufferTime: number;
|
|
|
|
afterBufferTime: number;
|
2022-06-15 20:54:31 +00:00
|
|
|
currentSeats?: CurrentSeats;
|
2022-03-12 06:52:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getFilteredTimes = (props: getFilteredTimesProps) => {
|
2022-05-24 13:19:12 +00:00
|
|
|
const { times, busy, eventLength, beforeBufferTime, afterBufferTime, currentSeats } = props;
|
2022-04-01 18:04:29 +00:00
|
|
|
const finalizationTime = times[times.length - 1]?.add(eventLength, "minutes");
|
2022-03-12 06:52:27 +00:00
|
|
|
// Check for conflicts
|
|
|
|
for (let i = times.length - 1; i >= 0; i -= 1) {
|
|
|
|
// const totalSlotLength = eventLength + beforeBufferTime + afterBufferTime;
|
|
|
|
// Check if the slot surpasses the user's availability end time
|
|
|
|
const slotEndTimeWithAfterBuffer = times[i].add(eventLength + afterBufferTime, "minutes");
|
|
|
|
if (slotEndTimeWithAfterBuffer.isAfter(finalizationTime, "minute")) {
|
|
|
|
times.splice(i, 1);
|
|
|
|
} else {
|
|
|
|
const slotStartTime = times[i];
|
|
|
|
const slotEndTime = times[i].add(eventLength, "minutes");
|
|
|
|
const slotStartTimeWithBeforeBuffer = times[i].subtract(beforeBufferTime, "minutes");
|
2022-05-24 13:19:12 +00:00
|
|
|
// If the event has seats then see if there is already a booking (want to show full bookings as well)
|
2022-06-15 20:54:31 +00:00
|
|
|
if (currentSeats?.some((booking) => booking.startTime === slotStartTime.toDate())) {
|
2022-05-24 13:19:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-03-12 06:52:27 +00:00
|
|
|
busy.every((busyTime): boolean => {
|
|
|
|
const startTime = dayjs(busyTime.start);
|
|
|
|
const endTime = dayjs(busyTime.end);
|
|
|
|
// Check if start times are the same
|
|
|
|
if (slotStartTime.isBetween(startTime, endTime, null, "[)")) {
|
|
|
|
times.splice(i, 1);
|
|
|
|
}
|
|
|
|
// Check if slot end time is between start and end time
|
|
|
|
else if (slotEndTime.isBetween(startTime, endTime)) {
|
|
|
|
times.splice(i, 1);
|
|
|
|
}
|
|
|
|
// Check if startTime is between slot
|
|
|
|
else if (startTime.isBetween(slotStartTime, slotEndTime)) {
|
|
|
|
times.splice(i, 1);
|
|
|
|
}
|
|
|
|
// Check if timeslot has before buffer time space free
|
|
|
|
else if (
|
|
|
|
slotStartTimeWithBeforeBuffer.isBetween(
|
|
|
|
startTime.subtract(beforeBufferTime, "minutes"),
|
|
|
|
endTime.add(afterBufferTime, "minutes")
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
times.splice(i, 1);
|
|
|
|
}
|
|
|
|
// Check if timeslot has after buffer time space free
|
|
|
|
else if (
|
|
|
|
slotEndTimeWithAfterBuffer.isBetween(
|
|
|
|
startTime.subtract(beforeBufferTime, "minutes"),
|
|
|
|
endTime.add(afterBufferTime, "minutes")
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
times.splice(i, 1);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return times;
|
|
|
|
};
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
export const useSlots = (props: UseSlotsProps) => {
|
2022-03-04 10:19:03 +00:00
|
|
|
const {
|
|
|
|
slotInterval,
|
|
|
|
eventLength,
|
|
|
|
minimumBookingNotice = 0,
|
|
|
|
beforeBufferTime = 0,
|
|
|
|
afterBufferTime = 0,
|
|
|
|
date,
|
|
|
|
users,
|
|
|
|
eventTypeId,
|
|
|
|
} = props;
|
2021-09-14 08:45:28 +00:00
|
|
|
const [slots, setSlots] = useState<Slot[]>([]);
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
|
|
const [error, setError] = useState<Error | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setSlots([]);
|
|
|
|
setLoading(true);
|
|
|
|
setError(null);
|
|
|
|
|
2021-09-23 17:18:29 +00:00
|
|
|
const dateFrom = date.startOf("day").format();
|
|
|
|
const dateTo = date.endOf("day").format();
|
|
|
|
const query = stringify({ dateFrom, dateTo, eventTypeId });
|
2021-09-14 08:45:28 +00:00
|
|
|
|
2022-05-14 13:49:39 +00:00
|
|
|
const handleAvailableSlots = async (res: Response) => {
|
|
|
|
const responseBody: AvailabilityUserResponse = await res.json();
|
2022-05-24 13:19:12 +00:00
|
|
|
const { workingHours, busy, currentSeats } = responseBody;
|
2022-05-14 13:49:39 +00:00
|
|
|
const times = getSlots({
|
|
|
|
frequency: slotInterval || eventLength,
|
|
|
|
inviteeDate: date,
|
2022-05-24 13:19:12 +00:00
|
|
|
workingHours,
|
2022-05-14 13:49:39 +00:00
|
|
|
minimumBookingNotice,
|
|
|
|
eventLength,
|
|
|
|
});
|
|
|
|
const filterTimeProps = {
|
|
|
|
times,
|
2022-05-24 13:19:12 +00:00
|
|
|
busy,
|
2022-05-14 13:49:39 +00:00
|
|
|
eventLength,
|
|
|
|
beforeBufferTime,
|
|
|
|
afterBufferTime,
|
2022-05-24 13:19:12 +00:00
|
|
|
currentSeats,
|
2022-05-14 13:49:39 +00:00
|
|
|
};
|
|
|
|
const filteredTimes = getFilteredTimes(filterTimeProps);
|
|
|
|
// temporary
|
|
|
|
const user = res.url.substring(res.url.lastIndexOf("/") + 1, res.url.indexOf("?"));
|
|
|
|
return filteredTimes.map((time) => ({
|
|
|
|
time,
|
|
|
|
users: [user],
|
2022-05-24 13:19:12 +00:00
|
|
|
// Conditionally add the attendees and booking id to slots object if there is already a booking during that time
|
2022-06-15 20:54:31 +00:00
|
|
|
...(currentSeats?.some((booking) => booking.startTime === time.toDate()) && {
|
2022-05-24 13:19:12 +00:00
|
|
|
attendees:
|
2022-06-15 20:54:31 +00:00
|
|
|
currentSeats[currentSeats.findIndex((booking) => booking.startTime === time.toDate())]._count
|
2022-05-24 13:19:12 +00:00
|
|
|
.attendees,
|
|
|
|
bookingUid:
|
2022-06-15 20:54:31 +00:00
|
|
|
currentSeats[currentSeats.findIndex((booking) => booking.startTime === time.toDate())].uid,
|
2022-05-24 13:19:12 +00:00
|
|
|
}),
|
2022-05-14 13:49:39 +00:00
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
Promise.all<Slot[]>(
|
|
|
|
users.map((user) => fetch(`/api/availability/${user.username}?${query}`).then(handleAvailableSlots))
|
|
|
|
)
|
|
|
|
.then((results) => {
|
|
|
|
let loadedSlots: Slot[] = results[0] || [];
|
|
|
|
if (results.length === 1) {
|
|
|
|
loadedSlots = loadedSlots?.sort((a, b) => (a.time.isAfter(b.time) ? 1 : -1));
|
|
|
|
setSlots(loadedSlots);
|
|
|
|
setLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
2021-09-14 08:45:28 +00:00
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
let poolingMethod;
|
|
|
|
switch (props.schedulingType) {
|
|
|
|
// intersect by time, does not take into account eventLength (yet)
|
|
|
|
case SchedulingType.COLLECTIVE:
|
|
|
|
poolingMethod = (slots: Slot[], compareWith: Slot[]) =>
|
|
|
|
slots.filter((slot) => compareWith.some((compare) => compare.time.isSame(slot.time)));
|
|
|
|
break;
|
|
|
|
case SchedulingType.ROUND_ROBIN:
|
|
|
|
// TODO: Create a Reservation (lock this slot for X minutes)
|
|
|
|
// this will make the following code redundant
|
|
|
|
poolingMethod = (slots: Slot[], compareWith: Slot[]) => {
|
|
|
|
compareWith.forEach((compare) => {
|
|
|
|
const match = slots.findIndex((slot) => slot.time.isSame(compare.time));
|
|
|
|
if (match !== -1) {
|
|
|
|
slots[match].users?.push(compare.users![0]);
|
|
|
|
} else {
|
|
|
|
slots.push(compare);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return slots;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
2021-09-14 08:45:28 +00:00
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
if (!poolingMethod) {
|
|
|
|
throw Error(`No poolingMethod found for schedulingType: "${props.schedulingType}""`);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 1; i < results.length; i++) {
|
|
|
|
loadedSlots = poolingMethod(loadedSlots, results[i]);
|
|
|
|
}
|
|
|
|
loadedSlots = loadedSlots.sort((a, b) => (a.time.isAfter(b.time) ? 1 : -1));
|
|
|
|
setSlots(loadedSlots);
|
|
|
|
setLoading(false);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
setError(e);
|
|
|
|
});
|
2022-05-14 13:49:39 +00:00
|
|
|
}, [
|
|
|
|
afterBufferTime,
|
|
|
|
beforeBufferTime,
|
|
|
|
eventLength,
|
|
|
|
minimumBookingNotice,
|
|
|
|
slotInterval,
|
|
|
|
eventTypeId,
|
|
|
|
props.schedulingType,
|
|
|
|
users,
|
|
|
|
date,
|
|
|
|
]);
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
slots,
|
|
|
|
loading,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useSlots;
|