From 0a911542e9cfbe7c8487e54b3559151a2bbcb4d0 Mon Sep 17 00:00:00 2001 From: supalarry Date: Mon, 30 Oct 2023 11:25:25 +0100 Subject: [PATCH] kysely query 6 --- .../trpc/server/routers/viewer/slots/util.ts | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/trpc/server/routers/viewer/slots/util.ts b/packages/trpc/server/routers/viewer/slots/util.ts index 6c1f8a3f0b..141f115445 100644 --- a/packages/trpc/server/routers/viewer/slots/util.ts +++ b/packages/trpc/server/routers/viewer/slots/util.ts @@ -808,14 +808,26 @@ async function getTeamIdFromSlug( organizationDetails: { currentOrgDomain: string | null; isValidOrgDomain: boolean } ) { const { currentOrgDomain, isValidOrgDomain } = organizationDetails; - const team = await prisma.team.findFirst({ - where: { - slug, - parent: isValidOrgDomain && currentOrgDomain ? getSlugOrRequestedSlug(currentOrgDomain) : null, - }, - select: { - id: true, - }, - }); + const team = await db + .selectFrom("Team") + .innerJoin("Team as Parent", "Parent.id", "Team.parentId") + .where((eb) => { + const and: Expression[] = []; + and.push(eb("Team.slug", "=", slug)); + + if (isValidOrgDomain && currentOrgDomain) { + const slugifiedValue = slugify(currentOrgDomain); + and.push( + eb.or([ + eb("Parent.slug", "=", slugifiedValue), + eb(traverseJSON(eb, "Parent.metadata", "requestedSlug"), "=", slugifiedValue), + ]) + ); + } + return eb.and(and); + }) + .select("Team.id") + .executeTakeFirst(); + return team?.id; }