* refactor: convert BookingRepository to use dependency injection pattern - Add constructor injection for PrismaClient to BookingRepository - Convert all static methods to instance methods using this.prismaClient - Update all usage sites to use two-step instantiation pattern: const bookingRepo = new BookingRepository(prisma); bookingRepo.method(...) - Apply same dependency injection pattern as UserRepository, TeamRepository, and SelectedSlotsRepository - Update test files to use correct prismaMock import paths - Maintain all existing functionality and type safety Files updated: - BookingRepository class structure and all 15 static methods - Video meeting pages and booking views - Booking utilities and services (handleNewBooking, originalRescheduledBookingUtils) - Round robin handlers and reassignment logic - Interval limits and booking limits checking - Test files with proper mocking setup Co-Authored-By: morgan@cal.com <morgan@cal.com> * chore: bump platform libs --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: morgan@cal.com <morgan@cal.com>
119 lines
3.3 KiB
TypeScript
119 lines
3.3 KiB
TypeScript
import dayjs from "@calcom/dayjs";
|
|
import { getErrorFromUnknown } from "@calcom/lib/errors";
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
import { withReporting } from "@calcom/lib/sentryWrapper";
|
|
import { BookingRepository } from "@calcom/lib/server/repository/booking";
|
|
import prisma from "@calcom/prisma";
|
|
import { BookingStatus } from "@calcom/prisma/enums";
|
|
|
|
import { ascendingLimitKeys, intervalLimitKeyToUnit } from "../intervalLimit";
|
|
import type { IntervalLimit, IntervalLimitKey } from "../intervalLimitSchema";
|
|
import { parseBookingLimit } from "../isBookingLimits";
|
|
|
|
async function _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) =>
|
|
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 });
|
|
}
|
|
}
|
|
|
|
export const checkBookingLimits = withReporting(_checkBookingLimits, "checkBookingLimits");
|
|
|
|
async function _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) {
|
|
const bookingRepo = new BookingRepository(prisma);
|
|
bookingsInPeriod = await bookingRepo.getAllAcceptedTeamBookingsOfUser({
|
|
user: { id: user.id, email: user.email },
|
|
teamId,
|
|
startDate: startDate,
|
|
endDate: endDate,
|
|
shouldReturnCount: true,
|
|
excludedUid: rescheduleUid,
|
|
includeManagedEvents,
|
|
});
|
|
} else {
|
|
bookingsInPeriod = await prisma.booking.count({
|
|
where: {
|
|
status: BookingStatus.ACCEPTED,
|
|
eventTypeId: eventId,
|
|
// FIXME: bookings that overlap on one side will never be counted
|
|
startTime: {
|
|
gte: startDate,
|
|
},
|
|
endTime: {
|
|
lte: endDate,
|
|
},
|
|
uid: {
|
|
not: rescheduleUid,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
if (bookingsInPeriod < limitingNumber) return;
|
|
|
|
throw new HttpError({
|
|
message: `booking_limit_reached`,
|
|
statusCode: 403,
|
|
});
|
|
}
|
|
}
|
|
|
|
export const checkBookingLimit = withReporting(_checkBookingLimit, "checkBookingLimit");
|