2021-08-19 12:27:01 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
2022-03-23 22:00:30 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2021-06-10 23:37:58 +00:00
|
|
|
|
2022-06-11 22:15:29 +00:00
|
|
|
import { getZoomAppKeys } from "../lib";
|
2021-06-10 23:37:58 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-08-19 12:27:01 +00:00
|
|
|
const { code } = req.query;
|
2022-06-11 22:15:29 +00:00
|
|
|
const { client_id, client_secret } = await getZoomAppKeys();
|
2022-05-02 23:13:34 +00:00
|
|
|
|
|
|
|
const redirectUri = encodeURI(WEBAPP_URL + "/api/integrations/zoomvideo/callback");
|
2021-08-19 12:27:01 +00:00
|
|
|
const authHeader = "Basic " + Buffer.from(client_id + ":" + client_secret).toString("base64");
|
|
|
|
const result = await fetch(
|
|
|
|
"https://zoom.us/oauth/token?grant_type=authorization_code&code=" + code + "&redirect_uri=" + redirectUri,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Authorization: authHeader,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-12-15 14:02:39 +00:00
|
|
|
|
|
|
|
const responseBody = await result.json();
|
|
|
|
|
|
|
|
responseBody.expiry_date = Math.round(Date.now() + responseBody.expires_in * 1000);
|
|
|
|
delete responseBody.expires_in;
|
2021-08-19 12:27:01 +00:00
|
|
|
|
2022-08-02 18:52:21 +00:00
|
|
|
const userId = req.session?.user.id;
|
|
|
|
if (!userId) {
|
|
|
|
return res.status(404).json({ message: "No user found" });
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* With this we take care of no duplicate zoom_video key for a single user
|
|
|
|
* when creating a video room we only do findFirst so the if they have more than 1
|
|
|
|
* others get ignored
|
|
|
|
* */
|
|
|
|
const existingCredentialZoomVideo = await prisma.credential.findMany({
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
type: "zoom_video",
|
|
|
|
userId: req.session?.user.id,
|
|
|
|
appId: "zoom",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Making sure we only delete zoom_video
|
|
|
|
const credentialIdsToDelete = existingCredentialZoomVideo.map((item) => item.id);
|
|
|
|
if (credentialIdsToDelete.length > 0) {
|
|
|
|
await prisma.credential.deleteMany({ where: { id: { in: credentialIdsToDelete }, userId } });
|
|
|
|
}
|
|
|
|
|
2021-12-10 21:14:54 +00:00
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
2022-03-23 22:00:30 +00:00
|
|
|
id: req.session?.user.id,
|
2021-12-10 21:14:54 +00:00
|
|
|
},
|
2021-08-19 12:27:01 +00:00
|
|
|
data: {
|
2021-12-10 21:14:54 +00:00
|
|
|
credentials: {
|
|
|
|
create: {
|
|
|
|
type: "zoom_video",
|
2021-12-15 14:02:39 +00:00
|
|
|
key: responseBody,
|
2022-06-23 22:00:39 +00:00
|
|
|
appId: "zoom",
|
2021-12-10 21:14:54 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
|
|
|
});
|
2021-12-10 21:14:54 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
res.redirect("/apps/installed");
|
2021-08-19 12:27:01 +00:00
|
|
|
}
|