feat: checkbox to count managed events for team-wide limits (#16923)
Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
1 parent
0d757039f7
commit
b5fa43f086
17 files changed
+266
-61
No files matched your search
@@ -14,7 +14,8 @@ export async function checkBookingLimits(
|
||||
eventStartDate: Date,
|
||||
eventId: number,
|
||||
rescheduleUid?: string | undefined,
|
||||
timeZone?: string | null
|
||||
timeZone?: string | null,
|
||||
includeManagedEvents?: boolean
|
||||
) {
|
||||
const parsedBookingLimits = parseBookingLimit(bookingLimits);
|
||||
if (!parsedBookingLimits) return false;
|
||||
@@ -28,6 +29,7 @@ export async function checkBookingLimits(
|
||||
eventId,
|
||||
timeZone,
|
||||
rescheduleUid,
|
||||
includeManagedEvents,
|
||||
})
|
||||
);
|
||||
|
||||
@@ -47,6 +49,7 @@ export async function checkBookingLimit({
|
||||
timeZone,
|
||||
teamId,
|
||||
user,
|
||||
includeManagedEvents = false,
|
||||
}: {
|
||||
eventStartDate: Date;
|
||||
eventId?: number;
|
||||
@@ -56,6 +59,7 @@ export async function checkBookingLimit({
|
||||
timeZone?: string | null;
|
||||
teamId?: number;
|
||||
user?: { id: number; email: string };
|
||||
includeManagedEvents?: boolean;
|
||||
}) {
|
||||
{
|
||||
const eventDateInOrganizerTz = timeZone ? dayjs(eventStartDate).tz(timeZone) : dayjs(eventStartDate);
|
||||
@@ -75,8 +79,9 @@ export async function checkBookingLimit({
|
||||
teamId,
|
||||
startDate: startDate,
|
||||
endDate: endDate,
|
||||
returnCount: true,
|
||||
shouldReturnCount: true,
|
||||
excludedUid: rescheduleUid,
|
||||
includeManagedEvents,
|
||||
});
|
||||
} else {
|
||||
bookingsInPeriod = await prisma.booking.count({
|
||||
|
||||
@@ -299,6 +299,7 @@ export async function getMinimalTeam(args: {
|
||||
isPrivate: true,
|
||||
metadata: true,
|
||||
bookingLimits: true,
|
||||
includeManagedEventsInLimits: true,
|
||||
parent: {
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
@@ -6,6 +6,22 @@ import { BookingStatus } from "@calcom/prisma/enums";
|
||||
|
||||
import { UserRepository } from "./user";
|
||||
|
||||
type TeamBookingsParamsBase = {
|
||||
user: { id: number; email: string };
|
||||
teamId: number;
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
excludedUid?: string | null;
|
||||
includeManagedEvents: boolean;
|
||||
shouldReturnCount?: boolean;
|
||||
};
|
||||
|
||||
type TeamBookingsParamsWithCount = TeamBookingsParamsBase & {
|
||||
shouldReturnCount: true;
|
||||
};
|
||||
|
||||
type TeamBookingsParamsWithoutCount = TeamBookingsParamsBase;
|
||||
|
||||
export class BookingRepository {
|
||||
static async getBookingAttendees(bookingId: number) {
|
||||
return await prisma.attendee.findMany({
|
||||
@@ -251,32 +267,14 @@ export class BookingRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async getAllAcceptedTeamBookingsOfUser(params: {
|
||||
user: { id: number; email: string };
|
||||
teamId: number;
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
excludedUid?: string | null;
|
||||
returnCount: true;
|
||||
}): Promise<number>;
|
||||
static async getAllAcceptedTeamBookingsOfUser(params: TeamBookingsParamsWithCount): Promise<number>;
|
||||
|
||||
static async getAllAcceptedTeamBookingsOfUser(params: {
|
||||
user: { id: number; email: string };
|
||||
teamId: number;
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
excludedUid?: string | null;
|
||||
}): Promise<Array<Booking>>;
|
||||
static async getAllAcceptedTeamBookingsOfUser(
|
||||
params: TeamBookingsParamsWithoutCount
|
||||
): Promise<Array<Booking>>;
|
||||
|
||||
static async getAllAcceptedTeamBookingsOfUser(params: {
|
||||
user: { id: number; email: string };
|
||||
teamId: number;
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
excludedUid?: string | null;
|
||||
returnCount?: boolean;
|
||||
}) {
|
||||
const { user, teamId, startDate, endDate, returnCount, excludedUid } = params;
|
||||
static async getAllAcceptedTeamBookingsOfUser(params: TeamBookingsParamsBase) {
|
||||
const { user, teamId, startDate, endDate, excludedUid, shouldReturnCount, includeManagedEvents } = params;
|
||||
|
||||
const baseWhere: Prisma.BookingWhereInput = {
|
||||
status: BookingStatus.ACCEPTED,
|
||||
@@ -313,7 +311,17 @@ export class BookingRepository {
|
||||
},
|
||||
};
|
||||
|
||||
if (returnCount) {
|
||||
const whereManagedBookings: Prisma.BookingWhereInput = {
|
||||
...baseWhere,
|
||||
userId: user.id,
|
||||
eventType: {
|
||||
parent: {
|
||||
teamId,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (shouldReturnCount) {
|
||||
const collectiveRoundRobinBookingsOwner = await prisma.booking.count({
|
||||
where: whereCollectiveRoundRobinOwner,
|
||||
});
|
||||
@@ -322,7 +330,16 @@ export class BookingRepository {
|
||||
where: whereCollectiveRoundRobinBookingsAttendee,
|
||||
});
|
||||
|
||||
const totalNrOfBooking = collectiveRoundRobinBookingsOwner + collectiveRoundRobinBookingsAttendee;
|
||||
let managedBookings = 0;
|
||||
|
||||
if (includeManagedEvents) {
|
||||
managedBookings = await prisma.booking.count({
|
||||
where: whereManagedBookings,
|
||||
});
|
||||
}
|
||||
|
||||
const totalNrOfBooking =
|
||||
collectiveRoundRobinBookingsOwner + collectiveRoundRobinBookingsAttendee + managedBookings;
|
||||
|
||||
return totalNrOfBooking;
|
||||
}
|
||||
@@ -334,7 +351,19 @@ export class BookingRepository {
|
||||
where: whereCollectiveRoundRobinBookingsAttendee,
|
||||
});
|
||||
|
||||
return [...collectiveRoundRobinBookingsOwner, ...collectiveRoundRobinBookingsAttendee];
|
||||
let managedBookings: typeof collectiveRoundRobinBookingsAttendee = [];
|
||||
|
||||
if (includeManagedEvents) {
|
||||
managedBookings = await prisma.booking.findMany({
|
||||
where: whereManagedBookings,
|
||||
});
|
||||
}
|
||||
|
||||
return [
|
||||
...collectiveRoundRobinBookingsOwner,
|
||||
...collectiveRoundRobinBookingsAttendee,
|
||||
...managedBookings,
|
||||
];
|
||||
}
|
||||
|
||||
static async findOriginalRescheduledBooking(uid: string, seatsEventType?: boolean) {
|
||||
|
||||
Reference in new issue
Block a user