11import ical from 'ical' ;
2+
3+ /**
4+ * Creates an ICAL instance from the input URL
5+ * @param {string } url
6+ */
7+ const getEventsFromCalendar = async url => {
8+ const response = await fetch ( url ) ;
9+ const text = await response . text ( ) ;
10+
11+ return Object . values ( ical . parseICS ( text ) ) ;
12+ } ;
13+
214/**
315 * Finds the next meeting event in Google Calendar for the current week
416 * @param {import('./types').MeetingConfig } meetingConfig - Meeting configuration object
517 * @returns {Promise<Date> } Calendar event object
618 */
7- export const findNextMeetingDate = async ( {
8- properties : { ICAL_URL , CALENDAR_FILTER , GROUP_NAME } ,
9- } ) => {
19+ export const findNextMeetingDate = async ( { properties } ) => {
1020 const now = new Date ( ) ;
1121
1222 // Calculate the start of the current week (Saturday 00:00:00 UTC)
@@ -23,21 +33,26 @@ export const findNextMeetingDate = async ({
2333 weekEnd . setUTCDate ( weekStart . getUTCDate ( ) + 6 ) ;
2434 weekEnd . setUTCHours ( 23 , 59 , 59 , 999 ) ;
2535
26- const ics = await fetch ( ICAL_URL ) . then ( r => r . text ( ) ) ;
36+ const allEvents = await getEventsFromCalendar ( properties . ICAL_URL ) ;
2737
28- const events = Object . values ( ical . parseICS ( ics ) ) . filter ( event =>
29- ( event . summary || event . description ) ?. includes ( CALENDAR_FILTER )
38+ const filteredEvents = allEvents . filter ( event =>
39+ // The event must be recurring
40+ event . rrule &&
41+ // The event must match our filter
42+ ( event . summary || event . description ) ?. includes ( properties . CALENDAR_FILTER )
3043 ) ;
3144
32- for ( const event of events ) {
33- const duringOurTimeframe = event . rrule ?. between ( weekStart , weekEnd ) ;
45+ for ( const event of filteredEvents ) {
46+ // Get all rucurrences in our timeframe
47+ const duringOurTimeframe = event . rrule . between ( weekStart , weekEnd ) ;
48+
3449 if ( duringOurTimeframe . length > 0 ) {
3550 return duringOurTimeframe [ 0 ] ;
3651 }
3752 }
3853
3954 throw new Error (
40- `No meeting found for ${ GROUP_NAME || 'this group' } ` +
55+ `No meeting found for ${ properties . GROUP_NAME || 'this group' } ` +
4156 `in the current week (${ weekStart . toISOString ( ) . split ( 'T' ) [ 0 ] } to ${ weekEnd . toISOString ( ) . split ( 'T' ) [ 0 ] } ). ` +
4257 `This is expected for bi-weekly meetings or meetings that don't occur every week.`
4358 ) ;
0 commit comments