2021-08-15 01:53:59 +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 { symmetricEncrypt } from "@calcom/lib/crypto";
|
|
|
|
import logger from "@calcom/lib/logger";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { CalendarService } from "../lib";
|
2021-08-15 01:53:59 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
if (req.method === "POST") {
|
|
|
|
const { username, password, url } = req.body;
|
|
|
|
// Get user
|
2021-12-10 21:14:54 +00:00
|
|
|
const user = await prisma.user.findFirst({
|
|
|
|
rejectOnNotFound: true,
|
2021-08-15 01:53:59 +00:00
|
|
|
where: {
|
2022-03-23 22:00:30 +00:00
|
|
|
id: req.session?.user?.id,
|
2021-08-15 01:53:59 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-12-10 21:14:54 +00:00
|
|
|
const data = {
|
|
|
|
type: "caldav_calendar",
|
|
|
|
key: symmetricEncrypt(
|
|
|
|
JSON.stringify({ username, password, url }),
|
|
|
|
process.env.CALENDSO_ENCRYPTION_KEY!
|
|
|
|
),
|
|
|
|
userId: user.id,
|
2022-05-02 20:39:35 +00:00
|
|
|
appId: "caldav-calendar",
|
2021-12-10 21:14:54 +00:00
|
|
|
};
|
|
|
|
|
2021-08-15 01:53:59 +00:00
|
|
|
try {
|
2022-03-23 22:00:30 +00:00
|
|
|
const dav = new CalendarService({
|
2021-08-16 14:34:49 +00:00
|
|
|
id: 0,
|
2021-12-10 21:14:54 +00:00
|
|
|
...data,
|
2021-08-15 01:53:59 +00:00
|
|
|
});
|
2022-01-06 17:28:31 +00:00
|
|
|
await dav?.listCalendars();
|
2021-08-16 14:34:49 +00:00
|
|
|
await prisma.credential.create({
|
2021-12-10 21:14:54 +00:00
|
|
|
data,
|
2021-08-16 14:34:49 +00:00
|
|
|
});
|
2021-08-15 01:53:59 +00:00
|
|
|
} catch (reason) {
|
|
|
|
logger.error("Could not add this caldav account", reason);
|
2021-08-15 14:22:05 +00:00
|
|
|
return res.status(500).json({ message: "Could not add this caldav account" });
|
2021-08-15 01:53:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 17:33:04 +00:00
|
|
|
return res.status(200).json({ url: "/apps/installed" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === "GET") {
|
|
|
|
return res.status(200).json({ url: "/apps/caldav-calendar/setup" });
|
2021-08-15 01:53:59 +00:00
|
|
|
}
|
|
|
|
}
|