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";
|
|
|
|
|
2022-09-15 19:53:09 +00:00
|
|
|
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
2022-03-23 22:00:30 +00:00
|
|
|
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
|
2022-08-31 19:44:47 +00:00
|
|
|
const user = await prisma.user.findFirstOrThrow({
|
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",
|
2022-12-07 21:47:02 +00:00
|
|
|
invalid: false,
|
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
|
|
|
});
|
2022-12-15 13:45:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
logger.error("Could not add this caldav account", e);
|
|
|
|
if (e instanceof Error) {
|
|
|
|
let message = e.message;
|
|
|
|
if (e.message.indexOf("Invalid credentials") > -1 && url.indexOf("dav.php") > -1) {
|
|
|
|
const parsedUrl = new URL(url);
|
|
|
|
const adminUrl =
|
|
|
|
parsedUrl.protocol +
|
|
|
|
"//" +
|
|
|
|
parsedUrl.hostname +
|
|
|
|
(parsedUrl.port ? ":" + parsedUrl.port : "") +
|
|
|
|
"/admin/?/settings/standard/";
|
|
|
|
message = `Couldn\'t connect to caldav account, please verify WebDAV authentication type is set to "Basic"`;
|
|
|
|
return res.status(500).json({ message, actionUrl: adminUrl });
|
|
|
|
}
|
|
|
|
}
|
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-09-15 19:53:09 +00:00
|
|
|
return res
|
|
|
|
.status(200)
|
|
|
|
.json({ url: getInstalledAppPath({ variant: "calendar", slug: "caldav-calendar" }) });
|
2022-06-11 17:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === "GET") {
|
|
|
|
return res.status(200).json({ url: "/apps/caldav-calendar/setup" });
|
2021-08-15 01:53:59 +00:00
|
|
|
}
|
|
|
|
}
|