* add booking booking to team settings * add update mutation * add missing export * fix dirty state * first version of global team limits in getUserAvailability * add test setup * create seperate test file for global limits * add tests for all units * move limitManager and booking limit functions outside of getUserAvailability * add migration * clean up code * move yearly booking count to booking repository * code clean up * don't count rescheduling booking * add test for getSchedule * fix type error * fix type error * fix type error * fix from and end date for fetching bookings * reuse functions * allow null for bookingLimits * remove bookings from managed event type * fix type error * code clean up * small fixes form clean up * fix type issue * same fixes in teams/_post * fix existing tz issue * tests for fix * adds missingn await * imrove description * remove spreading * fix reschedule issue with booking limits * fix reschedule error with booking durations * remove useeffect * undo commit * add bookingLimits to UpdateOrgTeamDto * fix unit tests * Prepare view for app router migration * throw error if not in ascending order * fix disabled update button --------- Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com> Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
108 lines
2.8 KiB
TypeScript
108 lines
2.8 KiB
TypeScript
import dayjs from "@calcom/dayjs";
|
|
import prisma from "@calcom/prisma";
|
|
import { BookingStatus } from "@calcom/prisma/enums";
|
|
import type { IntervalLimit } from "@calcom/types/Calendar";
|
|
|
|
import { getErrorFromUnknown } from "../errors";
|
|
import { HttpError } from "../http-error";
|
|
import { ascendingLimitKeys, intervalLimitKeyToUnit } from "../intervalLimit";
|
|
import { parseBookingLimit } from "../isBookingLimits";
|
|
import { BookingRepository } from "./repository/booking";
|
|
|
|
export async function checkBookingLimits(
|
|
bookingLimits: IntervalLimit,
|
|
eventStartDate: Date,
|
|
eventId: number,
|
|
rescheduleUid?: string | undefined,
|
|
timeZone?: string | null
|
|
) {
|
|
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,
|
|
})
|
|
);
|
|
|
|
try {
|
|
return !!(await Promise.all(limitCalculations));
|
|
} catch (error) {
|
|
throw new HttpError({ message: getErrorFromUnknown(error).message, statusCode: 401 });
|
|
}
|
|
}
|
|
|
|
export async function checkBookingLimit({
|
|
eventStartDate,
|
|
eventId,
|
|
key,
|
|
limitingNumber,
|
|
rescheduleUid,
|
|
timeZone,
|
|
teamId,
|
|
user,
|
|
}: {
|
|
eventStartDate: Date;
|
|
eventId?: number;
|
|
key: keyof IntervalLimit;
|
|
limitingNumber: number | undefined;
|
|
rescheduleUid?: string | undefined;
|
|
timeZone?: string | null;
|
|
teamId?: number;
|
|
user?: { id: number; email: string };
|
|
}) {
|
|
{
|
|
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 BookingRepository.getAllAcceptedTeamBookingsOfUser({
|
|
user: { id: user.id, email: user.email },
|
|
teamId,
|
|
startDate: startDate,
|
|
endDate: endDate,
|
|
returnCount: true,
|
|
excludedUid: rescheduleUid,
|
|
});
|
|
} 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,
|
|
});
|
|
}
|
|
}
|