feat: date range overhaul (#9802)
Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: Carina Wollendorfer <[email protected]> Co-authored-by: zomars <[email protected]> Co-authored-by: Hariom Balhara <[email protected]> Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
10 files changed
+793
-179
No files matched your search
@@ -0,0 +1,201 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
|
||||
import { buildDateRanges, processDateOverride, processWorkingHours, subtract } from "./date-ranges";
|
||||
|
||||
describe("processWorkingHours", () => {
|
||||
it("should return the correct working hours given a specific availability, timezone, and date range", () => {
|
||||
const item = {
|
||||
days: [1, 2, 3, 4, 5], // Monday to Friday
|
||||
startTime: new Date(Date.UTC(2023, 5, 12, 8, 0)), // 8 AM
|
||||
endTime: new Date(Date.UTC(2023, 5, 12, 17, 0)), // 5 PM
|
||||
};
|
||||
|
||||
const timeZone = "America/New_York";
|
||||
const dateFrom = dayjs.utc().startOf("day").day(2).add(1, "week");
|
||||
const dateTo = dayjs.utc().endOf("day").day(3).add(1, "week");
|
||||
|
||||
const results = processWorkingHours({ item, timeZone, dateFrom, dateTo });
|
||||
|
||||
expect(results.length).toBe(2); // There should be two working days between the range
|
||||
// "America/New_York" day shifts -1, so we need to add a day to correct this shift.
|
||||
expect(results[0]).toEqual({
|
||||
start: dayjs(`${dateFrom.tz(timeZone).add(1, "day").format("YYYY-MM-DD")}T12:00:00Z`).tz(timeZone),
|
||||
end: dayjs(`${dateFrom.tz(timeZone).add(1, "day").format("YYYY-MM-DD")}T21:00:00Z`).tz(timeZone),
|
||||
});
|
||||
expect(results[1]).toEqual({
|
||||
start: dayjs(`${dateTo.tz(timeZone).format("YYYY-MM-DD")}T12:00:00Z`).tz(timeZone),
|
||||
end: dayjs(`${dateTo.tz(timeZone).format("YYYY-MM-DD")}T21:00:00Z`).tz(timeZone),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("processDateOverrides", () => {
|
||||
it("should return the correct date override given a specific availability, timezone, and date", () => {
|
||||
const item = {
|
||||
date: new Date(Date.UTC(2023, 5, 12, 8, 0)),
|
||||
startTime: new Date(Date.UTC(2023, 5, 12, 8, 0)), // 8 AM
|
||||
endTime: new Date(Date.UTC(2023, 5, 12, 17, 0)), // 5 PM
|
||||
};
|
||||
|
||||
// 2023-06-12T20:00:00-04:00 (America/New_York)
|
||||
const timeZone = "America/New_York";
|
||||
|
||||
const result = processDateOverride({ item, timeZone });
|
||||
|
||||
expect(result.start.format()).toEqual(dayjs("2023-06-12T12:00:00Z").tz(timeZone).format());
|
||||
expect(result.end.format()).toEqual(dayjs("2023-06-12T21:00:00Z").tz(timeZone).format());
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildDateRanges", () => {
|
||||
it("should return the correct date ranges", () => {
|
||||
const items = [
|
||||
{
|
||||
date: new Date(Date.UTC(2023, 5, 13)),
|
||||
startTime: new Date(Date.UTC(0, 0, 0, 10, 0)), // 10 AM
|
||||
endTime: new Date(Date.UTC(0, 0, 0, 15, 0)), // 3 PM
|
||||
},
|
||||
{
|
||||
days: [1, 2, 3, 4, 5],
|
||||
startTime: new Date(Date.UTC(2023, 5, 12, 8, 0)), // 8 AM
|
||||
endTime: new Date(Date.UTC(2023, 5, 12, 17, 0)), // 5 PM
|
||||
},
|
||||
];
|
||||
|
||||
const dateFrom = dayjs("2023-06-13T00:00:00Z"); // 2023-06-12T20:00:00-04:00 (America/New_York)
|
||||
const dateTo = dayjs("2023-06-15T00:00:00Z");
|
||||
|
||||
const timeZone = "America/New_York";
|
||||
|
||||
const results = buildDateRanges({ availability: items, timeZone, dateFrom, dateTo });
|
||||
// [
|
||||
// { s: '2023-06-13T10:00:00-04:00', e: '2023-06-13T15:00:00-04:00' },
|
||||
// { s: '2023-06-14T08:00:00-04:00', e: '2023-06-14T17:00:00-04:00' }
|
||||
// ]
|
||||
|
||||
expect(results.length).toBe(2);
|
||||
|
||||
expect(results[0]).toEqual({
|
||||
start: dayjs("2023-06-13T14:00:00Z").tz(timeZone),
|
||||
end: dayjs("2023-06-13T19:00:00Z").tz(timeZone),
|
||||
});
|
||||
|
||||
expect(results[1]).toEqual({
|
||||
start: dayjs("2023-06-14T12:00:00Z").tz(timeZone),
|
||||
end: dayjs("2023-06-14T21:00:00Z").tz(timeZone),
|
||||
});
|
||||
});
|
||||
it("should return correct date ranges with full day unavailable date override", () => {
|
||||
const items = [
|
||||
{
|
||||
date: new Date(Date.UTC(2023, 5, 13)),
|
||||
startTime: new Date(Date.UTC(0, 0, 0, 0, 0)),
|
||||
endTime: new Date(Date.UTC(0, 0, 0, 0, 0)),
|
||||
},
|
||||
{
|
||||
days: [1, 2, 3, 4, 5],
|
||||
startTime: new Date(Date.UTC(2023, 5, 12, 8, 0)),
|
||||
endTime: new Date(Date.UTC(2023, 5, 12, 17, 0)),
|
||||
},
|
||||
];
|
||||
const timeZone = "Europe/London";
|
||||
|
||||
const dateFrom = dayjs("2023-06-13T00:00:00Z");
|
||||
const dateTo = dayjs("2023-06-15T00:00:00Z");
|
||||
|
||||
const results = buildDateRanges({ availability: items, timeZone, dateFrom, dateTo });
|
||||
|
||||
expect(results[0]).toEqual({
|
||||
start: dayjs("2023-06-14T07:00:00Z").tz(timeZone),
|
||||
end: dayjs("2023-06-14T16:00:00Z").tz(timeZone),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("subtract", () => {
|
||||
it("subtracts appropriately when excluded ranges are given in order", () => {
|
||||
const data = {
|
||||
sourceRanges: [
|
||||
{ start: dayjs.utc("2023-07-05T04:00:00.000Z"), end: dayjs.utc("2023-07-05T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-06T04:00:00.000Z"), end: dayjs.utc("2023-07-06T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-07T04:00:00.000Z"), end: dayjs.utc("2023-07-07T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-10T04:00:00.000Z"), end: dayjs.utc("2023-07-10T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-11T04:00:00.000Z"), end: dayjs.utc("2023-07-11T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-12T04:00:00.000Z"), end: dayjs.utc("2023-07-12T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-13T04:00:00.000Z"), end: dayjs.utc("2023-07-13T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-14T04:00:00.000Z"), end: dayjs.utc("2023-07-14T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-17T04:00:00.000Z"), end: dayjs.utc("2023-07-17T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-18T04:00:00.000Z"), end: dayjs.utc("2023-07-18T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-19T04:00:00.000Z"), end: dayjs.utc("2023-07-19T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-20T04:00:00.000Z"), end: dayjs.utc("2023-07-20T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-21T04:00:00.000Z"), end: dayjs.utc("2023-07-21T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-24T04:00:00.000Z"), end: dayjs.utc("2023-07-24T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-25T04:00:00.000Z"), end: dayjs.utc("2023-07-25T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-26T04:00:00.000Z"), end: dayjs.utc("2023-07-26T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-27T04:00:00.000Z"), end: dayjs.utc("2023-07-27T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-28T04:00:00.000Z"), end: dayjs.utc("2023-07-28T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-31T04:00:00.000Z"), end: dayjs.utc("2023-07-31T12:00:00.000Z") },
|
||||
],
|
||||
excludedRanges: [
|
||||
{ start: dayjs.utc("2023-07-05T04:00:00.000Z"), end: dayjs.utc("2023-07-05T04:15:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-05T04:45:00.000Z"), end: dayjs.utc("2023-07-05T05:00:00.000Z") },
|
||||
],
|
||||
};
|
||||
|
||||
const result = subtract(data["sourceRanges"], data["excludedRanges"]).map((range) => ({
|
||||
start: range.start.format(),
|
||||
end: range.end.format(),
|
||||
}));
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ start: "2023-07-05T04:15:00Z", end: "2023-07-05T04:45:00Z" },
|
||||
{ start: "2023-07-05T05:00:00Z", end: "2023-07-05T12:00:00Z" },
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it("subtracts appropriately when excluded ranges are not given in order", () => {
|
||||
const data = {
|
||||
sourceRanges: [
|
||||
{ start: dayjs.utc("2023-07-05T04:00:00.000Z"), end: dayjs.utc("2023-07-05T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-06T04:00:00.000Z"), end: dayjs.utc("2023-07-06T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-07T04:00:00.000Z"), end: dayjs.utc("2023-07-07T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-10T04:00:00.000Z"), end: dayjs.utc("2023-07-10T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-11T04:00:00.000Z"), end: dayjs.utc("2023-07-11T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-12T04:00:00.000Z"), end: dayjs.utc("2023-07-12T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-13T04:00:00.000Z"), end: dayjs.utc("2023-07-13T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-14T04:00:00.000Z"), end: dayjs.utc("2023-07-14T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-17T04:00:00.000Z"), end: dayjs.utc("2023-07-17T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-18T04:00:00.000Z"), end: dayjs.utc("2023-07-18T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-19T04:00:00.000Z"), end: dayjs.utc("2023-07-19T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-20T04:00:00.000Z"), end: dayjs.utc("2023-07-20T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-21T04:00:00.000Z"), end: dayjs.utc("2023-07-21T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-24T04:00:00.000Z"), end: dayjs.utc("2023-07-24T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-25T04:00:00.000Z"), end: dayjs.utc("2023-07-25T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-26T04:00:00.000Z"), end: dayjs.utc("2023-07-26T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-27T04:00:00.000Z"), end: dayjs.utc("2023-07-27T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-28T04:00:00.000Z"), end: dayjs.utc("2023-07-28T12:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-31T04:00:00.000Z"), end: dayjs.utc("2023-07-31T12:00:00.000Z") },
|
||||
],
|
||||
excludedRanges: [
|
||||
{ start: dayjs.utc("2023-07-05T04:45:00.000Z"), end: dayjs.utc("2023-07-05T05:00:00.000Z") },
|
||||
{ start: dayjs.utc("2023-07-05T04:00:00.000Z"), end: dayjs.utc("2023-07-05T04:15:00.000Z") },
|
||||
],
|
||||
};
|
||||
|
||||
const result = subtract(data["sourceRanges"], data["excludedRanges"]).map((range) => ({
|
||||
start: range.start.format(),
|
||||
end: range.end.format(),
|
||||
}));
|
||||
|
||||
expect(result).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ start: "2023-07-05T04:15:00Z", end: "2023-07-05T04:45:00Z" },
|
||||
{ start: "2023-07-05T05:00:00Z", end: "2023-07-05T12:00:00Z" },
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,191 @@
|
||||
import type { Dayjs } from "@calcom/dayjs";
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import type { Availability } from "@calcom/prisma/client";
|
||||
|
||||
export type DateRange = {
|
||||
start: Dayjs;
|
||||
end: Dayjs;
|
||||
};
|
||||
|
||||
export type DateOverride = Pick<Availability, "date" | "startTime" | "endTime">;
|
||||
export type WorkingHours = Pick<Availability, "days" | "startTime" | "endTime">;
|
||||
|
||||
export function processWorkingHours({
|
||||
item,
|
||||
timeZone,
|
||||
dateFrom,
|
||||
dateTo,
|
||||
}: {
|
||||
item: WorkingHours;
|
||||
timeZone: string;
|
||||
dateFrom: Dayjs;
|
||||
dateTo: Dayjs;
|
||||
}) {
|
||||
const results = [];
|
||||
for (let date = dateFrom.tz(timeZone).startOf("day"); dateTo.isAfter(date); date = date.add(1, "day")) {
|
||||
if (!item.days.includes(date.day())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const start = date.hour(item.startTime.getUTCHours()).minute(item.startTime.getUTCMinutes()).second(0);
|
||||
const end = date.hour(item.endTime.getUTCHours()).minute(item.endTime.getUTCMinutes()).second(0);
|
||||
|
||||
const startResult = dayjs.max(start, dateFrom.tz(timeZone));
|
||||
const endResult = dayjs.min(end, dateTo.tz(timeZone));
|
||||
|
||||
if (startResult.isAfter(endResult)) {
|
||||
// if an event ends before start, it's not a result.
|
||||
continue;
|
||||
}
|
||||
|
||||
results.push({
|
||||
start: startResult,
|
||||
end: endResult,
|
||||
});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
export function processDateOverride({ item, timeZone }: { item: DateOverride; timeZone: string }) {
|
||||
const date = dayjs.utc(item.date);
|
||||
|
||||
const startTime = dayjs(item.startTime).utc().subtract(dayjs().tz(timeZone).utcOffset(), "minute");
|
||||
const endTime = dayjs(item.endTime).utc().subtract(dayjs().tz(timeZone).utcOffset(), "minute");
|
||||
|
||||
return {
|
||||
start: date.hour(startTime.hour()).minute(startTime.minute()).second(0).tz(timeZone),
|
||||
end: date.hour(endTime.hour()).minute(endTime.minute()).second(0).tz(timeZone),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildDateRanges({
|
||||
availability,
|
||||
timeZone /* Organizer timeZone */,
|
||||
dateFrom /* Attendee dateFrom */,
|
||||
dateTo /* `` dateTo */,
|
||||
}: {
|
||||
timeZone: string;
|
||||
availability: (DateOverride | WorkingHours)[];
|
||||
dateFrom: Dayjs;
|
||||
dateTo: Dayjs;
|
||||
}): DateRange[] {
|
||||
const groupedWorkingHours = groupByDate(
|
||||
availability.reduce((processed: DateRange[], item) => {
|
||||
if ("days" in item) {
|
||||
processed = processed.concat(processWorkingHours({ item, timeZone, dateFrom, dateTo }));
|
||||
}
|
||||
return processed;
|
||||
}, [])
|
||||
);
|
||||
const groupedDateOverrides = groupByDate(
|
||||
availability.reduce((processed: DateRange[], item) => {
|
||||
if ("date" in item && !!item.date) {
|
||||
processed.push(processDateOverride({ item, timeZone }));
|
||||
}
|
||||
return processed;
|
||||
}, [])
|
||||
);
|
||||
|
||||
const dateRanges = Object.values({
|
||||
...groupedWorkingHours,
|
||||
...groupedDateOverrides,
|
||||
}).map(
|
||||
// remove 0-length overrides that were kept to cancel out working dates until now.
|
||||
(ranges) => ranges.filter((range) => !range.start.isSame(range.end))
|
||||
);
|
||||
|
||||
return dateRanges.flat();
|
||||
}
|
||||
|
||||
export function groupByDate(ranges: DateRange[]): { [x: string]: DateRange[] } {
|
||||
const results = ranges.reduce(
|
||||
(
|
||||
previousValue: {
|
||||
[date: string]: DateRange[];
|
||||
},
|
||||
currentValue
|
||||
) => {
|
||||
const dateString = dayjs.utc(currentValue.start).format("YYYY-MM-DD");
|
||||
|
||||
previousValue[dateString] =
|
||||
typeof previousValue[dateString] === "undefined"
|
||||
? [currentValue]
|
||||
: [...previousValue[dateString], currentValue];
|
||||
return previousValue;
|
||||
},
|
||||
{}
|
||||
);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
export function intersect(ranges: DateRange[][]): DateRange[] {
|
||||
if (!ranges.length) return [];
|
||||
// Get the ranges of the first user
|
||||
let commonAvailability = ranges[0];
|
||||
|
||||
// For each of the remaining users, find the intersection of their ranges with the current common availability
|
||||
for (let i = 1; i < ranges.length; i++) {
|
||||
const userRanges = ranges[i];
|
||||
|
||||
const intersectedRanges: {
|
||||
start: Dayjs;
|
||||
end: Dayjs;
|
||||
}[] = [];
|
||||
|
||||
commonAvailability.forEach((commonRange) => {
|
||||
userRanges.forEach((userRange) => {
|
||||
const intersection = getIntersection(commonRange, userRange);
|
||||
if (intersection !== null) {
|
||||
// If the current common range intersects with the user range, add the intersected time range to the new array
|
||||
intersectedRanges.push(intersection);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
commonAvailability = intersectedRanges;
|
||||
}
|
||||
|
||||
// If the common availability is empty, there is no time when all users are available
|
||||
if (commonAvailability.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return commonAvailability;
|
||||
}
|
||||
|
||||
function getIntersection(range1: DateRange, range2: DateRange) {
|
||||
const start = range1.start.isAfter(range2.start) ? range1.start : range2.start;
|
||||
const end = range1.end.isBefore(range2.end) ? range1.end : range2.end;
|
||||
if (start.isBefore(end)) {
|
||||
return { start, end };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function subtract(sourceRanges: DateRange[], excludedRanges: DateRange[]) {
|
||||
const result: DateRange[] = [];
|
||||
|
||||
for (const { start: sourceStart, end: sourceEnd } of sourceRanges) {
|
||||
let currentStart = sourceStart;
|
||||
|
||||
const overlappingRanges = excludedRanges.filter(
|
||||
({ start, end }) => start.isBefore(sourceEnd) && end.isAfter(sourceStart)
|
||||
);
|
||||
|
||||
overlappingRanges.sort((a, b) => (a.start.isAfter(b.start) ? 1 : -1));
|
||||
|
||||
for (const { start: excludedStart, end: excludedEnd } of overlappingRanges) {
|
||||
if (excludedStart.isAfter(currentStart)) {
|
||||
result.push({ start: currentStart, end: excludedStart });
|
||||
}
|
||||
currentStart = excludedEnd.isAfter(currentStart) ? excludedEnd : currentStart;
|
||||
}
|
||||
|
||||
if (sourceEnd.isAfter(currentStart)) {
|
||||
result.push({ start: currentStart, end: sourceEnd });
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
import { describe, expect, it, beforeAll, vi } from "vitest";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { MINUTES_DAY_END, MINUTES_DAY_START } from "@calcom/lib/availability";
|
||||
|
||||
import type { DateRange } from "./date-ranges";
|
||||
import getSlots from "./slots";
|
||||
|
||||
let dateRangesNextDay: DateRange[];
|
||||
|
||||
let dateRangesMockDay: DateRange[];
|
||||
|
||||
beforeAll(() => {
|
||||
vi.setSystemTime(dayjs.utc("2021-06-20T11:59:59Z").toDate());
|
||||
|
||||
dateRangesMockDay = [{ start: dayjs.utc().startOf("day"), end: dayjs.utc().endOf("day") }];
|
||||
|
||||
dateRangesNextDay = [
|
||||
{
|
||||
start: dayjs.utc().add(1, "day").startOf("day"),
|
||||
end: dayjs.utc().add(1, "day").endOf("day"),
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
describe("Tests the date-range slot logic", () => {
|
||||
it("can fit 24 hourly slots for an empty day", async () => {
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs.utc().add(1, "day"),
|
||||
frequency: 60,
|
||||
minimumBookingNotice: 0,
|
||||
eventLength: 60,
|
||||
organizerTimeZone: "Etc/GMT",
|
||||
dateRanges: dateRangesNextDay,
|
||||
})
|
||||
).toHaveLength(24);
|
||||
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs.utc().add(1, "day"),
|
||||
frequency: 60,
|
||||
minimumBookingNotice: 0,
|
||||
eventLength: 60,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
dateRanges: dateRangesNextDay,
|
||||
})
|
||||
).toHaveLength(24);
|
||||
});
|
||||
|
||||
it("only shows future booking slots on the same day", async () => {
|
||||
// The mock date is 1s to midday, so 12 slots should be open given 0 booking notice.
|
||||
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs.utc(),
|
||||
frequency: 60,
|
||||
minimumBookingNotice: 0,
|
||||
dateRanges: dateRangesMockDay,
|
||||
eventLength: 60,
|
||||
offsetStart: 0,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
})
|
||||
).toHaveLength(12);
|
||||
});
|
||||
|
||||
it("adds minimum booking notice correctly", async () => {
|
||||
// 24h in a day.
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs.utc().add(1, "day").startOf("day"),
|
||||
frequency: 60,
|
||||
minimumBookingNotice: 1500,
|
||||
dateRanges: dateRangesNextDay,
|
||||
eventLength: 60,
|
||||
offsetStart: 0,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
})
|
||||
).toHaveLength(11);
|
||||
});
|
||||
|
||||
it("shows correct time slots for 20 minutes long events with working hours that do not end at a full hour ", async () => {
|
||||
// 72 20-minutes events in a 24h day
|
||||
const result = getSlots({
|
||||
inviteeDate: dayjs().add(1, "day"),
|
||||
frequency: 20,
|
||||
minimumBookingNotice: 0,
|
||||
dateRanges: dateRangesNextDay,
|
||||
eventLength: 20,
|
||||
offsetStart: 0,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
});
|
||||
expect(result).toHaveLength(72);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Tests the slot logic", () => {
|
||||
it("can fit 24 hourly slots for an empty day", async () => {
|
||||
// 24h in a day.
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs.utc().add(1, "day"),
|
||||
frequency: 60,
|
||||
minimumBookingNotice: 0,
|
||||
workingHours: [
|
||||
{
|
||||
userId: 1,
|
||||
days: Array.from(Array(7).keys()),
|
||||
startTime: MINUTES_DAY_START,
|
||||
endTime: MINUTES_DAY_END,
|
||||
},
|
||||
],
|
||||
eventLength: 60,
|
||||
offsetStart: 0,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
})
|
||||
).toHaveLength(24);
|
||||
});
|
||||
|
||||
// TODO: This test is sound; it should pass!
|
||||
it("only shows future booking slots on the same day", async () => {
|
||||
// The mock date is 1s to midday, so 12 slots should be open given 0 booking notice.
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs.utc(),
|
||||
frequency: 60,
|
||||
minimumBookingNotice: 0,
|
||||
workingHours: [
|
||||
{
|
||||
userId: 1,
|
||||
days: Array.from(Array(7).keys()),
|
||||
startTime: MINUTES_DAY_START,
|
||||
endTime: MINUTES_DAY_END,
|
||||
},
|
||||
],
|
||||
eventLength: 60,
|
||||
offsetStart: 0,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
})
|
||||
).toHaveLength(12);
|
||||
});
|
||||
|
||||
it("can cut off dates that due to invitee timezone differences fall on the next day", async () => {
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs().tz("Europe/Amsterdam").startOf("day"), // time translation +01:00
|
||||
frequency: 60,
|
||||
minimumBookingNotice: 0,
|
||||
workingHours: [
|
||||
{
|
||||
userId: 1,
|
||||
days: [0],
|
||||
startTime: 23 * 60, // 23h
|
||||
endTime: MINUTES_DAY_END,
|
||||
},
|
||||
],
|
||||
eventLength: 60,
|
||||
offsetStart: 0,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
})
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("can cut off dates that due to invitee timezone differences fall on the previous day", async () => {
|
||||
const workingHours = [
|
||||
{
|
||||
userId: 1,
|
||||
days: [0],
|
||||
startTime: MINUTES_DAY_START,
|
||||
endTime: 1 * 60, // 1h
|
||||
},
|
||||
];
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs().tz("Atlantic/Cape_Verde").startOf("day"), // time translation -01:00
|
||||
frequency: 60,
|
||||
minimumBookingNotice: 0,
|
||||
workingHours,
|
||||
eventLength: 60,
|
||||
offsetStart: 0,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
})
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("adds minimum booking notice correctly", async () => {
|
||||
// 24h in a day.
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs.utc().add(1, "day").startOf("day"),
|
||||
frequency: 60,
|
||||
minimumBookingNotice: 1500,
|
||||
workingHours: [
|
||||
{
|
||||
userId: 1,
|
||||
days: Array.from(Array(7).keys()),
|
||||
startTime: MINUTES_DAY_START,
|
||||
endTime: MINUTES_DAY_END,
|
||||
},
|
||||
],
|
||||
eventLength: 60,
|
||||
offsetStart: 0,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
})
|
||||
).toHaveLength(11);
|
||||
});
|
||||
|
||||
it("shows correct time slots for 20 minutes long events with working hours that do not end at a full hour", async () => {
|
||||
const result = getSlots({
|
||||
inviteeDate: dayjs().add(1, "day"),
|
||||
frequency: 20,
|
||||
minimumBookingNotice: 0,
|
||||
dateRanges: [{ start: dayjs("2021-06-21T00:00:00.000Z"), end: dayjs("2021-06-21T23:45:00.000Z") }],
|
||||
/*workingHours: [
|
||||
{
|
||||
userId: 1,
|
||||
days: Array.from(Array(7).keys()),
|
||||
startTime: MINUTES_DAY_START,
|
||||
endTime: MINUTES_DAY_END - 14, // 23:45
|
||||
},
|
||||
],*/
|
||||
eventLength: 20,
|
||||
offsetStart: 0,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
});
|
||||
|
||||
// 71 20-minutes events in a 24h - 15m day
|
||||
expect(result).toHaveLength(71);
|
||||
});
|
||||
|
||||
it("can fit 48 25 minute slots with a 5 minute offset for an empty day", async () => {
|
||||
expect(
|
||||
getSlots({
|
||||
inviteeDate: dayjs.utc().add(1, "day"),
|
||||
frequency: 25,
|
||||
minimumBookingNotice: 0,
|
||||
workingHours: [
|
||||
{
|
||||
userId: 1,
|
||||
days: Array.from(Array(7).keys()),
|
||||
startTime: MINUTES_DAY_START,
|
||||
endTime: MINUTES_DAY_END,
|
||||
},
|
||||
],
|
||||
eventLength: 25,
|
||||
offsetStart: 5,
|
||||
organizerTimeZone: "America/Toronto",
|
||||
})
|
||||
).toHaveLength(48);
|
||||
});
|
||||
|
||||
it("tests the final slot of the day is included", async () => {
|
||||
const slots = getSlots({
|
||||
inviteeDate: dayjs.tz("2023-07-13T00:00:00.000+02:00", "Europe/Brussels"),
|
||||
eventLength: 15,
|
||||
workingHours: [
|
||||
{
|
||||
days: [1, 2, 3, 4, 5],
|
||||
startTime: 480,
|
||||
endTime: 960,
|
||||
userId: 9,
|
||||
},
|
||||
{
|
||||
days: [4],
|
||||
startTime: 1170,
|
||||
endTime: 1379,
|
||||
userId: 9,
|
||||
},
|
||||
],
|
||||
dateOverrides: [],
|
||||
offsetStart: 0,
|
||||
dateRanges: [
|
||||
{ start: dayjs("2023-07-13T07:00:00.000Z"), end: dayjs("2023-07-13T15:00:00.000Z") },
|
||||
{ start: dayjs("2023-07-13T18:30:00.000Z"), end: dayjs("2023-07-13T20:59:59.000Z") },
|
||||
],
|
||||
minimumBookingNotice: 120,
|
||||
frequency: 15,
|
||||
organizerTimeZone: "Europe/London",
|
||||
}).reverse();
|
||||
|
||||
expect(slots[0].time.format()).toBe("2023-07-13T22:45:00+02:00");
|
||||
});
|
||||
});
|
||||
+79
-7
@@ -4,15 +4,17 @@ import type { WorkingHours, TimeRange as DateOverride } from "@calcom/types/sche
|
||||
|
||||
import { getWorkingHours } from "./availability";
|
||||
import { getTimeZone } from "./date-fns";
|
||||
import type { DateRange } from "./date-ranges";
|
||||
|
||||
export type GetSlots = {
|
||||
inviteeDate: Dayjs;
|
||||
frequency: number;
|
||||
workingHours: WorkingHours[];
|
||||
workingHours?: WorkingHours[];
|
||||
dateOverrides?: DateOverride[];
|
||||
dateRanges?: DateRange[];
|
||||
minimumBookingNotice: number;
|
||||
eventLength: number;
|
||||
offsetStart: number;
|
||||
offsetStart?: number;
|
||||
organizerTimeZone: string;
|
||||
};
|
||||
export type TimeFrame = { userIds?: number[]; startTime: number; endTime: number };
|
||||
@@ -25,7 +27,7 @@ function buildSlots({
|
||||
computedLocalAvailability,
|
||||
frequency,
|
||||
eventLength,
|
||||
offsetStart,
|
||||
offsetStart = 0,
|
||||
startDate,
|
||||
organizerTimeZone,
|
||||
inviteeTimeZone,
|
||||
@@ -35,7 +37,7 @@ function buildSlots({
|
||||
startDate: Dayjs;
|
||||
frequency: number;
|
||||
eventLength: number;
|
||||
offsetStart: number;
|
||||
offsetStart?: number;
|
||||
organizerTimeZone: string;
|
||||
inviteeTimeZone: string;
|
||||
}) {
|
||||
@@ -133,6 +135,61 @@ function buildSlots({
|
||||
time: getTime(item.startTime),
|
||||
});
|
||||
}
|
||||
|
||||
return slots;
|
||||
}
|
||||
|
||||
function buildSlotsWithDateRanges({
|
||||
dateRanges,
|
||||
frequency,
|
||||
eventLength,
|
||||
timeZone,
|
||||
minimumBookingNotice,
|
||||
organizerTimeZone,
|
||||
offsetStart,
|
||||
}: {
|
||||
dateRanges: DateRange[];
|
||||
frequency: number;
|
||||
eventLength: number;
|
||||
timeZone: string;
|
||||
minimumBookingNotice: number;
|
||||
organizerTimeZone: string;
|
||||
offsetStart?: number;
|
||||
}) {
|
||||
// keep the old safeguards in; may be needed.
|
||||
frequency = minimumOfOne(frequency);
|
||||
eventLength = minimumOfOne(eventLength);
|
||||
offsetStart = offsetStart ? minimumOfOne(offsetStart) : 0;
|
||||
|
||||
const slots: { time: Dayjs; userIds?: number[] }[] = [];
|
||||
dateRanges.forEach((range) => {
|
||||
const startTimeWithMinNotice = dayjs.utc().add(minimumBookingNotice, "minute");
|
||||
|
||||
let slotStartTime = range.start.isAfter(startTimeWithMinNotice) ? range.start : startTimeWithMinNotice;
|
||||
|
||||
slotStartTime =
|
||||
slotStartTime.utc().minute() % 15 !== 0
|
||||
? slotStartTime
|
||||
.startOf("day")
|
||||
.add(slotStartTime.hour() * 60 + Math.ceil(slotStartTime.minute() / 15) * 15, "minute")
|
||||
: slotStartTime;
|
||||
|
||||
// Adding 1 minute to date ranges that end at midnight to ensure that the last slot is included
|
||||
const rangeEnd = range.end
|
||||
.add(dayjs().tz(organizerTimeZone).utcOffset(), "minutes")
|
||||
.isSame(range.end.endOf("day").add(dayjs().tz(organizerTimeZone).utcOffset(), "minutes"), "minute")
|
||||
? range.end.add(1, "minute")
|
||||
: range.end;
|
||||
|
||||
slotStartTime = slotStartTime.add(offsetStart ?? 0, "minutes");
|
||||
while (!slotStartTime.add(eventLength, "minutes").subtract(1, "second").isAfter(rangeEnd)) {
|
||||
slots.push({
|
||||
time: slotStartTime.tz(timeZone),
|
||||
});
|
||||
slotStartTime = slotStartTime.add(frequency + (offsetStart ?? 0), "minutes");
|
||||
}
|
||||
});
|
||||
|
||||
return slots;
|
||||
}
|
||||
|
||||
@@ -146,14 +203,29 @@ const getSlots = ({
|
||||
inviteeDate,
|
||||
frequency,
|
||||
minimumBookingNotice,
|
||||
workingHours,
|
||||
workingHours = [],
|
||||
dateOverrides = [],
|
||||
dateRanges,
|
||||
eventLength,
|
||||
offsetStart,
|
||||
offsetStart = 0,
|
||||
organizerTimeZone,
|
||||
}: GetSlots) => {
|
||||
if (dateRanges) {
|
||||
const slots = buildSlotsWithDateRanges({
|
||||
dateRanges,
|
||||
frequency,
|
||||
eventLength,
|
||||
timeZone: getTimeZone(inviteeDate),
|
||||
minimumBookingNotice,
|
||||
organizerTimeZone,
|
||||
offsetStart,
|
||||
});
|
||||
return slots;
|
||||
}
|
||||
|
||||
// current date in invitee tz
|
||||
const startDate = dayjs().utcOffset(inviteeDate.utcOffset()).add(minimumBookingNotice, "minute");
|
||||
|
||||
// This code is ran client side, startOf() does some conversions based on the
|
||||
// local tz of the client. Sometimes this shifts the day incorrectly.
|
||||
const startOfDayUTC = dayjs.utc().set("hour", 0).set("minute", 0).set("second", 0);
|
||||
@@ -218,7 +290,7 @@ const getSlots = ({
|
||||
return dayjs.utc(override.start).isBetween(startOfInviteeDay, startOfInviteeDay.endOf("day"), null, "[)");
|
||||
});
|
||||
|
||||
if (!!activeOverrides.length) {
|
||||
if (activeOverrides.length) {
|
||||
const overrides = activeOverrides.flatMap((override) => ({
|
||||
userIds: override.userId ? [override.userId] : [],
|
||||
startTime: override.start.getUTCHours() * 60 + override.start.getUTCMinutes(),
|
||||
|
||||
Reference in new issue
Block a user