Skip to content

Scheduling infrastructure for absolutely everyone.

Fixing booking limits (#6119)

sean-brydon2a5dd9035f00
diff --git a/packages/core/getUserAvailability.ts b/packages/core/getUserAvailability.ts
index 50df4224ceb..2f419b71e53 100644
--- a/packages/core/getUserAvailability.ts
+++ b/packages/core/getUserAvailability.ts
@@ -196,21 +196,32 @@ export async function getUserAvailability(
// Take PER_DAY and turn it into day and PER_WEEK into week etc.
const filter = limitKey.split("_")[1].toLocaleLowerCase() as "day" | "week" | "month" | "year";
- let total = 0;
-
- // Get all bookings that are within the filter period
- ourBookings.forEach((booking) => {
- const startDate = dayjs(booking.start).startOf(filter);
+ // loop through all dates and check if we have reached the limit
+ for (const date of dates) {
+ let total = 0;
+ const startDate = dayjs(date).startOf(filter);
// this is parsed above with parseBookingLimit so we know it's safe.
- const endDate = dayjs(startDate).endOf(filter);
- const bookingEventTypeId = booking.source?.split("-")[1];
- if (dayjs(booking.start).isBetween(startDate, endDate)) total++;
- // Only check OUR booking that matches the current eventTypeId
- // we don't care about another event type in this case as we dont need to know their booking limits
- if (total >= limit && bookingEventTypeId === eventType?.id?.toString()) {
- bufferedBusyTimes.push({ start: startDate.toISOString(), end: endDate.toISOString() });
+ const endDate = dayjs(date).endOf(filter);
+ for (const booking of ourBookings) {
+ const bookingEventTypeId = booking.source?.split("-")[1];
+ if (
+ // Only check OUR booking that matches the current eventTypeId
+ // we don't care about another event type in this case as we dont need to know their booking limits
+ !(bookingEventTypeId == eventType?.id && dayjs(booking.start).isBetween(startDate, endDate))
+ ) {
+ continue;
+ }
+ // increment total and check against the limit, adding a busy time if condition is met.
+ total++;
+ if (total >= limit) {
+ bufferedBusyTimes.push({
+ start: startDate.toISOString(),
+ end: endDate.toISOString(),
+ });
+ break;
+ }
}
- });
+ }
}
}
diff --git a/packages/lib/server/checkBookingLimits.ts b/packages/lib/server/checkBookingLimits.ts
index 7d81c1dd105..a187ead2263 100644
--- a/packages/lib/server/checkBookingLimits.ts
+++ b/packages/lib/server/checkBookingLimits.ts
@@ -62,7 +62,7 @@ export async function checkLimit({
{
id: eventId,
bookings: {
- some: {
+ every: {
startTime: {
gte: startDate,
},