Files
calendar/packages/features/bookings/lib/checkBookingLimits.ts
T
Benny JooandGitHub ff38d6c7db refactor: Remove circular deps between @calcom/lib and @calcom/features [1] (#24399)
* add eslint config

* migrate autoLock to features

* migrate teamService to features

* migrate userCreationService

* migrate insights services to features

* migrate ProfileRepository

* update imports

* migrate filter segmen tests

* migrate filter segment repository

* migrate getBusyTimes

* migrate getLocaleFromRequest

* refactor csvUtils

* make filename clearer

* migrate getLuckyUser integration test

* migrate autoLock test to features

* wip

* refactors

* migrate useBookerUrl

* migrate more

* wip

* Migrate eventTypeRepository

* membership repository

* update imports

* update imports

* migrate

* move organization repository

* update imports

* update imports

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix tests

* fix type checks

* fix

* fix

* migrate

* update imports

* fix tests

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix
2025-10-13 12:01:02 -03:00

111 lines
3.4 KiB
TypeScript

import dayjs from "@calcom/dayjs";
import type { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
import { getErrorFromUnknown } from "@calcom/lib/errors";
import { HttpError } from "@calcom/lib/http-error";
import { ascendingLimitKeys, intervalLimitKeyToUnit } from "@calcom/lib/intervalLimits/intervalLimit";
import type { IntervalLimit, IntervalLimitKey } from "@calcom/lib/intervalLimits/intervalLimitSchema";
import { parseBookingLimit } from "@calcom/lib/intervalLimits/isBookingLimits";
import { withReporting } from "@calcom/lib/sentryWrapper";
export interface ICheckBookingLimitsService {
bookingRepo: BookingRepository;
}
export class CheckBookingLimitsService {
constructor(private readonly dependencies: ICheckBookingLimitsService) {}
private async _checkBookingLimits(
bookingLimits: IntervalLimit,
eventStartDate: Date,
eventId: number,
rescheduleUid?: string | undefined,
timeZone?: string | null,
includeManagedEvents?: boolean
) {
const parsedBookingLimits = parseBookingLimit(bookingLimits);
if (!parsedBookingLimits) return false;
// not iterating entries to preserve types
const limitCalculations = ascendingLimitKeys.map((key) =>
this.checkBookingLimit({
key,
limitingNumber: parsedBookingLimits[key],
eventStartDate,
eventId,
timeZone,
rescheduleUid,
includeManagedEvents,
})
);
try {
return !!(await Promise.all(limitCalculations));
} catch (error) {
throw new HttpError({ message: getErrorFromUnknown(error).message, statusCode: 401 });
}
}
checkBookingLimits = withReporting(this._checkBookingLimits.bind(this), "checkBookingLimits");
private async _checkBookingLimit({
eventStartDate,
eventId,
key,
limitingNumber,
rescheduleUid,
timeZone,
teamId,
user,
includeManagedEvents = false,
}: {
eventStartDate: Date;
eventId?: number;
key: IntervalLimitKey;
limitingNumber: number | undefined;
rescheduleUid?: string | undefined;
timeZone?: string | null;
teamId?: number;
user?: { id: number; email: string };
includeManagedEvents?: boolean;
}) {
const eventDateInOrganizerTz = timeZone ? dayjs(eventStartDate).tz(timeZone) : dayjs(eventStartDate);
if (!limitingNumber) return;
const unit = intervalLimitKeyToUnit(key);
const startDate = dayjs(eventDateInOrganizerTz).startOf(unit).toDate();
const endDate = dayjs(eventDateInOrganizerTz).endOf(unit).toDate();
let bookingsInPeriod;
if (teamId && user) {
bookingsInPeriod = await this.dependencies.bookingRepo.getAllAcceptedTeamBookingsOfUser({
user: { id: user.id, email: user.email },
teamId,
startDate: startDate,
endDate: endDate,
shouldReturnCount: true,
excludedUid: rescheduleUid,
includeManagedEvents,
});
} else {
bookingsInPeriod = await this.dependencies.bookingRepo.countBookingsByEventTypeAndDateRange({
eventTypeId: eventId!,
startDate,
endDate,
excludedUid: rescheduleUid,
});
}
if (bookingsInPeriod < limitingNumber) return;
throw new HttpError({
message: `booking_limit_reached`,
statusCode: 403,
});
}
checkBookingLimit = withReporting(this._checkBookingLimit.bind(this), "checkBookingLimit");
}