Skip to content

Commit

Permalink
fix: confirm booking doesn't respect minimum booking notice (#14577)
Browse files Browse the repository at this point in the history
Signed-off-by: Abhiuday <contact.abhiuday@gmail.com>
  • Loading branch information
aeswibon committed Apr 26, 2024
1 parent ab26481 commit fc16ec5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
32 changes: 21 additions & 11 deletions packages/features/bookings/lib/handleNewBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -193,6 +197,7 @@ export const getEventTypesFromDB = async (eventTypeId: number) => {
lockTimeZoneToggleOnBookingPage: true,
requiresConfirmation: true,
requiresBookerEmailVerification: true,
minimumBookingNotice: true,
userId: true,
price: true,
currency: true,
Expand Down Expand Up @@ -701,6 +706,7 @@ async function createBooking({
newBookingData.recurringEventId = originalRescheduledBooking.recurringEventId;
}
}

const createBookingObj = {
include: {
user: {
Expand Down Expand Up @@ -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. ",
Expand Down
10 changes: 9 additions & 1 deletion packages/lib/isOutOfBounds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fc16ec5

Please sign in to comment.