fix: ignore original rescheduled booking for booking limits (#12625)

* ignore original rescheduled booking for booking limits

* fix unit test

---------

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
This commit is contained in:
Carina Wollendorfer 2023-12-07 02:43:19 -05:00 committed by GitHub
parent 55d44ce789
commit e1ac6f5454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 16 deletions

View File

@ -7,7 +7,7 @@ import logger from "@calcom/lib/logger";
import { getPiiFreeBooking } from "@calcom/lib/piiFreeData"; import { getPiiFreeBooking } from "@calcom/lib/piiFreeData";
import { performance } from "@calcom/lib/server/perfObserver"; import { performance } from "@calcom/lib/server/perfObserver";
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
import type { SelectedCalendar } from "@calcom/prisma/client"; import type { Prisma, SelectedCalendar } from "@calcom/prisma/client";
import { BookingStatus } from "@calcom/prisma/enums"; import { BookingStatus } from "@calcom/prisma/enums";
import type { EventBusyDetails } from "@calcom/types/Calendar"; import type { EventBusyDetails } from "@calcom/types/Calendar";
import type { CredentialPayload } from "@calcom/types/Credential"; import type { CredentialPayload } from "@calcom/types/Credential";
@ -264,8 +264,9 @@ export async function getBusyTimesForLimitChecks(params: {
eventTypeId: number; eventTypeId: number;
startDate: Date; startDate: Date;
endDate: Date; endDate: Date;
rescheduleUid?: string | null;
}) { }) {
const { userId, eventTypeId, startDate, endDate } = params; const { userId, eventTypeId, startDate, endDate, rescheduleUid } = params;
logger.silly( logger.silly(
`Fetch limit checks bookings in range ${startDate} to ${endDate} for input ${JSON.stringify({ `Fetch limit checks bookings in range ${startDate} to ${endDate} for input ${JSON.stringify({
userId, userId,
@ -275,19 +276,27 @@ export async function getBusyTimesForLimitChecks(params: {
); );
performance.mark("getBusyTimesForLimitChecksStart"); performance.mark("getBusyTimesForLimitChecksStart");
const bookings = await prisma.booking.findMany({ const where: Prisma.BookingWhereInput = {
where: { userId,
userId, eventTypeId,
eventTypeId, status: BookingStatus.ACCEPTED,
status: BookingStatus.ACCEPTED, // FIXME: bookings that overlap on one side will never be counted
// FIXME: bookings that overlap on one side will never be counted startTime: {
startTime: { gte: startDate,
gte: startDate,
},
endTime: {
lte: endDate,
},
}, },
endTime: {
lte: endDate,
},
};
if (rescheduleUid) {
where.NOT = {
uid: rescheduleUid,
};
}
const bookings = await prisma.booking.findMany({
where,
select: { select: {
id: true, id: true,
startTime: true, startTime: true,

View File

@ -215,7 +215,8 @@ const _getUserAvailability = async function getUsersWorkingHoursLifeTheUniverseA
dateTo, dateTo,
duration, duration,
eventType, eventType,
user.id user.id,
initialData?.rescheduleUid
) )
: []; : [];
@ -419,7 +420,8 @@ const _getBusyTimesFromLimits = async (
dateTo: Dayjs, dateTo: Dayjs,
duration: number | undefined, duration: number | undefined,
eventType: NonNullable<EventType>, eventType: NonNullable<EventType>,
userId: number userId: number,
rescheduleUid?: string | null
) => { ) => {
performance.mark("limitsStart"); performance.mark("limitsStart");
@ -445,6 +447,7 @@ const _getBusyTimesFromLimits = async (
eventTypeId: eventType.id, eventTypeId: eventType.id,
startDate: limitDateFrom.toDate(), startDate: limitDateFrom.toDate(),
endDate: limitDateTo.toDate(), endDate: limitDateTo.toDate(),
rescheduleUid: rescheduleUid,
}); });
// run this first, as counting bookings should always run faster.. // run this first, as counting bookings should always run faster..