2022-06-13 22:12:27 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import { symmetricEncrypt } from "@calcom/lib/crypto";
|
|
|
|
import logger from "@calcom/lib/logger";
|
|
|
|
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2022-09-15 19:53:09 +00:00
|
|
|
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
2022-06-13 22:12:27 +00:00
|
|
|
import { CalendarService } from "../lib";
|
|
|
|
|
|
|
|
const bodySchema = z
|
|
|
|
.object({
|
|
|
|
username: z.string(),
|
|
|
|
password: z.string(),
|
|
|
|
url: z.string().url(),
|
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
|
|
|
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
const body = bodySchema.parse(req.body);
|
|
|
|
// Get user
|
2022-08-31 19:44:47 +00:00
|
|
|
const user = await prisma.user.findFirstOrThrow({
|
2022-06-13 22:12:27 +00:00
|
|
|
where: {
|
|
|
|
id: req.session?.user?.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
type: "exchange2016_calendar",
|
2023-06-06 11:59:57 +00:00
|
|
|
key: symmetricEncrypt(JSON.stringify(body), process.env.CALENDSO_ENCRYPTION_KEY || ""),
|
2022-06-13 22:12:27 +00:00
|
|
|
userId: user.id,
|
2022-08-01 19:25:04 +00:00
|
|
|
appId: "exchange2016-calendar",
|
2022-12-07 21:47:02 +00:00
|
|
|
invalid: false,
|
2022-06-13 22:12:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const dav = new CalendarService({
|
|
|
|
id: 0,
|
|
|
|
...data,
|
|
|
|
});
|
|
|
|
await dav?.listCalendars();
|
|
|
|
await prisma.credential.create({
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
} catch (reason) {
|
|
|
|
logger.error("Could not add this exchange account", reason);
|
|
|
|
return res.status(500).json({ message: "Could not add this exchange account" });
|
|
|
|
}
|
|
|
|
|
2022-09-15 19:53:09 +00:00
|
|
|
return { url: getInstalledAppPath({ variant: "calendar", slug: "exchange2016-calendar" }) };
|
2022-06-13 22:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getHandler() {
|
|
|
|
return { url: "/apps/exchange2016-calendar/setup" };
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
GET: Promise.resolve({ default: defaultResponder(getHandler) }),
|
|
|
|
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
|
|
|
|
});
|