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

+13 -17
View File
@@ -65,21 +65,15 @@ export function getWorkingHours(
},
availability: { days: number[]; startTime: ConfigType; endTime: ConfigType }[]
) {
// clearly bail when availability is not set, set everything available.
if (!availability.length) {
return [
{
days: [0, 1, 2, 3, 4, 5, 6],
// shorthand for: dayjs().startOf("day").tz(timeZone).diff(dayjs.utc().startOf("day"), "minutes")
startTime: MINUTES_DAY_START,
endTime: MINUTES_DAY_END,
},
];
return [];
}
const utcOffset = relativeTimeUnit.utcOffset ?? dayjs().tz(relativeTimeUnit.timeZone).utcOffset();
const utcOffset =
relativeTimeUnit.utcOffset ??
(relativeTimeUnit.timeZone ? dayjs().tz(relativeTimeUnit.timeZone).utcOffset() : 0);
const workingHours = availability.reduce((workingHours: WorkingHours[], schedule) => {
const workingHours = availability.reduce((currentWorkingHours: WorkingHours[], schedule) => {
// Get times localised to the given utcOffset/timeZone
const startTime =
dayjs.utc(schedule.startTime).get("hour") * 60 +
@@ -90,9 +84,11 @@ export function getWorkingHours(
// add to working hours, keeping startTime and endTimes between bounds (0-1439)
const sameDayStartTime = Math.max(MINUTES_DAY_START, Math.min(MINUTES_DAY_END, startTime));
const sameDayEndTime = Math.max(MINUTES_DAY_START, Math.min(MINUTES_DAY_END, endTime));
if (sameDayEndTime < sameDayStartTime) {
return currentWorkingHours;
}
if (sameDayStartTime !== sameDayEndTime) {
workingHours.push({
currentWorkingHours.push({
days: schedule.days,
startTime: sameDayStartTime,
endTime: sameDayEndTime,
@@ -101,22 +97,22 @@ export function getWorkingHours(
// check for overflow to the previous day
// overflowing days constraint to 0-6 day range (Sunday-Saturday)
if (startTime < MINUTES_DAY_START || endTime < MINUTES_DAY_START) {
workingHours.push({
currentWorkingHours.push({
days: schedule.days.map((day) => (day - 1 >= 0 ? day - 1 : 6)),
startTime: startTime + MINUTES_IN_DAY,
endTime: Math.min(endTime + MINUTES_IN_DAY, MINUTES_DAY_END),
});
}
// else, check for overflow in the next day
else if (startTime > MINUTES_DAY_END || endTime > MINUTES_DAY_END) {
workingHours.push({
else if (startTime > MINUTES_DAY_END || endTime > MINUTES_IN_DAY) {
currentWorkingHours.push({
days: schedule.days.map((day) => (day + 1) % 7),
startTime: Math.max(startTime - MINUTES_IN_DAY, MINUTES_DAY_START),
endTime: endTime - MINUTES_IN_DAY,
});
}
return workingHours;
return currentWorkingHours;
}, []);
workingHours.sort((a, b) => a.startTime - b.startTime);
+1 -1
View File
@@ -9,7 +9,7 @@ async function leastRecentlyBookedUser<T extends Pick<User, "id">>({
availableUsers: T[];
eventTypeId: number;
}) {
const usersWithLastCreated = await prisma?.user.findMany({
const usersWithLastCreated = await prisma.user.findMany({
where: {
id: {
in: availableUsers.map((user) => user.id),
+13 -9
View File
@@ -12,6 +12,10 @@ export type GetSlots = {
};
export type WorkingHoursTimeFrame = { startTime: number; endTime: number };
/**
* TODO: What does this function do?
* Why is it needed?
*/
const splitAvailableTime = (
startTimeMinutes: number,
endTimeMinutes: number,
@@ -38,7 +42,7 @@ const splitAvailableTime = (
const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours, eventLength }: GetSlots) => {
// current date in invitee tz
const startDate = dayjs().add(minimumBookingNotice, "minute");
const startOfDay = dayjs.utc().startOf("day");
const startOfDayUTC = dayjs.utc().startOf("day");
const startOfInviteeDay = inviteeDate.startOf("day");
// checks if the start date is in the past
@@ -52,14 +56,14 @@ const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours,
return [];
}
const localWorkingHours = getWorkingHours(
{ utcOffset: -inviteeDate.utcOffset() },
workingHours.map((schedule) => ({
days: schedule.days,
startTime: startOfDay.add(schedule.startTime, "minute"),
endTime: startOfDay.add(schedule.endTime, "minute"),
}))
).filter((hours) => hours.days.includes(inviteeDate.day()));
const workingHoursUTC = workingHours.map((schedule) => ({
days: schedule.days,
startTime: /* Why? */ startOfDayUTC.add(schedule.startTime, "minute"),
endTime: /* Why? */ startOfDayUTC.add(schedule.endTime, "minute"),
}));
const localWorkingHours = getWorkingHours({ utcOffset: -inviteeDate.utcOffset() }, workingHoursUTC).filter(
(hours) => hours.days.includes(inviteeDate.day())
);
const slots: Dayjs[] = [];
+56
View File
@@ -1,4 +1,5 @@
import { faker } from "@faker-js/faker";
import { Prisma, User, UserPlan } from "@prisma/client";
import { CalendarEvent, Person, VideoCallData } from "@calcom/types/Calendar";
@@ -44,3 +45,58 @@ export const buildCalendarEvent = (event?: Partial<CalendarEvent>): CalendarEven
...event,
};
};
type UserPayload = Prisma.UserGetPayload<{
include: {
credentials: true;
destinationCalendar: true;
availability: true;
selectedCalendars: true;
schedules: true;
};
}>;
export const buildUser = <T extends Partial<UserPayload>>(user?: T): UserPayload => {
return {
name: faker.name.firstName(),
email: faker.internet.email(),
timeZone: faker.address.timeZone(),
username: faker.internet.userName(),
id: 0,
allowDynamicBooking: true,
availability: [],
avatar: "",
away: false,
bio: null,
brandColor: "#292929",
bufferTime: 0,
completedOnboarding: false,
createdDate: new Date(),
credentials: [],
darkBrandColor: "#fafafa",
defaultScheduleId: null,
destinationCalendar: null,
disableImpersonation: false,
emailVerified: null,
endTime: 0,
hideBranding: true,
identityProvider: "CAL",
identityProviderId: null,
invitedTo: null,
locale: "en",
metadata: null,
password: null,
plan: UserPlan.PRO,
role: "USER",
schedules: [],
selectedCalendars: [],
startTime: 0,
theme: null,
timeFormat: null,
trialEndsAt: null,
twoFactorEnabled: false,
twoFactorSecret: null,
verified: false,
weekStart: "",
...user,
};
};