Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some events are fetched but should not be shown (api issue?) #281

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading