From 0bfdf2f8efcf76666d54bbc7b619c4f89e83ea8a Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 17 May 2022 03:03:33 +0800 Subject: [PATCH] fix: split time correctly if the local working hours are just across mid night (#2766) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- apps/web/lib/slots.ts | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/apps/web/lib/slots.ts b/apps/web/lib/slots.ts index 2ed82d762a..2fb9920b3e 100644 --- a/apps/web/lib/slots.ts +++ b/apps/web/lib/slots.ts @@ -32,7 +32,7 @@ const splitAvailableTime = ( const periodTime = initialTime + frequency; const slotEndTime = initialTime + eventLength; /* - check if the slot end time surpasses availability end time of the user + check if the slot end time surpasses availability end time of the user 1 minute is added to round up the hour mark so that end of the slot is considered in the check instead of x9 eg: if finalization time is 11:59, slotEndTime is 12:00, we ideally want the slot to be available */ @@ -64,9 +64,32 @@ const getSlots = ({ inviteeDate, frequency, minimumBookingNotice, workingHours, const slots: Dayjs[] = []; const slotsTimeFrameAvailable = [] as Array; - - // Here we split working hour in chunks for every frequency available that can fit in whole working hour + // Here we split working hour in chunks for every frequency available that can fit in whole working hours + const computedLocalWorkingHours: WorkingHoursTimeFrame[] = []; + let tempComputeTimeFrame: WorkingHoursTimeFrame | undefined; + const computeLength = localWorkingHours.length - 1; + const makeTimeFrame = (item: typeof localWorkingHours[0]): WorkingHoursTimeFrame => ({ + startTime: item.startTime, + endTime: item.endTime, + }); localWorkingHours.forEach((item, index) => { + if (!tempComputeTimeFrame) { + tempComputeTimeFrame = makeTimeFrame(item); + } else { + // please check the comment in splitAvailableTime func for the added 1 minute + if (tempComputeTimeFrame.endTime + 1 === item.startTime) { + // to deal with time that across the day, e.g. from 11:59 to to 12:01 + tempComputeTimeFrame.endTime = item.endTime; + } else { + computedLocalWorkingHours.push(tempComputeTimeFrame); + tempComputeTimeFrame = makeTimeFrame(item); + } + } + if (index == computeLength) { + computedLocalWorkingHours.push(tempComputeTimeFrame); + } + }); + computedLocalWorkingHours.forEach((item, index) => { slotsTimeFrameAvailable.push(...splitAvailableTime(item.startTime, item.endTime, frequency, eventLength)); });