Skip to content

Commit

Permalink
use date ranges without excluded OOO in handleNewBooking
Browse files Browse the repository at this point in the history
  • Loading branch information
CarinaWolli committed Apr 10, 2024
1 parent 7ab8a94 commit f12d130
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/core/getAggregateWorkingHours.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { WorkingHours } from "@calcom/types/schedule";
export const getAggregateWorkingHours = (
usersWorkingHoursAndBusySlots: (Omit<
Awaited<ReturnType<Awaited<typeof import("./getUserAvailability")>["getUserAvailability"]>>,
"currentSeats" | "dateRanges" | "dateRangesWithoutOOO"
"currentSeats" | "dateRanges" | "oooExcludedDateRanges"
> & { user?: { isFixed?: boolean } })[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
schedulingType: SchedulingType | null
Expand Down
8 changes: 5 additions & 3 deletions packages/core/getAggregatedAvailability/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mergeOverlappingDateRanges } from "./date-range-utils/mergeOverlappingD
export const getAggregatedAvailability = (
userAvailability: {
dateRanges: DateRange[];
dateRangesWithoutOOO: DateRange[];
oooExcludedDateRanges: DateRange[];
user?: { isFixed?: boolean };
}[],
schedulingType: SchedulingType | null
Expand All @@ -20,12 +20,14 @@ export const getAggregatedAvailability = (
({ user }) => !schedulingType || schedulingType === SchedulingType.COLLECTIVE || user?.isFixed
);

const dateRangesToIntersect = fixedHosts.map((s) => (!isTeamEvent ? s.dateRanges : s.dateRangesWithoutOOO));
const dateRangesToIntersect = fixedHosts.map((s) =>
!isTeamEvent ? s.dateRanges : s.oooExcludedDateRanges
);

const unfixedHosts = userAvailability.filter(({ user }) => user?.isFixed !== true);
if (unfixedHosts.length) {
dateRangesToIntersect.push(
unfixedHosts.flatMap((s) => (!isTeamEvent ? s.dateRanges : s.dateRangesWithoutOOO))
unfixedHosts.flatMap((s) => (!isTeamEvent ? s.dateRanges : s.oooExcludedDateRanges))
);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/getUserAvailability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ const _getUserAvailability = async function getUsersWorkingHoursLifeTheUniverseA
availability,
});

const { dateRanges, dateRangesWithoutOOO } = buildDateRanges({
const { dateRanges, oooExcludedDateRanges } = buildDateRanges({
dateFrom,
dateTo,
availability,
Expand All @@ -354,7 +354,7 @@ const _getUserAvailability = async function getUsersWorkingHoursLifeTheUniverseA

const dateRangesInWhichUserIsAvailable = subtract(dateRanges, formattedBusyTimes);

const dateRangesInWhichUserIsAvailableWithoutOOO = subtract(dateRangesWithoutOOO, formattedBusyTimes);
const dateRangesInWhichUserIsAvailableWithoutOOO = subtract(oooExcludedDateRanges, formattedBusyTimes);

log.debug(
`getWorkingHours took ${endGetWorkingHours - startGetWorkingHours}ms for userId ${userId}`,
Expand All @@ -371,7 +371,7 @@ const _getUserAvailability = async function getUsersWorkingHoursLifeTheUniverseA
busy: detailedBusyTimes,
timeZone,
dateRanges: dateRangesInWhichUserIsAvailable,
dateRangesWithoutOOO: dateRangesInWhichUserIsAvailableWithoutOOO,
oooExcludedDateRanges: dateRangesInWhichUserIsAvailableWithoutOOO,
workingHours,
dateOverrides,
currentSeats,
Expand Down
2 changes: 1 addition & 1 deletion packages/features/bookings/lib/handleNewBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export async function ensureAvailableUsers(
}

for (const user of eventType.users) {
const { dateRanges, busy: bufferedBusyTimes } = await getUserAvailability(
const { oooExcludedDateRanges: dateRanges, busy: bufferedBusyTimes } = await getUserAvailability(
{
...input,
userId: user.id,
Expand Down
6 changes: 3 additions & 3 deletions packages/lib/date-ranges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ describe("buildDateRanges", () => {

const timeZone = "America/New_York";

const { dateRanges, dateRangesWithoutOOO } = buildDateRanges({
const { dateRanges, oooExcludedDateRanges } = buildDateRanges({
availability: items,
timeZone,
dateFrom,
Expand All @@ -408,8 +408,8 @@ describe("buildDateRanges", () => {
start: dayjs("2023-06-14T12:00:00Z").tz(timeZone),
end: dayjs("2023-06-14T21:00:00Z").tz(timeZone),
});
expect(dateRangesWithoutOOO.length).toBe(1);
expect(dateRangesWithoutOOO[0]).toEqual({
expect(oooExcludedDateRanges.length).toBe(1);
expect(oooExcludedDateRanges[0]).toEqual({
start: dayjs("2023-06-14T12:00:00Z").tz(timeZone),
end: dayjs("2023-06-14T21:00:00Z").tz(timeZone),
});
Expand Down
6 changes: 3 additions & 3 deletions packages/lib/date-ranges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export function buildDateRanges({
dateFrom: Dayjs;
dateTo: Dayjs;
outOfOffice?: IOutOfOfficeData;
}): { dateRanges: DateRange[]; dateRangesWithoutOOO: DateRange[] } {
}): { dateRanges: DateRange[]; oooExcludedDateRanges: DateRange[] } {
const dateFromOrganizerTZ = dateFrom.tz(timeZone);
const groupedWorkingHours = groupByDate(
availability.reduce((processed: DateRange[], item) => {
Expand Down Expand Up @@ -178,7 +178,7 @@ export function buildDateRanges({
(ranges) => ranges.filter((range) => range.start.valueOf() !== range.end.valueOf())
);

const dateRangesWithoutOOO = Object.values({
const oooExcludedDateRanges = Object.values({
...groupedWorkingHours,
...groupedDateOverrides,
...groupedOOO,
Expand All @@ -187,7 +187,7 @@ export function buildDateRanges({
(ranges) => ranges.filter((range) => range.start.valueOf() !== range.end.valueOf())
);

return { dateRanges: dateRanges.flat(), dateRangesWithoutOOO: dateRangesWithoutOOO.flat() };
return { dateRanges: dateRanges.flat(), oooExcludedDateRanges: oooExcludedDateRanges.flat() };
}

export function groupByDate(ranges: DateRange[]): { [x: string]: DateRange[] } {
Expand Down
4 changes: 2 additions & 2 deletions packages/trpc/server/routers/viewer/slots/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro
const {
busy,
dateRanges,
dateRangesWithoutOOO,
oooExcludedDateRanges,
currentSeats: _currentSeats,
timeZone,
datesOutOfOffice,
Expand Down Expand Up @@ -497,7 +497,7 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions): Pro
return {
timeZone,
dateRanges,
dateRangesWithoutOOO,
oooExcludedDateRanges,
busy,
user: currentUser,
datesOutOfOffice,
Expand Down

0 comments on commit f12d130

Please sign in to comment.