11import moment from 'moment-timezone' ;
2+ import type { CalendarResponse , VEvent } from 'node-ical' ;
23
34export async function fetchRemoteICalFile ( url : string ) : Promise < string | null > {
45 try {
@@ -13,11 +14,25 @@ export async function fetchRemoteICalFile(url: string): Promise<string | null> {
1314 }
1415}
1516
16- export function printEventsForNextWeeks ( icalData : { [ x : string ] : any } ) {
17- const arrayDates = [ ] ;
17+ function isVEvent ( component : CalendarResponse [ string ] ) : component is VEvent {
18+ return component . type === 'VEVENT' ;
19+ }
20+
21+ export interface CalendarEventInfo {
22+ title : string | undefined ;
23+ time : string ;
24+ day : string ;
25+ timezone : string ;
26+ parsedStartDate : string ;
27+ }
28+
29+ export function printEventsForNextWeeks (
30+ icalData : CalendarResponse ,
31+ ) : CalendarEventInfo [ ] {
32+ const arrayDates : CalendarEventInfo [ ] = [ ] ;
1833 if ( ! icalData ) {
1934 console . error ( 'iCal data is empty or invalid.' ) ;
20- return ;
35+ return [ ] ;
2136 }
2237
2338 // Calculate the range of dates for the next 12 weeks from today
@@ -26,14 +41,18 @@ export function printEventsForNextWeeks(icalData: { [x: string]: any }) {
2641
2742 // Loop through the events in the iCal data
2843 for ( const k in icalData ) {
29- const event = icalData [ k ] ;
44+ const component = icalData [ k ] ;
3045
31- if ( event . type === 'VEVENT' ) {
46+ if ( isVEvent ( component ) ) {
47+ const event = component ;
3248 const title = event . summary ;
3349
3450 const timezoneL = moment . tz . guess ( ) ; // Default to UTC if timezone information is not provided
3551
36- const startDate = moment . tz ( event . start , timezoneL ) ;
52+ const eventStart = event . start ;
53+ if ( ! eventStart ) continue ;
54+
55+ const startDate = moment . tz ( eventStart , timezoneL ) ;
3756
3857 // Complicated case - if an RRULE exists, handle multiple recurrences of the event.
3958 if ( event . rrule !== undefined ) {
@@ -46,18 +65,19 @@ export function printEventsForNextWeeks(icalData: { [x: string]: any }) {
4665 // Loop through the set of date entries to see which recurrences should be printed.
4766 for ( const date of dates ) {
4867 const startDate = moment . tz ( date , timezoneL ) ;
49- const eventtimezone = event . start . tz ;
68+ const eventtimezone = eventStart . tz ?? 'UTC' ;
5069 const owntimezone = moment . tz . guess ( ) ;
5170 const eventOffset = moment . tz ( eventtimezone ) . utcOffset ( ) ;
5271 const localOffset = moment . tz ( owntimezone ) . utcOffset ( ) ;
5372 const offsetDifference = localOffset - eventOffset ;
5473
5574 // Check if the event falls within the next 4 weeks from today
5675 if ( startDate . isBetween ( today , nextTwelveWeeksEnd , undefined , '[]' ) ) {
57- const dateTimezone = moment . tz . zone ( event . start . tz ) ;
76+ const dateTimezone = moment . tz . zone ( eventtimezone ) ;
5877 let offset ;
5978 if ( dateTimezone && offsetDifference )
60- offset = offsetDifference - dateTimezone . utcOffset ( date ) ;
79+ offset =
80+ offsetDifference - dateTimezone . utcOffset ( date . getTime ( ) ) ;
6181
6282 const newDate = moment ( date ) . subtract ( offset , 'minutes' ) . toDate ( ) ;
6383
0 commit comments