fix: Stores the DateRange in UTC instead of user machine that caused the bug (#26900)

* fix init

* remove comment

* fix to v2

* type fix

* edge case

* test fix to suit the date storage method

* fix for atoms

* backward compatibility

* fix test mock

* test fix?

* testing a theory

* address cubic
This commit is contained in:
Syed Ali Shahbaz authored and GitHub committed 2026-01-19 10:41:53 +02:00
1 parent 9f0d035a3b
commit c1d0a6bb1b
7 files changed
+647 -89

No files matched your search

@@ -2,44 +2,44 @@ import {
BookerLayoutsInputEnum_2024_06_14,
BookerLayoutsOutputEnum_2024_06_14,
ConfirmationPolicyEnum,
NoticeThresholdUnitEnum,
FrequencyInput,
NoticeThresholdUnitEnum,
} from "@calcom/platform-enums";
import type {
InputBookingField_2024_06_14,
InputLocation_2024_06_14,
BookerLayouts_2024_06_14,
BookingLimitsCount_2024_06_14,
BookingWindow_2024_06_14,
BookerLayouts_2024_06_14,
ConfirmationPolicy_2024_06_14,
EventTypeColor_2024_06_14,
Recurrence_2024_06_14,
CreateEventTypeInput_2024_06_14,
SeatOptionsTransformedSchema,
SeatOptionsDisabledSchema,
EventTypeColor_2024_06_14,
InputAttendeeAddressLocation_2024_06_14,
InputAttendeePhoneLocation_2024_06_14,
InputAttendeeDefinedLocation_2024_06_14,
InputAttendeePhoneLocation_2024_06_14,
InputBookingField_2024_06_14,
InputLocation_2024_06_14,
InputTeamLocation_2024_06_14,
Recurrence_2024_06_14,
SeatOptionsDisabledSchema,
SeatOptionsTransformedSchema,
} from "@calcom/platform-types";
import {
systemBeforeFieldEmail,
systemBeforeFieldName,
type CustomField,
type SystemField,
systemBeforeFieldEmail,
systemBeforeFieldName,
} from "../internal-to-api/booking-fields";
import {
transformLocationsApiToInternal,
transformBookerLayoutsApiToInternal,
transformBookingFieldsApiToInternal,
transformSelectOptionsApiToInternal,
transformIntervalLimitsApiToInternal,
transformConfirmationPolicyApiToInternal,
transformEventColorsApiToInternal,
transformFutureBookingLimitsApiToInternal,
transformIntervalLimitsApiToInternal,
transformLocationsApiToInternal,
transformRecurrenceApiToInternal,
transformSeatsApiToInternal,
transformEventColorsApiToInternal,
transformBookerLayoutsApiToInternal,
transformConfirmationPolicyApiToInternal,
transformSelectOptionsApiToInternal,
transformTeamLocationsApiToInternal,
} from "./index";
@@ -182,10 +182,10 @@ describe("transformBookingFieldsApiToInternal", () => {
disableOnPrefill: true,
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// @ts-expect-error
expectedField.variantsConfig.variants.fullName.fields[0].label = "Your name number";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// @ts-expect-error
expectedField.variantsConfig.variants.fullName.fields[0].placeholder = "123456789";
const expectedOutput: SystemField[] = [expectedField];
@@ -708,6 +708,62 @@ describe("transformFutureBookingLimitsApiToInternal", () => {
expect(result).toEqual(expectedOutput);
});
it("should normalize range type with full ISO strings to UTC midnight (positive offset)", () => {
// Client in Dubai (UTC+4) sends full ISO strings - without normalization this would shift dates
// e.g., "2024-08-06T00:00:00.000+04:00" would become 2024-08-05T20:00:00.000Z
const input: BookingWindow_2024_06_14 = {
type: "range",
value: ["2024-08-06T00:00:00.000+04:00", "2024-08-28T00:00:00.000+04:00"],
};
// Should extract date part and create UTC midnight dates
const expectedOutput = {
periodType: "RANGE",
periodStartDate: new Date("2024-08-06"),
periodEndDate: new Date("2024-08-28"),
};
const result = transformFutureBookingLimitsApiToInternal(input);
expect(result).toEqual(expectedOutput);
});
it("should normalize range type with full ISO strings to UTC midnight (negative offset)", () => {
// Client in Buenos Aires (UTC-3) sends full ISO strings - without normalization this would shift dates
// e.g., "2024-08-06T00:00:00.000-03:00" would become 2024-08-06T03:00:00.000Z (3 hours later)
const input: BookingWindow_2024_06_14 = {
type: "range",
value: ["2024-08-06T00:00:00.000-03:00", "2024-08-28T00:00:00.000-03:00"],
};
// Should extract date part and create UTC midnight dates
const expectedOutput = {
periodType: "RANGE",
periodStartDate: new Date("2024-08-06"),
periodEndDate: new Date("2024-08-28"),
};
const result = transformFutureBookingLimitsApiToInternal(input);
expect(result).toEqual(expectedOutput);
});
it("should normalize range type with full ISO strings to UTC midnight (UTC/Z suffix)", () => {
// Client sends UTC timestamps with Z suffix
const input: BookingWindow_2024_06_14 = {
type: "range",
value: ["2024-08-06T00:00:00.000Z", "2024-08-28T00:00:00.000Z"],
};
// Should extract date part and create UTC midnight dates
const expectedOutput = {
periodType: "RANGE",
periodStartDate: new Date("2024-08-06"),
periodEndDate: new Date("2024-08-28"),
};
const result = transformFutureBookingLimitsApiToInternal(input);
expect(result).toEqual(expectedOutput);
});
it("should transform calendar days", () => {
const input: BookingWindow_2024_06_14 = {
type: "calendarDays",
@@ -3,8 +3,8 @@ import {
BookingWindowPeriodOutputTypeEnum_2024_06_14,
} from "@calcom/platform-enums";
import {
type CreateEventTypeInput_2024_06_14,
type BusinessDaysWindow_2024_06_14,
type CreateEventTypeInput_2024_06_14,
type RangeWindow_2024_06_14,
type TransformFutureBookingsLimitSchema_2024_06_14,
} from "@calcom/platform-types";
@@ -24,7 +24,7 @@ export function transformFutureBookingLimitsApiToInternal(
case BookingWindowPeriodInputTypeEnum_2024_06_14.businessDays:
return {
periodDays: (inputBookingLimits as BusinessDaysWindow_2024_06_14).value,
periodType: !!(inputBookingLimits as BusinessDaysWindow_2024_06_14).rolling
periodType: (inputBookingLimits as BusinessDaysWindow_2024_06_14).rolling
? BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING_WINDOW
: BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING,
periodCountCalendarDays: false,
@@ -32,17 +32,20 @@ export function transformFutureBookingLimitsApiToInternal(
case BookingWindowPeriodInputTypeEnum_2024_06_14.calendarDays:
return {
periodDays: (inputBookingLimits as BusinessDaysWindow_2024_06_14).value,
periodType: !!(inputBookingLimits as BusinessDaysWindow_2024_06_14).rolling
periodType: (inputBookingLimits as BusinessDaysWindow_2024_06_14).rolling
? BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING_WINDOW
: BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING,
periodCountCalendarDays: true,
};
case BookingWindowPeriodInputTypeEnum_2024_06_14.range:
case BookingWindowPeriodInputTypeEnum_2024_06_14.range: {
const startDateStr = (inputBookingLimits as RangeWindow_2024_06_14).value[0].slice(0, 10);
const endDateStr = (inputBookingLimits as RangeWindow_2024_06_14).value[1].slice(0, 10);
return {
periodType: BookingWindowPeriodOutputTypeEnum_2024_06_14.RANGE,
periodStartDate: new Date((inputBookingLimits as RangeWindow_2024_06_14).value[0]),
periodEndDate: new Date((inputBookingLimits as RangeWindow_2024_06_14).value[1]),
periodStartDate: new Date(startDateStr),
periodEndDate: new Date(endDateStr),
};
}
default:
return undefined;
}
@@ -45,6 +45,25 @@ type EnabledAppType = App & {
locationOption: LocationOption | null;
};
/**
* Normalizes a period date to UTC midnight.
* Atoms receives JSON where dates are strings (e.g., "2024-01-20T00:00:00.000Z" or "2024-01-20"),
* but TypeScript types them as Date. This function handles both cases.
* We extract the date part (YYYY-MM-DD) to avoid timezone shifts.
*/
function normalizePeriodDate(date: Date | string | null | undefined): Date | null | undefined {
if (date === undefined) return undefined;
if (date === null) return null;
// Handle both string (from JSON) and Date object (if already parsed)
const dateStr = typeof date === "string" ? date : date.toISOString();
// Extract the date part (first 10 chars: YYYY-MM-DD) to avoid timezone shifts
// e.g., "2024-01-20T00:00:00.000+04:00" -> "2024-01-20" -> UTC midnight Jan 20
const dateOnly = dateStr.slice(0, 10);
return new Date(dateOnly);
}
@Injectable()
export class EventTypesAtomService {
constructor(
@@ -134,8 +153,21 @@ export class EventTypesAtomService {
bookingFields.push(systemBeforeFieldEmail);
}
// Normalize period dates to UTC midnight (only if provided)
const periodDates =
body.periodStartDate !== undefined || body.periodEndDate !== undefined
? {
...(body.periodStartDate !== undefined
? { periodStartDate: normalizePeriodDate(body.periodStartDate) }
: {}),
...(body.periodEndDate !== undefined
? { periodEndDate: normalizePeriodDate(body.periodEndDate) }
: {}),
}
: {};
const eventType = await updateEventType({
input: { ...body, id: eventTypeId, bookingFields },
input: { ...body, id: eventTypeId, bookingFields, ...periodDates },
ctx: {
user: eventTypeUser,
prisma: this.dbWrite.prisma,
@@ -162,8 +194,21 @@ export class EventTypesAtomService {
bookingFields.push(systemBeforeFieldEmail);
}
// Normalize period dates to UTC midnight (only if provided)
const periodDates =
body.periodStartDate !== undefined || body.periodEndDate !== undefined
? {
...(body.periodStartDate !== undefined
? { periodStartDate: normalizePeriodDate(body.periodStartDate) }
: {}),
...(body.periodEndDate !== undefined
? { periodEndDate: normalizePeriodDate(body.periodEndDate) }
: {}),
}
: {};
const eventType = await updateEventType({
input: { ...body, id: eventTypeId, bookingFields },
input: { ...body, id: eventTypeId, bookingFields, ...periodDates },
ctx: {
user: eventTypeUser,
prisma: this.dbWrite.prisma,
@@ -1,16 +1,12 @@
import { useAutoAnimate } from "@formkit/auto-animate/react";
import * as RadioGroup from "@radix-ui/react-radio-group";
import type { Key } from "react";
import React, { useEffect, useState } from "react";
import type { UseFormRegisterReturn, UseFormReturn } from "react-hook-form";
import { Controller, useFormContext } from "react-hook-form";
import type { SingleValue } from "react-select";
import useLockedFieldsManager from "@calcom/features/ee/managed-event-types/hooks/useLockedFieldsManager";
import { LearnMoreLink } from "@calcom/web/modules/event-types/components/LearnMoreLink";
import { getDefinedBufferTimes } from "@calcom/features/eventtypes/lib/getDefinedBufferTimes";
import type { FormValues, EventTypeSetupProps, InputClassNames } from "@calcom/features/eventtypes/lib/types";
import type { SelectClassNames, SettingsToggleClassNames } from "@calcom/features/eventtypes/lib/types";
import type {
EventTypeSetupProps,
FormValues,
InputClassNames,
SelectClassNames,
SettingsToggleClassNames,
} from "@calcom/features/eventtypes/lib/types";
import CheckboxField from "@calcom/features/form/components/CheckboxField";
import { ROLLING_WINDOW_PERIOD_MAX_DAYS_TO_CHECK } from "@calcom/lib/constants";
import type { DurationType } from "@calcom/lib/convertToNewDurationType";
@@ -23,15 +19,23 @@ import { PeriodType } from "@calcom/prisma/enums";
import classNames from "@calcom/ui/classNames";
import { Button } from "@calcom/ui/components/button";
import {
InputField,
DateRangePicker,
InputField,
Label,
TextField,
Select,
SettingsToggle,
TextField,
} from "@calcom/ui/components/form";
import { Icon } from "@calcom/ui/components/icon";
import { Tooltip } from "@calcom/ui/components/tooltip";
import { LearnMoreLink } from "@calcom/web/modules/event-types/components/LearnMoreLink";
import { useAutoAnimate } from "@formkit/auto-animate/react";
import * as RadioGroup from "@radix-ui/react-radio-group";
import type { Key } from "react";
import React, { useEffect, useState } from "react";
import type { UseFormRegisterReturn, UseFormReturn } from "react-hook-form";
import { Controller, useFormContext } from "react-hook-form";
import type { SingleValue } from "react-select";
import MaxActiveBookingsPerBookerController from "./MaxActiveBookingsPerBookerController";
@@ -158,9 +162,14 @@ function RangeLimitRadioItem({
}}
disabled={isDisabled}
onDatesChange={({ startDate, endDate }) => {
const toUTCMidnight = (date: Date | undefined): Date | undefined => {
if (!date) return undefined;
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
};
onChange({
startDate,
endDate,
startDate: toUTCMidnight(startDate),
endDate: toUTCMidnight(endDate),
});
}}
className={customClassNames?.datePicker}
@@ -965,9 +974,8 @@ export const IntervalLimitsManager = <K extends "durationLimits" | "bookingLimit
setValue(
propertyName,
// TODO: Remove @ts-ignore, type error no longer exists in later TS versions.
// TODO: Remove @ts-expect-error, type error no longer exists in later TS versions.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
{
...watchIntervalLimits,
[rest.value]: defaultLimit,
@@ -1,8 +1,8 @@
import {
TestData,
Timezones,
createBookingScenario,
replaceDates,
TestData,
Timezones,
} from "@calcom/testing/lib/bookingScenario/bookingScenario";
import type { ScenarioData } from "@calcom/testing/lib/bookingScenario/bookingScenario";
@@ -1489,13 +1489,12 @@ describe("getSchedule", () => {
{
id: 1,
length: 60,
// Makes today and tomorrow only available
// Makes plus1 and plus2 dates only available (June 1-2 in event timezone)
// Period dates are now stored as UTC midnight for the selected date
...getPeriodTypeData({
type: "RANGE",
// dayPlus1InIst
periodStartDate: new Date(`${todayDateString}T18:30:00.000Z`),
// datePlus2InIst
periodEndDate: new Date(`${plus1DateString}T18:30:00.000Z`),
periodStartDate: new Date(`${plus1DateString}T00:00:00.000Z`), // June 1 UTC midnight
periodEndDate: new Date(`${plus2DateString}T00:00:00.000Z`), // June 2 UTC midnight
periodCountCalendarDays: true,
}),
users: [
@@ -1574,14 +1573,12 @@ describe("getSchedule", () => {
{
id: 1,
length: 60,
// Makes today and tomorrow only available
// Makes 25th and 26th July (in event timezone) available
// Period dates are now stored as UTC midnight for the selected date
...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
periodStartDate: new Date(`2024-07-25T00:00:00.000Z`), // July 25 UTC midnight
periodEndDate: new Date(`2024-07-26T00:00:00.000Z`), // July 26 UTC midnight
periodCountCalendarDays: true,
}),
users: [
@@ -1614,23 +1611,17 @@ describe("getSchedule", () => {
orgSlug: null,
},
});
expect(scheduleForEventForPagoTz).toHaveDateDisabled({
dateString: "2024-07-21",
});
/**
* 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-22",
});
expect(scheduleForEventForPagoTz).toHaveDateDisabled({
dateString: "2024-07-23",
});
}
expect(scheduleForEventForPagoTz).toHaveDateDisabled({
dateString: "2024-07-23",
});
expect(scheduleForEventForPagoTz).toHaveTimeSlots(
[
@@ -1670,19 +1661,13 @@ describe("getSchedule", () => {
dateString: "2024-07-26",
doExactMatch: true,
});
expect(scheduleForEventForPagoTz).toHaveDateDisabled({
dateString: "2024-07-27",
});
/**
* Verify that timeslots beyond 26th July are disabled
*/
{
expect(scheduleForEventForPagoTz).toHaveDateDisabled({
dateString: "2024-07-27",
});
expect(scheduleForEventForPagoTz).toHaveDateDisabled({
dateString: "2024-07-28",
});
}
expect(scheduleForEventForPagoTz).toHaveDateDisabled({
dateString: "2024-07-28",
});
});
});
});