Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 116 additions & 26 deletions apps/web/modules/event-types/components/tabs/setup/EventSetupTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import type {
SettingsToggleClassNames,
} from "@calcom/features/eventtypes/lib/types";
import type { FormValues, LocationFormValues } from "@calcom/features/eventtypes/lib/types";
import { MAX_EVENT_DURATION_MINUTES, MIN_EVENT_DURATION_MINUTES } from "@calcom/lib/constants";
import {
MAX_EVENT_DURATION_MINUTES,
MAX_MULTI_DAY_EVENT_DURATION_MINUTES,
MIN_EVENT_DURATION_MINUTES,
} from "@calcom/lib/constants";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { md } from "@calcom/lib/markdownIt";
import { slugify } from "@calcom/lib/slugify";
Expand Down Expand Up @@ -77,7 +81,13 @@ export const EventSetupTab = (
);
const [firstRender, setFirstRender] = useState(true);

// Watch form values for multi-day configuration - use watched values as source of truth
const multiDayConfig = formMethods.watch("metadata")?.multiDayConfig;
const multiDayEnabled = multiDayConfig?.enabled ?? false;
const multiDayNumberOfDays = multiDayConfig?.numberOfDays ?? 1;

const seatsEnabled = formMethods.watch("seatsPerTimeSlotEnabled");
const recurringEventEnabled = !!formMethods.watch("recurringEvent");

const multipleDurationOptions = [
5, 10, 15, 20, 25, 30, 45, 50, 60, 75, 80, 90, 120, 150, 180, 240, 300, 360, 420, 480,
Expand Down Expand Up @@ -190,7 +200,19 @@ export const EventSetupTab = (
"border-subtle rounded-lg border p-6",
customClassNames?.durationSection?.container
)}>
{multipleDuration ? (
{multiDayEnabled ? (
<div className="stack-y-4">
<div>
<Label>{t("duration")}</Label>
<div className="text-default mt-2 text-sm">
{t("multi_day_duration_calculated", {
days: multiDayNumberOfDays,
hours: multiDayNumberOfDays * 24,
})}
</div>
</div>
</div>
) : multipleDuration ? (
<div
className={classNames(
"stack-y-6",
Expand Down Expand Up @@ -312,31 +334,99 @@ export const EventSetupTab = (
/>
)}
{!lengthLockedProps.disabled && (
<div className="mt-4! [&_label]:my-1 [&_label]:font-normal">
<SettingsToggle
title={t("allow_multiple_durations")}
checked={multipleDuration !== undefined}
disabled={seatsEnabled}
tooltip={seatsEnabled ? t("seat_options_doesnt_multiple_durations") : undefined}
labelClassName={customClassNames?.durationSection?.selectDurationToggle?.label}
descriptionClassName={customClassNames?.durationSection?.selectDurationToggle?.description}
switchContainerClassName={customClassNames?.durationSection?.selectDurationToggle?.container}
childrenClassName={customClassNames?.durationSection?.selectDurationToggle?.children}
onCheckedChange={() => {
if (multipleDuration !== undefined) {
setMultipleDuration(undefined);
setSelectedMultipleDuration([]);
setDefaultDuration(null);
formMethods.setValue("metadata.multipleDuration", undefined, { shouldDirty: true });
formMethods.setValue("length", eventType.length, { shouldDirty: true });
} else {
setMultipleDuration([]);
formMethods.setValue("metadata.multipleDuration", [], { shouldDirty: true });
formMethods.setValue("length", 0, { shouldDirty: true });
<>
<div className="mt-4! [&_label]:my-1 [&_label]:font-normal">
<SettingsToggle
title={t("allow_multiple_durations")}
checked={multipleDuration !== undefined}
disabled={seatsEnabled || multiDayEnabled}
tooltip={
seatsEnabled
? t("seat_options_doesnt_multiple_durations")
: multiDayEnabled
? t("multi_day_bookings_doesnt_support_multiple_durations")
: undefined
}
}}
/>
</div>
labelClassName={customClassNames?.durationSection?.selectDurationToggle?.label}
descriptionClassName={customClassNames?.durationSection?.selectDurationToggle?.description}
switchContainerClassName={customClassNames?.durationSection?.selectDurationToggle?.container}
childrenClassName={customClassNames?.durationSection?.selectDurationToggle?.children}
onCheckedChange={() => {
if (multipleDuration !== undefined) {
setMultipleDuration(undefined);
setSelectedMultipleDuration([]);
setDefaultDuration(null);
formMethods.setValue("metadata.multipleDuration", undefined, { shouldDirty: true });
formMethods.setValue("length", eventType.length, { shouldDirty: true });
} else {
setMultipleDuration([]);
formMethods.setValue("metadata.multipleDuration", [], { shouldDirty: true });
formMethods.setValue("length", 0, { shouldDirty: true });
}
}}
/>
</div>
<div className="mt-4! [&_label]:my-1 [&_label]:font-normal">
<SettingsToggle
title={t("enable_multi_day_bookings")}
description={t("multi_day_bookings_description")}
checked={multiDayEnabled}
disabled={seatsEnabled || multipleDuration !== undefined || recurringEventEnabled}
tooltip={
seatsEnabled
? t("seat_options_doesnt_support_multi_day")
: multipleDuration !== undefined
? t("multi_day_bookings_doesnt_support_multiple_durations")
: recurringEventEnabled
? t("multi_day_bookings_doesnt_support_recurring")
: undefined
}
onCheckedChange={(checked) => {
if (checked) {
formMethods.setValue(
"metadata.multiDayConfig",
{
enabled: true,
numberOfDays: multiDayNumberOfDays,
},
{ shouldDirty: true }
);
// Set duration to number of days * 24 hours * 60 minutes
formMethods.setValue("length", multiDayNumberOfDays * 24 * 60, { shouldDirty: true });
} else {
formMethods.setValue("metadata.multiDayConfig", undefined, { shouldDirty: true });
formMethods.setValue("length", eventType.length, { shouldDirty: true });
}
}}>
{multiDayEnabled && (
<div className="mt-4">
<TextField
type="number"
label={t("number_of_days")}
value={multiDayNumberOfDays}
onChange={(e) => {
const days = parseInt(e.target.value) || 1;
const clampedDays = Math.min(Math.max(days, 1), 30);
formMethods.setValue(
"metadata.multiDayConfig",
{
enabled: true,
numberOfDays: clampedDays,
},
{ shouldDirty: true }
);
// Update duration to reflect number of days
formMethods.setValue("length", clampedDays * 24 * 60, { shouldDirty: true });
}}
min={1}
max={30}
addOnSuffix={<>{t("days")}</>}
/>
</div>
)}
</SettingsToggle>
</div>
</>
)}
</div>
<div
Expand Down
8 changes: 8 additions & 0 deletions apps/web/public/static/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,14 @@
"you_cannot_see_team_members": "You cannot see all the team members of a private team.",
"you_cannot_see_teams_of_org": "You cannot see teams of a private organization.",
"allow_multiple_durations": "Allow multiple durations",
"enable_multi_day_bookings": "Enable multi-day bookings",
"multi_day_bookings_description": "Allow bookings that span multiple consecutive days",
"number_of_days": "Number of days",
"days": "days",
"multi_day_duration_calculated": "Duration: {{days}} day(s) ({{hours}} hours)",
"multi_day_bookings_doesnt_support_multiple_durations": "Multi-day bookings cannot be used with multiple durations",
"multi_day_bookings_doesnt_support_recurring": "Multi-day bookings are not compatible with recurring events",
"seat_options_doesnt_support_multi_day": "Multi-day bookings are not compatible with seat options",
"impersonate_user_tip": "All uses of this feature is audited.",
"impersonating_user_warning": "Impersonating username \"{{user}}\".",
"impersonating_stop_instructions": "Click here to stop",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import { describe, expect, it, vi } from "vitest";
import dayjs from "@calcom/dayjs";
import { HttpError } from "@calcom/lib/http-error";
import { validateEventLength } from "./validateEventLength";

describe("validateEventLength", () => {
const mockLogger = {
warn: vi.fn(),
info: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
} as any;

beforeEach(() => {
vi.clearAllMocks();
});

describe("Single Duration Mode", () => {
it("should accept booking with correct single duration", () => {
const start = dayjs().toISOString();
const end = dayjs().add(30, "minutes").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeLength: 30,
logger: mockLogger,
});
}).not.toThrow();
});

it("should reject booking with incorrect duration", () => {
const start = dayjs().toISOString();
const end = dayjs().add(45, "minutes").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeLength: 30,
logger: mockLogger,
});
}).toThrow(HttpError);
});
});

describe("Multiple Duration Mode", () => {
it("should accept booking with one of the multiple duration options", () => {
const start = dayjs().toISOString();
const end = dayjs().add(45, "minutes").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeMultipleDuration: [15, 30, 45, 60],
eventTypeLength: 30,
logger: mockLogger,
});
}).not.toThrow();
});

it("should reject booking with duration not in multiple duration options", () => {
const start = dayjs().toISOString();
const end = dayjs().add(90, "minutes").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeMultipleDuration: [15, 30, 45, 60],
eventTypeLength: 30,
logger: mockLogger,
});
}).toThrow(HttpError);
});
});

describe("Multi-Day Mode", () => {
it("should accept booking with correct multi-day duration (1 day)", () => {
const start = dayjs().toISOString();
const end = dayjs().add(1, "day").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeLength: 1440,
eventTypeMultiDayConfig: {
enabled: true,
numberOfDays: 1,
},
logger: mockLogger,
});
}).not.toThrow();
});

it("should accept booking with correct multi-day duration (3 days)", () => {
const start = dayjs().toISOString();
const end = dayjs().add(3, "days").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeLength: 4320,
eventTypeMultiDayConfig: {
enabled: true,
numberOfDays: 3,
},
logger: mockLogger,
});
}).not.toThrow();
});

it("should reject booking with incorrect multi-day duration", () => {
const start = dayjs().toISOString();
const end = dayjs().add(2, "days").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeLength: 4320,
eventTypeMultiDayConfig: {
enabled: true,
numberOfDays: 3,
},
logger: mockLogger,
});
}).toThrow(HttpError);
});

it("should accept booking with maximum multi-day duration (30 days)", () => {
const start = dayjs().toISOString();
const end = dayjs().add(30, "days").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeLength: 43200,
eventTypeMultiDayConfig: {
enabled: true,
numberOfDays: 30,
},
logger: mockLogger,
});
}).not.toThrow();
});

it("should prioritize multi-day config over multiple durations", () => {
const start = dayjs().toISOString();
const end = dayjs().add(2, "days").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeMultipleDuration: [30, 60, 90],
eventTypeLength: 30,
eventTypeMultiDayConfig: {
enabled: true,
numberOfDays: 2,
},
logger: mockLogger,
});
}).not.toThrow();
});
});

describe("Error Handling", () => {
it("should log warning when validation fails", () => {
const start = dayjs().toISOString();
const end = dayjs().add(45, "minutes").toISOString();

expect(() => {
validateEventLength({
reqBodyStart: start,
reqBodyEnd: end,
eventTypeLength: 30,
logger: mockLogger,
});
}).toThrow();

expect(mockLogger.warn).toHaveBeenCalledWith({
message: "NewBooking: Invalid event length",
});
});
});
});
Loading