2021-08-18 11:52:25 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2023-06-19 21:22:30 +00:00
|
|
|
import { z } from "zod";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import { getCalendarCredentials, getConnectedCalendars } from "@calcom/core/CalendarManager";
|
2023-03-10 23:45:24 +00:00
|
|
|
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
2022-03-23 22:00:30 +00:00
|
|
|
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
|
|
|
|
2023-06-19 21:22:30 +00:00
|
|
|
const selectedCalendarSelectSchema = z.object({
|
|
|
|
integration: z.string(),
|
|
|
|
externalId: z.string(),
|
|
|
|
});
|
|
|
|
|
2021-06-14 17:45:24 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2023-03-10 23:45:24 +00:00
|
|
|
const session = await getServerSession({ req, res });
|
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
|
|
|
|
2023-04-20 15:55:19 +00:00
|
|
|
const userWithCredentials = 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
|
|
|
},
|
|
|
|
});
|
2023-04-20 15:55:19 +00:00
|
|
|
if (!userWithCredentials) {
|
2021-10-12 09:35:44 +00:00
|
|
|
res.status(401).json({ message: "Not authenticated" });
|
|
|
|
return;
|
|
|
|
}
|
2023-04-20 15:55:19 +00:00
|
|
|
const { credentials, ...user } = userWithCredentials;
|
2021-10-12 09:35:44 +00:00
|
|
|
|
2021-12-09 15:51:37 +00:00
|
|
|
if (req.method === "POST") {
|
2023-06-19 21:22:30 +00:00
|
|
|
const { integration, externalId } = selectedCalendarSelectSchema.parse(req.body);
|
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,
|
2023-06-19 21:22:30 +00:00
|
|
|
integration,
|
|
|
|
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,
|
2023-06-19 21:22:30 +00:00
|
|
|
integration,
|
|
|
|
externalId,
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
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") {
|
2023-06-19 21:22:30 +00:00
|
|
|
const { integration, externalId } = selectedCalendarSelectSchema.parse(req.query);
|
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,
|
2023-06-19 21:22:30 +00:00
|
|
|
externalId,
|
|
|
|
integration,
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
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
|
2023-04-20 15:55:19 +00:00
|
|
|
const calendarCredentials = getCalendarCredentials(credentials);
|
2021-12-09 15:51:37 +00:00
|
|
|
// get all the connected integrations' calendars (from third party)
|
2023-02-28 03:11:35 +00:00
|
|
|
const { connectedCalendars } = await getConnectedCalendars(calendarCredentials, user.selectedCalendars);
|
2021-12-09 15:51:37 +00:00
|
|
|
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
|
|
|
}
|