perf: Load bookings used for limits in bulk (#13474)

This commit is contained in:
Keith Williams authored and GitHub committed 2024-02-06 16:56:05 -07:00
1 parent 903a154dbc
commit 512628e8ab
7 files changed
+97 -46

No files matched your search

@@ -3,12 +3,14 @@ import { countBy } from "lodash";
import { v4 as uuid } from "uuid";
import { getAggregatedAvailability } from "@calcom/core/getAggregatedAvailability";
import { getBusyTimesForLimitChecks } from "@calcom/core/getBusyTimes";
import type { CurrentSeats } from "@calcom/core/getUserAvailability";
import { getUserAvailability } from "@calcom/core/getUserAvailability";
import type { Dayjs } from "@calcom/dayjs";
import dayjs from "@calcom/dayjs";
import { getSlugOrRequestedSlug, orgDomainConfig } from "@calcom/ee/organizations/lib/orgDomains";
import { isEventTypeLoggingEnabled } from "@calcom/features/bookings/lib/isEventTypeLoggingEnabled";
import { parseBookingLimit, parseDurationLimit } from "@calcom/lib";
import { getDefaultEvent } from "@calcom/lib/defaultEvents";
import isTimeOutOfBounds from "@calcom/lib/isOutOfBounds";
import logger from "@calcom/lib/logger";
@@ -336,6 +338,8 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions) {
},
};
const allUserIds = usersWithCredentials.map((user) => user.id);
const currentBookingsAllUsers = await prisma.booking.findMany({
where: {
OR: [
@@ -343,7 +347,7 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions) {
{
...sharedQuery,
userId: {
in: usersWithCredentials.map((user) => user.id),
in: allUserIds,
},
},
// The current user has a different booking at this time he/she attends
@@ -386,6 +390,22 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions) {
},
});
const bookingLimits = parseBookingLimit(eventType?.bookingLimits);
const durationLimits = parseDurationLimit(eventType?.durationLimits);
let busyTimesFromLimitsBookingsAllUsers: Awaited<ReturnType<typeof getBusyTimesForLimitChecks>> = [];
if (eventType && (bookingLimits || durationLimits)) {
busyTimesFromLimitsBookingsAllUsers = await getBusyTimesForLimitChecks({
userIds: allUserIds,
eventTypeId: eventType.id,
startDate: startTime.format(),
endDate: endTime.format(),
rescheduleUid: input.rescheduleUid,
bookingLimits,
durationLimits,
});
}
/* We get all users working hours and busy slots */
const userAvailability = await Promise.all(
usersWithCredentials.map(async (currentUser) => {
@@ -418,6 +438,9 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions) {
const { attendees: _attendees, ...bookingWithoutAttendees } = bookings;
return bookingWithoutAttendees;
}),
busyTimesFromLimitsBookings: busyTimesFromLimitsBookingsAllUsers.filter(
(b) => b.userId === currentUser.id
),
}
);
if (!currentSeats && _currentSeats) currentSeats = _currentSeats;
@@ -46,7 +46,7 @@ export const getMemberAvailabilityHandler = async ({ ctx, input }: GetMemberAvai
dateFrom: input.dateFrom,
dateTo: input.dateTo,
},
{ user: member.user }
{ user: member.user, busyTimesFromLimitsBookings: [] }
);
};