From 59729e8fed0532f20414d70f508b221e14572d4d Mon Sep 17 00:00:00 2001 From: Tim Cosgrove Date: Fri, 25 Oct 2024 08:55:32 -0700 Subject: [PATCH] Check for ongoing recurring events when making date-based event calculations. (#795) --- src/lib/utils/date.test.ts | 23 ++++++++++++++++++----- src/lib/utils/date.ts | 10 +++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/lib/utils/date.test.ts b/src/lib/utils/date.test.ts index 62721c6a1..cdc104ef1 100644 --- a/src/lib/utils/date.test.ts +++ b/src/lib/utils/date.test.ts @@ -246,14 +246,27 @@ describe('formatDateObject', () => { }) describe('deriveMostRecentDate', () => { - it('should return the closest future event', () => { - const mockCurrentTime = new Date('2023-08-01T12:00:00Z').getTime() + it('should return the closest future or ongoing event', () => { + const mockCurrentTime = new Date('2023-08-01T18:00:00Z').getTime() jest.spyOn(Date, 'now').mockImplementation(() => mockCurrentTime) const datetimeRange = [ - { value: new Date('2023-07-01T14:00:00Z').getTime() / 1000 }, // Past event - { value: new Date('2023-09-01T14:00:00Z').getTime() / 1000 }, // Closest future event - { value: new Date('2023-10-01T14:00:00Z').getTime() / 1000 }, // Later future event + { + value: new Date('2023-07-01T14:00:00Z').getTime() / 1000, + endValue: new Date('2023-07-01T16:00:00Z').getTime() / 1000, + }, // Past event + { + value: new Date('2023-08-01T14:00:00Z').getTime() / 1000, + endValue: new Date('2023-08-01T19:00:00Z').getTime() / 1000, + }, + { + value: new Date('2023-09-01T14:00:00Z').getTime() / 1000, + endValue: new Date('2023-09-01T16:00:00Z').getTime() / 1000, + }, // Closest future event + { + value: new Date('2023-10-01T14:00:00Z').getTime() / 1000, + endValue: new Date('2023-10-01T16:00:00Z').getTime() / 1000, + }, // Later future event ] const closestEvent = deriveMostRecentDate(datetimeRange) diff --git a/src/lib/utils/date.ts b/src/lib/utils/date.ts index 9ee527823..4baa1837d 100644 --- a/src/lib/utils/date.ts +++ b/src/lib/utils/date.ts @@ -193,14 +193,14 @@ export const deriveMostRecentDate = ( ) => { const currentTime = Math.floor(Date.now() / 1000) - // Filter for future events - const futureEvents = datetimeRange.filter( - (event) => event.value > currentTime + // Filter for ongoing and future events + const ongoingAndFutureEvents = datetimeRange.filter( + (event) => event.endValue > currentTime ) // If there are future events, return closest event - if (futureEvents.length > 0) { - return futureEvents.reduce((closest, current) => { + if (ongoingAndFutureEvents.length > 0) { + return ongoingAndFutureEvents.reduce((closest, current) => { return closest.value < current.value ? closest : current }) }