Skip to content

Commit

Permalink
Check for ongoing recurring events when making date-based event calcu…
Browse files Browse the repository at this point in the history
…lations. (#795)
  • Loading branch information
timcosgrove authored Oct 25, 2024
1 parent 8217ee7 commit 59729e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
23 changes: 18 additions & 5 deletions src/lib/utils/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/lib/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand Down

0 comments on commit 59729e8

Please sign in to comment.