Files
calendar/packages/app-store/zohocrm/api/callback.ts
T
Keith WilliamsGitHubdevin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>keith@cal.com <keith@cal.com>
6615b885d1 chore: Make app compatible with Fluid Compute (#19841)
* chore: Make app compatible with Fluid Compute

* Removed isCold from me api endpoint

* chore: Remove handler caching for Fluid Compute compatibility (#20692)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: keith@cal.com <keith@cal.com>

* fix client_id issue

* chore: Remove UNSTABLE_HANDLER_CACHE for Fluid Compute compatibility (#20694)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: keith@cal.com <keith@cal.com>

* Fixing types

* Fixing type issues

* chore: Remove importHandler references for Fluid Compute compatibility

Co-Authored-By: keith@cal.com <keith@cal.com>

* chore: Remove importHandler references from attributes router for Fluid Compute compatibility

Co-Authored-By: keith@cal.com <keith@cal.com>

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: keith@cal.com <keith@cal.com>
2025-04-14 18:27:59 -04:00

86 lines
3.3 KiB
TypeScript

import axios from "axios";
import type { NextApiRequest, NextApiResponse } from "next";
import qs from "qs";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
import createOAuthAppCredential from "../../_utils/oauth/createOAuthAppCredential";
import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState";
import appConfig from "../config.json";
function isAuthorizedAccountsServerUrl(accountsServer: string) {
// As per https://www.zoho.com/crm/developer/docs/api/v6/multi-dc.html#:~:text=US:%20https://accounts.zoho,https://accounts.zohocloud.ca&text=The%20%22location=us%22%20parameter,domain%20in%20all%20API%20endpoints.&text=You%20must%20make%20the%20authorization,.zoho.com.cn.
const authorizedAccountServers = [
"https://accounts.zoho.com",
"https://accounts.zoho.eu",
"https://accounts.zoho.in",
"https://accounts.zoho.com.cn",
"https://accounts.zoho.jp",
"https://accounts.zohocloud.ca",
"https://accounts.zoho.com.au",
];
return authorizedAccountServers.includes(accountsServer);
}
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { code, "accounts-server": accountsServer } = req.query;
if (code === undefined && typeof code !== "string") {
res.status(400).json({ message: "`code` must be a string" });
return;
}
if (!accountsServer || typeof accountsServer !== "string") {
res.status(400).json({ message: "`accounts-server` is required and must be a string" });
return;
}
if (!isAuthorizedAccountsServerUrl(accountsServer)) {
res.status(400).json({ message: "`accounts-server` is not authorized" });
return;
}
if (!req.session?.user?.id) {
return res.status(401).json({ message: "You must be logged in to do this" });
}
let clientId = "";
let clientSecret = "";
const appKeys = await getAppKeysFromSlug("zohocrm");
if (typeof appKeys.client_id === "string") clientId = appKeys.client_id;
if (typeof appKeys.client_secret === "string") clientSecret = appKeys.client_secret;
if (!clientId) return res.status(400).json({ message: "Zoho Crm consumer key missing." });
if (!clientSecret) return res.status(400).json({ message: "Zoho Crm consumer secret missing." });
const url = `${accountsServer}/oauth/v2/token`;
const redirectUri = `${WEBAPP_URL}/api/integrations/zohocrm/callback`;
const formData = {
grant_type: "authorization_code",
client_id: clientId,
client_secret: clientSecret,
redirect_uri: redirectUri,
code: code,
};
const zohoCrmTokenInfo = await axios({
method: "post",
url: url,
data: qs.stringify(formData),
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
},
});
// set expiry date as offset from current time.
zohoCrmTokenInfo.data.expiryDate = Math.round(Date.now() + 60 * 60);
zohoCrmTokenInfo.data.accountServer = accountsServer;
await createOAuthAppCredential({ appId: appConfig.slug, type: appConfig.type }, zohoCrmTokenInfo.data, req);
const state = decodeOAuthState(req);
res.redirect(
getSafeRedirectUrl(state?.returnTo) ?? getInstalledAppPath({ variant: "other", slug: "zohocrm" })
);
}