From d3d4d2a9832c025e4b658a939f28b138ab58d5eb Mon Sep 17 00:00:00 2001 From: SanttuA Date: Wed, 22 May 2024 09:19:50 +0300 Subject: [PATCH] Added deadline for overnight same day reservations (#326) Normal users can't reserve same day after overnight start time. Admins can bypass this deadline. --- .../overnight-calendar/OvernightCalendar.js | 2 + .../overnight-calendar/overnightUtils.js | 6 ++- .../tests/overnightUtils.spec.js | 42 +++++++++++++++++-- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/app/shared/overnight-calendar/OvernightCalendar.js b/app/shared/overnight-calendar/OvernightCalendar.js index 1ad281fdf..a7848a766 100644 --- a/app/shared/overnight-calendar/OvernightCalendar.js +++ b/app/shared/overnight-calendar/OvernightCalendar.js @@ -129,6 +129,7 @@ function OvernightCalendar({ reservations: filteredReservations, minPeriod, hasAdminBypass: isUnitManagerOrHigher, + overnightStartTime, }); if (!reservingIsAllowed) { @@ -222,6 +223,7 @@ function OvernightCalendar({ reservations: filteredReservations, minPeriod, hasAdminBypass: isUnitManagerOrHigher, + overnightStartTime, })} enableOutsideDays firstDayOfWeek={1} diff --git a/app/shared/overnight-calendar/overnightUtils.js b/app/shared/overnight-calendar/overnightUtils.js index 2a6f015ae..43646fda3 100644 --- a/app/shared/overnight-calendar/overnightUtils.js +++ b/app/shared/overnight-calendar/overnightUtils.js @@ -50,13 +50,15 @@ export function handleDateSelect({ * @param {Object[]} params.openingHours * @param {Object[]} params.reservations * @param {boolean} params.hasAdminBypass + * @param {string} params.overnightStartTime * @returns {boolean} is day disabled */ export function handleDisableDays({ day, now, reservable, reservableAfter, reservableBefore, - openingHours, reservations, hasAdminBypass + openingHours, reservations, hasAdminBypass, overnightStartTime }) { - const isAfterToday = now.isAfter(day, 'day'); + const startTimedDay = setDatesTime(day, overnightStartTime).toDate(); + const isAfterToday = hasAdminBypass ? now.isAfter(day, 'day') : now.isAfter(startTimedDay); const beforeDate = reservableAfter || moment(); const isBeforeDate = moment(day).isBefore(beforeDate, 'day'); const afterDate = reservableBefore || moment().add(1, 'year'); diff --git a/app/shared/overnight-calendar/tests/overnightUtils.spec.js b/app/shared/overnight-calendar/tests/overnightUtils.spec.js index d047b1aab..7f8efc684 100644 --- a/app/shared/overnight-calendar/tests/overnightUtils.spec.js +++ b/app/shared/overnight-calendar/tests/overnightUtils.spec.js @@ -129,7 +129,7 @@ describe('app/shared/overnight-calendar/overnightUtils', () => { }); describe('handleDisableDays', () => { - const now = moment('2024-04-20'); + const now = moment('2024-04-20T05:00:00+03:00'); const day = now.toDate(); const reservable = true; const reservableAfter = '2024-04-19T00:00:00+03:00'; @@ -151,6 +151,7 @@ describe('app/shared/overnight-calendar/overnightUtils', () => { const reservations = [ Reservation.build({ begin: '2024-04-22T13:00:00+03:00', end: '2024-04-24T09:00:00+03:00' }) ]; + const overnightStartTime = '09:00:00'; const hasAdminBypass = false; const params = { @@ -161,11 +162,34 @@ describe('app/shared/overnight-calendar/overnightUtils', () => { reservableBefore, openingHours, reservations, - hasAdminBypass + hasAdminBypass, + overnightStartTime }; - test('returns false with correct params', () => { - expect(handleDisableDays(params)).toBe(false); + describe('returns false when', () => { + test('using with correct params', () => { + expect(handleDisableDays(params)).toBe(false); + }); + + test('non admin tries to reserve same day before overnight start time', () => { + expect(handleDisableDays( + { + ...params, + hasAdminBypass: false, + overnightStartTime: '10:00:00' + } + )).toBe(false); + }); + + test('admin tries to reserve same day after overnight start time', () => { + expect(handleDisableDays( + { + ...params, + hasAdminBypass: true, + overnightStartTime: '01:00:00' + } + )).toBe(false); + }); }); describe('returns true when', () => { @@ -184,6 +208,16 @@ describe('app/shared/overnight-calendar/overnightUtils', () => { )).toBe(true); }); + test('non admin tries to reserve same day after overnight start time', () => { + expect(handleDisableDays( + { + ...params, + hasAdminBypass: false, + overnightStartTime: '01:00:00' + } + )).toBe(true); + }); + test('day is before reservable after limit', () => { expect(handleDisableDays( {