perf: leverage DB index for user profile fetching (booking page & payment page) (#21814)

* leverage DB index for findByUserIdAndOrgSlug method

* booking view

* payment view

* fix

* fix

* address comment
This commit is contained in:
Benny Joo authored and GitHub committed 2025-06-18 09:53:12 +03:00
1 parent 7900be3a30
commit e27de47d9f
4 files changed
+22 -13

No files matched your search

@@ -234,7 +234,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
eventTypeId: eventType.id, eventTypeId: eventType.id,
team: eventType.team, team: eventType.team,
owner: eventType.users[0] ?? null, owner: eventType.users[0] ?? null,
orgSlug: currentOrgDomain, organizationId: session?.user?.profile?.organizationId ?? session?.user?.org?.id ?? null,
}), }),
profile, profile,
eventType, eventType,
@@ -1,7 +1,7 @@
import type { GetServerSidePropsContext } from "next"; import type { GetServerSidePropsContext } from "next";
import { z } from "zod"; import { z } from "zod";
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains"; import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { getClientSecretFromPayment } from "@calcom/features/ee/payments/pages/getClientSecretFromPayment"; import { getClientSecretFromPayment } from "@calcom/features/ee/payments/pages/getClientSecretFromPayment";
import { shouldHideBrandingForEvent } from "@calcom/lib/hideBranding"; import { shouldHideBrandingForEvent } from "@calcom/lib/hideBranding";
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
@@ -18,7 +18,7 @@ const querySchema = z.object({
export const getServerSideProps = async (context: GetServerSidePropsContext) => { export const getServerSideProps = async (context: GetServerSidePropsContext) => {
const { uid } = querySchema.parse(context.query); const { uid } = querySchema.parse(context.query);
const { currentOrgDomain } = orgDomainConfig(context.req); const session = await getServerSession({ req: context.req });
const rawPayment = await prisma.payment.findUnique({ const rawPayment = await prisma.payment.findUnique({
where: { where: {
@@ -59,7 +59,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
eventTypeId: eventType.id, eventTypeId: eventType.id,
team: eventType.team, team: eventType.team,
owner: eventType.users[0] ?? null, owner: eventType.users[0] ?? null,
orgSlug: currentOrgDomain, organizationId: session?.user?.profile?.organizationId ?? session?.user?.org?.id ?? null,
}), }),
}; };
+4 -6
View File
@@ -24,8 +24,6 @@ type UserWithProfile = UserWithoutProfile & {
profile: Profile | null; profile: Profile | null;
}; };
type OrgSlug = string | null;
/** /**
* Determines if branding should be hidden by checking entity and organization settings * Determines if branding should be hidden by checking entity and organization settings
*/ */
@@ -79,12 +77,12 @@ export async function shouldHideBrandingForEvent({
eventTypeId, eventTypeId,
team, team,
owner, owner,
orgSlug, organizationId,
}: { }: {
eventTypeId: number; eventTypeId: number;
team: Team | null; team: Team | null;
owner: UserWithoutProfile | null; owner: UserWithoutProfile | null;
orgSlug: OrgSlug; organizationId: number | null;
}) { }) {
let ownerProfile = null; let ownerProfile = null;
if (team) { if (team) {
@@ -94,10 +92,10 @@ export async function shouldHideBrandingForEvent({
}); });
} else if (owner) { } else if (owner) {
// Needed only for User events, not for Team events // Needed only for User events, not for Team events
ownerProfile = orgSlug ownerProfile = organizationId
? await ProfileRepository.findByUserIdAndOrgSlug({ ? await ProfileRepository.findByUserIdAndOrgSlug({
userId: owner.id, userId: owner.id,
orgSlug, organizationId,
}) })
: null; : null;
+14 -3
View File
@@ -665,9 +665,20 @@ export class ProfileRepository {
return normalizeProfile(profile); return normalizeProfile(profile);
} }
static async findByUserIdAndOrgSlug({ userId, orgSlug }: { userId: number; orgSlug: string }) { static async findByUserIdAndOrgSlug({
const profile = await prisma.profile.findFirst({ userId,
where: { userId, organization: { slug: orgSlug } }, organizationId,
}: {
userId: number;
organizationId: number;
}) {
const profile = await prisma.profile.findUnique({
where: {
userId_organizationId: {
userId,
organizationId,
},
},
include: { include: {
organization: { organization: {
select: organizationSelect, select: organizationSelect,