Files
calendar/packages/features/bookings/lib/handleNewBooking/validateBookingTimeIsNotOutOfBounds.ts
T
Syed Ali ShahbazandGitHub 92678dc042 fix: eventtype null cant be booked error (#18975)
* fix err msg and add more description

* improvements

* fix cause

* add warn logs for when failing to specific limit checks

* typefix

* safestringify log

* improve error

* test fix and log min booking notice

* adds out of bounds error code

* test fix

* fix
2025-01-29 13:00:51 +00:00

57 lines
1.8 KiB
TypeScript

import type { Logger } from "tslog";
import { getUTCOffsetByTimezone } from "@calcom/lib/date-fns";
import { ErrorCode } from "@calcom/lib/errorCodes";
import { HttpError } from "@calcom/lib/http-error";
import isOutOfBounds, { BookingDateInPastError } from "@calcom/lib/isOutOfBounds";
import type { EventType } from "@calcom/prisma/client";
type ValidateBookingTimeEventType = Pick<
EventType,
| "periodType"
| "periodDays"
| "periodEndDate"
| "periodStartDate"
| "periodCountCalendarDays"
| "minimumBookingNotice"
| "eventName"
| "id"
| "title"
>;
export const validateBookingTimeIsNotOutOfBounds = async <T extends ValidateBookingTimeEventType>(
reqBodyStartTime: string,
reqBodyTimeZone: string,
eventType: T,
eventTimeZone: string | null | undefined,
logger: Logger<unknown>
) => {
let timeOutOfBounds = false;
try {
timeOutOfBounds = isOutOfBounds(
reqBodyStartTime,
{
periodType: eventType.periodType,
periodDays: eventType.periodDays,
periodEndDate: eventType.periodEndDate,
periodStartDate: eventType.periodStartDate,
periodCountCalendarDays: eventType.periodCountCalendarDays,
bookerUtcOffset: getUTCOffsetByTimezone(reqBodyTimeZone) ?? 0,
eventUtcOffset: eventTimeZone ? getUTCOffsetByTimezone(eventTimeZone) ?? 0 : 0,
},
eventType.minimumBookingNotice
);
} catch (error) {
logger.warn({
message: "NewBooking: Unable to determine timeOutOfBounds status. Defaulting to false.",
});
if (error instanceof BookingDateInPastError) {
logger.info(`Booking eventType ${eventType.id} failed`, JSON.stringify({ error }));
throw new HttpError({ statusCode: 400, message: error.message });
}
}
if (timeOutOfBounds) throw new Error(ErrorCode.BookingTimeOutOfBounds);
};