From 939a2cd206f89f5d42d3245cf79f7a642ebb4d25 Mon Sep 17 00:00:00 2001 From: Valentin Chmara Date: Tue, 7 May 2024 23:02:06 +0200 Subject: [PATCH] fix: server location on credentials instead app keys --- .../app-store/_utils/updateAppServerLocation.ts | 15 --------------- packages/app-store/zohocalendar/api/callback.ts | 14 ++++++++------ .../app-store/zohocalendar/lib/CalendarService.ts | 13 +++++-------- .../app-store/zohocalendar/types/ZohoCalendar.ts | 1 + packages/app-store/zohocalendar/zod.ts | 1 - 5 files changed, 14 insertions(+), 30 deletions(-) delete mode 100644 packages/app-store/_utils/updateAppServerLocation.ts diff --git a/packages/app-store/_utils/updateAppServerLocation.ts b/packages/app-store/_utils/updateAppServerLocation.ts deleted file mode 100644 index 69edd2bcd18ac..0000000000000 --- a/packages/app-store/_utils/updateAppServerLocation.ts +++ /dev/null @@ -1,15 +0,0 @@ -import prisma from "@calcom/prisma"; - -import { appKeysSchema as zohoKeysSchema } from "../zohocalendar/zod"; - -async function updateAppServerLocation(slug: string, serverUrl: string) { - const app = await prisma.app.findUnique({ where: { slug } }); - const { client_id, client_secret } = zohoKeysSchema.parse(app?.keys) || {}; - const updatedKeys = { client_id, client_secret, server_location: serverUrl }; - await prisma.app.update({ - where: { slug }, - data: { keys: updatedKeys }, - }); -} - -export default updateAppServerLocation; diff --git a/packages/app-store/zohocalendar/api/callback.ts b/packages/app-store/zohocalendar/api/callback.ts index d0c17b47d06ce..ac36514d41908 100644 --- a/packages/app-store/zohocalendar/api/callback.ts +++ b/packages/app-store/zohocalendar/api/callback.ts @@ -12,7 +12,6 @@ import { Prisma } from "@calcom/prisma/client"; import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug"; import getInstalledAppPath from "../../_utils/getInstalledAppPath"; import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState"; -import updateAppServerLocation from "../../_utils/updateAppServerLocation"; import config from "../config.json"; import type { ZohoAuthCredentials } from "../types/ZohoCalendar"; import { appKeysSchema as zohoKeysSchema } from "../zod"; @@ -33,16 +32,17 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { return; } - if (!req.session?.user?.id) { - return res.status(401).json({ message: "You must be logged in to do this" }); + if (location && typeof location !== "string") { + res.status(400).json({ message: "`location` must be a string" }); + return; } - if (location && typeof location === "string") { - updateAppServerLocation(config.slug, location); + if (!req.session?.user?.id) { + return res.status(401).json({ message: "You must be logged in to do this" }); } const appKeys = await getAppKeysFromSlug(config.slug); - const { client_id, client_secret, server_location } = zohoKeysSchema.parse(appKeys); + const { client_id, client_secret } = zohoKeysSchema.parse(appKeys); const params = { client_id, @@ -51,6 +51,7 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { redirect_uri: `${WEBAPP_URL}/api/integrations/${config.slug}/callback`, code, }; + const server_location = location === "us" ? "com" : location; const query = stringify(params); @@ -72,6 +73,7 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { access_token: responseBody.access_token, refresh_token: responseBody.refresh_token, expires_in: Math.round(+new Date() / 1000 + responseBody.expires_in), + server_location: server_location || "com", }; function getCalenderUri(domain: string): string { diff --git a/packages/app-store/zohocalendar/lib/CalendarService.ts b/packages/app-store/zohocalendar/lib/CalendarService.ts index 121806c5ab376..0797732fe77ac 100644 --- a/packages/app-store/zohocalendar/lib/CalendarService.ts +++ b/packages/app-store/zohocalendar/lib/CalendarService.ts @@ -36,8 +36,8 @@ export default class ZohoCalendarService implements Calendar { const refreshAccessToken = async () => { try { const appKeys = await getAppKeysFromSlug("zohocalendar"); - const { client_id, client_secret, server_location } = zohoKeysSchema.parse(appKeys); - + const { client_id, client_secret } = zohoKeysSchema.parse(appKeys); + const server_location = zohoCredentials.server_location; const params = { client_id, grant_type: "refresh_token", @@ -65,6 +65,7 @@ export default class ZohoCalendarService implements Calendar { access_token: token.access_token, refresh_token: zohoCredentials.refresh_token, expires_in: Math.round(+new Date() / 1000 + token.expires_in), + server_location, }; await prisma.credential.update({ where: { id: credential.id }, @@ -87,9 +88,7 @@ export default class ZohoCalendarService implements Calendar { private fetcher = async (endpoint: string, init?: RequestInit | undefined) => { const credentials = await this.auth.getToken(); - const appKeys = await getAppKeysFromSlug("zohocalendar"); - const { server_location } = zohoKeysSchema.parse(appKeys); - return fetch(`https://calendar.zoho.${server_location}/api/v1${endpoint}`, { + return fetch(`https://calendar.zoho.${credentials.server_location}/api/v1${endpoint}`, { method: "GET", ...init, headers: { @@ -102,9 +101,7 @@ export default class ZohoCalendarService implements Calendar { private getUserInfo = async () => { const credentials = await this.auth.getToken(); - const appKeys = await getAppKeysFromSlug("zohocalendar"); - const { server_location } = zohoKeysSchema.parse(appKeys); - const response = await fetch(`https://accounts.zoho.${server_location}/oauth/user/info`, { + const response = await fetch(`https://accounts.zoho.${credentials.server_location}/oauth/user/info`, { method: "GET", headers: { Authorization: `Bearer ${credentials.access_token}`, diff --git a/packages/app-store/zohocalendar/types/ZohoCalendar.ts b/packages/app-store/zohocalendar/types/ZohoCalendar.ts index 0ebd9ea2c1974..77de55d071a8d 100644 --- a/packages/app-store/zohocalendar/types/ZohoCalendar.ts +++ b/packages/app-store/zohocalendar/types/ZohoCalendar.ts @@ -2,6 +2,7 @@ export type ZohoAuthCredentials = { access_token: string; refresh_token: string; expires_in: number; + server_location: string; }; export type FreeBusy = { diff --git a/packages/app-store/zohocalendar/zod.ts b/packages/app-store/zohocalendar/zod.ts index 40d89ae03c48a..0a84054ebef3f 100644 --- a/packages/app-store/zohocalendar/zod.ts +++ b/packages/app-store/zohocalendar/zod.ts @@ -5,5 +5,4 @@ export const appDataSchema = z.object({}); export const appKeysSchema = z.object({ client_id: z.string().min(1), client_secret: z.string().min(1), - server_location: z.string().optional(), });