From dd61851572a17a1e4051b133683af85c934bc2d0 Mon Sep 17 00:00:00 2001 From: Leo Giovanetti Date: Tue, 15 Nov 2022 18:50:00 -0300 Subject: [PATCH] Stop relying on sendgrid client pkg --- packages/app-store/_utils/getCalendar.ts | 5 ++- .../app-store/_utils/useAddAppMutation.ts | 4 +-- .../closecomothercalendar/test/globals.ts | 11 +++--- .../test/lib/CalendarService.test.ts | 10 +++++- packages/app-store/sendgrid/package.json | 3 +- .../features/bookings/lib/handleNewBooking.ts | 3 +- packages/lib/Sendgrid.ts | 35 ++++++++++++++----- packages/lib/package.json | 1 - 8 files changed, 48 insertions(+), 24 deletions(-) diff --git a/packages/app-store/_utils/getCalendar.ts b/packages/app-store/_utils/getCalendar.ts index 742ac51a7a..213f150ae0 100644 --- a/packages/app-store/_utils/getCalendar.ts +++ b/packages/app-store/_utils/getCalendar.ts @@ -8,7 +8,10 @@ const log = logger.getChildLogger({ prefix: ["CalendarManager"] }); export const getCalendar = (credential: CredentialPayload | null): Calendar | null => { if (!credential || !credential.key) return null; - const { type: calendarType } = credential; + let { type: calendarType } = credential; + if (calendarType === "sendgrid_other_calendar") { + calendarType = "sendgrid"; + } const calendarApp = appStore[calendarType.split("_").join("") as keyof typeof appStore]; if (!(calendarApp && "lib" in calendarApp && "CalendarService" in calendarApp.lib)) { log.warn(`calendar of type ${calendarType} is not implemented`); diff --git a/packages/app-store/_utils/useAddAppMutation.ts b/packages/app-store/_utils/useAddAppMutation.ts index 479448596c..dfbe43f782 100644 --- a/packages/app-store/_utils/useAddAppMutation.ts +++ b/packages/app-store/_utils/useAddAppMutation.ts @@ -28,8 +28,8 @@ function useAddAppMutation(_type: App["type"] | null, options?: Parameters ({ - default: { - getChildLogger: () => ({ - debug: jest.fn(), - error: jest.fn(), - log: jest.fn(), - }), - }, + debug: jest.fn(), + error: jest.fn(), + log: jest.fn(), + getChildLogger: jest.fn(), })); jest.mock("@calcom/lib/crypto", () => ({ diff --git a/packages/app-store/closecomothercalendar/test/lib/CalendarService.test.ts b/packages/app-store/closecomothercalendar/test/lib/CalendarService.test.ts index 04fbd1044a..da4e316a6d 100644 --- a/packages/app-store/closecomothercalendar/test/lib/CalendarService.test.ts +++ b/packages/app-store/closecomothercalendar/test/lib/CalendarService.test.ts @@ -7,6 +7,14 @@ import { } from "@calcom/lib/CloseComeUtils"; import type { CalendarEvent } from "@calcom/types/Calendar"; +jest.mock("@calcom/lib/CloseCom", () => ({ + default: class { + constructor() { + /* Mock */ + } + }, +})); + afterEach(() => { jest.resetAllMocks(); }); @@ -160,7 +168,7 @@ test("prepare data to create custom activity type instance: two attendees, no ad const event = { attendees, startTime: now.toISOString(), - } as unknown as CalendarEvent; + } as CalendarEvent; CloseCom.prototype.activity = { type: { diff --git a/packages/app-store/sendgrid/package.json b/packages/app-store/sendgrid/package.json index 0292fb930d..6682e3315f 100644 --- a/packages/app-store/sendgrid/package.json +++ b/packages/app-store/sendgrid/package.json @@ -7,8 +7,7 @@ "description": "SendGrid delivers your transactional and marketing emails through the world's largest cloud-based email delivery platform.", "dependencies": { "@calcom/lib": "*", - "@calcom/prisma": "*", - "@sendgrid/client": "^7.7.0" + "@calcom/prisma": "*" }, "devDependencies": { "@calcom/types": "*" diff --git a/packages/features/bookings/lib/handleNewBooking.ts b/packages/features/bookings/lib/handleNewBooking.ts index 0d039ff4be..490ed9bf7b 100644 --- a/packages/features/bookings/lib/handleNewBooking.ts +++ b/packages/features/bookings/lib/handleNewBooking.ts @@ -869,7 +869,8 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) { results = createManager.results; referencesToCreate = createManager.referencesToCreate; - if (results.length > 0 && results.every((res) => !res.success)) { + const nonAppResults = results.filter((res) => !res.appName); + if (nonAppResults.length > 0 && nonAppResults.every((res) => !res.success)) { const error = { errorCode: "BookingCreatingMeetingFailed", message: "Booking failed", diff --git a/packages/lib/Sendgrid.ts b/packages/lib/Sendgrid.ts index b9e37d4a1e..bea5e6b48e 100644 --- a/packages/lib/Sendgrid.ts +++ b/packages/lib/Sendgrid.ts @@ -1,5 +1,3 @@ -import client from "@sendgrid/client"; - import logger from "@calcom/lib/logger"; export type SendgridFieldOptions = [string, string][]; @@ -48,11 +46,14 @@ const environmentApiKey = process.env.SENDGRID_SYNC_API_KEY || ""; */ export default class Sendgrid { private log: typeof logger; + private apiKey: string; + private apiUrl: string; constructor(providedApiKey = "") { - this.log = logger.getChildLogger({ prefix: [`[[lib] sendgrid`] }); + this.log = logger.getChildLogger({ prefix: [`[lib] sendgrid`] }); if (!providedApiKey && !environmentApiKey) throw Error("Sendgrid Api Key not present"); - client.setApiKey(providedApiKey || environmentApiKey); + this.apiKey = providedApiKey || environmentApiKey; + this.apiUrl = "https://api.sendgrid.com"; } public username = async () => { @@ -63,12 +64,28 @@ export default class Sendgrid { return username; }; - public async sendgridRequest(data: any): Promise { - this.log.debug("sendgridRequest:request", data); - const results = await client.request(data); + public async sendgridRequest(requestData: { url: string; method: string; body?: unknown }): Promise { + this.log.debug("sendgridRequest:request", requestData); + const results = await fetch(`${this.apiUrl}${requestData.url}`, { + method: requestData.method, + headers: new Headers({ + Authorization: `Bearer ${this.apiKey}`, + "Content-Type": "application/json", + }), + ...(requestData.body ? { body: JSON.stringify(requestData.body) } : {}), + }) + .then(async (res) => { + const jsonRes = await res.json(); + if (jsonRes.errors) { + throw Error(`Sendgrid request error: ${jsonRes.errors[0]}`); + } + return jsonRes; + }) + .catch((error) => { + throw Error(`Sendgrid request error: ${error}`); + }); this.log.debug("sendgridRequest:results", results); - if (results[1].errors) throw Error(`Sendgrid request error: ${results[1].errors}`); - return results[1]; + return results; } public async getSendgridContactId(email: string) { diff --git a/packages/lib/package.json b/packages/lib/package.json index 5649b4679c..220efa528a 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -14,7 +14,6 @@ "@calcom/config": "*", "@calcom/dayjs": "*", "@prisma/client": "^4.2.1", - "@sendgrid/client": "^7.7.0", "@vercel/og": "^0.0.19", "bcryptjs": "^2.4.3", "ical.js": "^1.4.0",