Skip to content

Commit

Permalink
perf: run parallel bookings queries (#16398)
Browse files Browse the repository at this point in the history
  • Loading branch information
keithwillcode authored and zomars committed Sep 4, 2024
1 parent 056652e commit a96075b
Showing 1 changed file with 71 additions and 54 deletions.
125 changes: 71 additions & 54 deletions packages/trpc/server/routers/viewer/slots/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof getBusyTimesForLimitChecks>> = [];
Expand Down

0 comments on commit a96075b

Please sign in to comment.