From 85d970754bb30f1935fe05151af5e096ba62e0f7 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Thu, 14 Mar 2024 23:38:21 +0530 Subject: [PATCH] cleanup --- .../widget/helpers/availabilityHelper.js | 75 +++++++------------ .../helpers/specs/availabilityHelper.spec.js | 30 ++++++-- 2 files changed, 51 insertions(+), 54 deletions(-) diff --git a/app/javascript/widget/helpers/availabilityHelper.js b/app/javascript/widget/helpers/availabilityHelper.js index 5c2befaf1..a3f65ff23 100644 --- a/app/javascript/widget/helpers/availabilityHelper.js +++ b/app/javascript/widget/helpers/availabilityHelper.js @@ -10,81 +10,62 @@ const daysOfWeek = [ // Function to find the next available time const findNextAvailableTime = ( - working_hours, + workingHours, currentDayOfWeek, currentHour, currentMinutes ) => { + let nextAvailableDays = []; let nextAvailableTime = null; // Find the next available time for today - const todayHours = working_hours.filter( - hours => - hours.day_of_week === currentDayOfWeek && - !hours.closed_all_day && - (hours.open_hour > currentHour || - (hours.open_hour === currentHour && - hours.open_minutes > currentMinutes)) + const nextAvailableTimeToday = workingHours.find( + workingHour => + currentDayOfWeek === workingHour.day_of_week && + !workingHour.closed_all_day && + (currentHour < workingHour.close_hour || + (currentHour === workingHour.close_hour && + currentMinutes < workingHour.close_minutes)) ); - // Loop through today's hours to find the earliest available time - todayHours.forEach(hours => { - if ( - nextAvailableTime === null || - hours.open_hour < nextAvailableTime.hour || - (hours.open_hour === nextAvailableTime.hour && - hours.open_minutes < nextAvailableTime.minutes) - ) { - nextAvailableTime = { - hour: hours.open_hour, - minutes: hours.open_minutes, - day: 'today', - }; - } - }); + if (nextAvailableTimeToday) { + nextAvailableTime = { + hour: nextAvailableTimeToday.open_hour, + minutes: nextAvailableTimeToday.open_minutes, + day: 'today', + }; + } // If no available time for today, find the next available time for the next day(s) if (nextAvailableTime === null) { - let nextDaysHours = working_hours.filter( - hours => hours.day_of_week > currentDayOfWeek && !hours.closed_all_day - ); + nextAvailableDays = workingHours.filter(hours => !hours.closed_all_day); - // If there are no available hours for the next day, consider hours for the following days - if (nextDaysHours.length === 0) { - nextDaysHours = working_hours.filter( - hours => hours.day_of_week < currentDayOfWeek && !hours.closed_all_day - ); - } - - // Find the earliest available time among the next day(s) - let nextDay = null; for (let i = 0; i < 7; i += 1) { - nextDay = nextDaysHours.find(hours => hours.day_of_week === i); + const nextDayIndex = (currentDayOfWeek + 1 + i) % 7; + const nextDay = nextAvailableDays.find( + hours => hours.day_of_week === nextDayIndex + ); if (nextDay) { + nextAvailableTime = { + hour: nextDay.open_hour, + minutes: nextDay.open_minutes, + day: nextDay.day_of_week, + }; break; } } - - // If there's an available time for the next day(s), set it as next available time - if (nextDay) { - nextAvailableTime = { - hour: nextDay.open_hour, - minutes: nextDay.open_minutes, - day: nextDay.day_of_week, - }; - } } return nextAvailableTime; }; // Function to get the next availability message -export const getNextAvailabilityMessage = (working_hours, currentTime) => { +export const getNextAvailabilityMessage = (workingHours, currentTime) => { const currentDayOfWeek = currentTime.getDay(); // 0 for Sunday, 1 for Monday, ... const currentHour = currentTime.getHours(); const currentMinutes = currentTime.getMinutes(); const nextAvailableTime = findNextAvailableTime( - working_hours, + workingHours, currentDayOfWeek, currentHour, currentMinutes diff --git a/app/javascript/widget/helpers/specs/availabilityHelper.spec.js b/app/javascript/widget/helpers/specs/availabilityHelper.spec.js index 1a5a8d840..5bcd2fca2 100644 --- a/app/javascript/widget/helpers/specs/availabilityHelper.spec.js +++ b/app/javascript/widget/helpers/specs/availabilityHelper.spec.js @@ -35,7 +35,7 @@ describe('getNextAvailabilityMessage function', () => { inbox_id: 2, account_id: 1, day_of_week: 2, - closed_all_day: false, + closed_all_day: true, open_hour: 15, open_minutes: 0, close_hour: 23, @@ -103,23 +103,27 @@ describe('getNextAvailabilityMessage function', () => { ]; test('returns correct message for less than a minute', () => { - jest.useFakeTimers('modern').setSystemTime(new Date('2024-03-14T14:59:00')); const lessThanMinute = new Date('2024-03-14T14:59:59'); // Current time + 59 seconds expect(getNextAvailabilityMessage(working_hours, lessThanMinute)).toBe( 'We will be back online in less than a minute.' ); }); + test('returns correct message for less than 10 minutes', () => { + const lessThan10Minutes = new Date('2024-03-14T14:50:00'); // Current time + 9 minutes + expect(getNextAvailabilityMessage(working_hours, lessThan10Minutes)).toBe( + 'We will be back online in 10 minutes.' + ); + }); + test('returns minutes message for less than an hour', () => { - jest.useFakeTimers('modern').setSystemTime(new Date('2024-03-14T14:45:00')); const lessThanHour = new Date('2024-03-14T14:45:00'); // Current time + 30 minutes expect(getNextAvailabilityMessage(working_hours, lessThanHour)).toBe( 'We will be back online in 15 minutes.' ); }); - test('returns correct message for greater than an hour', () => { - jest.useFakeTimers('modern').setSystemTime(new Date('2024-03-14T12:00:00')); + test('returns correct message for greater than 2 hours', () => { const lessThanHour = new Date('2024-03-14T12:30:00'); // Current time + 30 minutes expect(getNextAvailabilityMessage(working_hours, lessThanHour)).toBe( 'We will be back online in 2 hours' @@ -127,7 +131,6 @@ describe('getNextAvailabilityMessage function', () => { }); test('returns correct message for next day', () => { - jest.useFakeTimers('modern').setSystemTime(new Date('2024-03-14T00:00:00')); const nextDay = new Date('2024-03-14T00:00:00'); // Next day working_hours[4].closed_all_day = true; expect(getNextAvailabilityMessage(working_hours, nextDay)).toBe( @@ -136,7 +139,6 @@ describe('getNextAvailabilityMessage function', () => { }); test('returns correct message for specific day', () => { - jest.useFakeTimers('modern').setSystemTime(new Date('2024-03-14T00:00:00')); const specificDay = new Date('2024-03-14T00:00:00'); // Next day working_hours[4].closed_all_day = true; working_hours[5].closed_all_day = true; @@ -145,6 +147,20 @@ describe('getNextAvailabilityMessage function', () => { ); }); + test('returns correct message for next week', () => { + const nextWeek = new Date('2024-03-21T21:00:00'); // Next day + working_hours[5].closed_all_day = true; + working_hours[6].closed_all_day = true; + working_hours[0].closed_all_day = true; + working_hours[1].closed_all_day = true; + working_hours[2].closed_all_day = true; + working_hours[3].closed_all_day = true; + working_hours[4].closed_all_day = false; + expect(getNextAvailabilityMessage(working_hours, nextWeek)).toBe( + 'We will be back online on Thursday' + ); + }); + afterEach(() => { jest.useRealTimers(); });