From d00f0bae1d506f59434481e6dde7cc5bf0365774 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Mon, 9 May 2022 21:34:42 +0530 Subject: [PATCH] All non recurring bookings were clubbed into one distinct booking (#2706) --- apps/web/server/routers/viewer.tsx | 32 +++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/apps/web/server/routers/viewer.tsx b/apps/web/server/routers/viewer.tsx index 7c35cbf8d4..b5d84e025c 100644 --- a/apps/web/server/routers/viewer.tsx +++ b/apps/web/server/routers/viewer.tsx @@ -369,18 +369,8 @@ const loggedInViewerRouter = createProtectedRouter() past: { startTime: "desc" }, cancelled: { startTime: "desc" }, }; - const bookingListingDistinct: Record< - typeof bookingListingByStatus, - Prisma.Enumerable | undefined - > = { - upcoming: Prisma.BookingScalarFieldEnum.recurringEventId, - recurring: undefined, - past: undefined, - cancelled: undefined, - }; const passedBookingsFilter = bookingListingFilters[bookingListingByStatus]; const orderBy = bookingListingOrderby[bookingListingByStatus]; - const distinct = bookingListingDistinct[bookingListingByStatus]; const bookingsQuery = await prisma.booking.findMany({ where: { @@ -430,7 +420,6 @@ const loggedInViewerRouter = createProtectedRouter() rescheduled: true, }, orderBy, - distinct, take: take + 1, skip, }); @@ -440,7 +429,7 @@ const loggedInViewerRouter = createProtectedRouter() _count: true, }); - const bookings = bookingsQuery.map((booking) => { + let bookings = bookingsQuery.map((booking) => { return { ...booking, eventType: { @@ -452,6 +441,25 @@ const loggedInViewerRouter = createProtectedRouter() }; }); + const seenBookings: Record = {}; + + // Remove duplicate recurring bookings for upcoming status. + // Couldn't use distinct in query because the distinct column would be different for recurring and non recurring event. + // We might be actually sending less then the limit, due to this filter + // TODO: Figure out a way to fix it. + if (bookingListingByStatus === "upcoming") { + bookings = bookings.filter((booking) => { + if (!booking.recurringEventId) { + return true; + } + if (seenBookings[booking.recurringEventId]) { + return false; + } + seenBookings[booking.recurringEventId] = true; + return true; + }); + } + let nextCursor: typeof skip | null = skip; if (bookings.length > take) { bookings.shift();