diff --git a/apps/web/lib/getBooking.tsx b/apps/web/lib/getBooking.tsx new file mode 100644 index 0000000000..2c61e93d43 --- /dev/null +++ b/apps/web/lib/getBooking.tsx @@ -0,0 +1,33 @@ +import { PrismaClient } from "@prisma/client"; + +import { Prisma } from "@calcom/prisma/client"; + +async function getBooking(prisma: PrismaClient, uid: string) { + const booking = await prisma.booking.findFirst({ + where: { + uid, + }, + select: { + startTime: true, + description: true, + attendees: { + select: { + email: true, + name: true, + }, + }, + }, + }); + + if (booking) { + // @NOTE: had to do this because Server side cant return [Object objects] + // probably fixable with json.stringify -> json.parse + booking["startTime"] = (booking?.startTime as Date)?.toISOString() as unknown as Date; + } + + return booking; +} + +export type GetBookingType = Prisma.PromiseReturnType; + +export default getBooking; diff --git a/apps/web/pages/[user]/[type].tsx b/apps/web/pages/[user]/[type].tsx index f03d53d51e..6f54f17102 100644 --- a/apps/web/pages/[user]/[type].tsx +++ b/apps/web/pages/[user]/[type].tsx @@ -8,6 +8,7 @@ import { useLocale } from "@calcom/lib/hooks/useLocale"; import { asStringOrNull } from "@lib/asStringOrNull"; import { getWorkingHours } from "@lib/availability"; +import getBooking, { GetBookingType } from "@lib/getBooking"; import prisma from "@lib/prisma"; import { inferSSRProps } from "@lib/types/inferSSRProps"; @@ -262,41 +263,9 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => eventTypeObject.schedule = null; eventTypeObject.availability = []; - type Booking = { - startTime: Date | string; - description: string | null; - attendees?: { - email: string; - name: string; - }[]; - } | null; - // @NOTE: being used several times refactor to exported function - async function getBooking(rescheduleUid: string): Promise { - return await prisma.booking.findFirst({ - where: { - uid: rescheduleUid, - }, - select: { - startTime: true, - description: true, - attendees: { - select: { - email: true, - name: true, - }, - }, - }, - }); - } - - let booking: Booking | null = null; + let booking: GetBookingType | null = null; if (rescheduleUid) { - booking = await getBooking(rescheduleUid); - if (booking) { - // @NOTE: had to do this because Server side cant return [Object objects] - // probably fixable with json.stringify -> json.parse - booking["startTime"] = (booking?.startTime as Date)?.toISOString(); - } + booking = await getBooking(prisma, rescheduleUid); } const dynamicNames = isDynamicGroup diff --git a/apps/web/pages/[user]/book.tsx b/apps/web/pages/[user]/book.tsx index fb571aa4a9..804a366137 100644 --- a/apps/web/pages/[user]/book.tsx +++ b/apps/web/pages/[user]/book.tsx @@ -1,4 +1,3 @@ -import { Prisma } from "@prisma/client"; import dayjs from "dayjs"; import timezone from "dayjs/plugin/timezone"; import utc from "dayjs/plugin/utc"; @@ -15,6 +14,7 @@ import { import { useLocale } from "@calcom/lib/hooks/useLocale"; import { asStringOrThrow } from "@lib/asStringOrNull"; +import getBooking, { GetBookingType } from "@lib/getBooking"; import prisma from "@lib/prisma"; import { inferSSRProps } from "@lib/types/inferSSRProps"; @@ -148,42 +148,9 @@ export async function getServerSideProps(context: GetServerSidePropsContext) { }; })[0]; - type Booking = { - startTime: Date | string; - description: string | null; - attendees: { - email: string; - name: string; - }[]; - } | null; - - // @NOTE: being used several times refactor to exported function - async function getBooking(): Promise { - return await prisma.booking.findFirst({ - where: { - uid: asStringOrThrow(context.query.rescheduleUid), - }, - select: { - startTime: true, - description: true, - attendees: { - select: { - email: true, - name: true, - }, - }, - }, - }); - } - - let booking: Booking | null = null; + let booking: GetBookingType | null = null; if (context.query.rescheduleUid) { - booking = await getBooking(); - if (booking) { - // @NOTE: had to do this because Server side cant return [Object objects] - // probably fixable with json.stringify -> json.parse - booking["startTime"] = (booking?.startTime as Date)?.toISOString(); - } + booking = await getBooking(prisma, context.query.rescheduleUid as string); } const isDynamicGroupBooking = users.length > 1; diff --git a/apps/web/pages/team/[slug]/[type].tsx b/apps/web/pages/team/[slug]/[type].tsx index 40d34ed7bd..8f15f1be42 100644 --- a/apps/web/pages/team/[slug]/[type].tsx +++ b/apps/web/pages/team/[slug]/[type].tsx @@ -5,6 +5,7 @@ import { UserPlan } from "@calcom/prisma/client"; import { asStringOrNull } from "@lib/asStringOrNull"; import { getWorkingHours } from "@lib/availability"; +import getBooking, { GetBookingType } from "@lib/getBooking"; import prisma from "@lib/prisma"; import { inferSSRProps } from "@lib/types/inferSSRProps"; @@ -111,41 +112,9 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => eventTypeObject.availability = []; - type Booking = { - startTime: Date | string; - description: string | null; - attendees?: { - email: string; - name: string; - }[]; - } | null; - // @NOTE: being used several times refactor to exported function - async function getBooking(rescheduleUid: string): Promise { - return await prisma.booking.findFirst({ - where: { - uid: rescheduleUid, - }, - select: { - startTime: true, - description: true, - attendees: { - select: { - email: true, - name: true, - }, - }, - }, - }); - } - - let booking: Booking | null = null; + let booking: GetBookingType | null = null; if (rescheduleUid) { - booking = await getBooking(rescheduleUid); - if (booking) { - // @NOTE: had to do this because Server side cant return [Object objects] - // probably fixable with json.stringify -> json.parse - booking["startTime"] = (booking?.startTime as Date)?.toISOString(); - } + booking = await getBooking(prisma, rescheduleUid); } return { diff --git a/apps/web/pages/team/[slug]/book.tsx b/apps/web/pages/team/[slug]/book.tsx index dfd58f34da..faa0831132 100644 --- a/apps/web/pages/team/[slug]/book.tsx +++ b/apps/web/pages/team/[slug]/book.tsx @@ -5,6 +5,7 @@ import { JSONObject } from "superjson/dist/types"; import { getLocationLabels } from "@calcom/app-store/utils"; import { asStringOrThrow } from "@lib/asStringOrNull"; +import getBooking, { GetBookingType } from "@lib/getBooking"; import prisma from "@lib/prisma"; import { inferSSRProps } from "@lib/types/inferSSRProps"; @@ -75,42 +76,9 @@ export async function getServerSideProps(context: GetServerSidePropsContext) { }; })[0]; - type Booking = { - startTime: Date | string; - description: string | null; - attendees: { - email: string; - name: string; - }[]; - } | null; - - // @NOTE: being used several times refactor to exported function - async function getBooking(): Promise { - return prisma.booking.findFirst({ - where: { - uid: asStringOrThrow(context.query.rescheduleUid), - }, - select: { - startTime: true, - description: true, - attendees: { - select: { - email: true, - name: true, - }, - }, - }, - }); - } - - let booking: Booking | null = null; + let booking: GetBookingType | null = null; if (context.query.rescheduleUid) { - booking = await getBooking(); - if (booking) { - // @NOTE: had to do this because Server side cant return [Object objects] - // probably fixable with json.stringify -> json.parse - booking["startTime"] = (booking?.startTime as Date)?.toISOString(); - } + booking = await getBooking(prisma, context.query.rescheduleUid as string); } const t = await getTranslation(context.locale ?? "en", "common");