Files
calendar/packages/app-store/webex/api/callback.ts
T
e2cfd0d1d1 Webex integration (#7651)
* start webex app creation

* webex integration wip

* fix lint errors

* fix lint errors

* add webex env vars in appStore.example

* webex app wip

* fix lint errors

* edit webex oauth scopes

* add location in webex app config

* add site url placeholder and regex in webex config location

* debug translateEvent

* fix utc formatting for event start time, add test boilerplate for webex, add envs

* fix location and datetime formatting

* get correct videoCredentials for deleteMeeting

* Move webex specific readme content to webex README

* Fix app not visible in app-store

* Delete setup route

* add webex icon

* delete prev icon

* webex api fix

* add app screenshots

* Revert tests changes as they dont run

* Use config instead of hardcoding vales

* Update README

* Remove all env variables related to WEBEX app. They can be added through settings->admin->apps interface

* update from origin

* fix icon path

* update webex readme

* Update yarn.lock

* update webex readme

* Remove unnecessary URL from webex

* revert changes in cancel booking handler

* simply webex zod schemas, logs for debugging

---------

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
2023-05-11 07:14:32 +00:00

100 lines
2.8 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from "next";
import { WEBAPP_URL } from "@calcom/lib/constants";
import prisma from "@calcom/prisma";
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
import config from "../config.json";
import { getWebexAppKeys } from "../lib/getWebexAppKeys";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { code } = req.query;
const { client_id, client_secret } = await getWebexAppKeys();
/** @link https://developer.webex.com/docs/integrations#getting-an-access-token **/
const redirectUri = encodeURI(`${WEBAPP_URL}/api/integrations/${config.slug}/callback`);
const authHeader = "Basic " + Buffer.from(client_id + ":" + client_secret).toString("base64");
const result = await fetch(
"https://webexapis.com/v1/access_token?grant_type=authorization_code&client_id" +
client_id +
"&client_secret=" +
client_secret +
"&code=" +
code +
"&redirect_uri=" +
redirectUri,
{
method: "POST",
headers: {
Authorization: authHeader,
"Content-Type": "application/x-www-form-urlencoded",
},
}
);
if (result.status !== 200) {
let errorMessage = "Something is wrong with Webex API";
try {
const responseBody = await result.json();
errorMessage = responseBody.error;
} catch (e) {}
res.status(400).json({ message: errorMessage });
return;
}
const responseBody = await result.json();
if (responseBody.error) {
res.status(400).json({ message: responseBody.error });
return;
}
responseBody.expiry_date = Math.round(Date.now() + responseBody.expires_in * 1000);
delete responseBody.expires_in;
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 webex 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 existingCredentialWebexVideo = await prisma.credential.findMany({
select: {
id: true,
},
where: {
type: config.type,
userId: req.session?.user.id,
appId: config.slug,
},
});
// Making sure we only delete webex_video
const credentialIdsToDelete = existingCredentialWebexVideo.map((item) => item.id);
if (credentialIdsToDelete.length > 0) {
await prisma.credential.deleteMany({ where: { id: { in: credentialIdsToDelete }, userId } });
}
await prisma.user.update({
where: {
id: req.session?.user.id,
},
data: {
credentials: {
create: {
type: config.type,
key: responseBody,
appId: config.slug,
},
},
},
});
res.redirect(getInstalledAppPath({ variant: config.variant, slug: config.slug }));
}