Skip to content

Commit

Permalink
some events are fetched but should not be shown (api issue?) (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
idaho committed Apr 2, 2024
1 parent 610fca0 commit 7910eea
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
54 changes: 54 additions & 0 deletions src/utils/findActiveEvents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { findActiveEvents } from './findActiveEvents';

import type { CalendarEvent } from './calendarEvents';

describe(`findActiveEvents`, () => {
test(`filter events which starts not in range but recived by api`, () => {
const data = [
{
date: {
start: new Date('2024-03-29T23:00:00.000Z'),
end: new Date('2024-03-30T23:00:00.000Z')
},
isWholeDayEvent: true,
content: {
summary: 'refuse bin',
description: null,
location: null,
uid: null,
recurrence_id: null,
rrule: null,
entity: 'calendar.portsmouth_city_council'
}
},
{
date: {
start: new Date('2024-03-29T23:00:00.000Z'),
end: new Date('2024-03-30T23:00:00.000Z')
},
isWholeDayEvent: true,
content: {
summary: 'food waste bin',
description: '',
location: null,
uid: 'removed when posted',
recurrence_id: '20240401',
rrule: 'FREQ=WEEKLY;BYDAY=MO',
entity: 'calendar.home_assistant_calendar'
}
}
];

const result = findActiveEvents(data as CalendarEvent[], {
config: {
filter_events: false,
pattern: []
},
dropAfter: false,
now: new Date('2024-03-29T12:09:08.879Z'),
filterFutureEventsDay: '2024-03-29'
});

expect(result).toEqual([]);
});
});
12 changes: 10 additions & 2 deletions src/utils/findActiveEvents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getDayFromDate } from './getDayFromDate';
import { getTimeZoneOffset } from './getTimeZoneOffset';

import type { CalendarEvent } from './calendarEvents';
import type { TrashCardConfig } from '../cards/trash-card/trash-card-config';
Expand All @@ -13,6 +14,7 @@ interface Options {
config: Config;
now: Date;
dropAfter: boolean;
filterFutureEventsDay: string;
}

const isMatchingAnyPatterns = (item: CalendarEvent, config: Config) => {
Expand All @@ -30,13 +32,19 @@ const isNotPastWholeDayEvent = (item: CalendarEvent, now: Date, dropAfter: boole
(item.isWholeDayEvent && getDayFromDate(item.date.start) === getDayFromDate(now) && !dropAfter) ||
(item.isWholeDayEvent && getDayFromDate(item.date.start) !== getDayFromDate(now));

const findActiveEvents = (items: CalendarEvent[], { config, now, dropAfter }: Options): CalendarEvent[] => {
const findActiveEvents = (items: CalendarEvent[], { config, now, dropAfter, filterFutureEventsDay }: Options): CalendarEvent[] => {
const dateString = `${filterFutureEventsDay}T00:00:00${getTimeZoneOffset()}`;
const dateMaxStart = new Date(dateString);

const activeItems = items.
filter((item): boolean => {
if (item.date.start > dateMaxStart) {
return false;
}

if (item.isWholeDayEvent) {
return item.date.end > now;
}

if (item.date.end < now) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/getCalendarData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ const getCalendarData = async (
filter_events: config.filter_events
},
dropAfter,
now
now,
filterFutureEventsDay: end
});

debuggerInstance.log(`activeElements`, activeEvents);
Expand Down

0 comments on commit 7910eea

Please sign in to comment.