From bad97b90ab29cec844fba85c371d8f46bd4125d7 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Tue, 14 Apr 2026 21:40:03 -0300 Subject: [PATCH] refactor: map Zoho Calendar location to known domains --- .../app-store/zohocalendar/api/callback.ts | 11 ++------- .../zohocalendar/lib/zoho-domains.test.ts | 24 +++++++++++++++++++ .../zohocalendar/lib/zoho-domains.ts | 15 ++++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 packages/app-store/zohocalendar/lib/zoho-domains.test.ts create mode 100644 packages/app-store/zohocalendar/lib/zoho-domains.ts diff --git a/packages/app-store/zohocalendar/api/callback.ts b/packages/app-store/zohocalendar/api/callback.ts index 23266d609d17eb..7720e4e76229d3 100644 --- a/packages/app-store/zohocalendar/api/callback.ts +++ b/packages/app-store/zohocalendar/api/callback.ts @@ -14,6 +14,7 @@ import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug"; import getInstalledAppPath from "../../_utils/getInstalledAppPath"; import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState"; import config from "../config.json"; +import { getValidZohoDomain } from "../lib/zoho-domains"; import type { ZohoAuthCredentials } from "../types/ZohoCalendar"; import { appKeysSchema as zohoKeysSchema } from "../zod"; @@ -53,15 +54,7 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { code, }; - let server_location; - - if (location === "us") { - server_location = "com"; - } else if (location === "au") { - server_location = "com.au"; - } else { - server_location = location; - } + const server_location = getValidZohoDomain(location); const query = stringify(params); diff --git a/packages/app-store/zohocalendar/lib/zoho-domains.test.ts b/packages/app-store/zohocalendar/lib/zoho-domains.test.ts new file mode 100644 index 00000000000000..ebb25b22afba8b --- /dev/null +++ b/packages/app-store/zohocalendar/lib/zoho-domains.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from "vitest"; + +import { getValidZohoDomain } from "./zoho-domains"; + +describe("getValidZohoDomain", () => { + it.each([ + ["us", "com"], + ["eu", "eu"], + ["in", "in"], + ["cn", "com.cn"], + ["jp", "jp"], + ["au", "com.au"], + ])("maps location '%s' to domain '%s'", (location, expected) => { + expect(getValidZohoDomain(location)).toBe(expected); + }); + + it("falls back to 'com' for undefined location", () => { + expect(getValidZohoDomain(undefined)).toBe("com"); + }); + + it("falls back to 'com' for unknown location", () => { + expect(getValidZohoDomain("evil.com")).toBe("com"); + }); +}); diff --git a/packages/app-store/zohocalendar/lib/zoho-domains.ts b/packages/app-store/zohocalendar/lib/zoho-domains.ts new file mode 100644 index 00000000000000..5f1134d8bcd50d --- /dev/null +++ b/packages/app-store/zohocalendar/lib/zoho-domains.ts @@ -0,0 +1,15 @@ +// Valid Zoho data center domains per https://www.zoho.com/crm/developer/docs/api/v6/multi-dc.html +const LOCATION_TO_DOMAIN: Record = { + us: "com", + eu: "eu", + in: "in", + cn: "com.cn", + jp: "jp", + au: "com.au", +}; + +const DEFAULT_DOMAIN = "com"; + +export function getValidZohoDomain(location: string | undefined): string { + return LOCATION_TO_DOMAIN[location || "us"] || DEFAULT_DOMAIN; +}