Skip to content

Commit

Permalink
chore: Remove passing optional prisma select prop to Repository class…
Browse files Browse the repository at this point in the history
…es (calcom#16600)
  • Loading branch information
hbjORbj authored Sep 12, 2024
1 parent 4a59841 commit 55e1e0f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 61 deletions.
8 changes: 2 additions & 6 deletions apps/web/app/future/availability/[schedule]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 : "",
});

Expand Down
28 changes: 1 addition & 27 deletions apps/web/lib/video/[uid]/getServerSideProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 1 addition & 16 deletions apps/web/lib/video/meeting-ended/[uid]/getServerSideProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,8 @@ import { type inferSSRProps } from "@lib/types/inferSSRProps";

export type PageProps = inferSSRProps<typeof getServerSideProps>;
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
69 changes: 61 additions & 8 deletions packages/lib/server/repository/booking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
});
}

Expand Down
16 changes: 14 additions & 2 deletions packages/lib/server/repository/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 55e1e0f

Please sign in to comment.