From fc16ec59d3673b37db088af79e149df095ff6fa3 Mon Sep 17 00:00:00 2001 From: Abhiuday Gupta <77210185+aeswibon@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:28:11 +0530 Subject: [PATCH] fix: confirm booking doesn't respect minimum booking notice (#14577) Signed-off-by: Abhiuday --- .../features/bookings/lib/handleNewBooking.ts | 32 ++++++++++++------- packages/lib/isOutOfBounds.tsx | 10 +++++- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/packages/features/bookings/lib/handleNewBooking.ts b/packages/features/bookings/lib/handleNewBooking.ts index 8f5090ed8b..03246cbd9b 100644 --- a/packages/features/bookings/lib/handleNewBooking.ts +++ b/packages/features/bookings/lib/handleNewBooking.ts @@ -15,9 +15,9 @@ import processExternalId from "@calcom/app-store/_utils/calendars/processExterna import { metadata as GoogleMeetMetadata } from "@calcom/app-store/googlevideo/_metadata"; import type { LocationObject } from "@calcom/app-store/locations"; import { - getLocationValueForDB, MeetLocationType, OrganizerDefaultConferencingAppType, + getLocationValueForDB, } from "@calcom/app-store/locations"; import type { EventTypeAppsList } from "@calcom/app-store/utils"; import { getAppFromSlug } from "@calcom/app-store/utils"; @@ -54,8 +54,12 @@ import { getFullName } from "@calcom/features/form-builder/utils"; import type { GetSubscriberOptions } from "@calcom/features/webhooks/lib/getWebhooks"; import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks"; import { cancelScheduledJobs, scheduleTrigger } from "@calcom/features/webhooks/lib/scheduleTrigger"; -import { parseBookingLimit, parseDurationLimit } from "@calcom/lib"; -import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib"; +import { + isPrismaObjOrUndefined, + parseBookingLimit, + parseDurationLimit, + parseRecurringEvent, +} from "@calcom/lib"; import { getVideoCallUrlFromCalEvent } from "@calcom/lib/CalEventParser"; import { getDefaultEvent, getUsernameList } from "@calcom/lib/defaultEvents"; import { ErrorCode } from "@calcom/lib/errorCodes"; @@ -80,9 +84,9 @@ import type { BookingReference } from "@calcom/prisma/client"; import { BookingStatus, SchedulingType, WebhookTriggerEvents } from "@calcom/prisma/enums"; import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential"; import { + EventTypeMetaDataSchema, bookingCreateSchemaLegacyPropsForApi, customInputSchema, - EventTypeMetaDataSchema, userMetadata as userMetadataSchema, } from "@calcom/prisma/zod-utils"; import type { @@ -193,6 +197,7 @@ export const getEventTypesFromDB = async (eventTypeId: number) => { lockTimeZoneToggleOnBookingPage: true, requiresConfirmation: true, requiresBookerEmailVerification: true, + minimumBookingNotice: true, userId: true, price: true, currency: true, @@ -701,6 +706,7 @@ async function createBooking({ newBookingData.recurringEventId = originalRescheduledBooking.recurringEventId; } } + const createBookingObj = { include: { user: { @@ -1008,13 +1014,17 @@ async function handler( let timeOutOfBounds = false; try { - timeOutOfBounds = isOutOfBounds(reqBody.start, { - periodType: eventType.periodType, - periodDays: eventType.periodDays, - periodEndDate: eventType.periodEndDate, - periodStartDate: eventType.periodStartDate, - periodCountCalendarDays: eventType.periodCountCalendarDays, - }); + timeOutOfBounds = isOutOfBounds( + reqBody.start, + { + periodType: eventType.periodType, + periodDays: eventType.periodDays, + periodEndDate: eventType.periodEndDate, + periodStartDate: eventType.periodStartDate, + periodCountCalendarDays: eventType.periodCountCalendarDays, + }, + eventType.minimumBookingNotice + ); } catch (error) { loggerWithEventDetails.warn({ message: "NewBooking: Unable set timeOutOfBounds. Using false. ", diff --git a/packages/lib/isOutOfBounds.tsx b/packages/lib/isOutOfBounds.tsx index d66a73e081..7779dbb278 100644 --- a/packages/lib/isOutOfBounds.tsx +++ b/packages/lib/isOutOfBounds.tsx @@ -28,13 +28,21 @@ function isOutOfBounds( }: Pick< EventType, "periodType" | "periodDays" | "periodCountCalendarDays" | "periodStartDate" | "periodEndDate" - > + >, + minimumBookingNotice?: number ) { const date = dayjs(time); guardAgainstBookingInThePast(date.toDate()); periodDays = periodDays || 0; + if (minimumBookingNotice) { + const minimumBookingStartDate = dayjs().add(minimumBookingNotice, "minutes"); + if (date.isBefore(minimumBookingStartDate)) { + return true; + } + } + switch (periodType) { case PeriodType.ROLLING: { const periodRollingEndDay = periodCountCalendarDays