2022-09-14 16:09:00 +00:00
|
|
|
import { SoapFaultDetails } from "ews-javascript-api";
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import { symmetricEncrypt } from "@calcom/lib/crypto";
|
|
|
|
import logger from "@calcom/lib/logger";
|
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import checkSession from "../../_utils/auth";
|
2023-04-17 14:21:14 +00:00
|
|
|
import { ExchangeAuthentication, ExchangeVersion } from "../enums";
|
2022-09-14 16:09:00 +00:00
|
|
|
import { CalendarService } from "../lib";
|
|
|
|
|
|
|
|
const formSchema = z
|
|
|
|
.object({
|
|
|
|
url: z.string().url(),
|
|
|
|
username: z.string().email(),
|
|
|
|
password: z.string(),
|
|
|
|
authenticationMethod: z.number().default(ExchangeAuthentication.STANDARD),
|
2023-04-17 14:21:14 +00:00
|
|
|
exchangeVersion: z.number().default(ExchangeVersion.Exchange2016),
|
2022-09-14 16:09:00 +00:00
|
|
|
useCompression: z.boolean().default(false),
|
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
|
|
|
export async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
const session = checkSession(req);
|
|
|
|
const body = formSchema.parse(req.body);
|
|
|
|
const encrypted = symmetricEncrypt(JSON.stringify(body), process.env.CALENDSO_ENCRYPTION_KEY || "");
|
2022-12-07 21:47:02 +00:00
|
|
|
const data = {
|
|
|
|
type: "exchange_calendar",
|
|
|
|
key: encrypted,
|
|
|
|
userId: session.user?.id,
|
2023-08-01 11:10:52 +00:00
|
|
|
teamId: null,
|
2022-12-07 21:47:02 +00:00
|
|
|
appId: "exchange",
|
|
|
|
invalid: false,
|
|
|
|
};
|
2022-09-14 16:09:00 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const service = new CalendarService({ id: 0, ...data });
|
|
|
|
await service?.listCalendars();
|
|
|
|
await prisma.credential.create({ data });
|
|
|
|
} catch (reason) {
|
|
|
|
logger.info(reason);
|
|
|
|
if (reason instanceof SoapFaultDetails && reason.message != "") {
|
|
|
|
return res.status(500).json({ message: reason.message });
|
|
|
|
}
|
|
|
|
return res.status(500).json({ message: "Could not add this exchange account" });
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(200).json({ url: "/apps/installed" });
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(getHandler);
|