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:
1 parent
9f0d035a3b
commit
c1d0a6bb1b
7 files changed
+647
-89
No files matched your search
+75
-19
@@ -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",
|
||||
|
||||
+9
-6
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user