* feat: add HubSpot owner(organizer in cal) * refactor: improve logging and token handling in HubSpot integration * Update packages/app-store/hubspot/lib/CrmService.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * feat: enhance error handling for missing scopes * refactor * remove unused --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> Co-authored-by: Amit Sharma <74371312+Amit91848@users.noreply.github.com>
31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import * as hubspot from "@hubspot/api-client";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { WEBAPP_URL_FOR_OAUTH } 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", "crm.objects.owners.read"];
|
|
|
|
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");
|
|
let clientId = "";
|
|
if (typeof appKeys.client_id === "string") clientId = appKeys.client_id;
|
|
if (!clientId) return res.status(400).json({ message: "HubSpot client id missing." });
|
|
|
|
const redirectUri = `${WEBAPP_URL_FOR_OAUTH}/api/integrations/hubspot/callback`;
|
|
const url = hubspotClient.oauth.getAuthorizationUrl(
|
|
clientId,
|
|
redirectUri,
|
|
scopes.join(" "),
|
|
undefined,
|
|
encodeOAuthState(req)
|
|
);
|
|
res.status(200).json({ url });
|
|
}
|