26 lines
972 B
TypeScript
26 lines
972 B
TypeScript
import type { DateRange } from "@calcom/lib/date-ranges";
|
|
import { intersect } from "@calcom/lib/date-ranges";
|
|
import { SchedulingType } from "@calcom/prisma/enums";
|
|
|
|
import { mergeOverlappingDateRanges } from "./date-range-utils/mergeOverlappingDateRanges";
|
|
|
|
export const getAggregatedAvailability = (
|
|
userAvailability: { dateRanges: DateRange[]; user?: { isFixed?: boolean } }[],
|
|
schedulingType: SchedulingType | null
|
|
): DateRange[] => {
|
|
const fixedHosts = userAvailability.filter(
|
|
({ user }) => !schedulingType || schedulingType === SchedulingType.COLLECTIVE || user?.isFixed
|
|
);
|
|
|
|
const dateRangesToIntersect = fixedHosts.map((s) => s.dateRanges);
|
|
|
|
const unfixedHosts = userAvailability.filter(({ user }) => user?.isFixed !== true);
|
|
if (unfixedHosts.length) {
|
|
dateRangesToIntersect.push(unfixedHosts.flatMap((s) => s.dateRanges));
|
|
}
|
|
|
|
const availability = intersect(dateRangesToIntersect);
|
|
|
|
return mergeOverlappingDateRanges(availability);
|
|
};
|