fix/slots-calculate-hours (#1994)

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/1996/head
alannnc 2022-02-27 15:51:41 -07:00 committed by GitHub
parent eb59908c84
commit c8ba5e1aa1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 24 deletions

View File

@ -16,12 +16,22 @@ export type GetSlots = {
workingHours: WorkingHours[]; workingHours: WorkingHours[];
minimumBookingNotice: number; minimumBookingNotice: number;
}; };
export type WorkingHoursTimeFrame = { startTime: number; endTime: number };
const getMinuteOffset = (date: Dayjs, frequency: number) => { const splitAvailableTime = (
// Diffs the current time with the given date and iff same day; (handled by 1440) - return difference; otherwise 0 startTimeMinutes: number,
const minuteOffset = Math.min(date.diff(dayjs().utc(), "minute"), 1440) % 1440; endTimeMinutes: number,
// round down to nearest step frequency: number
return Math.ceil(minuteOffset / frequency) * frequency; ): Array<WorkingHoursTimeFrame> => {
let initialTime = startTimeMinutes;
const finalizationTime = endTimeMinutes;
const result = [] as Array<WorkingHoursTimeFrame>;
while (initialTime < finalizationTime) {
const periodTime = initialTime + frequency;
result.push({ startTime: initialTime, endTime: periodTime });
initialTime += frequency;
}
return result;
}; };
const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours }: GetSlots) => { const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours }: GetSlots) => {
@ -44,27 +54,21 @@ const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours }
).filter((hours) => hours.days.includes(inviteeDate.day())); ).filter((hours) => hours.days.includes(inviteeDate.day()));
const slots: Dayjs[] = []; const slots: Dayjs[] = [];
for (let minutes = getMinuteOffset(inviteeDate, frequency); minutes < 1440; minutes += frequency) {
const slot = startOfInviteeDay.add(minutes, "minute"); const slotsTimeFrameAvailable = [] as Array<WorkingHoursTimeFrame>;
// check if slot happened already
if (slot.isBefore(startDate)) { // Here we split working hour in chunks for every frequency available that can fit in whole working hour
continue; localWorkingHours.forEach((item, index) => {
} slotsTimeFrameAvailable.push(...splitAvailableTime(item.startTime, item.endTime, frequency));
// add slots to available slots if it is found to be between the start and end time of the checked working hours. });
if (
localWorkingHours.some((hours) => slotsTimeFrameAvailable.forEach((item) => {
slot.isBetween( const slot = startOfInviteeDay.add(item.startTime, "minute");
startOfInviteeDay.add(hours.startTime, "minute"), // Validating slot its not on the past
startOfInviteeDay.add(hours.endTime, "minute"), if (!slot.isBefore(startDate)) {
null,
"[)"
)
)
) {
slots.push(slot); slots.push(slot);
} }
} });
return slots; return slots;
}; };