Files
cal.diy/packages/app-store/applecalendar/api/add.ts
T
2dbc73c75b chore: fix all Typescript warnings (#8618)
* [CAL-1517] fix all Typescript warnings

* solve conflicts

* Update stripeCheckoutSession.handler.ts

Parse is a guard, so even though the variable is unused the parse itself is needed.

* Update ToolbarPlugin.tsx

Don't change dependency tree

---------

Co-authored-by: gitstart-calcom <gitstart@users.noreply.github.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
2023-06-06 11:59:57 +00:00

57 lines
1.5 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 { CalendarService } from "../lib";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") {
const { username, password } = req.body;
// Get user
const user = await prisma.user.findFirstOrThrow({
where: {
id: req.session?.user?.id,
},
select: {
id: true,
},
});
const data = {
type: "apple_calendar",
key: symmetricEncrypt(
JSON.stringify({ username, password }),
process.env.CALENDSO_ENCRYPTION_KEY || ""
),
userId: user.id,
appId: "apple-calendar",
invalid: false,
};
try {
const dav = new CalendarService({
id: 0,
...data,
});
await dav?.listCalendars();
await prisma.credential.create({
data,
});
} catch (reason) {
logger.error("Could not add this caldav account", reason);
return res.status(500).json({ message: "Could not add this caldav account" });
}
return res
.status(200)
.json({ url: getInstalledAppPath({ variant: "calendar", slug: "apple-calendar" }) });
}
if (req.method === "GET") {
return res.status(200).json({ url: "/apps/apple-calendar/setup" });
}
}