2021-08-18 11:52:25 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import { getCalendarCredentials, getConnectedCalendars } from "@calcom/core/CalendarManager";
|
|
|
|
import notEmpty from "@calcom/lib/notEmpty";
|
2022-07-28 19:58:26 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-23 22:00:30 +00:00
|
|
|
|
2021-08-18 11:52:25 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-06-14 17:45:24 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-12-09 15:51:37 +00:00
|
|
|
const session = await getSession({ req });
|
2021-06-14 17:45:24 +00:00
|
|
|
|
2021-10-12 09:35:44 +00:00
|
|
|
if (!session?.user?.id) {
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(401).json({ message: "Not authenticated" });
|
|
|
|
return;
|
|
|
|
}
|
2021-06-14 17:45:24 +00:00
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
const user = await prisma.user.findUnique({
|
2021-08-19 12:27:01 +00:00
|
|
|
where: {
|
|
|
|
id: session.user.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
credentials: true,
|
|
|
|
timeZone: true,
|
|
|
|
id: true,
|
2021-12-09 15:51:37 +00:00
|
|
|
selectedCalendars: true,
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
if (!user) {
|
2021-10-12 09:35:44 +00:00
|
|
|
res.status(401).json({ message: "Not authenticated" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
if (req.method === "POST") {
|
2021-10-12 09:35:44 +00:00
|
|
|
await prisma.selectedCalendar.upsert({
|
|
|
|
where: {
|
|
|
|
userId_integration_externalId: {
|
2021-12-09 15:51:37 +00:00
|
|
|
userId: user.id,
|
2021-10-12 09:35:44 +00:00
|
|
|
integration: req.body.integration,
|
|
|
|
externalId: req.body.externalId,
|
2021-06-14 17:45:24 +00:00
|
|
|
},
|
2021-10-12 09:35:44 +00:00
|
|
|
},
|
|
|
|
create: {
|
2021-12-09 15:51:37 +00:00
|
|
|
userId: user.id,
|
2021-08-19 12:27:01 +00:00
|
|
|
integration: req.body.integration,
|
|
|
|
externalId: req.body.externalId,
|
|
|
|
},
|
2021-10-12 09:35:44 +00:00
|
|
|
// already exists
|
|
|
|
update: {},
|
2021-06-14 17:45:24 +00:00
|
|
|
});
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(200).json({ message: "Calendar Selection Saved" });
|
|
|
|
}
|
2021-06-14 17:45:24 +00:00
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
if (req.method === "DELETE") {
|
2021-08-19 12:27:01 +00:00
|
|
|
await prisma.selectedCalendar.delete({
|
|
|
|
where: {
|
|
|
|
userId_integration_externalId: {
|
2021-12-09 15:51:37 +00:00
|
|
|
userId: user.id,
|
2021-08-19 12:27:01 +00:00
|
|
|
externalId: req.body.externalId,
|
|
|
|
integration: req.body.integration,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2021-06-14 17:45:24 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
res.status(200).json({ message: "Calendar Selection Saved" });
|
|
|
|
}
|
2021-06-14 17:45:24 +00:00
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
if (req.method === "GET") {
|
2021-08-19 12:27:01 +00:00
|
|
|
const selectedCalendarIds = await prisma.selectedCalendar.findMany({
|
|
|
|
where: {
|
2021-12-09 15:51:37 +00:00
|
|
|
userId: user.id,
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
externalId: true,
|
|
|
|
},
|
|
|
|
});
|
2021-06-14 17:45:24 +00:00
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
// get user's credentials + their connected integrations
|
|
|
|
const calendarCredentials = getCalendarCredentials(user.credentials, user.id);
|
|
|
|
// get all the connected integrations' calendars (from third party)
|
|
|
|
const connectedCalendars = await getConnectedCalendars(calendarCredentials, user.selectedCalendars);
|
|
|
|
const calendars = connectedCalendars.flatMap((c) => c.calendars).filter(notEmpty);
|
2021-08-19 12:27:01 +00:00
|
|
|
const selectableCalendars = calendars.map((cal) => {
|
|
|
|
return { selected: selectedCalendarIds.findIndex((s) => s.externalId === cal.externalId) > -1, ...cal };
|
|
|
|
});
|
|
|
|
res.status(200).json(selectableCalendars);
|
|
|
|
}
|
2021-06-14 17:45:24 +00:00
|
|
|
}
|