Skip to content

Commit

Permalink
perf: Use UNION to get public user event types
Browse files Browse the repository at this point in the history
  • Loading branch information
keithwillcode committed Oct 10, 2024
1 parent c8e8800 commit 7a66640
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions packages/lib/event-types/getEventTypesPublic.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -21,42 +23,46 @@ export async function getEventTypesPublic(userId: number) {
}));
}

type BaseEventType = Prisma.EventTypeGetPayload<{
select: typeof baseEventTypeSelect;
}>;

type RawEventType = BaseEventType & {
metadata: Record<string, any> | 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<RawEventType[]>`
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<typeof eventTypes>((eventTypes, eventType) => {
Expand Down

0 comments on commit 7a66640

Please sign in to comment.