| 
									
										
										
										
											2022-04-16 02:23:38 +00:00
										 |  |  | import * as hubspot from "@hubspot/api-client"; | 
					
						
							|  |  |  | import { TokenResponseIF } from "@hubspot/api-client/lib/codegen/oauth/models/TokenResponseIF"; | 
					
						
							|  |  |  | import type { NextApiRequest, NextApiResponse } from "next"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { WEBAPP_URL } from "@calcom/lib/constants"; | 
					
						
							| 
									
										
										
										
											2022-04-27 14:28:36 +00:00
										 |  |  | import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl"; | 
					
						
							| 
									
										
										
										
											2022-04-16 02:23:38 +00:00
										 |  |  | import prisma from "@calcom/prisma"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { decodeOAuthState } from "../../_utils/decodeOAuthState"; | 
					
						
							| 
									
										
										
										
											2022-05-02 20:39:35 +00:00
										 |  |  | import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug"; | 
					
						
							| 
									
										
										
										
											2022-04-16 02:23:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:39:35 +00:00
										 |  |  | let client_id = ""; | 
					
						
							|  |  |  | let client_secret = ""; | 
					
						
							| 
									
										
										
										
											2022-04-16 02:23:38 +00:00
										 |  |  | const hubspotClient = new hubspot.Client(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export type HubspotToken = TokenResponseIF & { | 
					
						
							|  |  |  |   expiryDate?: number; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default async function handler(req: NextApiRequest, res: NextApiResponse) { | 
					
						
							|  |  |  |   const { code } = req.query; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (code && typeof code !== "string") { | 
					
						
							|  |  |  |     res.status(400).json({ message: "`code` must be a string" }); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:39:35 +00:00
										 |  |  |   if (!req.session?.user?.id) { | 
					
						
							|  |  |  |     return res.status(401).json({ message: "You must be logged in to do this" }); | 
					
						
							| 
									
										
										
										
											2022-04-16 02:23:38 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:39:35 +00:00
										 |  |  |   const appKeys = await getAppKeysFromSlug("hubspot"); | 
					
						
							|  |  |  |   if (typeof appKeys.client_id === "string") client_id = appKeys.client_id; | 
					
						
							|  |  |  |   if (typeof appKeys.client_secret === "string") client_secret = appKeys.client_secret; | 
					
						
							|  |  |  |   if (!client_id) return res.status(400).json({ message: "HubSpot client id missing." }); | 
					
						
							|  |  |  |   if (!client_secret) return res.status(400).json({ message: "HubSpot client secret missing." }); | 
					
						
							| 
									
										
										
										
											2022-04-16 02:23:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const hubspotToken: HubspotToken = await hubspotClient.oauth.tokensApi.createToken( | 
					
						
							|  |  |  |     "authorization_code", | 
					
						
							|  |  |  |     code, | 
					
						
							|  |  |  |     WEBAPP_URL + "/api/integrations/hubspotothercalendar/callback", | 
					
						
							|  |  |  |     client_id, | 
					
						
							|  |  |  |     client_secret | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // set expiry date as offset from current time.
 | 
					
						
							|  |  |  |   hubspotToken.expiryDate = Math.round(Date.now() + hubspotToken.expiresIn * 1000); | 
					
						
							|  |  |  |   await prisma.credential.create({ | 
					
						
							|  |  |  |     data: { | 
					
						
							|  |  |  |       type: "hubspot_other_calendar", | 
					
						
							|  |  |  |       key: hubspotToken as any, | 
					
						
							| 
									
										
										
										
											2022-05-02 20:39:35 +00:00
										 |  |  |       userId: req.session.user.id, | 
					
						
							|  |  |  |       appId: "hubspot", | 
					
						
							| 
									
										
										
										
											2022-04-16 02:23:38 +00:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const state = decodeOAuthState(req); | 
					
						
							| 
									
										
										
										
											2022-04-27 14:28:36 +00:00
										 |  |  |   res.redirect(getSafeRedirectUrl(state?.returnTo) ?? "/apps/installed"); | 
					
						
							| 
									
										
										
										
											2022-04-16 02:23:38 +00:00
										 |  |  | } |