refactor: Remove circular deps between @calcom/lib and @calcom/features [2] (#24438)

* move SystemField to features

* migrate workflow service

* merge two tests for team repository

* update imports and migrate team repository

* migrate delegation credential repository

* migrate credential repository

* migrate entityPermissionUtils

* migrate hashedLink service and repository

* migrate membership service

* update imports

* remove file

* migrate buildEventUrlFromBooking

* migrate getAllUserBookings to features

* update imports

* update organizationMock

* migrate slots

* migrate date-ranges to schedules dir

* migrate getAggregatedAvailability

* fix

* refactor

* migrate useCreateEventType hook to features

* migrate assignValueToUser

* migrate validateUsername to auth features

* migrate system field back to lib

* migrate getLabelValueMapFromResponses back to lib

* update imports

* use relative path

* fix type checks

* fix

* fix

* fix tests

* update gh codeowners

* fix

* fix
This commit is contained in:
Benny Joo authored and GitHub committed 2025-10-17 06:48:08 -03:00
1 parent 79b319c1dd
commit e91bf53d80
145 files changed
+412 -405

No files matched your search

@@ -0,0 +1,142 @@
import { describe, it, expect } from "vitest";
import dayjs from "@calcom/dayjs";
import { filterRedundantDateRanges } from "./filterRedundantDateRanges";
describe("filterRedundantDateRanges", () => {
it("should remove date ranges that are completely contained within others", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
{ start: dayjs("2025-01-23T11:15:00.000Z"), end: dayjs("2025-01-23T11:45:00.000Z") }, // Contained within the first range
{ start: dayjs("2025-01-23T13:00:00.000Z"), end: dayjs("2025-01-23T14:00:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(2);
expect(result[0].start.format()).toEqual(dayjs("2025-01-23T11:00:00.000Z").format());
expect(result[0].end.format()).toEqual(dayjs("2025-01-23T12:00:00.000Z").format());
expect(result[1].start.format()).toEqual(dayjs("2025-01-23T13:00:00.000Z").format());
expect(result[1].end.format()).toEqual(dayjs("2025-01-23T14:00:00.000Z").format());
});
it("should not remove overlapping ranges that aren't completely contained", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
{ start: dayjs("2025-01-23T11:30:00.000Z"), end: dayjs("2025-01-23T12:30:00.000Z") }, // Overlaps but not contained
{ start: dayjs("2025-01-23T13:00:00.000Z"), end: dayjs("2025-01-23T14:00:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(3);
});
it("should return an empty array when given an empty array", () => {
expect(filterRedundantDateRanges([])).toEqual([]);
});
it("should return the same array when there's only one range", () => {
const dateRanges = [{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") }];
expect(filterRedundantDateRanges(dateRanges)).toEqual(dateRanges);
});
it("should handle three identical ranges", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(1);
expect(result[0].start.format()).toEqual(dayjs("2025-01-23T11:00:00.000Z").format());
expect(result[0].end.format()).toEqual(dayjs("2025-01-23T12:00:00.000Z").format());
});
it("should handle nested containment scenario", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T10:00:00.000Z"), end: dayjs("2025-01-23T14:00:00.000Z") },
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
{ start: dayjs("2025-01-23T11:30:00.000Z"), end: dayjs("2025-01-23T12:30:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(1);
expect(result[0].start.format()).toEqual(dayjs("2025-01-23T10:00:00.000Z").format());
expect(result[0].end.format()).toEqual(dayjs("2025-01-23T14:00:00.000Z").format());
});
it("should handle ranges with same start time", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T14:00:00.000Z") },
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(1);
expect(result[0].end.format()).toEqual(dayjs("2025-01-23T14:00:00.000Z").format());
});
it("should handle ranges with same end time", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T09:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
{ start: dayjs("2025-01-23T10:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(1);
expect(result[0].start.format()).toEqual(dayjs("2025-01-23T09:00:00.000Z").format());
});
it("should preserve invalid ranges", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T12:00:00.000Z"), end: dayjs("2025-01-23T11:00:00.000Z") },
{ start: dayjs("2025-01-23T10:00:00.000Z"), end: dayjs("2025-01-23T14:00:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(2);
});
it("should handle touching ranges", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T10:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
{ start: dayjs("2025-01-23T12:00:00.000Z"), end: dayjs("2025-01-23T14:00:00.000Z") },
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(2);
});
it("should handle overlapping but non-containing ranges", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T10:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(2);
expect(result[0].start.format()).toEqual(dayjs("2025-01-23T10:00:00.000Z").format());
expect(result[1].start.format()).toEqual(dayjs("2025-01-23T11:00:00.000Z").format());
});
it("should handle complex overlapping pattern that exposed cubic-dev-ai bug", () => {
const dateRanges = [
{ start: dayjs("2025-01-23T10:00:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
{ start: dayjs("2025-01-23T09:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
];
const result = filterRedundantDateRanges(dateRanges);
expect(result.length).toBe(3);
expect(result[0].start.format()).toEqual(dayjs("2025-01-23T09:00:00.000Z").format());
expect(result[1].start.format()).toEqual(dayjs("2025-01-23T10:00:00.000Z").format());
expect(result[2].start.format()).toEqual(dayjs("2025-01-23T11:00:00.000Z").format());
});
});
@@ -0,0 +1,50 @@
import type { DateRange } from "@calcom/features/schedules/lib/date-ranges";
import { IntervalTree, ContainmentSearchAlgorithm, createIntervalNodes } from "@calcom/lib/intervalTree";
/**
* Filters out date ranges that are completely covered by other date ranges.
* Uses an interval tree for O(n log n) worst-case complexity.
* Unlike mergeOverlappingDateRanges, this doesn't merge overlapping ranges,
* it only removes ranges that are completely contained within others.
*/
export function filterRedundantDateRanges(dateRanges: DateRange[]): DateRange[] {
if (dateRanges.length <= 1) return dateRanges;
const sortedRanges = [...dateRanges].sort((a, b) => a.start.valueOf() - b.start.valueOf());
const intervalNodes = createIntervalNodes(
sortedRanges,
(range) => range.start.valueOf(),
(range) => range.end.valueOf()
);
const intervalTree = new IntervalTree(intervalNodes);
const searchAlgorithm = new ContainmentSearchAlgorithm(intervalTree);
return sortedRanges.filter((range, index) => {
if (range.end.valueOf() < range.start.valueOf()) {
return true;
}
const containingIntervals = searchAlgorithm.findContainingIntervals(
range.start.valueOf(),
range.end.valueOf(),
index
);
for (const containingNode of containingIntervals) {
const otherRange = containingNode.item;
const otherIndex = containingNode.index;
if (
otherRange.start.valueOf() === range.start.valueOf() &&
otherRange.end.valueOf() === range.end.valueOf()
) {
return otherIndex > index; // Keep current range only if other range has higher index
}
// If we reach here, the other range actually contains this range
return false;
}
return true; // Keep this range
});
}
@@ -0,0 +1,62 @@
import { describe, it, expect } from "vitest";
import dayjs from "@calcom/dayjs";
import type { DateRange } from "@calcom/features/schedules/lib/date-ranges";
import { mergeOverlappingDateRanges } from "./mergeOverlappingDateRanges";
const november2 = "2023-11-02";
const november3 = "2023-11-03";
describe("mergeOverlappingDateRanges", () => {
it("should merge all ranges into one when one range includes all others", () => {
const dateRanges = [
createDateRange(`${november2}T23:00:00.000Z`, `${november3}T07:00:00.000Z`), // Includes all others
createDateRange(`${november2}T23:15:00.000Z`, `${november3}T00:00:00.000Z`),
createDateRange(`${november3}T00:15:00.000Z`, `${november3}T01:00:00.000Z`),
createDateRange(`${november3}T01:15:00.000Z`, `${november3}T02:00:00.000Z`),
];
const mergedRanges = mergeOverlappingDateRanges(dateRanges);
expect(mergedRanges).toHaveLength(1);
expect(mergedRanges[0].start.isSame(dayjs(dateRanges[0].start))).toBe(true);
expect(mergedRanges[0].end.isSame(dayjs(dateRanges[0].end))).toBe(true);
});
it("should merge only overlapping ranges over 2 days and leave non-overlapping ranges as is", () => {
const dateRanges = [
createDateRange(`${november2}T23:00:00.000Z`, `${november3}T07:00:00.000Z`),
createDateRange(`${november3}T05:00:00.000Z`, `${november3}T06:00:00.000Z`),
createDateRange(`${november3}T08:00:00.000Z`, `${november3}T10:00:00.000Z`), // This range should not be merged
];
const mergedRanges = mergeOverlappingDateRanges(dateRanges);
expect(mergedRanges).toHaveLength(2);
expect(mergedRanges[0].start.isSame(dayjs(dateRanges[0].start))).toBe(true);
expect(mergedRanges[0].end.isSame(dayjs(dateRanges[0].end))).toBe(true);
expect(mergedRanges[1].start.isSame(dayjs(dateRanges[2].start))).toBe(true);
expect(mergedRanges[1].end.isSame(dayjs(dateRanges[2].end))).toBe(true);
});
it("should merge ranges that overlap on the same day", () => {
const dateRanges = [
createDateRange(`${november2}T01:00:00.000Z`, `${november2}T04:00:00.000Z`),
createDateRange(`${november2}T02:00:00.000Z`, `${november2}T03:00:00.000Z`), // This overlaps with the first range
createDateRange(`${november2}T05:00:00.000Z`, `${november2}T06:00:00.000Z`), // This doesn't overlap with above
];
const mergedRanges = mergeOverlappingDateRanges(dateRanges);
expect(mergedRanges).toHaveLength(2);
expect(mergedRanges[0].start.isSame(dayjs(dateRanges[0].start))).toBe(true);
expect(mergedRanges[0].end.isSame(dayjs(dateRanges[0].end))).toBe(true);
expect(mergedRanges[1].start.isSame(dayjs(dateRanges[2].start))).toBe(true);
expect(mergedRanges[1].end.isSame(dayjs(dateRanges[2].end))).toBe(true);
});
});
function createDateRange(start: string, end: string): DateRange {
return {
start: dayjs(start),
end: dayjs(end),
};
}
@@ -0,0 +1,36 @@
import type { DateRange } from "@calcom/features/schedules/lib/date-ranges";
export function mergeOverlappingDateRanges(dateRanges: DateRange[]) {
dateRanges.sort((a, b) => a.start.valueOf() - b.start.valueOf());
const mergedDateRanges: DateRange[] = [];
let currentRange = dateRanges[0];
if (!currentRange) {
return [];
}
for (let i = 1; i < dateRanges.length; i++) {
const nextRange = dateRanges[i];
if (isCurrentRangeOverlappingNext(currentRange, nextRange)) {
currentRange = {
start: currentRange.start,
end: currentRange.end.valueOf() > nextRange.end.valueOf() ? currentRange.end : nextRange.end,
};
} else {
mergedDateRanges.push(currentRange);
currentRange = nextRange;
}
}
mergedDateRanges.push(currentRange);
return mergedDateRanges;
}
function isCurrentRangeOverlappingNext(currentRange: DateRange, nextRange: DateRange): boolean {
return (
currentRange.start.valueOf() <= nextRange.start.valueOf() &&
currentRange.end.valueOf() > nextRange.start.valueOf()
);
}
@@ -0,0 +1,472 @@
import { describe, it, expect } from "vitest";
import type { Dayjs } from "@calcom/dayjs";
import dayjs from "@calcom/dayjs";
import { getAggregatedAvailability } from "./getAggregatedAvailability";
// Helper to check if a time range overlaps with availability
const isAvailable = (availability: { start: Dayjs; end: Dayjs }[], range: { start: Dayjs; end: Dayjs }) => {
return availability.some(({ start, end }) => {
return start <= range.start && end >= range.end;
});
};
describe("getAggregatedAvailability", () => {
// rr-host availability used to combine into erroneous slots, this confirms it no longer happens
it("should not merge RR availability resulting in an unavailable slot due to overlap", () => {
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:20:00.000Z") },
{ start: dayjs("2025-01-23T16:10:00.000Z"), end: dayjs("2025-01-23T16:30:00.000Z") },
],
user: { isFixed: false },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:15:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
{ start: dayjs("2025-01-23T13:20:00.000Z"), end: dayjs("2025-01-23T13:30:00.000Z") },
],
user: { isFixed: false },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
const timeRangeToCheckBusy = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:30:00.000Z"),
};
expect(isAvailable(result, timeRangeToCheckBusy)).toBe(false);
const timeRangeToCheckAvailable = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:20:00.000Z"),
};
expect(isAvailable(result, timeRangeToCheckAvailable)).toBe(true);
});
it("it returns the right amount of date ranges even if the end time is before the start time", () => {
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-27T14:00:00.000Z"), end: dayjs("2025-01-27T04:30-05:00") },
],
user: { isFixed: false },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-27T14:00:00.000Z"), end: dayjs("2025-01-27T14:45:00.000Z") },
],
user: { isFixed: false },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
expect(result).toEqual([
{
start: dayjs("2025-01-27T14:00:00.000Z"),
end: dayjs("2025-01-27T09:30:00.000Z"),
},
{
start: dayjs("2025-01-27T14:00:00.000Z"),
end: dayjs("2025-01-27T14:45:00.000Z"),
},
]);
});
// validates fixed host behaviour, they all have to be available
it("correctly joins fixed host availability resulting in one or more combined date ranges", () => {
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:20:00.000Z") },
{ start: dayjs("2025-01-23T16:10:00.000Z"), end: dayjs("2025-01-23T16:30:00.000Z") },
],
user: { isFixed: true },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:15:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
{ start: dayjs("2025-01-23T13:20:00.000Z"), end: dayjs("2025-01-23T13:30:00.000Z") },
],
user: { isFixed: true },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
const timeRangeToCheckBusy = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:30:00.000Z"),
};
expect(isAvailable(result, timeRangeToCheckBusy)).toBe(false);
expect(result[0].start.format()).toEqual(dayjs("2025-01-23T11:15:00.000Z").format());
expect(result[0].end.format()).toEqual(dayjs("2025-01-23T11:20:00.000Z").format());
});
// Combines rr hosts and fixed hosts, both fixed and one of the rr hosts has to be available for the whole period
// All fixed user ranges are merged with each rr-host
it("Fixed hosts and at least one rr host available between 11:00-11:30 & 12:30-13:00 on January 23, 2025", () => {
// Both fixed user A and B are available 11:00-11:30 & 12:30-13:00 & 13:15-13:30
// Only user C (rr) is available 11:00-11:30 and only user D (rr) is available 12:30-13:00
// No rr users are available 13:15-13:30 and this date range should not be a result.
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
{ start: dayjs("2025-01-23T12:30:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
{ start: dayjs("2025-01-23T13:15:00.000Z"), end: dayjs("2025-01-23T13:30:00.000Z") },
{ start: dayjs("2025-01-23T16:10:00.000Z"), end: dayjs("2025-01-23T16:30:00.000Z") },
],
user: { isFixed: true },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
{ start: dayjs("2025-01-23T12:30:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
{ start: dayjs("2025-01-23T13:15:00.000Z"), end: dayjs("2025-01-23T13:30:00.000Z") },
{ start: dayjs("2025-01-23T13:20:00.000Z"), end: dayjs("2025-01-23T13:30:00.000Z") },
],
user: { isFixed: true },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
],
user: { isFixed: false },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T12:30:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
],
user: { isFixed: false },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
const timeRangeToCheckAvailable = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:30:00.000Z"),
};
expect(isAvailable(result, timeRangeToCheckAvailable)).toBe(true);
expect(result[0].start.format()).toEqual(dayjs("2025-01-23T11:00:00.000Z").format());
expect(result[0].end.format()).toEqual(dayjs("2025-01-23T11:30:00.000Z").format());
expect(result[1].start.format()).toEqual(dayjs("2025-01-23T12:30:00.000Z").format());
expect(result[1].end.format()).toEqual(dayjs("2025-01-23T13:00:00.000Z").format());
});
it("does not duplicate slots when multiple rr-hosts offer the same availability", () => {
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
{ start: dayjs("2025-01-23T12:30:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
{ start: dayjs("2025-01-23T13:15:00.000Z"), end: dayjs("2025-01-23T13:30:00.000Z") },
{ start: dayjs("2025-01-23T16:10:00.000Z"), end: dayjs("2025-01-23T16:30:00.000Z") },
],
user: { isFixed: true },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
],
user: { isFixed: false },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
],
user: { isFixed: false },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
const timeRangeToCheckAvailable = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:30:00.000Z"),
};
expect(isAvailable(result, timeRangeToCheckAvailable)).toBe(true);
expect(result.length).toBe(1);
});
it("requires at least one RR host from each group to be available", () => {
// Test scenario with two groups:
// Group 1: Host A (available 11:00-11:30), Host B (available 12:00-12:30)
// Group 2: Host C (available 11:15-11:45), Host D (available 12:15-12:45)
// Fixed host: available 11:00-13:00
// Expected: Only slots where at least one host from each group is available
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
],
user: { isFixed: true },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
],
user: { isFixed: false, groupId: "group1" },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T12:00:00.000Z"), end: dayjs("2025-01-23T12:30:00.000Z") },
],
user: { isFixed: false, groupId: "group1" },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:15:00.000Z"), end: dayjs("2025-01-23T11:45:00.000Z") },
],
user: { isFixed: false, groupId: "group2" },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T12:15:00.000Z"), end: dayjs("2025-01-23T12:45:00.000Z") },
],
user: { isFixed: false, groupId: "group2" },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
const timeRangeAvailable = {
start: dayjs("2025-01-23T11:15:00.000Z"),
end: dayjs("2025-01-23T11:30:00.000Z"),
};
expect(isAvailable(result, timeRangeAvailable)).toBe(true);
const timeRangeAvailable2 = {
start: dayjs("2025-01-23T12:15:00.000Z"),
end: dayjs("2025-01-23T12:30:00.000Z"),
};
expect(isAvailable(result, timeRangeAvailable2)).toBe(true);
// Should NOT be available when only one group has hosts available
const timeRangeNotAvailable = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:15:00.000Z"),
};
expect(isAvailable(result, timeRangeNotAvailable)).toBe(false);
// Should NOT be available when only one group has hosts available
const timeRangeNotAvailable2 = {
start: dayjs("2025-01-23T12:30:00.000Z"),
end: dayjs("2025-01-23T12:45:00.000Z"),
};
expect(isAvailable(result, timeRangeNotAvailable2)).toBe(false);
});
it("handles single group with multiple RR hosts (union behavior)", () => {
// Test that when all RR hosts are in the same group, we get union behavior
// Host A: available 11:00-11:20, 16:10-16:30
// Host B: available 11:15-11:30, 13:20-13:30
// Expected: Union of both hosts' availability
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:20:00.000Z") },
{ start: dayjs("2025-01-23T16:10:00.000Z"), end: dayjs("2025-01-23T16:30:00.000Z") },
],
user: { isFixed: false },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:15:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
{ start: dayjs("2025-01-23T13:20:00.000Z"), end: dayjs("2025-01-23T13:30:00.000Z") },
],
user: { isFixed: false },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
// Should be available when either host is available
const timeRangeAvailable = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:20:00.000Z"),
};
expect(isAvailable(result, timeRangeAvailable)).toBe(true);
const timeRangeAvailable2 = {
start: dayjs("2025-01-23T13:20:00.000Z"),
end: dayjs("2025-01-23T13:30:00.000Z"),
};
expect(isAvailable(result, timeRangeAvailable2)).toBe(true);
// Should NOT be available when neither host is available
const timeRangeNotAvailable = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:30:00.000Z"),
};
expect(isAvailable(result, timeRangeNotAvailable)).toBe(false);
});
it("handles mixed groups with some hosts having groupId and others not", () => {
// Test scenario:
// Group 1: Host A (available 11:00-11:45)
// Group 2: Host B (available 11:15-12:00)
// Default group: Host C (available 11:30-12:30), Host D (available 12:15-12:45)
// Fixed host: available 11:00-13:00
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
],
user: { isFixed: true },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:45:00.000Z") },
],
user: { isFixed: false, groupId: "group1" },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:15:00.000Z"), end: dayjs("2025-01-23T12:00:00.000Z") },
],
user: { isFixed: false, groupId: "group2" },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:30:00.000Z"), end: dayjs("2025-01-23T12:30:00.000Z") },
],
user: { isFixed: false },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T12:15:00.000Z"), end: dayjs("2025-01-23T12:45:00.000Z") },
],
user: { isFixed: false },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
// Should be available when all groups have at least one host available
// Fixed host + Group 1 + Group 2 + Default group all available in this time range
const timeRangeAvailable = {
start: dayjs("2025-01-23T11:30:00.000Z"),
end: dayjs("2025-01-23T11:45:00.000Z"),
};
expect(isAvailable(result, timeRangeAvailable)).toBe(true);
// Should NOT be available when not all groups have hosts available
// Only Group 1, Group 2, and fixed host available, but Default group not available
const timeRangeNotAvailable = {
start: dayjs("2025-01-23T11:15:00.000Z"),
end: dayjs("2025-01-23T11:30:00.000Z"),
};
expect(isAvailable(result, timeRangeNotAvailable)).toBe(false);
// Should NOT be available when not all groups have hosts available
// Only Group 1 and fixed host available, Group 2 and Default group not available
const timeRangeNotAvailable2 = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:15:00.000Z"),
};
expect(isAvailable(result, timeRangeNotAvailable2)).toBe(false);
// Should NOT be available when not all groups have hosts available
// Only Group 2 and fixed host available, Group 1 and Default group not available
const timeRangeNotAvailable3 = {
start: dayjs("2025-01-23T11:45:00.000Z"),
end: dayjs("2025-01-23T12:00:00.000Z"),
};
expect(isAvailable(result, timeRangeNotAvailable3)).toBe(false);
});
it("handles empty groups gracefully", () => {
// Test scenario with empty groups
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
],
user: { isFixed: true },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
// Should only have fixed host availability
const timeRangeAvailable = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:30:00.000Z"),
};
expect(isAvailable(result, timeRangeAvailable)).toBe(true);
expect(result.length).toBe(1);
});
it("handles scenario where one group has no available hosts", () => {
// Test scenario where one group has no available hosts
// Group 1: Host A (available 11:00-11:30)
// Group 2: Host B (not available at all)
// Fixed host: available 11:00-13:00
const userAvailability = [
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T13:00:00.000Z") },
],
user: { isFixed: true },
},
{
dateRanges: [],
oooExcludedDateRanges: [
{ start: dayjs("2025-01-23T11:00:00.000Z"), end: dayjs("2025-01-23T11:30:00.000Z") },
],
user: { isFixed: false, groupId: "group1" },
},
{
dateRanges: [],
oooExcludedDateRanges: [], // No availability
user: { isFixed: false, groupId: "group2" },
},
];
const result = getAggregatedAvailability(userAvailability, "ROUND_ROBIN");
// Should NOT be available when one group has no hosts available
const timeRangeNotAvailable = {
start: dayjs("2025-01-23T11:00:00.000Z"),
end: dayjs("2025-01-23T11:30:00.000Z"),
};
expect(isAvailable(result, timeRangeNotAvailable)).toBe(false);
});
});
@@ -0,0 +1,74 @@
import type { DateRange } from "@calcom/features/schedules/lib/date-ranges";
import { intersect } from "@calcom/features/schedules/lib/date-ranges";
import { DEFAULT_GROUP_ID } from "@calcom/lib/constants";
import { SchedulingType } from "@calcom/prisma/enums";
import { filterRedundantDateRanges } from "./date-range-utils/filterRedundantDateRanges";
import { mergeOverlappingDateRanges } from "./date-range-utils/mergeOverlappingDateRanges";
function uniqueAndSortedDateRanges(ranges: DateRange[]): DateRange[] {
const seen = new Set<string>();
return ranges
.sort((a, b) => {
const startDiff = a.start.valueOf() - b.start.valueOf();
return startDiff !== 0 ? startDiff : a.end.valueOf() - b.end.valueOf();
})
.filter((range) => {
const key = `${range.start.valueOf()}-${range.end.valueOf()}`;
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}
export const getAggregatedAvailability = (
userAvailability: {
dateRanges: DateRange[];
oooExcludedDateRanges: DateRange[];
user?: { isFixed?: boolean; groupId?: string | null };
}[],
schedulingType: SchedulingType | null
): DateRange[] => {
const isTeamEvent =
schedulingType === SchedulingType.COLLECTIVE ||
schedulingType === SchedulingType.ROUND_ROBIN ||
userAvailability.length > 1;
const fixedHosts = userAvailability.filter(
({ user }) => !schedulingType || schedulingType === SchedulingType.COLLECTIVE || user?.isFixed
);
const fixedDateRanges = mergeOverlappingDateRanges(
intersect(fixedHosts.map((s) => (!isTeamEvent ? s.dateRanges : s.oooExcludedDateRanges)))
);
const dateRangesToIntersect = !!fixedDateRanges.length ? [fixedDateRanges] : [];
const roundRobinHosts = userAvailability.filter(({ user }) => user?.isFixed !== true);
if (roundRobinHosts.length) {
// Group round robin hosts by their groupId
const hostsByGroup = roundRobinHosts.reduce((groups, host) => {
const groupId = host.user?.groupId || DEFAULT_GROUP_ID;
if (!groups[groupId]) {
groups[groupId] = [];
}
groups[groupId].push(host);
return groups;
}, {} as Record<string, typeof roundRobinHosts>);
// at least one host from each group needs to be available
Object.values(hostsByGroup).forEach((groupHosts) => {
if (groupHosts.length > 0) {
const groupDateRanges = groupHosts.flatMap((s) =>
!isTeamEvent ? s.dateRanges : s.oooExcludedDateRanges
);
dateRangesToIntersect.push(groupDateRanges ?? []);
}
});
}
const availability = intersect(dateRangesToIntersect);
const uniqueRanges = uniqueAndSortedDateRanges(availability);
return filterRedundantDateRanges(uniqueRanges);
};
@@ -12,9 +12,9 @@ import {
import { getBusyTimesService } from "@calcom/features/di/containers/BusyTimes";
import { EventTypeRepository } from "@calcom/features/eventtypes/repositories/eventTypeRepository";
import type { IRedisService } from "@calcom/features/redis/IRedisService";
import type { DateOverride, WorkingHours } from "@calcom/features/schedules/lib/date-ranges";
import { buildDateRanges, subtract } from "@calcom/features/schedules/lib/date-ranges";
import { getWorkingHours } from "@calcom/lib/availability";
import type { DateOverride, WorkingHours } from "@calcom/lib/date-ranges";
import { buildDateRanges, subtract } from "@calcom/lib/date-ranges";
import { stringToDayjsZod } from "@calcom/lib/dayjs";
import { ErrorCode } from "@calcom/lib/errorCodes";
import { HttpError } from "@calcom/lib/http-error";
@@ -641,9 +641,9 @@ export class UserAvailabilityService {
// you can obtain that from user availability defined outside of here
fromUser: { id: user.id, displayName: user.name },
// optional chaining destructuring toUser
toUser: !!toUser ? { id: toUser.id, displayName: toUser.name, username: toUser.username } : null,
reason: !!reason ? reason.reason : null,
emoji: !!reason ? reason.emoji : null,
toUser: toUser ? { id: toUser.id, displayName: toUser.name, username: toUser.username } : null,
reason: reason ? reason.reason : null,
emoji: reason ? reason.emoji : null,
};
}