Files
calendar/packages/features/bookings/lib/conflictChecker/checkForConflicts.ts
T
Benny JooandGitHub bb68cd73ef refactor: circular deps between app store and lib [6] (#23971)
* move delegation credential repository to features

* mv credential repository to features

* update imports

* mv

* fix

* fix

* fix

* fix

* fix

* update imports

* update imports

* update eslint rule

* fix

* fix

* mv getConnectedDestinationCalendars

* fix import errors

* mv getCalendarsEvents

* remove getUsersCredentials

* wip

* revert eslint rule change for now

* fix type checks

* fix

* format

* cleanup

* fix

* fix

* fix

* fix

* fix tests

* migrate getUserAvailability

* migrate

* fix tests

* fix type checks

* fix

* fix

* migrate crmManager

* update imports

* migrate raqbUtils to appstore

* migrate getLuckyUser to features

* migrate findTeamMembersMatchingAttributeLogic to appstore

* update imports

* fix

* fix

* fix test

* fix unit tests

* fix

* fix

* add eslint config
2025-10-09 14:02:12 +00:00

51 lines
1.3 KiB
TypeScript

import type { Dayjs } from "dayjs";
import dayjs from "@calcom/dayjs";
import type { CurrentSeats } from "@calcom/features/availability/lib/getUserAvailability";
import type { BufferedBusyTime } from "@calcom/types/BufferedBusyTime";
type BufferedBusyTimes = BufferedBusyTime[];
// if true, there are conflicts.
export function checkForConflicts({
busy,
time,
eventLength,
currentSeats,
}: {
busy: BufferedBusyTimes;
time: Dayjs;
eventLength: number;
currentSeats?: CurrentSeats;
}) {
// Early return
if (!Array.isArray(busy) || busy.length < 1) {
return false; // guaranteed no conflicts when there is no busy times.
}
// no conflicts if some seats are found for the current time slot
if (currentSeats?.some((booking) => booking.startTime.toISOString() === time.toISOString())) {
return false;
}
const slotStart = time.valueOf();
const slotEnd = slotStart + eventLength * 60 * 1000;
const sortedBusyTimes = busy
.map((busyTime) => ({
start: dayjs.utc(busyTime.start).valueOf(),
end: dayjs.utc(busyTime.end).valueOf(),
}))
.sort((a, b) => a.start - b.start);
for (const busyTime of sortedBusyTimes) {
if (busyTime.start >= slotEnd) {
break;
}
if (busyTime.end <= slotStart) {
continue;
}
return true;
}
return false;
}