cal.pub0.org/pages/api/availability/[user].ts

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-07-06 21:09:53 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
2021-08-04 20:28:35 +00:00
import prisma from "@lib/prisma";
import { getBusyCalendarTimes } from "@lib/calendarClient";
import { getBusyVideoTimes } from "@lib/videoClient";
import dayjs from "dayjs";
2021-03-22 13:48:48 +00:00
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
2021-07-06 21:09:53 +00:00
const { user } = req.query;
const currentUser = await prisma.user.findFirst({
where: {
username: user,
},
select: {
credentials: true,
timeZone: true,
bufferTime: true,
id: true,
},
});
const selectedCalendars = await prisma.selectedCalendar.findMany({
where: {
userId: currentUser.id,
},
});
2021-08-04 20:28:35 +00:00
const calendarBusyTimes = await getBusyCalendarTimes(
2021-07-06 21:09:53 +00:00
currentUser.credentials,
req.query.dateFrom,
req.query.dateTo,
selectedCalendars
);
2021-08-04 20:28:35 +00:00
const videoBusyTimes = await getBusyVideoTimes(
2021-07-06 21:09:53 +00:00
currentUser.credentials,
req.query.dateFrom,
req.query.dateTo
);
2021-08-04 20:28:35 +00:00
calendarBusyTimes.push(...videoBusyTimes);
2021-07-06 21:09:53 +00:00
2021-08-04 20:28:35 +00:00
const bufferedBusyTimes = calendarBusyTimes.map((a) => ({
2021-07-06 21:09:53 +00:00
start: dayjs(a.start).subtract(currentUser.bufferTime, "minute").toString(),
end: dayjs(a.end).add(currentUser.bufferTime, "minute").toString(),
}));
2021-08-04 20:28:35 +00:00
res.status(200).json(bufferedBusyTimes);
}