diff --git a/packages/features/calendars/lib/CalendarManager.test.ts b/packages/features/calendars/lib/CalendarManager.test.ts index 0c2effdb5b..a9f7b637d7 100644 --- a/packages/features/calendars/lib/CalendarManager.test.ts +++ b/packages/features/calendars/lib/CalendarManager.test.ts @@ -1,17 +1,22 @@ import { prisma } from "@calcom/prisma/__mocks__/prisma"; - -import { describe, expect, it, vi, beforeEach } from "vitest"; - +import type { CalendarEvent } from "@calcom/types/Calendar"; +import { beforeEach, describe, expect, it, vi } from "vitest"; import { - getCalendarCredentials, deduplicateCredentialsBasedOnSelectedCalendars, + deleteEvent, + getCalendarCredentials, processEvent, } from "./CalendarManager"; +import { getCalendar } from "@calcom/app-store/_utils/getCalendar"; vi.mock("@calcom/prisma", () => ({ prisma, })); +vi.mock("@calcom/app-store/_utils/getCalendar", () => ({ + getCalendar: vi.fn(), +})); + vi.mock("@calcom/lib/constants", () => ({ ORGANIZER_EMAIL_EXEMPT_DOMAINS: "", IS_PRODUCTION: false, @@ -560,4 +565,37 @@ describe("CalendarManager tests", () => { expect(uniqueCredentials).toEqual(credentials); }); }); + + describe("fn: deleteEvent", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it("should return early when bookingRefUid is empty string", async () => { + vi.mocked(getCalendar).mockResolvedValue({ deleteEvent: vi.fn() } as any); + + const credential = { + id: 1, + type: "google_calendar", + key: { access_token: "test_token" }, + encryptedKey: null, + userId: 1, + user: { email: "test@example.com" }, + teamId: null, + appId: "google-calendar", + invalid: false, + delegationCredentialId: null, + delegatedTo: null, + }; + + const result = await deleteEvent({ + credential, + bookingRefUid: "", + event: buildCalendarEvent(), + externalCalendarId: "calendar-id", + }); + + expect(result).toEqual({}); + }); + }); }); diff --git a/packages/features/calendars/lib/CalendarManager.ts b/packages/features/calendars/lib/CalendarManager.ts index c0422945df..f1eb527d83 100644 --- a/packages/features/calendars/lib/CalendarManager.ts +++ b/packages/features/calendars/lib/CalendarManager.ts @@ -546,6 +546,16 @@ export const deleteEvent = async ({ event: CalendarEvent; externalCalendarId?: string | null; }): Promise => { + if (!bookingRefUid) { + log.error( + "deleteEvent failed - bookingRefUid is empty, skipping calendar deletion to prevent malformed API request", + safeStringify({ + credential: getPiiFreeCredential(credential), + event: getPiiFreeCalendarEvent(event), + }) + ); + return Promise.resolve({}); + } const calendar = await getCalendar(credential, "booking"); log.debug( "Deleting calendar event",