fix: resolve circular dependency in getUserAvailability by extracting getPeriodStartDatesBetween utility (#22913)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
124fd8ebca
commit
badd0de33c
5 files changed
+36
-30
No files matched your search
@@ -37,6 +37,7 @@ import type { EventBusyDetails, IntervalLimitUnit } from "@calcom/types/Calendar
|
||||
import type { TimeRange } from "@calcom/types/schedule";
|
||||
|
||||
import { getBusyTimes } from "./getBusyTimes";
|
||||
import { getPeriodStartDatesBetween as getPeriodStartDatesBetweenUtil } from "./intervalLimits/utils/getPeriodStartDatesBetween";
|
||||
import { withReporting } from "./sentryWrapper";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["getUserAvailability"] });
|
||||
@@ -595,25 +596,9 @@ export class UserAvailabilityService {
|
||||
|
||||
getUserAvailability = withReporting(this._getUserAvailability.bind(this), "getUserAvailability");
|
||||
|
||||
_getPeriodStartDatesBetween(
|
||||
dateFrom: Dayjs,
|
||||
dateTo: Dayjs,
|
||||
period: IntervalLimitUnit,
|
||||
timeZone?: string
|
||||
): Dayjs[] {
|
||||
const dates = [];
|
||||
let startDate = timeZone ? dayjs(dateFrom).tz(timeZone).startOf(period) : dayjs(dateFrom).startOf(period);
|
||||
const endDate = timeZone ? dayjs(dateTo).tz(timeZone).endOf(period) : dayjs(dateTo).endOf(period);
|
||||
|
||||
while (startDate.isBefore(endDate)) {
|
||||
dates.push(startDate);
|
||||
startDate = startDate.add(1, period);
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
getPeriodStartDatesBetween = withReporting(
|
||||
this._getPeriodStartDatesBetween.bind(this),
|
||||
(dateFrom: Dayjs, dateTo: Dayjs, period: IntervalLimitUnit, timeZone?: string) =>
|
||||
getPeriodStartDatesBetweenUtil(dateFrom, dateTo, period, timeZone),
|
||||
"getPeriodStartDatesBetween"
|
||||
);
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { Dayjs } from "@calcom/dayjs";
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { getCheckBookingLimitsService } from "@calcom/lib/di/containers/booking-limits";
|
||||
import { getUserAvailabilityService } from "@calcom/lib/di/containers/get-user-availability";
|
||||
import { getStartEndDateforLimitCheck } from "@calcom/lib/getBusyTimes";
|
||||
import type { EventType } from "@calcom/lib/getUserAvailability";
|
||||
import { getPeriodStartDatesBetween } from "@calcom/lib/intervalLimits/utils/getPeriodStartDatesBetween";
|
||||
import { withReporting } from "@calcom/lib/sentryWrapper";
|
||||
import { performance } from "@calcom/lib/server/perfObserver";
|
||||
import { getTotalBookingDuration } from "@calcom/lib/server/queries/booking";
|
||||
@@ -86,7 +86,6 @@ const _getBusyTimesFromBookingLimits = async (params: {
|
||||
includeManagedEvents?: boolean;
|
||||
timeZone?: string | null;
|
||||
}) => {
|
||||
const userAvailabilityService = getUserAvailabilityService();
|
||||
const {
|
||||
bookings,
|
||||
bookingLimits,
|
||||
@@ -106,7 +105,7 @@ const _getBusyTimesFromBookingLimits = async (params: {
|
||||
if (!limit) continue;
|
||||
|
||||
const unit = intervalLimitKeyToUnit(key);
|
||||
const periodStartDates = userAvailabilityService.getPeriodStartDatesBetween(dateFrom, dateTo, unit);
|
||||
const periodStartDates = getPeriodStartDatesBetween(dateFrom, dateTo, unit);
|
||||
|
||||
for (const periodStart of periodStartDates) {
|
||||
if (limitManager.isAlreadyBusy(periodStart, unit)) continue;
|
||||
@@ -163,14 +162,12 @@ const _getBusyTimesFromDurationLimits = async (
|
||||
timeZone: string,
|
||||
rescheduleUid?: string
|
||||
) => {
|
||||
const userAvailabilityService = getUserAvailabilityService();
|
||||
|
||||
for (const key of descendingLimitKeys) {
|
||||
const limit = durationLimits?.[key];
|
||||
if (!limit) continue;
|
||||
|
||||
const unit = intervalLimitKeyToUnit(key);
|
||||
const periodStartDates = userAvailabilityService.getPeriodStartDatesBetween(dateFrom, dateTo, unit);
|
||||
const periodStartDates = getPeriodStartDatesBetween(dateFrom, dateTo, unit);
|
||||
|
||||
for (const periodStart of periodStartDates) {
|
||||
if (limitManager.isAlreadyBusy(periodStart, unit)) continue;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { Dayjs } from "@calcom/dayjs";
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { withReporting } from "@calcom/lib/sentryWrapper";
|
||||
|
||||
import type { IntervalLimitUnit } from "../intervalLimitSchema";
|
||||
|
||||
function _getPeriodStartDatesBetween(
|
||||
dateFrom: Dayjs,
|
||||
dateTo: Dayjs,
|
||||
period: IntervalLimitUnit,
|
||||
timeZone?: string
|
||||
): Dayjs[] {
|
||||
const dates = [];
|
||||
let startDate = timeZone ? dayjs(dateFrom).tz(timeZone).startOf(period) : dayjs(dateFrom).startOf(period);
|
||||
const endDate = timeZone ? dayjs(dateTo).tz(timeZone).endOf(period) : dayjs(dateTo).endOf(period);
|
||||
|
||||
while (startDate.isBefore(endDate)) {
|
||||
dates.push(startDate);
|
||||
startDate = startDate.add(1, period);
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
export const getPeriodStartDatesBetween = withReporting(_getPeriodStartDatesBetween, "getPeriodStartDatesBetween");
|
||||
Reference in new issue
Block a user