From 06ce1988f32f26db9475f1a2f53b50727dfe05b3 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Fri, 2 Aug 2024 18:00:50 +0530 Subject: [PATCH] fix: Future Limit Calculations (#15825) --- .../getSchedule/futureLimit.timezone.test.ts | 121 +++++++++++++- .../features/bookings/lib/handleNewBooking.ts | 8 +- packages/lib/isOutOfBounds.timezone.test.ts | 48 +++--- packages/lib/isOutOfBounds.tsx | 151 +++++++++++------- .../trpc/server/routers/viewer/slots/util.ts | 16 +- 5 files changed, 253 insertions(+), 91 deletions(-) diff --git a/apps/web/test/lib/getSchedule/futureLimit.timezone.test.ts b/apps/web/test/lib/getSchedule/futureLimit.timezone.test.ts index 4de779c4a6..a720eb43b0 100644 --- a/apps/web/test/lib/getSchedule/futureLimit.timezone.test.ts +++ b/apps/web/test/lib/getSchedule/futureLimit.timezone.test.ts @@ -675,7 +675,6 @@ describe("getSchedule", () => { ]; expect(scheduleForEvent).toHaveTimeSlots( - // All slots on current day are available [ // "2024-05-30T04:30:00.000Z", // Not available as before the start of the range "2024-05-31T04:30:00.000Z", @@ -1548,6 +1547,126 @@ describe("getSchedule", () => { dateString: plus4DateString, }); }); + + describe("GMT-11 Browsing", () => { + test("should show correct timeslots only for 24th and 25th July(of IST Timezone)", async () => { + vi.setSystemTime(`2024-07-05T01:30:00Z`); + + const scenarioData = { + eventTypes: [ + { + id: 1, + length: 60, + // Makes today and tomorrow only available + ...getPeriodTypeData({ + type: "RANGE", + + // Only 25th and 26th(as per the event timezone(IST)) should be available + periodStartDate: new Date(`2024-07-24T18:30:00.000Z`), // 25th July in IST + periodEndDate: new Date(`2024-07-25T18:30:00.000Z`), // 26th July in IST + + periodCountCalendarDays: true, + }), + users: [ + { + id: 101, + }, + ], + }, + ], + users: [ + { + ...TestData.users.example, + id: 101, + schedules: [TestData.schedules.IstWorkHours], + }, + ], + } satisfies ScenarioData; + + await createBookingScenario(scenarioData); + + const scheduleForEventForPagoTz = await getSchedule({ + input: { + eventTypeId: 1, + eventTypeSlug: "", + usernameList: [], + startTime: `2024-06-30T18:30:00.000Z`, + endTime: `2024-07-31T18:29:59.999Z`, + timeZone: Timezones["-11:00"], + isTeamEvent: false, + }, + }); + + /** + * Current day in test is 5th July, so verify that earlier timeslots than 24th July are disabled + */ + { + expect(scheduleForEventForPagoTz).toHaveDateDisabled({ + dateString: "2024-07-21", + }); + + expect(scheduleForEventForPagoTz).toHaveDateDisabled({ + dateString: "2024-07-22", + }); + + expect(scheduleForEventForPagoTz).toHaveDateDisabled({ + dateString: "2024-07-23", + }); + } + + expect(scheduleForEventForPagoTz).toHaveTimeSlots( + [ + "2024-07-25T04:30:00.000Z", + "2024-07-25T05:30:00.000Z", + "2024-07-25T06:30:00.000Z", + "2024-07-25T07:30:00.000Z", + "2024-07-25T08:30:00.000Z", + "2024-07-25T09:30:00.000Z", + "2024-07-25T10:30:00.000Z", + ], + { + // 25th timeslots are shown mostly on 24th of Pago Pago + dateString: "2024-07-24", + doExactMatch: true, + } + ); + + expect(scheduleForEventForPagoTz).toHaveTimeSlots( + [ + "2024-07-25T11:30:00.000Z", + "2024-07-26T04:30:00.000Z", + "2024-07-26T05:30:00.000Z", + "2024-07-26T06:30:00.000Z", + "2024-07-26T07:30:00.000Z", + "2024-07-26T08:30:00.000Z", + "2024-07-26T09:30:00.000Z", + "2024-07-26T10:30:00.000Z", + ], + { + dateString: "2024-07-25", + doExactMatch: true, + } + ); + + expect(scheduleForEventForPagoTz).toHaveTimeSlots(["2024-07-26T11:30:00.000Z"], { + dateString: "2024-07-26", + doExactMatch: true, + }); + + /** + * Verify that timeslots beyond 26th July are disabled + */ + { + expect(scheduleForEventForPagoTz).toHaveDateDisabled({ + dateString: "2024-07-27", + }); + + expect(scheduleForEventForPagoTz).toHaveDateDisabled({ + dateString: "2024-07-28", + }); + } + }); + }); }); }); }); diff --git a/packages/features/bookings/lib/handleNewBooking.ts b/packages/features/bookings/lib/handleNewBooking.ts index a265239e61..af2b300e82 100644 --- a/packages/features/bookings/lib/handleNewBooking.ts +++ b/packages/features/bookings/lib/handleNewBooking.ts @@ -258,7 +258,6 @@ async function handler( } const fullName = getFullName(bookerName); - // Why are we only using "en" locale const tGuests = await getTranslation("en", "common"); @@ -295,6 +294,8 @@ async function handler( }) ); + const eventTimeZone = eventType.schedule?.timeZone ?? null; + let timeOutOfBounds = false; try { timeOutOfBounds = isOutOfBounds( @@ -305,7 +306,8 @@ async function handler( periodEndDate: eventType.periodEndDate, periodStartDate: eventType.periodStartDate, periodCountCalendarDays: eventType.periodCountCalendarDays, - utcOffset: getUTCOffsetByTimezone(reqBody.timeZone) ?? 0, + bookerUtcOffset: getUTCOffsetByTimezone(reqBody.timeZone) ?? 0, + eventUtcOffset: eventTimeZone ? getUTCOffsetByTimezone(eventTimeZone) ?? 0 : 0, }, eventType.minimumBookingNotice ); @@ -428,7 +430,7 @@ async function handler( startAsDate, eventType.id, rescheduleUid, - eventType.schedule?.timeZone + eventTimeZone ); } if (eventType.durationLimits) { diff --git a/packages/lib/isOutOfBounds.timezone.test.ts b/packages/lib/isOutOfBounds.timezone.test.ts index 0b742d157e..2f3f5bd2c0 100644 --- a/packages/lib/isOutOfBounds.timezone.test.ts +++ b/packages/lib/isOutOfBounds.timezone.test.ts @@ -22,12 +22,12 @@ const getDayJsTimeWithUtcOffset = ({ describe("getRollingWindowEndDate", () => { it("should return the startDate itself when that date is bookable and 0 days in future are needed", () => { const endDay = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T05:09:46+11:00", utcOffset: 11, }), daysNeeded: 0, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-02": { isBookable: true }, "2024-05-03": { isBookable: false }, }, @@ -38,12 +38,12 @@ describe("getRollingWindowEndDate", () => { it("should return the last possible time of the date so that all the timeslots of the last day are considered within range ", () => { const endDay = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T05:09:46+11:00", utcOffset: 11, }), daysNeeded: 2, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-02": { isBookable: true }, "2024-05-03": { isBookable: true }, }, @@ -55,12 +55,12 @@ describe("getRollingWindowEndDate", () => { it("Input startDate normalization - should return the startDate with 00:00 time when that date is bookable and only 1 day is needed", () => { const endDay = getRollingWindowEndDate({ // startDate has a time other than 00:00 - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-11T05:09:46+11:00", utcOffset: 11, }), daysNeeded: 1, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-10": { isBookable: true }, "2024-05-11": { isBookable: true }, }, @@ -71,12 +71,12 @@ describe("getRollingWindowEndDate", () => { it("should return the first bookable date when only 1 day is needed and the startDate is unavailable", () => { const endDay = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T05:09:46+11:00", utcOffset: 11, }), daysNeeded: 1, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-02": { isBookable: false }, "2024-05-03": { isBookable: false }, "2024-05-04": { isBookable: true }, @@ -99,12 +99,12 @@ describe("getRollingWindowEndDate", () => { return; function testWhenNonBusinessDaysAreBooked() { const endDay = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T15:09:46+11:00", utcOffset: 11, }), daysNeeded: 3, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-02": { isBookable: true }, "2024-05-03": { isBookable: true }, // Skipped because Saturday is non-business day @@ -123,12 +123,12 @@ describe("getRollingWindowEndDate", () => { function testWhenNonBusinessDaysAreNotBooked() { const endDay2 = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T15:09:46+11:00", utcOffset: 11, }), daysNeeded: 3, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-02": { isBookable: true }, "2024-05-03": { isBookable: true }, "2024-05-04": { isBookable: false }, @@ -145,12 +145,12 @@ describe("getRollingWindowEndDate", () => { it("should return the first `daysNeeded` bookable days", () => { const endDay = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T05:09:46+11:00", utcOffset: 11, }), daysNeeded: 3, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-02": { isBookable: false }, "2024-05-03": { isBookable: false }, "2024-05-04": { isBookable: true }, @@ -165,12 +165,12 @@ describe("getRollingWindowEndDate", () => { it("should return the last bookable day if enough `daysNeeded` bookable days aren't found", () => { const endDay = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T05:09:46+11:00", utcOffset: 11, }), daysNeeded: 30, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-02": { isBookable: false }, "2024-05-03": { isBookable: false }, "2024-05-04": { isBookable: true }, @@ -182,14 +182,14 @@ describe("getRollingWindowEndDate", () => { expect(endDay?.format()).toEqual("2024-05-05T23:59:59+11:00"); }); - it("should treat non existing dates in `allDatesWithBookabilityStatus` as having isBookable:false the first `daysNeeded` bookable days", () => { + it("should treat non existing dates in `allDatesWithBookabilityStatusInBookerTz` as having isBookable:false the first `daysNeeded` bookable days", () => { const endDay = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T05:09:46+11:00", utcOffset: 11, }), daysNeeded: 3, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-02": { isBookable: true }, "2024-05-03": { isBookable: false }, "2024-05-04": { isBookable: true }, @@ -202,25 +202,25 @@ describe("getRollingWindowEndDate", () => { it("should return the last day in maximum window(that would be ROLLING_WINDOW_PERIOD_MAX_DAYS_TO_CHECK days ahead) if no bookable day is found at all", () => { const endDay = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T05:09:46+11:00", utcOffset: 11, }), daysNeeded: 3, - allDatesWithBookabilityStatus: {}, + allDatesWithBookabilityStatusInBookerTz: {}, countNonBusinessDays: true, }); expect(endDay?.format()).toEqual("2024-07-02T23:59:59+11:00"); }); - it("should should consider the bookable day very close to ROLLING_WINDOW_PERIOD_MAX_DAYS_TO_CHECK but not beyond it", () => { + it("should consider the bookable day very close to ROLLING_WINDOW_PERIOD_MAX_DAYS_TO_CHECK but not beyond it", () => { const endDay = getRollingWindowEndDate({ - startDate: getDayJsTimeWithUtcOffset({ + startDateInBookerTz: getDayJsTimeWithUtcOffset({ dateStringWithOffset: "2024-05-02T05:09:46+11:00", utcOffset: 11, }), daysNeeded: 3, - allDatesWithBookabilityStatus: { + allDatesWithBookabilityStatusInBookerTz: { "2024-05-02": { isBookable: true }, "2024-06-04": { isBookable: true }, "2024-07-01": { isBookable: true }, diff --git a/packages/lib/isOutOfBounds.tsx b/packages/lib/isOutOfBounds.tsx index 9e9d421027..f38b0ac42e 100644 --- a/packages/lib/isOutOfBounds.tsx +++ b/packages/lib/isOutOfBounds.tsx @@ -33,8 +33,9 @@ export function calculatePeriodLimits({ /** * These dates will be considered in the same utfcOffset as provided */ - allDatesWithBookabilityStatus, - utcOffset, + allDatesWithBookabilityStatusInBookerTz, + eventUtcOffset, + bookerUtcOffset, /** * This is temporary till we find a way to provide allDatesWithBookabilityStatus in handleNewBooking without re-computing availability. * It is okay for handleNewBooking to pass it as true as the frontend won't allow selecting a timeslot that is out of bounds of ROLLING_WINDOW @@ -45,11 +46,14 @@ export function calculatePeriodLimits({ EventType, "periodType" | "periodDays" | "periodCountCalendarDays" | "periodStartDate" | "periodEndDate" > & { - allDatesWithBookabilityStatus: Record | null; - utcOffset: number; + allDatesWithBookabilityStatusInBookerTz: Record | null; + eventUtcOffset: number; + bookerUtcOffset: number; _skipRollingWindowCheck?: boolean; }): PeriodLimits { - const currentTime = dayjs().utcOffset(utcOffset); + const currentTime = dayjs(); + const currentTimeInEventTz = currentTime.utcOffset(eventUtcOffset); + const currentTimeInBookerTz = currentTime.utcOffset(bookerUtcOffset); periodDays = periodDays || 0; const log = logger.getSubLogger({ prefix: ["calculatePeriodLimits"] }); log.debug( @@ -59,84 +63,100 @@ export function calculatePeriodLimits({ periodCountCalendarDays, periodStartDate: periodStartDate, periodEndDate: periodEndDate, - currentTime: currentTime.format(), + currentTime: currentTimeInEventTz.format(), }) ); switch (periodType) { case PeriodType.ROLLING: { + // We use booker's timezone to calculate the end of the rolling period(for both ROLLING and ROLLING_WINDOW). This is because we want earliest possible timeslot to be available to be booked which could be on an earlier day(compared to event timezone) as per booker's timezone. + // So, if 2 day rolling period is set and 2024-07-24T8:30:00 slot is available in event timezone, the corresponding slot in GMT-11 would be 2024-07-24T21:30:00. So, 24th should be bookable for that timeslot, which could only be made available if we consider things in booker timezone. const rollingEndDay = periodCountCalendarDays - ? currentTime.add(periodDays, "days") - : currentTime.businessDaysAdd(periodDays); + ? currentTimeInBookerTz.add(periodDays, "days") + : currentTimeInBookerTz.businessDaysAdd(periodDays); // The future limit talks in terms of days so we take the end of the day here to consider the entire day - return { rollingEndDay: rollingEndDay.endOf("day"), rangeStartDay: null, rangeEndDay: null }; + return { + endOfRollingPeriodEndDayInBookerTz: rollingEndDay.endOf("day"), + startOfRangeStartDayInEventTz: null, + endOfRangeEndDayInEventTz: null, + }; } case PeriodType.ROLLING_WINDOW: { if (_skipRollingWindowCheck) { - return { rollingEndDay: null, rangeStartDay: null, rangeEndDay: null }; + return { + endOfRollingPeriodEndDayInBookerTz: null, + startOfRangeStartDayInEventTz: null, + endOfRangeEndDayInEventTz: null, + }; } - if (!allDatesWithBookabilityStatus) { + if (!allDatesWithBookabilityStatusInBookerTz) { throw new Error("`allDatesWithBookabilityStatus` is required"); } - const rollingEndDay = getRollingWindowEndDate({ - startDate: currentTime, + const endOfRollingPeriodEndDayInBookerTz = getRollingWindowEndDate({ + startDateInBookerTz: currentTimeInBookerTz, daysNeeded: periodDays, - allDatesWithBookabilityStatus, + allDatesWithBookabilityStatusInBookerTz, countNonBusinessDays: periodCountCalendarDays, }); - return { rollingEndDay, rangeStartDay: null, rangeEndDay: null }; + return { + endOfRollingPeriodEndDayInBookerTz, + startOfRangeStartDayInEventTz: null, + endOfRangeEndDayInEventTz: null, + }; } case PeriodType.RANGE: { - // The future limit talks in terms of days so we take the start of the day for starting range and endOf the day for ending range - const rangeStartDay = dayjs(periodStartDate).utcOffset(utcOffset).startOf("day"); - const rangeEndDay = dayjs(periodEndDate).utcOffset(utcOffset).endOf("day"); + // We take the start of the day for the start of the range and endOf the day for end of range, so that entire days are covered + // We use organizer's timezone here(in contrast with ROLLING/ROLLING_WINDOW where number of days is available and not the specific date objects). + // This is because in case of range the start and end date objects are determined by the organizer, so we should consider the range in organizer/event's timezone. + const startOfRangeStartDayInEventTz = dayjs(periodStartDate).utcOffset(eventUtcOffset).startOf("day"); + const endOfRangeEndDayInEventTz = dayjs(periodEndDate).utcOffset(eventUtcOffset).endOf("day"); return { - rollingEndDay: null, - rangeStartDay, - rangeEndDay, + endOfRollingPeriodEndDayInBookerTz: null, + startOfRangeStartDayInEventTz, + endOfRangeEndDayInEventTz, }; } } return { - rollingEndDay: null, - rangeStartDay: null, - rangeEndDay: null, + endOfRollingPeriodEndDayInBookerTz: null, + startOfRangeStartDayInEventTz: null, + endOfRangeEndDayInEventTz: null, }; } export function getRollingWindowEndDate({ - startDate, + startDateInBookerTz, daysNeeded, - allDatesWithBookabilityStatus, + allDatesWithBookabilityStatusInBookerTz, countNonBusinessDays, }: { /** * It should be provided in the same utcOffset as the dates in `allDatesWithBookabilityStatus` * This is because we do a lookup by day in `allDatesWithBookabilityStatus` */ - startDate: dayjs.Dayjs; + startDateInBookerTz: dayjs.Dayjs; daysNeeded: number; - allDatesWithBookabilityStatus: Record; + allDatesWithBookabilityStatusInBookerTz: Record; countNonBusinessDays: boolean | null; }) { const log = logger.getSubLogger({ prefix: ["getRollingWindowEndDate"] }); - log.debug("called:", safeStringify({ startDay: startDate.format(), daysNeeded })); + log.debug("called:", safeStringify({ startDay: startDateInBookerTz.format(), daysNeeded })); let counter = 1; let rollingEndDay; - let currentDate = startDate.startOf("day"); - + const startOfStartDayInEventTz = startDateInBookerTz.startOf("day"); // It helps to break out of the loop if we don't find enough bookable days. const maxDaysToCheck = ROLLING_WINDOW_PERIOD_MAX_DAYS_TO_CHECK; let bookableDaysCount = 0; + let startOfIterationDay = startOfStartDayInEventTz; // Add periodDays to currentDate, skipping non-bookable days. while (bookableDaysCount < daysNeeded) { // What if we don't find any bookable days. We should break out of the loop after a certain number of days. @@ -144,36 +164,41 @@ export function getRollingWindowEndDate({ break; } - const isBookable = !!allDatesWithBookabilityStatus[currentDate.format("YYYY-MM-DD")]?.isBookable; + const onlyDateOfIterationDay = startOfIterationDay.format("YYYY-MM-DD"); + + const isBookable = !!allDatesWithBookabilityStatusInBookerTz[onlyDateOfIterationDay]?.isBookable; if (isBookable) { bookableDaysCount++; - rollingEndDay = currentDate; + rollingEndDay = startOfIterationDay; } log.silly( `Loop Iteration: ${counter}`, safeStringify({ - currentDate: currentDate.format(), + iterationDayBeginning: startOfIterationDay.format(), isBookable, bookableDaysCount, rollingEndDay: rollingEndDay?.format(), + allDatesWithBookabilityStatusInBookerTz, }) ); - currentDate = countNonBusinessDays ? currentDate.add(1, "days") : currentDate.businessDaysAdd(1); + startOfIterationDay = countNonBusinessDays + ? startOfIterationDay.add(1, "days") + : startOfIterationDay.businessDaysAdd(1); counter++; } /** * We can't just return null(if rollingEndDay couldn't be obtained) and allow days farther than ROLLING_WINDOW_PERIOD_MAX_DAYS_TO_CHECK to be booked as that would mean there is no future limit + * The future limit talks in terms of days so we take the end of the day here to consider the entire day */ - const rollingEndDayOrLastPossibleDayAsPerLimit = rollingEndDay ?? currentDate; - log.debug("Returning rollingEndDay", rollingEndDayOrLastPossibleDayAsPerLimit.format()); + const endOfLastDayOfWindowInBookerTz = (rollingEndDay ?? startOfIterationDay).endOf("day"); + log.debug("Returning rollingEndDay", endOfLastDayOfWindowInBookerTz.format()); - // The future limit talks in terms of days so we take the end of the day here to consider the entire day - return rollingEndDayOrLastPossibleDayAsPerLimit.endOf("day"); + return endOfLastDayOfWindowInBookerTz; } /** @@ -202,15 +227,11 @@ export function isTimeOutOfBounds({ } type PeriodLimits = { - rollingEndDay: dayjs.Dayjs | null; - rangeStartDay: dayjs.Dayjs | null; - rangeEndDay: dayjs.Dayjs | null; + endOfRollingPeriodEndDayInBookerTz: dayjs.Dayjs | null; + startOfRangeStartDayInEventTz: dayjs.Dayjs | null; + endOfRangeEndDayInEventTz: dayjs.Dayjs | null; }; -/** - * To be used when we work on just Dates(and not specific timeslots) to check boundaries - * e.g. It checks for Future Limits which operate on dates and not times. - */ export function isTimeViolatingFutureLimit({ time, periodLimits, @@ -219,19 +240,30 @@ export function isTimeViolatingFutureLimit({ periodLimits: PeriodLimits; }) { const log = logger.getSubLogger({ prefix: ["isTimeViolatingFutureLimit"] }); - const date = dayjs(time); - if (periodLimits.rollingEndDay) { - const isAfterRollingEndDay = date.isAfter(periodLimits.rollingEndDay); - log.debug({ - formattedDate: date.format(), + // 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); + if (periodLimits.endOfRollingPeriodEndDayInBookerTz) { + const isAfterRollingEndDay = dateInSystemTz.isAfter(periodLimits.endOfRollingPeriodEndDayInBookerTz); + log.debug("rollingEndDayCheck", { + formattedDate: dateInSystemTz.format(), isAfterRollingEndDay, - rollingEndDay: periodLimits.rollingEndDay.format(), + endOfRollingPeriodEndDayInBookerTz: periodLimits.endOfRollingPeriodEndDayInBookerTz.format(), }); return isAfterRollingEndDay; } - if (periodLimits.rangeStartDay && periodLimits.rangeEndDay) { - return date.isBefore(periodLimits.rangeStartDay) || date.isAfter(periodLimits.rangeEndDay); + if (periodLimits.startOfRangeStartDayInEventTz && periodLimits.endOfRangeEndDayInEventTz) { + const isBeforeRangeStart = dateInSystemTz.isBefore(periodLimits.startOfRangeStartDayInEventTz); + const isAfterRangeEnd = dateInSystemTz.isAfter(periodLimits.endOfRangeEndDayInEventTz); + log.debug("rangeCheck", { + formattedDate: dateInSystemTz.format(), + isAfterRangeEnd, + isBeforeRangeStart, + startOfRangeStartDayInEventTz: periodLimits.startOfRangeStartDayInEventTz.format(), + endOfRangeEndDayInEventTz: periodLimits.endOfRangeEndDayInEventTz.format(), + }); + return isBeforeRangeStart || isAfterRangeEnd; } return false; } @@ -244,12 +276,14 @@ export default function isOutOfBounds( periodCountCalendarDays, periodStartDate, periodEndDate, - utcOffset, + eventUtcOffset, + bookerUtcOffset, }: Pick< EventType, "periodType" | "periodDays" | "periodCountCalendarDays" | "periodStartDate" | "periodEndDate" > & { - utcOffset: number; + eventUtcOffset: number; + bookerUtcOffset: number; }, minimumBookingNotice?: number ) { @@ -264,9 +298,10 @@ export default function isOutOfBounds( periodStartDate, periodEndDate, // Temporary till we find a way to provide allDatesWithBookabilityStatus in handleNewBooking without re-computing availability for the booked timeslot - allDatesWithBookabilityStatus: null, + allDatesWithBookabilityStatusInBookerTz: null, _skipRollingWindowCheck: true, - utcOffset, + eventUtcOffset, + bookerUtcOffset, }), }) ); diff --git a/packages/trpc/server/routers/viewer/slots/util.ts b/packages/trpc/server/routers/viewer/slots/util.ts index d8e2571813..f1e724f95b 100644 --- a/packages/trpc/server/routers/viewer/slots/util.ts +++ b/packages/trpc/server/routers/viewer/slots/util.ts @@ -618,6 +618,11 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro eventType.schedulingType === SchedulingType.ROUND_ROBIN || allUsersAvailability.length > 1; + // timeZone isn't directly set on eventType now(So, it is legacy) + // schedule is always expected to be set for an eventType now so it must never fallback to allUsersAvailability[0].timeZone(fallback is again legacy behavior) + // TODO: Also, handleNewBooking only seems to be using eventType?.schedule?.timeZone which seems to confirm that we should simplify it as well. + const eventTimeZone = + eventType.timeZone || eventType?.schedule?.timeZone || allUsersAvailability?.[0]?.timeZone; const timeSlots = getSlots({ inviteeDate: startTime, eventLength: input.duration || eventType.length, @@ -625,8 +630,7 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro dateRanges: aggregatedAvailability, minimumBookingNotice: eventType.minimumBookingNotice, frequency: eventType.slotInterval || input.duration || eventType.length, - organizerTimeZone: - eventType.timeZone || eventType?.schedule?.timeZone || allUsersAvailability?.[0]?.timeZone, + organizerTimeZone: eventTimeZone, datesOutOfOffice: !isTeamEvent ? allUsersAvailability[0]?.datesOutOfOffice : undefined, }); @@ -765,15 +769,17 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro const allDatesWithBookabilityStatus = getAllDatesWithBookabilityStatus(availableDates); loggerWithEventDetails.debug(safeStringify({ availableDates })); - const utcOffset = input.timeZone ? getUTCOffsetByTimezone(input.timeZone) ?? 0 : 0; + const eventUtcOffset = getUTCOffsetByTimezone(eventTimeZone) ?? 0; + const bookerUtcOffset = input.timeZone ? getUTCOffsetByTimezone(input.timeZone) ?? 0 : 0; const periodLimits = calculatePeriodLimits({ periodType: eventType.periodType, periodDays: eventType.periodDays, periodCountCalendarDays: eventType.periodCountCalendarDays, periodStartDate: eventType.periodStartDate, periodEndDate: eventType.periodEndDate, - allDatesWithBookabilityStatus, - utcOffset, + allDatesWithBookabilityStatusInBookerTz: allDatesWithBookabilityStatus, + eventUtcOffset, + bookerUtcOffset, }); let foundAFutureLimitViolation = false; const withinBoundsSlotsMappedToDate = Object.entries(slotsMappedToDate).reduce(