2022-03-23 22:00:30 +00:00
|
|
|
// import { getBusyVideoTimes } from "@calcom/core/videoClient";
|
2021-10-26 16:17:24 +00:00
|
|
|
import { Prisma } from "@prisma/client";
|
2021-06-15 16:19:00 +00:00
|
|
|
import dayjs from "dayjs";
|
2021-11-10 11:16:32 +00:00
|
|
|
import timezone from "dayjs/plugin/timezone";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
2021-09-22 18:36:13 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import { getBusyCalendarTimes } from "@calcom/core/CalendarManager";
|
|
|
|
|
2021-09-22 19:52:38 +00:00
|
|
|
import { asStringOrNull } from "@lib/asStringOrNull";
|
2021-11-18 01:03:19 +00:00
|
|
|
import { getWorkingHours } from "@lib/availability";
|
2021-09-22 19:52:38 +00:00
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
|
2021-03-22 13:48:48 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-09-14 08:45:28 +00:00
|
|
|
const user = asStringOrNull(req.query.user);
|
|
|
|
const dateFrom = dayjs(asStringOrNull(req.query.dateFrom));
|
|
|
|
const dateTo = dayjs(asStringOrNull(req.query.dateTo));
|
2021-09-23 17:18:29 +00:00
|
|
|
const eventTypeId = parseInt(asStringOrNull(req.query.eventTypeId) || "");
|
2021-07-06 21:09:53 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
if (!dateFrom.isValid() || !dateTo.isValid()) {
|
|
|
|
return res.status(400).json({ message: "Invalid time range given." });
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
const rawUser = await prisma.user.findUnique({
|
2021-07-06 21:09:53 +00:00
|
|
|
where: {
|
2021-09-22 18:36:13 +00:00
|
|
|
username: user as string,
|
2021-07-06 21:09:53 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
credentials: true,
|
|
|
|
timeZone: true,
|
|
|
|
bufferTime: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
availability: true,
|
2021-07-06 21:09:53 +00:00
|
|
|
id: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
selectedCalendars: true,
|
2022-03-17 16:48:23 +00:00
|
|
|
schedules: {
|
|
|
|
select: {
|
|
|
|
availability: true,
|
|
|
|
timeZone: true,
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultScheduleId: true,
|
2021-07-06 21:09:53 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-09-24 10:16:46 +00:00
|
|
|
const getEventType = (id: number) =>
|
|
|
|
prisma.eventType.findUnique({
|
|
|
|
where: { id },
|
|
|
|
select: {
|
|
|
|
timeZone: true,
|
2022-03-17 16:48:23 +00:00
|
|
|
schedule: {
|
|
|
|
select: {
|
|
|
|
availability: true,
|
|
|
|
timeZone: true,
|
|
|
|
},
|
|
|
|
},
|
2021-09-24 10:16:46 +00:00
|
|
|
availability: {
|
|
|
|
select: {
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
days: true,
|
|
|
|
},
|
2021-09-23 17:18:29 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-24 10:16:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
type EventType = Prisma.PromiseReturnType<typeof getEventType>;
|
|
|
|
let eventType: EventType | null = null;
|
|
|
|
if (eventTypeId) eventType = await getEventType(eventTypeId);
|
2021-09-23 17:18:29 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
if (!rawUser) throw new Error("No user found");
|
|
|
|
|
|
|
|
const { selectedCalendars, ...currentUser } = rawUser;
|
2021-07-06 21:09:53 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
const busyTimes = await getBusyCalendarTimes(
|
2021-07-06 21:09:53 +00:00
|
|
|
currentUser.credentials,
|
2021-09-14 08:45:28 +00:00
|
|
|
dateFrom.format(),
|
|
|
|
dateTo.format(),
|
2021-07-06 21:09:53 +00:00
|
|
|
selectedCalendars
|
|
|
|
);
|
2021-09-14 08:45:28 +00:00
|
|
|
|
|
|
|
// busyTimes.push(...await getBusyVideoTimes(currentUser.credentials, dateFrom.format(), dateTo.format()));
|
|
|
|
|
|
|
|
const bufferedBusyTimes = busyTimes.map((a) => ({
|
2022-04-12 12:01:50 +00:00
|
|
|
start: dayjs(a.start).subtract(currentUser.bufferTime, "minute"),
|
|
|
|
end: dayjs(a.end).add(currentUser.bufferTime, "minute"),
|
2021-07-06 21:09:53 +00:00
|
|
|
}));
|
|
|
|
|
2022-03-17 16:48:23 +00:00
|
|
|
const schedule = eventType?.schedule
|
|
|
|
? { ...eventType?.schedule }
|
|
|
|
: {
|
|
|
|
...currentUser.schedules.filter(
|
|
|
|
(schedule) => !currentUser.defaultScheduleId || schedule.id === currentUser.defaultScheduleId
|
|
|
|
)[0],
|
|
|
|
};
|
|
|
|
|
|
|
|
const timeZone = schedule.timeZone || eventType?.timeZone || currentUser.timeZone;
|
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
const workingHours = getWorkingHours(
|
2022-03-17 16:48:23 +00:00
|
|
|
{
|
|
|
|
timeZone,
|
|
|
|
},
|
|
|
|
schedule.availability ||
|
|
|
|
(eventType?.availability.length ? eventType.availability : currentUser.availability)
|
2021-11-18 01:03:19 +00:00
|
|
|
);
|
2021-09-23 17:18:29 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
res.status(200).json({
|
|
|
|
busy: bufferedBusyTimes,
|
2021-09-23 17:18:29 +00:00
|
|
|
timeZone,
|
2021-11-18 01:03:19 +00:00
|
|
|
workingHours,
|
2021-09-14 08:45:28 +00:00
|
|
|
});
|
2021-04-16 02:09:22 +00:00
|
|
|
}
|