2022-04-16 02:23:38 +00:00
|
|
|
import * as hubspot from "@hubspot/api-client";
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
|
|
|
2023-07-11 07:41:21 +00:00
|
|
|
import { encodeOAuthState } from "../../_utils/encodeOAuthState";
|
2022-05-02 23:13:34 +00:00
|
|
|
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
|
|
|
|
2022-04-16 02:23:38 +00:00
|
|
|
const scopes = ["crm.objects.contacts.read", "crm.objects.contacts.write"];
|
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
let client_id = "";
|
2022-04-16 02:23:38 +00:00
|
|
|
const hubspotClient = new hubspot.Client();
|
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2022-05-02 23:13:34 +00:00
|
|
|
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." });
|
2022-04-16 02:23:38 +00:00
|
|
|
|
2022-11-22 20:44:08 +00:00
|
|
|
const redirectUri = WEBAPP_URL + "/api/integrations/hubspot/callback";
|
2023-07-11 07:41:21 +00:00
|
|
|
const url = hubspotClient.oauth.getAuthorizationUrl(
|
|
|
|
client_id,
|
|
|
|
redirectUri,
|
|
|
|
scopes.join(" "),
|
|
|
|
undefined,
|
|
|
|
encodeOAuthState(req)
|
|
|
|
);
|
2022-05-02 23:13:34 +00:00
|
|
|
res.status(200).json({ url });
|
2022-04-16 02:23:38 +00:00
|
|
|
}
|