Files
calendar/packages/core/bookingLimits/limitManager.ts
T
9e980dc0ab feat: global booking limits for teams (#16614)
* add booking booking to team settings

* add update mutation

* add missing export

* fix dirty state

* first version of global team limits in getUserAvailability

* add test setup

* create seperate test file for global limits

* add tests for all units

* move limitManager and booking limit functions outside of getUserAvailability

* add migration

* clean up code

* move yearly booking count to booking repository

* code clean up

* don't count rescheduling booking

* add test for getSchedule

* fix type error

* fix type error

* fix type error

* fix from and end date for fetching bookings

* reuse functions

* allow null for bookingLimits

* remove bookings from managed event type

* fix type error

* code clean up

* small fixes form clean up

* fix type issue

* same fixes in teams/_post

* fix existing tz issue

* tests for fix

* adds missingn await

* imrove description

* remove spreading

* fix reschedule issue with booking limits

* fix reschedule error with booking durations

* remove useeffect

* undo commit

* add bookingLimits to UpdateOrgTeamDto

* fix unit tests

* Prepare view for app router migration

* throw error if not in ascending order

* fix disabled update button

---------

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
2024-09-23 14:21:40 -04:00

64 lines
1.8 KiB
TypeScript

import type { Dayjs } from "@calcom/dayjs";
import type { EventBusyDate, IntervalLimitUnit } from "@calcom/types/Calendar";
type BusyMapKey = `${IntervalLimitUnit}-${ReturnType<Dayjs["toISOString"]>}`;
/**
* Helps create, check, and return busy times from limits (with parallel support)
*/
export default class LimitManager {
private busyMap: Map<BusyMapKey, EventBusyDate> = new Map();
/**
* Creates a busy map key
*/
private static createKey(start: Dayjs, unit: IntervalLimitUnit): BusyMapKey {
return `${unit}-${start.startOf(unit).toISOString()}`;
}
/**
* Checks if already marked busy by ancestors or siblings
*/
isAlreadyBusy(start: Dayjs, unit: IntervalLimitUnit) {
if (this.busyMap.has(LimitManager.createKey(start, "year"))) return true;
if (unit === "month" && this.busyMap.has(LimitManager.createKey(start, "month"))) {
return true;
} else if (
unit === "week" &&
// weeks can be part of two months
((this.busyMap.has(LimitManager.createKey(start, "month")) &&
this.busyMap.has(LimitManager.createKey(start.endOf("week"), "month"))) ||
this.busyMap.has(LimitManager.createKey(start, "week")))
) {
return true;
} else if (
unit === "day" &&
(this.busyMap.has(LimitManager.createKey(start, "month")) ||
this.busyMap.has(LimitManager.createKey(start, "week")) ||
this.busyMap.has(LimitManager.createKey(start, "day")))
) {
return true;
} else {
return false;
}
}
/**
* Adds a new busy time
*/
addBusyTime(start: Dayjs, unit: IntervalLimitUnit) {
this.busyMap.set(`${unit}-${start.toISOString()}`, {
start: start.toISOString(),
end: start.endOf(unit).toISOString(),
});
}
/**
* Returns all busy times
*/
getBusyTimes() {
return Array.from(this.busyMap.values());
}
}