2022-05-02 20:39:35 +00:00
|
|
|
import type { Availability } from "@prisma/client";
|
2022-02-15 20:30:52 +00:00
|
|
|
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { ConfigType } from "@calcom/dayjs";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-02-15 20:30:52 +00:00
|
|
|
import type { Schedule, TimeRange, WorkingHours } from "@calcom/types/schedule";
|
|
|
|
|
2022-05-26 17:07:14 +00:00
|
|
|
import { nameOfDay } from "./weekday";
|
|
|
|
|
2022-02-15 20:30:52 +00:00
|
|
|
// sets the desired time in current date, needs to be current date for proper DST translation
|
|
|
|
export const defaultDayRange: TimeRange = {
|
|
|
|
start: new Date(new Date().setUTCHours(9, 0, 0, 0)),
|
|
|
|
end: new Date(new Date().setUTCHours(17, 0, 0, 0)),
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DEFAULT_SCHEDULE: Schedule = [
|
|
|
|
[],
|
|
|
|
[defaultDayRange],
|
|
|
|
[defaultDayRange],
|
|
|
|
[defaultDayRange],
|
|
|
|
[defaultDayRange],
|
|
|
|
[defaultDayRange],
|
|
|
|
[],
|
|
|
|
];
|
|
|
|
|
|
|
|
export function getAvailabilityFromSchedule(schedule: Schedule): Availability[] {
|
|
|
|
return schedule.reduce((availability: Availability[], times: TimeRange[], day: number) => {
|
|
|
|
const addNewTime = (time: TimeRange) =>
|
|
|
|
({
|
|
|
|
days: [day],
|
|
|
|
startTime: time.start,
|
|
|
|
endTime: time.end,
|
|
|
|
} as Availability);
|
|
|
|
|
|
|
|
const filteredTimes = times.filter((time) => {
|
|
|
|
let idx;
|
|
|
|
if (
|
|
|
|
(idx = availability.findIndex(
|
2022-03-17 16:48:23 +00:00
|
|
|
(schedule) =>
|
|
|
|
schedule.startTime.toString() === time.start.toString() &&
|
|
|
|
schedule.endTime.toString() === time.end.toString()
|
2022-02-15 20:30:52 +00:00
|
|
|
)) !== -1
|
|
|
|
) {
|
|
|
|
availability[idx].days.push(day);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
filteredTimes.forEach((time) => {
|
|
|
|
availability.push(addNewTime(time));
|
|
|
|
});
|
|
|
|
return availability;
|
|
|
|
}, [] as Availability[]);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MINUTES_IN_DAY = 60 * 24;
|
|
|
|
export const MINUTES_DAY_END = MINUTES_IN_DAY - 1;
|
|
|
|
export const MINUTES_DAY_START = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows "casting" availability (days, startTime, endTime) given in UTC to a timeZone or utcOffset
|
|
|
|
*/
|
|
|
|
export function getWorkingHours(
|
|
|
|
relativeTimeUnit: {
|
|
|
|
timeZone?: string;
|
|
|
|
utcOffset?: number;
|
|
|
|
},
|
2022-12-21 19:32:42 +00:00
|
|
|
availability: { userId?: number | null; days: number[]; startTime: ConfigType; endTime: ConfigType }[]
|
2022-02-15 20:30:52 +00:00
|
|
|
) {
|
|
|
|
if (!availability.length) {
|
2022-08-22 23:53:51 +00:00
|
|
|
return [];
|
2022-02-15 20:30:52 +00:00
|
|
|
}
|
2022-08-22 23:53:51 +00:00
|
|
|
const utcOffset =
|
|
|
|
relativeTimeUnit.utcOffset ??
|
|
|
|
(relativeTimeUnit.timeZone ? dayjs().tz(relativeTimeUnit.timeZone).utcOffset() : 0);
|
2022-02-15 20:30:52 +00:00
|
|
|
|
2022-08-22 23:53:51 +00:00
|
|
|
const workingHours = availability.reduce((currentWorkingHours: WorkingHours[], schedule) => {
|
2022-12-14 17:30:55 +00:00
|
|
|
// Include only recurring weekly availability, not date overrides
|
|
|
|
if (!schedule.days.length) return currentWorkingHours;
|
2022-02-15 20:30:52 +00:00
|
|
|
// Get times localised to the given utcOffset/timeZone
|
|
|
|
const startTime =
|
|
|
|
dayjs.utc(schedule.startTime).get("hour") * 60 +
|
|
|
|
dayjs.utc(schedule.startTime).get("minute") -
|
|
|
|
utcOffset;
|
|
|
|
const endTime =
|
|
|
|
dayjs.utc(schedule.endTime).get("hour") * 60 + dayjs.utc(schedule.endTime).get("minute") - utcOffset;
|
|
|
|
// 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));
|
2022-08-22 23:53:51 +00:00
|
|
|
if (sameDayEndTime < sameDayStartTime) {
|
|
|
|
return currentWorkingHours;
|
|
|
|
}
|
2022-02-15 20:30:52 +00:00
|
|
|
if (sameDayStartTime !== sameDayEndTime) {
|
2022-12-21 19:32:42 +00:00
|
|
|
const newWorkingHours: WorkingHours = {
|
2022-02-15 20:30:52 +00:00
|
|
|
days: schedule.days,
|
|
|
|
startTime: sameDayStartTime,
|
|
|
|
endTime: sameDayEndTime,
|
2022-12-21 19:32:42 +00:00
|
|
|
};
|
|
|
|
if (schedule.userId) newWorkingHours.userId = schedule.userId;
|
|
|
|
currentWorkingHours.push(newWorkingHours);
|
2022-02-15 20:30:52 +00:00
|
|
|
}
|
|
|
|
// check for overflow to the previous day
|
2022-06-27 21:01:46 +00:00
|
|
|
// overflowing days constraint to 0-6 day range (Sunday-Saturday)
|
2022-02-15 20:30:52 +00:00
|
|
|
if (startTime < MINUTES_DAY_START || endTime < MINUTES_DAY_START) {
|
2022-12-21 19:32:42 +00:00
|
|
|
const newWorkingHours: WorkingHours = {
|
2022-06-27 21:01:46 +00:00
|
|
|
days: schedule.days.map((day) => (day - 1 >= 0 ? day - 1 : 6)),
|
2022-02-15 20:30:52 +00:00
|
|
|
startTime: startTime + MINUTES_IN_DAY,
|
|
|
|
endTime: Math.min(endTime + MINUTES_IN_DAY, MINUTES_DAY_END),
|
2022-12-21 19:32:42 +00:00
|
|
|
};
|
|
|
|
if (schedule.userId) newWorkingHours.userId = schedule.userId;
|
|
|
|
currentWorkingHours.push(newWorkingHours);
|
2022-02-15 20:30:52 +00:00
|
|
|
}
|
|
|
|
// else, check for overflow in the next day
|
2022-08-22 23:53:51 +00:00
|
|
|
else if (startTime > MINUTES_DAY_END || endTime > MINUTES_IN_DAY) {
|
2022-12-21 19:32:42 +00:00
|
|
|
const newWorkingHours: WorkingHours = {
|
2022-06-27 21:01:46 +00:00
|
|
|
days: schedule.days.map((day) => (day + 1) % 7),
|
2022-02-15 20:30:52 +00:00
|
|
|
startTime: Math.max(startTime - MINUTES_IN_DAY, MINUTES_DAY_START),
|
|
|
|
endTime: endTime - MINUTES_IN_DAY,
|
2022-12-21 19:32:42 +00:00
|
|
|
};
|
|
|
|
if (schedule.userId) newWorkingHours.userId = schedule.userId;
|
|
|
|
currentWorkingHours.push(newWorkingHours);
|
2022-02-15 20:30:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 23:53:51 +00:00
|
|
|
return currentWorkingHours;
|
2022-02-15 20:30:52 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
workingHours.sort((a, b) => a.startTime - b.startTime);
|
|
|
|
return workingHours;
|
|
|
|
}
|
2022-03-17 16:48:23 +00:00
|
|
|
|
2022-09-06 18:34:29 +00:00
|
|
|
export function availabilityAsString(
|
|
|
|
availability: Availability,
|
|
|
|
{ locale, hour12 }: { locale?: string; hour12?: boolean }
|
|
|
|
) {
|
2022-03-17 16:48:23 +00:00
|
|
|
const weekSpan = (availability: Availability) => {
|
|
|
|
const days = availability.days.slice(1).reduce(
|
|
|
|
(days, day) => {
|
|
|
|
if (days[days.length - 1].length === 1 && days[days.length - 1][0] === day - 1) {
|
|
|
|
// append if the range is not complete (but the next day needs adding)
|
|
|
|
days[days.length - 1].push(day);
|
|
|
|
} else if (days[days.length - 1][days[days.length - 1].length - 1] === day - 1) {
|
|
|
|
// range complete, overwrite if the last day directly preceeds the current day
|
|
|
|
days[days.length - 1] = [days[days.length - 1][0], day];
|
|
|
|
} else {
|
|
|
|
// new range
|
|
|
|
days.push([day]);
|
|
|
|
}
|
|
|
|
return days;
|
|
|
|
},
|
|
|
|
[[availability.days[0]]] as number[][]
|
|
|
|
);
|
|
|
|
return days
|
|
|
|
.map((dayRange) => dayRange.map((day) => nameOfDay(locale, day, "short")).join(" - "))
|
|
|
|
.join(", ");
|
|
|
|
};
|
|
|
|
|
|
|
|
const timeSpan = (availability: Availability) => {
|
|
|
|
return (
|
2022-09-06 18:34:29 +00:00
|
|
|
new Intl.DateTimeFormat(locale, { hour: "numeric", minute: "numeric", hour12 }).format(
|
2022-03-17 16:48:23 +00:00
|
|
|
new Date(availability.startTime.toISOString().slice(0, -1))
|
|
|
|
) +
|
|
|
|
" - " +
|
2022-09-06 18:34:29 +00:00
|
|
|
new Intl.DateTimeFormat(locale, { hour: "numeric", minute: "numeric", hour12 }).format(
|
2022-03-17 16:48:23 +00:00
|
|
|
new Date(availability.endTime.toISOString().slice(0, -1))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return weekSpan(availability) + ", " + timeSpan(availability);
|
|
|
|
}
|