perf: Improve performance of period limits (#20974)

* perf: Improve performance of period limits

* Type fixes

* Fixed correct check for instanceof -> isDayjs
This commit is contained in:
Alex van Andel authored and GitHub committed 2025-04-25 21:07:18 +01:00
1 parent d51e0182b5
commit eacd05bf3b
1 file changed
+15 -26
+15 -26
View File
@@ -268,51 +268,40 @@ export function isTimeViolatingFutureLimit({
time,
periodLimits,
}: {
time: dayjs.ConfigType;
time: string | Date | number;
periodLimits: PeriodLimits;
}) {
const log = logger.getSubLogger({ prefix: ["isTimeViolatingFutureLimit"] });
// Because we are just going to compare times, we need not convert the time to a particular timezone
// date.isAfter/isBefore are timezone neutral
const dateInSystemTz = dayjs(time);
const dateObj = new Date(time);
if (periodLimits.endOfRollingPeriodEndDayInBookerTz) {
const isAfterRollingEndDay = dateInSystemTz.isAfter(periodLimits.endOfRollingPeriodEndDayInBookerTz);
log.silly("rollingEndDayCheck", {
formattedDate: dateInSystemTz.format(),
isAfterRollingEndDay,
endOfRollingPeriodEndDayInBookerTz: periodLimits.endOfRollingPeriodEndDayInBookerTz.format(),
});
const isAfterRollingEndDay =
dateObj.valueOf() > periodLimits.endOfRollingPeriodEndDayInBookerTz.valueOf();
if (isAfterRollingEndDay)
log.warn(
"Booking is out of bounds due to rolling period end day.",
safeStringify({
formattedDate: dateInSystemTz.format(),
formattedDate: dateObj.toISOString(),
isAfterRollingEndDay,
endOfRollingPeriodEndDayInBookerTz: periodLimits.endOfRollingPeriodEndDayInBookerTz.format(),
endOfRollingPeriodEndDayTs: periodLimits.endOfRollingPeriodEndDayInBookerTz.valueOf(),
})
);
return isAfterRollingEndDay;
}
if (periodLimits.startOfRangeStartDayInEventTz && periodLimits.endOfRangeEndDayInEventTz) {
const isBeforeRangeStart = dateInSystemTz.isBefore(periodLimits.startOfRangeStartDayInEventTz);
const isAfterRangeEnd = dateInSystemTz.isAfter(periodLimits.endOfRangeEndDayInEventTz);
log.silly("rangeCheck", {
formattedDate: dateInSystemTz.format(),
isAfterRangeEnd,
isBeforeRangeStart,
startOfRangeStartDayInEventTz: periodLimits.startOfRangeStartDayInEventTz.format(),
endOfRangeEndDayInEventTz: periodLimits.endOfRangeEndDayInEventTz.format(),
});
const isBeforeRangeStart = dateObj.valueOf() < periodLimits.startOfRangeStartDayInEventTz.valueOf();
const isAfterRangeEnd = dateObj.valueOf() > periodLimits.endOfRangeEndDayInEventTz.valueOf();
if (isBeforeRangeStart || isAfterRangeEnd)
log.warn(
"Booking is out of bounds due to range start and end.",
safeStringify({
formattedDate: dateInSystemTz.format(),
formattedDate: dateObj.toISOString(),
isBeforeRangeStart,
isAfterRangeEnd,
startOfRangeStartDayInEventTz: periodLimits.startOfRangeStartDayInEventTz.format(),
endOfRangeEndDayInEventTz: periodLimits.endOfRangeEndDayInEventTz.format(),
startOfRangeStartDayInEventTz: periodLimits.startOfRangeStartDayInEventTz.toDate().toISOString(),
endOfRangeEndDayInEventTz: periodLimits.endOfRangeEndDayInEventTz.toDate().toISOString(),
})
);
return isBeforeRangeStart || isAfterRangeEnd;
@@ -321,7 +310,7 @@ export function isTimeViolatingFutureLimit({
}
export default function isOutOfBounds(
time: dayjs.ConfigType,
time: NonNullable<dayjs.ConfigType>,
{
periodType,
periodDays,
@@ -354,7 +343,7 @@ export default function isOutOfBounds(
});
const isOutOfBoundsByPeriod = isTimeViolatingFutureLimit({
time,
time: dayjs.isDayjs(time) ? time.toDate() : time,
periodLimits,
});