2022-02-04 18:30:52 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-02-04 18:30:52 +00:00
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
2022-09-15 19:53:09 +00:00
|
|
|
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
2022-05-02 23:13:34 +00:00
|
|
|
|
|
|
|
let client_id = "";
|
|
|
|
let client_secret = "";
|
|
|
|
let base_url = "https://tandem.chat";
|
2022-02-04 18:30:52 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
if (!req.query.code) {
|
|
|
|
res.status(401).json({ message: "Missing code" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const code = req.query.code as string;
|
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
const appKeys = await getAppKeysFromSlug("tandem");
|
|
|
|
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
|
|
|
|
if (typeof appKeys.client_secret === "string") client_secret = appKeys.client_secret;
|
|
|
|
if (typeof appKeys.base_url === "string") base_url = appKeys.base_url;
|
|
|
|
if (!client_id) return res.status(400).json({ message: "Tandem client_id missing." });
|
|
|
|
if (!client_secret) return res.status(400).json({ message: "Tandem client_secret missing." });
|
|
|
|
if (!base_url) return res.status(400).json({ message: "Tandem base_url missing." });
|
|
|
|
|
|
|
|
const result = await fetch(`${base_url}/api/v1/oauth/v2/token`, {
|
2022-02-04 18:30:52 +00:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ code, client_id, client_secret }),
|
|
|
|
});
|
|
|
|
|
|
|
|
const responseBody = await result.json();
|
|
|
|
|
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" });
|
|
|
|
}
|
|
|
|
|
|
|
|
const existingCredentialTandemVideo = await prisma.credential.findMany({
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
type: "tandem_video",
|
|
|
|
userId: req.session?.user.id,
|
|
|
|
appId: "tandem",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const credentialIdsToDelete = existingCredentialTandemVideo.map((item) => item.id);
|
|
|
|
if (credentialIdsToDelete.length > 0) {
|
|
|
|
await prisma.credential.deleteMany({ where: { id: { in: credentialIdsToDelete }, userId } });
|
|
|
|
}
|
2022-02-04 18:30:52 +00:00
|
|
|
if (result.ok) {
|
|
|
|
responseBody.expiry_date = Math.round(Date.now() + responseBody.expires_in * 1000);
|
|
|
|
delete responseBody.expires_in;
|
|
|
|
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
2022-03-23 22:00:30 +00:00
|
|
|
id: req.session?.user.id,
|
2022-02-04 18:30:52 +00:00
|
|
|
},
|
|
|
|
data: {
|
|
|
|
credentials: {
|
|
|
|
create: {
|
|
|
|
type: "tandem_video",
|
|
|
|
key: responseBody,
|
2022-05-02 23:13:34 +00:00
|
|
|
appId: "tandem",
|
2022-02-04 18:30:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-15 19:53:09 +00:00
|
|
|
res.redirect(getInstalledAppPath({ variant: "conferencing", slug: "tandem" }));
|
2022-02-04 18:30:52 +00:00
|
|
|
}
|