Fixes collective availability for teams with overlapping day timezones (#3898)

* WIP

* Fix for team availability with time offsets

* Prevent empty schedule from opening up everything

* When no utcOffset or timeZone's are given, default to 0 utcOffset (UTC)

* timeZone should not be part of getUserAvailability

* Prevents {days:[X],startTime:0,endTime:0} error entry

* Added getAggregateWorkingHours() (#3913)

* Added test for getAggregateWorkingHours

* Timezone isn't used here anymore

* fix: developer docs url (#3914)

* fix: developer docs url added

* chore : remove /

* chore : import url

Co-authored-by: Zach Waterfield <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>

* Test fixes

* Reinstate prisma (generate only) and few comments

* Test fixes

* Skipping getSchedule again

* Added await to expect() as it involves async logic causing the promise to timeout

* Test cleanup

* Update jest.config.ts

Co-authored-by: Alan <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>
Co-authored-by: Zach Waterfield <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
authored and GitHub committed 2022-08-22 23:53:51 +00:00
1 parent cb1d881cc0
commit f4ea385c7f
15 files changed
+317 -181

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
import { SchedulingType } from "@prisma/client";
import type { WorkingHours } from "@calcom/types/schedule";
/**
* This function gets team members working hours and busy slots,
* offsets them to UTC and intersects them for collective events.
**/
export const getAggregateWorkingHours = (
usersWorkingHoursAndBusySlots: Omit<
Awaited<ReturnType<Awaited<typeof import("./getUserAvailability")>["getUserAvailability"]>>,
"currentSeats"
>[],
schedulingType: SchedulingType | null
): WorkingHours[] => {
if (schedulingType !== SchedulingType.COLLECTIVE) {
return usersWorkingHoursAndBusySlots.flatMap((s) => s.workingHours);
}
return usersWorkingHoursAndBusySlots.reduce((currentWorkingHours: WorkingHours[], s) => {
const updatedWorkingHours: typeof currentWorkingHours = [];
s.workingHours.forEach((workingHour) => {
const sameDayWorkingHours = currentWorkingHours.filter((compare) =>
compare.days.find((day) => workingHour.days.includes(day))
);
if (!sameDayWorkingHours.length) {
updatedWorkingHours.push(workingHour); // the first day is always added.
return;
}
// days are overlapping when different users are involved, instead of adding we now need to subtract
updatedWorkingHours.push(
...sameDayWorkingHours.map((compare) => {
const intersect = workingHour.days.filter((day) => compare.days.includes(day));
return {
days: intersect,
startTime: Math.max(workingHour.startTime, compare.startTime),
endTime: Math.min(workingHour.endTime, compare.endTime),
};
})
);
});
return updatedWorkingHours;
}, []);
};
+3 -4
View File
@@ -16,7 +16,6 @@ const availabilitySchema = z
dateFrom: stringToDayjs,
dateTo: stringToDayjs,
eventTypeId: z.number().optional(),
timezone: z.string().optional(),
username: z.string().optional(),
userId: z.number().optional(),
afterEventBuffer: z.number().optional(),
@@ -78,6 +77,7 @@ export const getCurrentSeats = (eventTypeId: number, dateFrom: Dayjs, dateTo: Da
export type CurrentSeats = Awaited<ReturnType<typeof getCurrentSeats>>;
/** This should be called getUsersWorkingHoursAndBusySlots (...and remaining seats, and final timezone) */
export async function getUserAvailability(
query: {
username?: string;
@@ -85,7 +85,6 @@ export async function getUserAvailability(
dateFrom: string;
dateTo: string;
eventTypeId?: number;
timezone?: string;
afterEventBuffer?: number;
},
initialData?: {
@@ -94,7 +93,7 @@ export async function getUserAvailability(
currentSeats?: CurrentSeats;
}
) {
const { username, userId, dateFrom, dateTo, eventTypeId, timezone, afterEventBuffer } =
const { username, userId, dateFrom, dateTo, eventTypeId, afterEventBuffer } =
availabilitySchema.parse(query);
if (!dateFrom.isValid() || !dateTo.isValid())
@@ -144,9 +143,9 @@ export async function getUserAvailability(
)[0],
};
const timeZone = timezone || schedule?.timeZone || eventType?.timeZone || currentUser.timeZone;
const startGetWorkingHours = performance.now();
const timeZone = schedule.timeZone || eventType?.timeZone || currentUser.timeZone;
const workingHours = getWorkingHours(
{ timeZone },
schedule.availability ||