Files
calendar/packages/app-store/exchangecalendar/api/_postAdd.ts
T
06d13e0f6e feat: Added option to select the exchange server version (#7908)
* Added option to select the exchange server version

* Fixed linting issues

* Removed yarn changes

* Update packages/app-store/exchangecalendar/lib/CalendarService.ts

Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>

* Removed requested change

---------

Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
2023-04-17 10:21:14 -04:00

53 lines
1.7 KiB
TypeScript

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";
import { ExchangeAuthentication, ExchangeVersion } from "../enums";
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),
exchangeVersion: z.number().default(ExchangeVersion.Exchange2016),
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 || "");
const data = {
type: "exchange_calendar",
key: encrypted,
userId: session.user?.id,
appId: "exchange",
invalid: false,
};
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);