Files
cal.diy/packages/app-store/hubspot/api/add.ts
T
Joe Au-Yeung 5ee962948f feat: Readd app syncing (#11453)
* Revert "Revert "feat: Sync app credentials between Cal.com & self-hosted plat… (#11450)"

This reverts commit 2565d0915a.

* Add typing to `parseRefreshTokenResponse`

* Add typing

* Return safeParse data

* Type fix and refactor in Zoom
2023-09-20 19:01:54 +01:00

31 lines
1.1 KiB
TypeScript

import * as hubspot from "@hubspot/api-client";
import type { NextApiRequest, NextApiResponse } from "next";
import { WEBAPP_URL } from "@calcom/lib/constants";
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
import { encodeOAuthState } from "../../_utils/oauth/encodeOAuthState";
const scopes = ["crm.objects.contacts.read", "crm.objects.contacts.write"];
let client_id = "";
const hubspotClient = new hubspot.Client();
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "GET") return res.status(405).json({ message: "Method not allowed" });
const appKeys = await getAppKeysFromSlug("hubspot");
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
if (!client_id) return res.status(400).json({ message: "HubSpot client id missing." });
const redirectUri = WEBAPP_URL + "/api/integrations/hubspot/callback";
const url = hubspotClient.oauth.getAuthorizationUrl(
client_id,
redirectUri,
scopes.join(" "),
undefined,
encodeOAuthState(req)
);
res.status(200).json({ url });
}