From ff6c6ccec061d94fd780727cdbce275be25db674 Mon Sep 17 00:00:00 2001 From: alannnc Date: Mon, 22 May 2023 02:30:24 -0700 Subject: [PATCH] Replacing bookingAttendees for bookingUid (#8997) --- apps/web/components/booking/AvailableTimes.tsx | 2 +- apps/web/components/booking/pages/BookingPage.tsx | 2 +- .../routers/viewer/slots/reserveSlot.handler.ts | 11 ++++++++--- packages/trpc/server/routers/viewer/slots/types.ts | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/apps/web/components/booking/AvailableTimes.tsx b/apps/web/components/booking/AvailableTimes.tsx index a48b802d98..4d4eebb6ad 100644 --- a/apps/web/components/booking/AvailableTimes.tsx +++ b/apps/web/components/booking/AvailableTimes.tsx @@ -72,7 +72,7 @@ const AvailableTimes: FC = ({ slotUtcStartDate: slot.time, eventTypeId, slotUtcEndDate: dayjs(slot.time).utc().add(duration, "minutes").format(), - bookingAttendees: bookingAttendees || undefined, + bookingUid: slot.bookingUid, }); }; diff --git a/apps/web/components/booking/pages/BookingPage.tsx b/apps/web/components/booking/pages/BookingPage.tsx index 4b208c5951..bb2c42ab83 100644 --- a/apps/web/components/booking/pages/BookingPage.tsx +++ b/apps/web/components/booking/pages/BookingPage.tsx @@ -237,7 +237,7 @@ const BookingPage = ({ eventTypeId: eventType.id, slotUtcStartDate: dayjs(queryDate).utc().format(), slotUtcEndDate: dayjs(queryDate).utc().add(parseInt(queryDuration), "minutes").format(), - bookingAttendees: currentSlotBooking ? currentSlotBooking.attendees.length : undefined, + bookingUid: currentSlotBooking?.uid, }); } }; diff --git a/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.ts b/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.ts index b258c78b46..5ec95b1866 100644 --- a/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.ts +++ b/packages/trpc/server/routers/viewer/slots/reserveSlot.handler.ts @@ -21,7 +21,7 @@ interface ReserveSlotOptions { export const reserveSlotHandler = async ({ ctx, input }: ReserveSlotOptions) => { const { prisma, req, res } = ctx; const uid = req?.cookies?.uid || uuid(); - const { slotUtcStartDate, slotUtcEndDate, eventTypeId, bookingAttendees } = input; + const { slotUtcStartDate, slotUtcEndDate, eventTypeId, bookingUid } = input; const releaseAt = dayjs.utc().add(parseInt(MINUTES_TO_BOOK), "minutes").format(); const eventType = await prisma.eventType.findUnique({ where: { id: eventTypeId }, @@ -40,8 +40,13 @@ export const reserveSlotHandler = async ({ ctx, input }: ReserveSlotOptions) => // If this is a seated event then don't reserve a slot if (eventType.seatsPerTimeSlot) { // Check to see if this is the last attendee - if (bookingAttendees) { - const seatsLeft = eventType.seatsPerTimeSlot - bookingAttendees; + const bookingWithAttendees = await prisma.booking.findFirst({ + where: { uid: bookingUid }, + select: { attendees: true }, + }); + const bookingAttendeesLength = bookingWithAttendees?.attendees?.length; + if (bookingAttendeesLength) { + const seatsLeft = eventType.seatsPerTimeSlot - bookingAttendeesLength; if (seatsLeft < 1) shouldReserveSlot = false; } else { // If there is no booking yet then don't reserve the slot diff --git a/packages/trpc/server/routers/viewer/slots/types.ts b/packages/trpc/server/routers/viewer/slots/types.ts index c980f558b2..b488cee2e3 100644 --- a/packages/trpc/server/routers/viewer/slots/types.ts +++ b/packages/trpc/server/routers/viewer/slots/types.ts @@ -33,7 +33,7 @@ export const reserveSlotSchema = z slotUtcStartDate: z.string(), // endTime ISOString slotUtcEndDate: z.string(), - bookingAttendees: z.number().optional(), + bookingUid: z.string().optional(), }) .refine( (data) => !!data.eventTypeId || !!data.slotUtcStartDate || !!data.slotUtcEndDate,