From 55e1e0fa3d62eb3f89bf6609c1487b4aa0db1d47 Mon Sep 17 00:00:00 2001 From: Benny Joo Date: Wed, 11 Sep 2024 21:14:19 -0400 Subject: [PATCH] chore: Remove passing optional prisma select prop to Repository classes (#16600) --- .../future/availability/[schedule]/page.tsx | 8 +-- .../video/meeting-not-started/[uid]/page.tsx | 2 +- .../web/lib/video/[uid]/getServerSideProps.ts | 28 +------- .../meeting-ended/[uid]/getServerSideProps.ts | 17 +---- .../[uid]/getServerSideProps.ts | 2 +- packages/lib/server/repository/booking.ts | 69 ++++++++++++++++--- packages/lib/server/repository/user.ts | 16 ++++- 7 files changed, 81 insertions(+), 61 deletions(-) diff --git a/apps/web/app/future/availability/[schedule]/page.tsx b/apps/web/app/future/availability/[schedule]/page.tsx index ec41afeb0329a7..8955baa8c7301e 100644 --- a/apps/web/app/future/availability/[schedule]/page.tsx +++ b/apps/web/app/future/availability/[schedule]/page.tsx @@ -43,12 +43,8 @@ const Page = async ({ params }: PageProps) => { let userData, schedule, travelSchedules; try { - userData = await UserRepository.findById({ - id: userId, - select: { - timeZone: true, - defaultScheduleId: true, - }, + userData = await UserRepository.getTimeZoneAndDefaultScheduleId({ + userId, }); if (!userData?.timeZone || !userData?.defaultScheduleId) { throw new Error("timeZone and defaultScheduleId not found"); diff --git a/apps/web/app/future/video/meeting-not-started/[uid]/page.tsx b/apps/web/app/future/video/meeting-not-started/[uid]/page.tsx index 790367733ee0df..3fc30bd6c2ac3e 100644 --- a/apps/web/app/future/video/meeting-not-started/[uid]/page.tsx +++ b/apps/web/app/future/video/meeting-not-started/[uid]/page.tsx @@ -12,7 +12,7 @@ import MeetingNotStarted from "~/videos/views/videos-meeting-not-started-single- export const generateMetadata = async ({ params, searchParams }: _PageProps) => { const p = { ...params, ...searchParams }; - const booking = await BookingRepository.findBookingByUidWithOptionalSelect({ + const booking = await BookingRepository.findBookingByUid({ bookingUid: typeof p?.uid === "string" ? p.uid : "", }); diff --git a/apps/web/lib/video/[uid]/getServerSideProps.ts b/apps/web/lib/video/[uid]/getServerSideProps.ts index ea41d739d61c0b..181f3b7925cdef 100644 --- a/apps/web/lib/video/[uid]/getServerSideProps.ts +++ b/apps/web/lib/video/[uid]/getServerSideProps.ts @@ -20,34 +20,8 @@ export async function getServerSideProps(context: GetServerSidePropsContext) { const ssr = await ssrInit(context); - const booking = await BookingRepository.findBookingByUidWithOptionalSelect({ + const booking = await BookingRepository.findBookingForMeetingPage({ bookingUid: context.query.uid as string, - select: { - uid: true, - description: true, - isRecorded: true, - user: { - select: { - id: true, - timeZone: true, - name: true, - email: true, - username: true, - }, - }, - references: { - select: { - id: true, - uid: true, - type: true, - meetingUrl: true, - meetingPassword: true, - }, - where: { - type: "daily_video", - }, - }, - }, }); if (!booking || booking.references.length === 0 || !booking.references[0].meetingUrl) { diff --git a/apps/web/lib/video/meeting-ended/[uid]/getServerSideProps.ts b/apps/web/lib/video/meeting-ended/[uid]/getServerSideProps.ts index 9e073704ebdf51..3aa4fc476c751a 100644 --- a/apps/web/lib/video/meeting-ended/[uid]/getServerSideProps.ts +++ b/apps/web/lib/video/meeting-ended/[uid]/getServerSideProps.ts @@ -6,23 +6,8 @@ import { type inferSSRProps } from "@lib/types/inferSSRProps"; export type PageProps = inferSSRProps; export async function getServerSideProps(context: GetServerSidePropsContext) { - const booking = await BookingRepository.findBookingByUidWithOptionalSelect({ + const booking = await BookingRepository.findBookingForMeetingEndedPage({ bookingUid: context.query.uid as string, - select: { - uid: true, - user: { - select: { - credentials: true, - }, - }, - references: { - select: { - uid: true, - type: true, - meetingUrl: true, - }, - }, - }, }); if (!booking) { diff --git a/apps/web/lib/video/meeting-not-started/[uid]/getServerSideProps.ts b/apps/web/lib/video/meeting-not-started/[uid]/getServerSideProps.ts index 379a34bd81d048..ce1e6d3a9ccd67 100644 --- a/apps/web/lib/video/meeting-not-started/[uid]/getServerSideProps.ts +++ b/apps/web/lib/video/meeting-not-started/[uid]/getServerSideProps.ts @@ -4,7 +4,7 @@ import { BookingRepository } from "@calcom/lib/server/repository/booking"; // change the type export async function getServerSideProps(context: GetServerSidePropsContext) { - const booking = await BookingRepository.findBookingByUidWithOptionalSelect({ + const booking = await BookingRepository.findBookingByUid({ bookingUid: context.query.uid as string, }); diff --git a/packages/lib/server/repository/booking.ts b/packages/lib/server/repository/booking.ts index 87581b3856d07e..a82c98d47c91b3 100644 --- a/packages/lib/server/repository/booking.ts +++ b/packages/lib/server/repository/booking.ts @@ -113,18 +113,71 @@ export class BookingRepository { return allBookings; } - static async findBookingByUidWithOptionalSelect({ - bookingUid, - select, - }: { - bookingUid: string; - select?: Prisma.BookingSelect; - }) { + static async findBookingByUid({ bookingUid }: { bookingUid: string }) { + return await prisma.booking.findUnique({ + where: { + uid: bookingUid, + }, + select: bookingMinimalSelect, + }); + } + + static async findBookingForMeetingPage({ bookingUid }: { bookingUid: string }) { return await prisma.booking.findUnique({ where: { uid: bookingUid, }, - select: { ...bookingMinimalSelect, ...select }, + select: { + ...bookingMinimalSelect, + uid: true, + description: true, + isRecorded: true, + user: { + select: { + id: true, + timeZone: true, + name: true, + email: true, + username: true, + }, + }, + references: { + select: { + id: true, + uid: true, + type: true, + meetingUrl: true, + meetingPassword: true, + }, + where: { + type: "daily_video", + }, + }, + }, + }); + } + + static async findBookingForMeetingEndedPage({ bookingUid }: { bookingUid: string }) { + return await prisma.booking.findUnique({ + where: { + uid: bookingUid, + }, + select: { + ...bookingMinimalSelect, + uid: true, + user: { + select: { + credentials: true, + }, + }, + references: { + select: { + uid: true, + type: true, + meetingUrl: true, + }, + }, + }, }); } diff --git a/packages/lib/server/repository/user.ts b/packages/lib/server/repository/user.ts index e67f3415995b93..4772332c7846a4 100644 --- a/packages/lib/server/repository/user.ts +++ b/packages/lib/server/repository/user.ts @@ -239,12 +239,12 @@ export class UserRepository { }; } - static async findById({ id, select }: { id: number; select?: Prisma.UserSelect }) { + static async findById({ id }: { id: number }) { const user = await prisma.user.findUnique({ where: { id, }, - select: { ...userSelect, ...select }, + select: userSelect, }); if (!user) { @@ -566,6 +566,18 @@ export class UserRepository { return teamIds; } + static async getTimeZoneAndDefaultScheduleId({ userId }: { userId: number }) { + return await prisma.user.findUnique({ + where: { + id: userId, + }, + select: { + timeZone: true, + defaultScheduleId: true, + }, + }); + } + static async adminFindById(userId: number) { return await prisma.user.findUniqueOrThrow({ where: {