Files
calendar/packages/app-store/ics-feedcalendar/api/add.ts
T
7046f6f80a feat: Fix Proton Calendar with ICS (#15454)
* feat: fix proton calendar with ics

* ics-feed: add note & clarify checkbox

* fix review

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Amit Sharma <74371312+Amit91848@users.noreply.github.com>
2024-07-16 12:08:54 +02:00

61 lines
1.7 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from "next";
import { symmetricEncrypt } from "@calcom/lib/crypto";
import logger from "@calcom/lib/logger";
import prisma from "@calcom/prisma";
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
import appConfig from "../config.json";
import { CalendarService } from "../lib";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") {
const { urls, skipWriting } = req.body;
// Get user
const user = await prisma.user.findFirstOrThrow({
where: {
id: req.session?.user?.id,
},
select: {
id: true,
email: true,
},
});
const data = {
type: appConfig.type,
key: symmetricEncrypt(JSON.stringify({ urls, skipWriting }), process.env.CALENDSO_ENCRYPTION_KEY || ""),
userId: user.id,
teamId: null,
appId: appConfig.slug,
invalid: false,
};
try {
const dav = new CalendarService({
id: 0,
...data,
user: { email: user.email },
});
const listedCals = await dav.listCalendars();
if (listedCals.length !== urls.length) {
throw new Error(`Listed cals and URLs mismatch: ${listedCals.length} vs. ${urls.length}`);
}
await prisma.credential.create({
data,
});
} catch (e) {
logger.error("Could not add ICS feeds", e);
return res.status(500).json({ message: "Could not add ICS feeds" });
}
return res.status(200).json({ url: getInstalledAppPath({ variant: "calendar", slug: "ics-feed" }) });
}
if (req.method === "GET") {
return res.status(200).json({ url: "/apps/ics-feed/setup" });
}
}