From 5f8fcdf100d5a2beac933cc93e918648b7ec28e7 Mon Sep 17 00:00:00 2001 From: supalarry Date: Mon, 30 Oct 2023 10:38:36 +0100 Subject: [PATCH] kysely query 5 --- packages/kysely/utils/json/traverse/index.ts | 14 ++++++++ .../trpc/server/routers/viewer/slots/util.ts | 35 ++++++++++++++----- 2 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 packages/kysely/utils/json/traverse/index.ts diff --git a/packages/kysely/utils/json/traverse/index.ts b/packages/kysely/utils/json/traverse/index.ts new file mode 100644 index 0000000000..10c63ec42c --- /dev/null +++ b/packages/kysely/utils/json/traverse/index.ts @@ -0,0 +1,14 @@ +import type { ExpressionBuilder, StringReference } from "kysely"; +import { sql } from "kysely"; + +export function traverseJSON( + eb: ExpressionBuilder, + column: StringReference, + path: string | [string, ...string[]] +) { + if (!Array.isArray(path)) { + path = [path]; + } + + return sql`${sql.ref(column)}->${sql.raw(path.map((item) => `'${item}'`).join("->"))}`; +} diff --git a/packages/trpc/server/routers/viewer/slots/util.ts b/packages/trpc/server/routers/viewer/slots/util.ts index f8bac04958..6c1f8a3f0b 100644 --- a/packages/trpc/server/routers/viewer/slots/util.ts +++ b/packages/trpc/server/routers/viewer/slots/util.ts @@ -12,11 +12,13 @@ import dayjs from "@calcom/dayjs"; import { getSlugOrRequestedSlug, orgDomainConfig } from "@calcom/ee/organizations/lib/orgDomains"; import { isEventTypeLoggingEnabled } from "@calcom/features/bookings/lib/isEventTypeLoggingEnabled"; import db from "@calcom/kysely"; +import { traverseJSON } from "@calcom/kysely/utils/json/traverse"; import { getDefaultEvent } from "@calcom/lib/defaultEvents"; import isTimeOutOfBounds from "@calcom/lib/isOutOfBounds"; import logger from "@calcom/lib/logger"; import { performance } from "@calcom/lib/server/perfObserver"; import getSlots from "@calcom/lib/slots"; +import slugify from "@calcom/lib/slugify"; import prisma, { availabilityUserSelect } from "@calcom/prisma"; import { SchedulingType } from "@calcom/prisma/enums"; import { BookingStatus } from "@calcom/prisma/enums"; @@ -774,15 +776,30 @@ async function getUserIdFromUsername( organizationDetails: { currentOrgDomain: string | null; isValidOrgDomain: boolean } ) { const { currentOrgDomain, isValidOrgDomain } = organizationDetails; - const user = await prisma.user.findFirst({ - where: { - username, - organization: isValidOrgDomain && currentOrgDomain ? getSlugOrRequestedSlug(currentOrgDomain) : null, - }, - select: { - id: true, - }, - }); + + const user = await db + .selectFrom("users") + .innerJoin("Team", "Team.id", "users.organizationId") + .where((eb) => { + const and: Expression[] = []; + + and.push(eb("username", "=", username)); + + if (isValidOrgDomain && currentOrgDomain) { + const slugifiedValue = slugify(currentOrgDomain); + and.push( + eb.or([ + eb("Team.slug", "=", slugifiedValue), + eb(traverseJSON(eb, "metadata", "requestedSlug"), "=", slugifiedValue), + ]) + ); + } + + return eb.and(and); + }) + .select("users.id") + .executeTakeFirst(); + return user?.id; }