diff --git a/packages/trpc/server/routers/viewer/slots/util.ts b/packages/trpc/server/routers/viewer/slots/util.ts index 6be834b693b125..c90db2e86d6914 100644 --- a/packages/trpc/server/routers/viewer/slots/util.ts +++ b/packages/trpc/server/routers/viewer/slots/util.ts @@ -505,71 +505,88 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro }; const allUserIds = usersWithCredentials.map((user) => user.id); + const bookingsSelect = { + id: true, + uid: true, + userId: true, + startTime: true, + endTime: true, + title: true, + attendees: true, + eventType: { + select: { + id: true, + onlyShowFirstAvailableSlot: true, + afterEventBuffer: true, + beforeEventBuffer: true, + seatsPerTimeSlot: true, + requiresConfirmationWillBlockSlot: true, + requiresConfirmation: true, + }, + }, + ...(!!eventType?.seatsPerTimeSlot && { + _count: { + select: { + seatsReferences: true, + }, + }, + }), + }; - const currentBookingsAllUsers = await prisma.booking.findMany({ + const currentBookingsAllUsersQueryOne = prisma.booking.findMany({ where: { - OR: [ - // User is primary host (individual events, or primary organizer) - { - ...sharedQuery, - userId: { - in: allUserIds, - }, - }, - // The current user has a different booking at this time he/she attends - { - ...sharedQuery, - attendees: { - some: { - email: { - in: usersWithCredentials.map((user) => user.email), - }, - }, - }, - }, - { - startTime: { lte: endTimeDate }, - endTime: { gte: startTimeDate }, - eventType: { - id: eventType.id, - requiresConfirmation: true, - requiresConfirmationWillBlockSlot: true, - }, - status: { - in: [BookingStatus.PENDING], + ...sharedQuery, + userId: { + in: allUserIds, + }, + }, + select: { + ...bookingsSelect, + }, + }); + + const currentBookingsAllUsersQueryTwo = prisma.booking.findMany({ + where: { + ...sharedQuery, + attendees: { + some: { + email: { + in: usersWithCredentials.map((user) => user.email), }, }, - ], + }, }, select: { - id: true, - uid: true, - userId: true, - startTime: true, - endTime: true, - title: true, - attendees: true, + ...bookingsSelect, + }, + }); + + const currentBookingsAllUsersQueryThree = prisma.booking.findMany({ + where: { + startTime: { lte: endTimeDate }, + endTime: { gte: startTimeDate }, eventType: { - select: { - id: true, - onlyShowFirstAvailableSlot: true, - afterEventBuffer: true, - beforeEventBuffer: true, - seatsPerTimeSlot: true, - requiresConfirmationWillBlockSlot: true, - requiresConfirmation: true, - }, + id: eventType.id, + requiresConfirmation: true, + requiresConfirmationWillBlockSlot: true, + }, + status: { + in: [BookingStatus.PENDING], }, - ...(!!eventType?.seatsPerTimeSlot && { - _count: { - select: { - seatsReferences: true, - }, - }, - }), + }, + select: { + ...bookingsSelect, }, }); + const [resultOne, resultTwo, resultThree] = await Promise.all([ + currentBookingsAllUsersQueryOne, + currentBookingsAllUsersQueryTwo, + currentBookingsAllUsersQueryThree, + ]); + + const currentBookingsAllUsers = [...resultOne, ...resultTwo, ...resultThree]; + const bookingLimits = parseBookingLimit(eventType?.bookingLimits); const durationLimits = parseDurationLimit(eventType?.durationLimits); let busyTimesFromLimitsBookingsAllUsers: Awaited> = [];