|
| 1 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 2 | + |
| 3 | +import { WEBAPP_URL } from "@calcom/lib/constants"; |
| 4 | +import prisma from "@calcom/prisma"; |
| 5 | + |
| 6 | +import getInstalledAppPath from "../../_utils/getInstalledAppPath"; |
| 7 | +import config from "../config.json"; |
| 8 | +import { getWebexAppKeys } from "../lib/getWebexAppKeys"; |
| 9 | + |
| 10 | +export default async function handler(req: NextApiRequest, res: NextApiResponse) { |
| 11 | + const { code } = req.query; |
| 12 | + const { client_id, client_secret } = await getWebexAppKeys(); |
| 13 | + |
| 14 | + /** @link https://developer.webex.com/docs/integrations#getting-an-access-token **/ |
| 15 | + |
| 16 | + const redirectUri = encodeURI(`${WEBAPP_URL}/api/integrations/${config.slug}/callback`); |
| 17 | + const authHeader = "Basic " + Buffer.from(client_id + ":" + client_secret).toString("base64"); |
| 18 | + const result = await fetch( |
| 19 | + "https://webexapis.com/v1/access_token?grant_type=authorization_code&client_id" + |
| 20 | + client_id + |
| 21 | + "&client_secret=" + |
| 22 | + client_secret + |
| 23 | + "&code=" + |
| 24 | + code + |
| 25 | + "&redirect_uri=" + |
| 26 | + redirectUri, |
| 27 | + { |
| 28 | + method: "POST", |
| 29 | + headers: { |
| 30 | + Authorization: authHeader, |
| 31 | + "Content-Type": "application/x-www-form-urlencoded", |
| 32 | + }, |
| 33 | + } |
| 34 | + ); |
| 35 | + |
| 36 | + if (result.status !== 200) { |
| 37 | + let errorMessage = "Something is wrong with Webex API"; |
| 38 | + try { |
| 39 | + const responseBody = await result.json(); |
| 40 | + errorMessage = responseBody.error; |
| 41 | + } catch (e) {} |
| 42 | + |
| 43 | + res.status(400).json({ message: errorMessage }); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const responseBody = await result.json(); |
| 48 | + |
| 49 | + if (responseBody.error) { |
| 50 | + res.status(400).json({ message: responseBody.error }); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + responseBody.expiry_date = Math.round(Date.now() + responseBody.expires_in * 1000); |
| 55 | + delete responseBody.expires_in; |
| 56 | + |
| 57 | + const userId = req.session?.user.id; |
| 58 | + if (!userId) { |
| 59 | + return res.status(404).json({ message: "No user found" }); |
| 60 | + } |
| 61 | + /** |
| 62 | + * With this we take care of no duplicate webex key for a single user |
| 63 | + * when creating a video room we only do findFirst so the if they have more than 1 |
| 64 | + * others get ignored |
| 65 | + * */ |
| 66 | + const existingCredentialWebexVideo = await prisma.credential.findMany({ |
| 67 | + select: { |
| 68 | + id: true, |
| 69 | + }, |
| 70 | + where: { |
| 71 | + type: config.type, |
| 72 | + userId: req.session?.user.id, |
| 73 | + appId: config.slug, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + // Making sure we only delete webex_video |
| 78 | + const credentialIdsToDelete = existingCredentialWebexVideo.map((item) => item.id); |
| 79 | + if (credentialIdsToDelete.length > 0) { |
| 80 | + await prisma.credential.deleteMany({ where: { id: { in: credentialIdsToDelete }, userId } }); |
| 81 | + } |
| 82 | + |
| 83 | + await prisma.user.update({ |
| 84 | + where: { |
| 85 | + id: req.session?.user.id, |
| 86 | + }, |
| 87 | + data: { |
| 88 | + credentials: { |
| 89 | + create: { |
| 90 | + type: config.type, |
| 91 | + key: responseBody, |
| 92 | + appId: config.slug, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + res.redirect(getInstalledAppPath({ variant: config.variant, slug: config.slug })); |
| 99 | +} |
0 commit comments