From 717a26f22323fbfe17bba30c0eda3ed737e9049e Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Wed, 19 Nov 2025 15:46:05 -0500 Subject: [PATCH] fix: prevent bulk update of locked locations in child managed event types (#24978) * fix: prevent bulk update of locked locations in child managed event types - Filter out child managed event types with locked locations in getBulkUserEventTypes - Add validation in bulkUpdateEventsToDefaultLocation to prevent updating locked fields - Implements defense in depth with validation at multiple layers Co-Authored-By: joe@cal.com * Abstract filtering logic * test: add comprehensive tests for bulk location update filtering - Add unit tests for filterEventTypesWhereLocationUpdateIsAllowed - Add unit tests for bulkUpdateEventsToDefaultLocation - Add integration tests for getBulkUserEventTypes - Fix bug: change unlockedFields?.locations check from !== undefined to === true This ensures that locations: false is properly treated as locked, addressing the security issue identified in PR review comments Co-Authored-By: joe@cal.com * fix: filter locked managed event types on app installation page - Add parentId to eventTypeSelect in getEventTypes function - Apply filterEventTypesWhereLocationUpdateIsAllowed to both team and user event types - Only filter when isConferencing is true to avoid affecting other app types - Fixes issue where locked managed event types were showing in the event type selection list on /apps/installation/event-types page Co-Authored-By: joe@cal.com * fix(embed-react): remove obsolete availabilityLoaded event listener The availabilityLoaded event does not exist in the EventDataMap type system in embed-core. This code was causing 5 TypeScript errors in CI: - Type 'availabilityLoaded' does not satisfy constraint 'keyof EventDataMap' - 'data' is of type 'unknown' (2 occurrences) - Type 'availabilityLoaded' is not assignable to action union (2 occurrences) Since this is an example file and the event is not defined in the type system, removing this obsolete code resolves the type errors. Co-Authored-By: joe@cal.com * fix: correct Prisma type for metadata in test helper function Co-Authored-By: joe@cal.com * fix: use flexible PrismaLike type for better test compatibility Co-Authored-By: joe@cal.com * fix: properly type mock Prisma objects in test files Co-Authored-By: joe@cal.com * fix: properly mock Prisma methods in test file Co-Authored-By: joe@cal.com * Filter out metadata * Undo change in embed file * Address feedback --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../[[...step]]/getServerSideProps.ts | 64 +-- .../bulkUpdateEventsToDefaultLocation.test.ts | 348 +++++++++++++++++ .../bulkUpdateEventsToDefaultLocation.ts | 29 +- .../_utils/getBulkEventTypes.test.ts | 364 ++++++++++++++++++ .../app-store/_utils/getBulkEventTypes.ts | 37 +- .../inviteMember.handler.integration-test.ts | 3 +- 6 files changed, 818 insertions(+), 27 deletions(-) create mode 100644 packages/app-store/_utils/bulkUpdateEventsToDefaultLocation.test.ts create mode 100644 packages/app-store/_utils/getBulkEventTypes.test.ts diff --git a/apps/web/lib/apps/installation/[[...step]]/getServerSideProps.ts b/apps/web/lib/apps/installation/[[...step]]/getServerSideProps.ts index aff5221966..bc92a0a781 100644 --- a/apps/web/lib/apps/installation/[[...step]]/getServerSideProps.ts +++ b/apps/web/lib/apps/installation/[[...step]]/getServerSideProps.ts @@ -1,6 +1,7 @@ import type { GetServerSidePropsContext } from "next"; import { z } from "zod"; +import { filterEventTypesWhereLocationUpdateIsAllowed } from "@calcom/app-store/_utils/getBulkEventTypes"; import { appStoreMetadata } from "@calcom/app-store/appStoreMetaData"; import type { LocationObject } from "@calcom/app-store/locations"; import { isConferencing as isConferencingApp } from "@calcom/app-store/utils"; @@ -73,7 +74,15 @@ const getAppBySlug = async (appSlug: string) => { return app; }; -const getEventTypes = async (userId: number, teamIds?: number[]) => { +const getEventTypes = async ({ + userId, + teamIds, + isConferencing = false, +}: { + userId: number; + teamIds?: number[]; + isConferencing?: boolean; +}) => { const eventTypeSelect = { id: true, description: true, @@ -96,6 +105,7 @@ const getEventTypes = async (userId: number, teamIds?: number[]) => { destinationCalendar: true, bookingFields: true, calVideoSettings: true, + parentId: true, } satisfies Prisma.EventTypeSelect; let eventTypeGroups: TEventTypeGroup[] | null = []; @@ -119,22 +129,28 @@ const getEventTypes = async (userId: number, teamIds?: number[]) => { }, }, }); - eventTypeGroups = teams.map((team) => ({ - teamId: team.id, - slug: team.slug, - name: team.name, - isOrganisation: team.isOrganization, - image: getPlaceholderAvatar(team.logoUrl, team.name), - eventTypes: team.eventTypes - .map((item) => ({ - ...item, - URL: `${CAL_URL}/${item.team ? `team/${item.team.slug}` : item?.users?.[0]?.username}/${item.slug}`, - selected: false, - locations: item.locations as unknown as LocationObject[], - bookingFields: eventTypeBookingFields.parse(item.bookingFields || []), - })) - .sort((eventTypeA, eventTypeB) => eventTypeB.position - eventTypeA.position), - })); + eventTypeGroups = teams.map((team) => { + const filteredEventTypes = isConferencing + ? filterEventTypesWhereLocationUpdateIsAllowed(team.eventTypes) + : team.eventTypes; + + return { + teamId: team.id, + slug: team.slug, + name: team.name, + isOrganisation: team.isOrganization, + image: getPlaceholderAvatar(team.logoUrl, team.name), + eventTypes: filteredEventTypes + .map((item) => ({ + ...item, + URL: `${CAL_URL}/${item.team ? `team/${item.team.slug}` : item?.users?.[0]?.username}/${item.slug}`, + selected: false, + locations: item.locations as unknown as LocationObject[], + bookingFields: eventTypeBookingFields.parse(item.bookingFields || []), + })) + .sort((eventTypeA, eventTypeB) => eventTypeB.position - eventTypeA.position), + }; + }); } else { const user = await prisma.user.findUnique({ where: { @@ -155,12 +171,16 @@ const getEventTypes = async (userId: number, teamIds?: number[]) => { }); if (user) { + const filteredEventTypes = isConferencing + ? filterEventTypesWhereLocationUpdateIsAllowed(user.eventTypes) + : user.eventTypes; + eventTypeGroups.push({ userId: user.id, slug: user.username, name: user.name, image: getPlaceholderAvatar(user.avatarUrl, user.name), - eventTypes: user.eventTypes + eventTypes: filteredEventTypes .map((item) => ({ ...item, URL: `${CAL_URL}/${item.team ? `team/${item.team.slug}` : item?.users?.[0]?.username}/${ @@ -208,7 +228,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => const _ = stepsEnum.parse(parsedStepParam); const session = await getServerSession({ req }); if (!session?.user?.id) return { redirect: { permanent: false, destination: "/auth/login" } }; - const locale = await getLocale(context.req); + const _locale = await getLocale(context.req); const app = await getAppBySlug(parsedAppSlug); if (!app) return { redirect: { permanent: false, destination: "/apps" } }; const appMetadata = appStoreMetadata[app.dirName as keyof typeof appStoreMetadata]; @@ -246,11 +266,11 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => } if (isOrg) { const teamIds = userTeams.map((item) => item.id); - eventTypeGroups = await getEventTypes(user.id, teamIds); + eventTypeGroups = await getEventTypes({ userId: user.id, teamIds, isConferencing }); } else if (parsedTeamIdParam) { - eventTypeGroups = await getEventTypes(user.id, [parsedTeamIdParam]); + eventTypeGroups = await getEventTypes({ userId: user.id, teamIds: [parsedTeamIdParam], isConferencing }); } else { - eventTypeGroups = await getEventTypes(user.id); + eventTypeGroups = await getEventTypes({ userId: user.id, isConferencing }); } if (isConferencing && eventTypeGroups) { const destinationCalendar = await prisma.destinationCalendar.findFirst({ diff --git a/packages/app-store/_utils/bulkUpdateEventsToDefaultLocation.test.ts b/packages/app-store/_utils/bulkUpdateEventsToDefaultLocation.test.ts new file mode 100644 index 0000000000..c9a7209179 --- /dev/null +++ b/packages/app-store/_utils/bulkUpdateEventsToDefaultLocation.test.ts @@ -0,0 +1,348 @@ +import { describe, expect, it, beforeEach, vi } from "vitest"; + +import type { PrismaClient } from "@calcom/prisma"; + +import { bulkUpdateEventsToDefaultLocation } from "./bulkUpdateEventsToDefaultLocation"; + +vi.mock("../utils", () => ({ + getAppFromSlug: vi.fn((slug: string) => { + if (slug === "google-meet") { + return { + slug: "google-meet", + appData: { + location: { + type: "integrations:google:meet", + }, + }, + }; + } + if (slug === "zoom") { + return { + slug: "zoom", + appData: { + location: { + type: "integrations:zoom", + }, + }, + }; + } + return null; + }), +})); + +type MockPrisma = { + credential: { + findFirst: ReturnType; + }; + eventType: { + findMany: ReturnType; + updateMany: ReturnType; + }; +}; + +const createMockPrisma = (): MockPrisma => ({ + credential: { + findFirst: vi.fn(), + }, + eventType: { + findMany: vi.fn(), + updateMany: vi.fn(), + }, +}); + +describe("bulkUpdateEventsToDefaultLocation", () => { + let mockPrisma: MockPrisma; + + beforeEach(() => { + mockPrisma = createMockPrisma(); + vi.clearAllMocks(); + }); + + it("should throw error when default conferencing app is not set", async () => { + const user = { + id: 1, + metadata: {}, + }; + + await expect( + bulkUpdateEventsToDefaultLocation({ + eventTypeIds: [1, 2], + user, + prisma: mockPrisma as unknown as PrismaClient, + }) + ).rejects.toThrow("Default conferencing app not set"); + }); + + it("should throw error when default conferencing app doesn't exist", async () => { + const user = { + id: 1, + metadata: { + defaultConferencingApp: { + appSlug: "non-existent-app", + appLink: "https://example.com", + }, + }, + }; + + await expect( + bulkUpdateEventsToDefaultLocation({ + eventTypeIds: [1, 2], + user, + prisma: mockPrisma as unknown as PrismaClient, + }) + ).rejects.toThrow("Default conferencing app 'non-existent-app' doesnt exist."); + }); + + it("should update event types that are not child managed events", async () => { + const user = { + id: 1, + metadata: { + defaultConferencingApp: { + appSlug: "google-meet", + appLink: "https://meet.google.com", + }, + }, + }; + + const credential = { id: 100 }; + const eventTypes = [ + { id: 1, parentId: null, metadata: {} }, + { id: 2, parentId: null, metadata: {} }, + ]; + + mockPrisma.credential.findFirst.mockResolvedValue(credential); + mockPrisma.eventType.findMany.mockResolvedValue(eventTypes); + mockPrisma.eventType.updateMany.mockResolvedValue({ count: 2 }); + + const result = await bulkUpdateEventsToDefaultLocation({ + eventTypeIds: [1, 2], + user, + prisma: mockPrisma as unknown as PrismaClient, + }); + + expect(result.count).toBe(2); + expect(mockPrisma.eventType.updateMany).toHaveBeenCalledWith({ + where: { + id: { + in: [1, 2], + }, + userId: 1, + }, + data: { + locations: [ + { + type: "integrations:google:meet", + link: "https://meet.google.com", + credentialId: 100, + }, + ], + }, + }); + }); + + it("should filter out child managed event types with locked locations", async () => { + const user = { + id: 1, + metadata: { + defaultConferencingApp: { + appSlug: "zoom", + appLink: "https://zoom.us", + }, + }, + }; + + const credential = { id: 200 }; + const eventTypes = [ + { id: 1, parentId: null, metadata: {} }, + { + id: 2, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + }, + }, + }, + }, + { + id: 3, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + locations: true, // unlocked + }, + }, + }, + }, + ]; + + mockPrisma.credential.findFirst.mockResolvedValue(credential); + mockPrisma.eventType.findMany.mockResolvedValue(eventTypes); + mockPrisma.eventType.updateMany.mockResolvedValue({ count: 2 }); + + const result = await bulkUpdateEventsToDefaultLocation({ + eventTypeIds: [1, 2, 3], + user, + prisma: mockPrisma as unknown as PrismaClient, + }); + + expect(result.count).toBe(2); + expect(mockPrisma.eventType.updateMany).toHaveBeenCalledWith({ + where: { + id: { + in: [1, 3], + }, + userId: 1, + }, + data: { + locations: [ + { + type: "integrations:zoom", + link: "https://zoom.us", + credentialId: 200, + }, + ], + }, + }); + }); + + it("should return count 0 when all event types have locked locations", async () => { + const user = { + id: 1, + metadata: { + defaultConferencingApp: { + appSlug: "google-meet", + appLink: "https://meet.google.com", + }, + }, + }; + + const credential = { id: 300 }; + const eventTypes = [ + { + id: 1, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + }, + }, + }, + }, + { + id: 2, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + locations: false, // explicitly locked + }, + }, + }, + }, + ]; + + mockPrisma.credential.findFirst.mockResolvedValue(credential); + mockPrisma.eventType.findMany.mockResolvedValue(eventTypes); + + const result = await bulkUpdateEventsToDefaultLocation({ + eventTypeIds: [1, 2], + user, + prisma: mockPrisma as unknown as PrismaClient, + }); + + expect(result.count).toBe(0); + expect(mockPrisma.eventType.updateMany).not.toHaveBeenCalled(); + }); + + it("should work without credential when credential is not found", async () => { + const user = { + id: 1, + metadata: { + defaultConferencingApp: { + appSlug: "google-meet", + appLink: "https://meet.google.com", + }, + }, + }; + + const eventTypes = [{ id: 1, parentId: null, metadata: {} }]; + + mockPrisma.credential.findFirst.mockResolvedValue(null); + mockPrisma.eventType.findMany.mockResolvedValue(eventTypes); + mockPrisma.eventType.updateMany.mockResolvedValue({ count: 1 }); + + const result = await bulkUpdateEventsToDefaultLocation({ + eventTypeIds: [1], + user, + prisma: mockPrisma as unknown as PrismaClient, + }); + + expect(result.count).toBe(1); + expect(mockPrisma.eventType.updateMany).toHaveBeenCalledWith({ + where: { + id: { + in: [1], + }, + userId: 1, + }, + data: { + locations: [ + { + type: "integrations:google:meet", + link: "https://meet.google.com", + credentialId: undefined, + }, + ], + }, + }); + }); + + it("should only query event types belonging to the user", async () => { + const user = { + id: 5, + metadata: { + defaultConferencingApp: { + appSlug: "zoom", + appLink: "https://zoom.us", + }, + }, + }; + + const credential = { id: 400 }; + const eventTypes = [{ id: 10, parentId: null, metadata: {} }]; + + mockPrisma.credential.findFirst.mockResolvedValue(credential); + mockPrisma.eventType.findMany.mockResolvedValue(eventTypes); + mockPrisma.eventType.updateMany.mockResolvedValue({ count: 1 }); + + await bulkUpdateEventsToDefaultLocation({ + eventTypeIds: [10, 11, 12], + user, + prisma: mockPrisma as unknown as PrismaClient, + }); + + expect(mockPrisma.eventType.findMany).toHaveBeenCalledWith({ + where: { + id: { + in: [10, 11, 12], + }, + userId: 5, + }, + select: { + id: true, + metadata: true, + parentId: true, + }, + }); + + expect(mockPrisma.eventType.updateMany).toHaveBeenCalledWith( + expect.objectContaining({ + where: expect.objectContaining({ + userId: 5, + }), + }) + ); + }); +}); diff --git a/packages/app-store/_utils/bulkUpdateEventsToDefaultLocation.ts b/packages/app-store/_utils/bulkUpdateEventsToDefaultLocation.ts index 1dd0bc3788..f676e8f1fa 100644 --- a/packages/app-store/_utils/bulkUpdateEventsToDefaultLocation.ts +++ b/packages/app-store/_utils/bulkUpdateEventsToDefaultLocation.ts @@ -4,6 +4,9 @@ import { userMetadata as userMetadataSchema } from "@calcom/prisma/zod-utils"; import type { LocationObject } from "../locations"; import { getAppFromSlug } from "../utils"; +import { filterEventTypesWhereLocationUpdateIsAllowed } from "./getBulkEventTypes"; + +type PrismaLike = Pick; export const bulkUpdateEventsToDefaultLocation = async ({ eventTypeIds, @@ -12,7 +15,7 @@ export const bulkUpdateEventsToDefaultLocation = async ({ }: { eventTypeIds: number[]; user: Pick; - prisma: PrismaClient; + prisma: PrismaLike; }) => { const defaultApp = userMetadataSchema.parse(user.metadata)?.defaultConferencingApp; @@ -36,13 +39,35 @@ export const bulkUpdateEventsToDefaultLocation = async ({ }, }); - return await prisma.eventType.updateMany({ + const eventTypesToUpdate = await prisma.eventType.findMany({ where: { id: { in: eventTypeIds, }, userId: user.id, }, + select: { + id: true, + metadata: true, + parentId: true, + }, + }); + + const validEventTypeIds = filterEventTypesWhereLocationUpdateIsAllowed(eventTypesToUpdate).map( + (eventType) => eventType.id + ); + + if (validEventTypeIds.length === 0) { + return { count: 0 }; + } + + return await prisma.eventType.updateMany({ + where: { + id: { + in: validEventTypeIds, + }, + userId: user.id, + }, data: { locations: [ { type: appType, link: defaultApp.appLink, credentialId: credential?.id }, diff --git a/packages/app-store/_utils/getBulkEventTypes.test.ts b/packages/app-store/_utils/getBulkEventTypes.test.ts new file mode 100644 index 0000000000..c5af114a89 --- /dev/null +++ b/packages/app-store/_utils/getBulkEventTypes.test.ts @@ -0,0 +1,364 @@ +import { describe, expect, it, beforeEach, vi } from "vitest"; + +import type { Prisma } from "@calcom/prisma/client"; + +import { filterEventTypesWhereLocationUpdateIsAllowed, getBulkUserEventTypes } from "./getBulkEventTypes"; + +vi.mock("@calcom/prisma", () => ({ + prisma: { + eventType: { + findMany: vi.fn(), + }, + }, +})); + +vi.mock("../utils", () => ({ + getAppFromLocationValue: vi.fn((locationType: string) => { + if (locationType === "integrations:daily") { + return { logo: "/daily-logo.svg" }; + } + if (locationType === "integrations:google:meet") { + return { logo: "/google-meet-logo.svg" }; + } + return { logo: "/default-logo.svg" }; + }), +})); + +import { prisma } from "@calcom/prisma"; + +describe("filterEventTypesWhereLocationUpdateIsAllowed", () => { + it("should allow location updates for event types without a parent", () => { + const eventTypes = [ + { + id: 1, + parentId: null, + metadata: {}, + }, + { + id: 2, + parentId: null, + metadata: null, + }, + ]; + + const result = filterEventTypesWhereLocationUpdateIsAllowed(eventTypes); + expect(result).toHaveLength(2); + expect(result).toEqual(eventTypes); + }); + + it("should allow location updates for child event types with unlocked locations", () => { + const eventTypes = [ + { + id: 1, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + locations: true, + }, + }, + }, + }, + ]; + + const result = filterEventTypesWhereLocationUpdateIsAllowed(eventTypes); + expect(result).toHaveLength(1); + expect(result[0].id).toBe(1); + }); + + it("should block location updates for child event types with locked locations (locations field undefined)", () => { + const eventTypes = [ + { + id: 1, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + }, + }, + }, + }, + ]; + + const result = filterEventTypesWhereLocationUpdateIsAllowed(eventTypes); + expect(result).toHaveLength(0); + }); + + it("should block location updates for child event types with explicitly locked locations (locations: false)", () => { + const eventTypes = [ + { + id: 1, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + locations: false, + }, + }, + }, + }, + ]; + + const result = filterEventTypesWhereLocationUpdateIsAllowed(eventTypes); + expect(result).toHaveLength(0); + }); + + it("should allow location updates for child event types without managedEventConfig", () => { + const eventTypes = [ + { + id: 1, + parentId: 100, + metadata: { + }, + }, + ]; + + const result = filterEventTypesWhereLocationUpdateIsAllowed(eventTypes); + expect(result).toHaveLength(1); + expect(result[0].id).toBe(1); + }); + + it("should allow location updates for child event types with invalid metadata", () => { + const eventTypes = [ + { + id: 1, + parentId: 100, + metadata: "invalid metadata", + }, + ]; + + const result = filterEventTypesWhereLocationUpdateIsAllowed(eventTypes); + expect(result).toHaveLength(1); + expect(result[0].id).toBe(1); + }); + + it("should handle mixed scenarios correctly", () => { + const eventTypes = [ + { + id: 1, + parentId: null, + metadata: {}, + }, + { + id: 2, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + locations: true, + }, + }, + }, + }, + { + id: 3, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + }, + }, + }, + }, + { + id: 4, + parentId: 100, + metadata: { + managedEventConfig: { + unlockedFields: { + locations: false, + }, + }, + }, + }, + { + id: 5, + parentId: 100, + metadata: null, + }, + ]; + + const result = filterEventTypesWhereLocationUpdateIsAllowed(eventTypes); + expect(result).toHaveLength(3); + expect(result.map((et) => et.id)).toEqual([1, 2, 5]); + }); + + it("should preserve all properties of filtered event types", () => { + const eventTypes = [ + { + id: 1, + parentId: null, + metadata: {}, + title: "Test Event", + locations: [{ type: "integrations:daily" }], + }, + ]; + + const result = filterEventTypesWhereLocationUpdateIsAllowed(eventTypes); + expect(result[0]).toEqual(eventTypes[0]); + expect(result[0]).toHaveProperty("title"); + expect(result[0]).toHaveProperty("locations"); + }); +}); + +describe("getBulkUserEventTypes", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it("should return user event types with logos and filter out locked child events", async () => { + const userId = 1; + const mockEventTypes = [ + { + id: 1, + title: "Regular Event", + locations: [{ type: "integrations:daily" }] as Prisma.JsonValue, + metadata: {} as Prisma.JsonValue, + parentId: null, + }, + { + id: 2, + title: "Child Event - Unlocked", + locations: [{ type: "integrations:google:meet" }] as Prisma.JsonValue, + metadata: { + managedEventConfig: { + unlockedFields: { + locations: true, + }, + }, + } as Prisma.JsonValue, + parentId: 100, + }, + { + id: 3, + title: "Child Event - Locked", + locations: [{ type: "integrations:daily" }] as Prisma.JsonValue, + metadata: { + managedEventConfig: { + unlockedFields: { + }, + }, + } as Prisma.JsonValue, + parentId: 100, + }, + ]; + + vi.mocked(prisma.eventType.findMany).mockResolvedValue( + mockEventTypes as unknown as Awaited> + ); + + const result = await getBulkUserEventTypes(userId); + + expect(result.eventTypes).toHaveLength(2); + expect(result.eventTypes[0].id).toBe(1); + expect(result.eventTypes[0].title).toBe("Regular Event"); + expect(result.eventTypes[0].logo).toBe("/daily-logo.svg"); + expect(result.eventTypes[1].id).toBe(2); + expect(result.eventTypes[1].title).toBe("Child Event - Unlocked"); + expect(result.eventTypes[1].logo).toBe("/google-meet-logo.svg"); + + expect(prisma.eventType.findMany).toHaveBeenCalledWith({ + where: { + userId: userId, + teamId: null, + }, + select: { + id: true, + title: true, + locations: true, + metadata: true, + parentId: true, + }, + }); + }); + + it("should return all event types when none have locked locations", async () => { + const userId = 2; + const mockEventTypes = [ + { + id: 10, + title: "Event 1", + locations: [{ type: "integrations:daily" }] as Prisma.JsonValue, + metadata: {} as Prisma.JsonValue, + parentId: null, + }, + { + id: 11, + title: "Event 2", + locations: [{ type: "integrations:daily" }] as Prisma.JsonValue, + metadata: {} as Prisma.JsonValue, + parentId: null, + }, + ]; + + vi.mocked(prisma.eventType.findMany).mockResolvedValue( + mockEventTypes as unknown as Awaited> + ); + + const result = await getBulkUserEventTypes(userId); + + expect(result.eventTypes).toHaveLength(2); + expect(result.eventTypes.map((et) => et.id)).toEqual([10, 11]); + }); + + it("should return empty array when all event types have locked locations", async () => { + const userId = 3; + const mockEventTypes = [ + { + id: 20, + title: "Locked Event 1", + locations: [{ type: "integrations:daily" }] as Prisma.JsonValue, + metadata: { + managedEventConfig: { + unlockedFields: {}, + }, + } as Prisma.JsonValue, + parentId: 100, + }, + { + id: 21, + title: "Locked Event 2", + locations: [{ type: "integrations:daily" }] as Prisma.JsonValue, + metadata: { + managedEventConfig: { + unlockedFields: { + locations: false, + }, + }, + } as Prisma.JsonValue, + parentId: 100, + }, + ]; + + vi.mocked(prisma.eventType.findMany).mockResolvedValue( + mockEventTypes as unknown as Awaited> + ); + + const result = await getBulkUserEventTypes(userId); + + expect(result.eventTypes).toHaveLength(0); + }); + + it("should handle event types with null locations gracefully", async () => { + const userId = 4; + const mockEventTypes = [ + { + id: 30, + title: "Event with null location", + locations: null as unknown as Prisma.JsonValue, + metadata: {} as Prisma.JsonValue, + parentId: null, + }, + ]; + + vi.mocked(prisma.eventType.findMany).mockResolvedValue( + mockEventTypes as unknown as Awaited> + ); + + const result = await getBulkUserEventTypes(userId); + + expect(result.eventTypes).toHaveLength(1); + expect(result.eventTypes[0].id).toBe(30); + expect(result.eventTypes[0].logo).toBe("/daily-logo.svg"); + }); +}); diff --git a/packages/app-store/_utils/getBulkEventTypes.ts b/packages/app-store/_utils/getBulkEventTypes.ts index d2c573ae1d..9876d33466 100644 --- a/packages/app-store/_utils/getBulkEventTypes.ts +++ b/packages/app-store/_utils/getBulkEventTypes.ts @@ -1,5 +1,8 @@ import { prisma } from "@calcom/prisma"; -import { eventTypeLocations as eventTypeLocationsSchema } from "@calcom/prisma/zod-utils"; +import { + eventTypeLocations as eventTypeLocationsSchema, + eventTypeMetaDataSchemaWithoutApps, +} from "@calcom/prisma/zod-utils"; import { getAppFromLocationValue } from "../utils"; @@ -38,10 +41,19 @@ export const getBulkUserEventTypes = async (userId: number) => { id: true, title: true, locations: true, + metadata: true, + parentId: true, }, }); - return processEventTypes(eventTypes); + const filteredEventTypes = filterEventTypesWhereLocationUpdateIsAllowed(eventTypes); + + return processEventTypes( + filteredEventTypes.map((eventType) => { + const { metadata: _metadata, ...rest } = eventType; + return { ...rest }; + }) + ); }; export const getBulkTeamEventTypes = async (teamId: number) => { @@ -59,3 +71,24 @@ export const getBulkTeamEventTypes = async (teamId: number) => { return processEventTypes(eventTypes); }; + +export const filterEventTypesWhereLocationUpdateIsAllowed = < + T extends { parentId: number | null; metadata: unknown } +>( + eventTypes: T[] +): T[] => { + const filteredEventTypes = eventTypes.filter((eventType) => { + if (!eventType.parentId) { + return true; + } + + const metadata = eventTypeMetaDataSchemaWithoutApps.safeParse(eventType.metadata); + if (!metadata.success || !metadata.data?.managedEventConfig) { + return true; + } + + const unlockedFields = metadata.data.managedEventConfig.unlockedFields; + return unlockedFields?.locations === true; + }); + return filteredEventTypes; +}; diff --git a/packages/trpc/server/routers/viewer/teams/inviteMember/inviteMember.handler.integration-test.ts b/packages/trpc/server/routers/viewer/teams/inviteMember/inviteMember.handler.integration-test.ts index dd5262f5a4..eb0c59588d 100644 --- a/packages/trpc/server/routers/viewer/teams/inviteMember/inviteMember.handler.integration-test.ts +++ b/packages/trpc/server/routers/viewer/teams/inviteMember/inviteMember.handler.integration-test.ts @@ -4,6 +4,7 @@ import { prisma } from "@calcom/prisma"; import type { Team, User, Membership, Profile } from "@calcom/prisma/client"; import { MembershipRole } from "@calcom/prisma/enums"; import type { TrpcSessionUser } from "@calcom/trpc/server/types"; +import type { JsonValue } from "@calcom/types/Json"; import inviteMemberHandler, { inviteMembersWithNoInviterPermissionCheck } from "./inviteMember.handler"; @@ -65,7 +66,7 @@ async function createTestTeam(data: { slug: string; isOrganization?: boolean; parentId?: number; - metadata?: Record; + metadata?: JsonValue; organizationSettings?: { orgAutoAcceptEmail: string; isOrganizationVerified?: boolean;