From e1ac6f545479dee12298048d079551b0a391fad8 Mon Sep 17 00:00:00 2001 From: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com> Date: Thu, 7 Dec 2023 02:43:19 -0500 Subject: [PATCH] fix: ignore original rescheduled booking for booking limits (#12625) * ignore original rescheduled booking for booking limits * fix unit test --------- Co-authored-by: CarinaWolli Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> --- packages/core/getBusyTimes.ts | 37 +++++++++++++++++----------- packages/core/getUserAvailability.ts | 7 ++++-- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/core/getBusyTimes.ts b/packages/core/getBusyTimes.ts index 80dc92d609f86a..52ac861a23fc24 100644 --- a/packages/core/getBusyTimes.ts +++ b/packages/core/getBusyTimes.ts @@ -7,7 +7,7 @@ import logger from "@calcom/lib/logger"; import { getPiiFreeBooking } from "@calcom/lib/piiFreeData"; import { performance } from "@calcom/lib/server/perfObserver"; 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 type { EventBusyDetails } from "@calcom/types/Calendar"; import type { CredentialPayload } from "@calcom/types/Credential"; @@ -264,8 +264,9 @@ export async function getBusyTimesForLimitChecks(params: { eventTypeId: number; startDate: Date; endDate: Date; + rescheduleUid?: string | null; }) { - const { userId, eventTypeId, startDate, endDate } = params; + const { userId, eventTypeId, startDate, endDate, rescheduleUid } = params; logger.silly( `Fetch limit checks bookings in range ${startDate} to ${endDate} for input ${JSON.stringify({ userId, @@ -275,19 +276,27 @@ export async function getBusyTimesForLimitChecks(params: { ); performance.mark("getBusyTimesForLimitChecksStart"); - const bookings = await prisma.booking.findMany({ - where: { - userId, - eventTypeId, - status: BookingStatus.ACCEPTED, - // FIXME: bookings that overlap on one side will never be counted - startTime: { - gte: startDate, - }, - endTime: { - lte: endDate, - }, + const where: Prisma.BookingWhereInput = { + userId, + eventTypeId, + status: BookingStatus.ACCEPTED, + // FIXME: bookings that overlap on one side will never be counted + startTime: { + gte: startDate, + }, + endTime: { + lte: endDate, }, + }; + + if (rescheduleUid) { + where.NOT = { + uid: rescheduleUid, + }; + } + + const bookings = await prisma.booking.findMany({ + where, select: { id: true, startTime: true, diff --git a/packages/core/getUserAvailability.ts b/packages/core/getUserAvailability.ts index cfb0b4b7274f66..4f33347792c21c 100644 --- a/packages/core/getUserAvailability.ts +++ b/packages/core/getUserAvailability.ts @@ -215,7 +215,8 @@ const _getUserAvailability = async function getUsersWorkingHoursLifeTheUniverseA dateTo, duration, eventType, - user.id + user.id, + initialData?.rescheduleUid ) : []; @@ -419,7 +420,8 @@ const _getBusyTimesFromLimits = async ( dateTo: Dayjs, duration: number | undefined, eventType: NonNullable, - userId: number + userId: number, + rescheduleUid?: string | null ) => { performance.mark("limitsStart"); @@ -445,6 +447,7 @@ const _getBusyTimesFromLimits = async ( eventTypeId: eventType.id, startDate: limitDateFrom.toDate(), endDate: limitDateTo.toDate(), + rescheduleUid: rescheduleUid, }); // run this first, as counting bookings should always run faster..