From 7a66640808756981b257eacf77cca33c90497046 Mon Sep 17 00:00:00 2001 From: Keith Williams Date: Thu, 10 Oct 2024 15:49:25 -0300 Subject: [PATCH] perf: Use UNION to get public user event types --- .../lib/event-types/getEventTypesPublic.ts | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/packages/lib/event-types/getEventTypesPublic.ts b/packages/lib/event-types/getEventTypesPublic.ts index 628de9e6b98e2b..18a3f7d378d7f6 100644 --- a/packages/lib/event-types/getEventTypesPublic.ts +++ b/packages/lib/event-types/getEventTypesPublic.ts @@ -1,6 +1,8 @@ +import type { Prisma } from "@prisma/client"; + import logger from "@calcom/lib/logger"; import prisma from "@calcom/prisma"; -import { baseEventTypeSelect } from "@calcom/prisma/selects"; +import type { baseEventTypeSelect } from "@calcom/prisma/selects"; import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils"; import { markdownToSafeHTML } from "../markdownToSafeHTML"; @@ -21,42 +23,46 @@ export async function getEventTypesPublic(userId: number) { })); } +type BaseEventType = Prisma.EventTypeGetPayload<{ + select: typeof baseEventTypeSelect; +}>; + +type RawEventType = BaseEventType & { + metadata: Record | null; +}; + const getEventTypesWithHiddenFromDB = async (userId: number) => { - const eventTypes = await prisma.eventType.findMany({ - where: { - AND: [ - { - teamId: null, - }, - { - OR: [ - { - userId, - }, - { - users: { - some: { - id: userId, - }, - }, - }, - ], - }, - ], - }, - orderBy: [ - { - position: "desc", - }, - { - id: "asc", - }, - ], - select: { - ...baseEventTypeSelect, - metadata: true, - }, - }); + const eventTypes = await prisma.$queryRaw` + SELECT data."id", data."title", data."description", data."length", data."schedulingType"::text, + data."recurringEvent", data."slug", data."hidden", data."price", data."currency", + data."lockTimeZoneToggleOnBookingPage", data."requiresConfirmation", data."requiresBookerEmailVerification", + data."metadata" + FROM ( + SELECT "public"."EventType"."id", "public"."EventType"."title", "public"."EventType"."description", + "public"."EventType"."position", "public"."EventType"."length", "public"."EventType"."schedulingType"::text, + "public"."EventType"."recurringEvent", "public"."EventType"."slug", "public"."EventType"."hidden", + "public"."EventType"."price", "public"."EventType"."currency", + "public"."EventType"."lockTimeZoneToggleOnBookingPage", "public"."EventType"."requiresConfirmation", + "public"."EventType"."requiresBookerEmailVerification", "public"."EventType"."metadata" + FROM "public"."EventType" + WHERE "public"."EventType"."teamId" IS NULL AND "public"."EventType"."userId" = ${userId} + UNION + SELECT "public"."EventType"."id", "public"."EventType"."title", "public"."EventType"."description", + "public"."EventType"."position", "public"."EventType"."length", "public"."EventType"."schedulingType"::text, + "public"."EventType"."recurringEvent", "public"."EventType"."slug", "public"."EventType"."hidden", + "public"."EventType"."price", "public"."EventType"."currency", + "public"."EventType"."lockTimeZoneToggleOnBookingPage", "public"."EventType"."requiresConfirmation", + "public"."EventType"."requiresBookerEmailVerification", "public"."EventType"."metadata" + FROM "public"."EventType" + WHERE "public"."EventType"."teamId" IS NULL + AND "public"."EventType"."id" IN ( + SELECT "uet1"."A" FROM "public"."_user_eventtype" AS "uet1" + INNER JOIN "public"."users" AS "u1" ON "u1"."id" = "uet1"."B" + WHERE "u1"."id" = ${userId} AND "uet1"."A" IS NOT NULL + ) + ) data + ORDER BY data."position" DESC, data."id" ASC`; + // map and filter metadata, exclude eventType entirely when faulty metadata is found. // report error to exception so we don't lose the error. return eventTypes.reduce((eventTypes, eventType) => {