From 04036c32a7fed5723bfcf0f3ee4bc2ad7f50c837 Mon Sep 17 00:00:00 2001 From: Benny Joo Date: Thu, 12 Dec 2024 12:12:56 -0500 Subject: [PATCH] fix: filling users logic (#18128) * fix filling users logic * fix type check * refactor --- .../team/[slug]/[type]/getServerSideProps.tsx | 66 ++++++++++++++----- .../lib/[user]/[type]/getServerSideProps.ts | 10 ++- .../bookings/components/BookerSeo.tsx | 6 +- 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/apps/web/lib/team/[slug]/[type]/getServerSideProps.tsx b/apps/web/lib/team/[slug]/[type]/getServerSideProps.tsx index 8d65380e51..e6b068dcf0 100644 --- a/apps/web/lib/team/[slug]/[type]/getServerSideProps.tsx +++ b/apps/web/lib/team/[slug]/[type]/getServerSideProps.tsx @@ -6,10 +6,7 @@ import type { GetBookingType } from "@calcom/features/bookings/lib/get-booking"; import { getBookingForReschedule } from "@calcom/features/bookings/lib/get-booking"; import { getSlugOrRequestedSlug, orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains"; import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage"; -import { getUsernameList } from "@calcom/lib/defaultEvents"; -import { getBookerBaseUrlSync } from "@calcom/lib/getBookerUrl/client"; import { OrganizationRepository } from "@calcom/lib/server/repository/organization"; -import { UserRepository } from "@calcom/lib/server/repository/user"; import slugify from "@calcom/lib/slugify"; import prisma from "@calcom/prisma"; import { RedirectType } from "@calcom/prisma/client"; @@ -58,6 +55,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => }, select: { id: true, + isPrivate: true, hideBranding: true, parent: { select: { @@ -87,6 +85,16 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => metadata: true, length: true, hidden: true, + hosts: { + select: { + user: { + select: { + name: true, + username: true, + }, + }, + }, + }, }, }, isOrganization: true, @@ -103,21 +111,51 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => notFound: true, } as const; } + const eventData = team.eventTypes[0]; + const eventTypeId = eventData.id; // INFO: This code was pulled from getPublicEvent and used here. // Calling the tRPC fetch to get the public event data is incredibly slow // for large teams and we don't want to add it back. Future refactors will happen // to speed up this call. - const usernameList = getUsernameList(teamSlug); + let users: { username: string; name: string }[] = []; + + if (!team.isPrivate && eventData.hosts.length > 0) { + users = eventData.hosts + .filter((host) => host.user.username) + .map((host) => ({ + username: host.user.username ?? "", + name: host.user.name ?? "", + })); + } + if (!team.isPrivate && eventData.hosts.length === 0) { + // a minimalistic version of `getOwnerFromUsersArray` in `getPublicEvent.ts` + // backward compatibility logic for team event types that have users[] but not hosts[] + const { users: data } = await prisma.eventType.findUniqueOrThrow({ + where: { id: eventTypeId }, + select: { + users: { + select: { + username: true, + name: true, + }, + }, + }, + }); + + users = + data.length > 0 + ? [ + { + username: data[0].username ?? "", + name: data[0].name ?? "", + }, + ] + : []; + } + const orgSlug = isValidOrgDomain ? currentOrgDomain : null; const name = team.parent?.name ?? team.name ?? null; - const usersInOrgContext = await UserRepository.findUsersByUsername({ - usernameList, - orgSlug: team.parent?.slug || null, - }); - - const eventData = team.eventTypes[0]; - const eventTypeId = eventData.id; let booking: GetBookingType | null = null; if (rescheduleUid) { @@ -169,11 +207,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => username: orgSlug ?? null, }, title: eventData.title, - users: usersInOrgContext.map((user) => ({ - ...user, - metadata: undefined, - bookerUrl: getBookerBaseUrlSync(user.profile?.organization?.slug ?? null), - })), + users, hidden: eventData.hidden, }, booking, diff --git a/apps/web/server/lib/[user]/[type]/getServerSideProps.ts b/apps/web/server/lib/[user]/[type]/getServerSideProps.ts index 749cbcb987..fd5d405460 100644 --- a/apps/web/server/lib/[user]/[type]/getServerSideProps.ts +++ b/apps/web/server/lib/[user]/[type]/getServerSideProps.ts @@ -22,13 +22,17 @@ type Props = { NonNullable>>, "id" | "length" | "metadata" | "entity" | "profile" | "title" | "users" | "hidden" >, - "profile" + "profile" | "users" > & { profile: { image: string | undefined; name: string | null; username: string | null; }; + users: { + username: string; + name: string; + }[]; }; booking?: GetBookingType; @@ -159,7 +163,7 @@ async function getDynamicGroupPageProps(context: GetServerSidePropsContext) { username: eventData.profile.username ?? null, }, title: eventData.title, - users: eventData.users, + users: eventData.users.map((user) => ({ username: user.username ?? "", name: user.name ?? "" })), hidden: eventData.hidden, }, user: usernames.join("+"), @@ -255,7 +259,7 @@ async function getUserPageProps(context: GetServerSidePropsContext) { username: eventData.profile.username ?? null, }, title: eventData.title, - users: eventData.users, + users: eventData.users.map((user) => ({ username: user.username ?? "", name: user.name ?? "" })), hidden: eventData.hidden, }, user: username, diff --git a/packages/features/bookings/components/BookerSeo.tsx b/packages/features/bookings/components/BookerSeo.tsx index 021cdb788b..050925e68f 100644 --- a/packages/features/bookings/components/BookerSeo.tsx +++ b/packages/features/bookings/components/BookerSeo.tsx @@ -14,13 +14,17 @@ interface BookerSeoProps { isTeamEvent?: boolean; eventData?: Omit< Pick>>, "profile" | "title" | "users" | "hidden">, - "profile" + "profile" | "users" > & { profile: { image: string | undefined; name: string | null; username: string | null; }; + users: { + username: string; + name: string; + }[]; }; entity: { fromRedirectOfNonOrgLink: boolean;