Files
calendar/packages/features/bookings/lib/handleNewBooking/validateBookingTimeIsNotOutOfBounds.ts
T
b4f1b5aebe refactor: handleNewBooking #4 (#15673)
* refactor: handleNewBooking #3

* refactor: create booking factor

* refactor: handleNewBooking

* refactor: seats and rescheduleUId

* chore: remove comment

* fix: type err

* chore: add missing statement

* chore: use less params and other improvements

* chore: name

* chore: improvement

* fix: type err

* Typo fix

* chore: fix type err

* refactor: more readable

* refactor: improve code

* fix: conflicts

---------

Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>
2024-10-02 10:26:25 -04:00

63 lines
2.0 KiB
TypeScript

import type { Logger } from "tslog";
import { getUTCOffsetByTimezone } from "@calcom/lib/date-fns";
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"
>;
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 set timeOutOfBounds. Using 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) {
const error = {
errorCode: "BookingTimeOutOfBounds",
message: `EventType '${eventType.eventName}' cannot be booked at this time.`,
};
logger.warn({
message: `NewBooking: EventType '${eventType.eventName}' cannot be booked at this time.`,
});
throw new HttpError({ statusCode: 400, message: error.message });
}
};