From db78a0121e88a9d2ad1368932ab7037e147f6b7c Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Fri, 8 Nov 2024 02:21:22 +0300 Subject: [PATCH] refactor: Refactor addEventsToCalendar to properly handle date validation and usage - Updated the handling of start and end dates in the addEventsToCalendar function. - Added validation checks directly on startDate and endDate to ensure they are valid before updating event properties. - Improved logging for better traceability of event addition and date handling. Note: The changes are intended to enhance the handling of date properties, but further testing is required to ensure full functionality. --- server.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 6b47159..4636b66 100644 --- a/server.js +++ b/server.js @@ -64,10 +64,25 @@ const addEventsToCalendar = (calendarComponent, results) => { const newEvent = new ICAL.Component('vevent'); console.log(`Adding event: ${vevent.summary} to calendar.`); + + // Get start and end dates directly + const startDate = vevent.startDate; + const endDate = vevent.endDate; + + // Log the start and end dates + console.log(`Start Date: ${startDate}`); + console.log(`End Date: ${endDate}`); + + // Check if the dates are valid + if (!startDate.isValid() || !endDate.isValid()) { + console.warn(`Invalid date for event: ${vevent.summary}`); + return; // Skip this event or handle it accordingly + } + newEvent.updatePropertyWithValue('uid', vevent.uid); newEvent.updatePropertyWithValue('summary', result.override ? result.prefix : `${result.prefix} ${vevent.summary}`); - newEvent.updatePropertyWithValue('dtstart', vevent.startDate.toICALString()); - newEvent.updatePropertyWithValue('dtend', vevent.endDate.toICALString()); + newEvent.updatePropertyWithValue('dtstart', startDate.toICALString()); + newEvent.updatePropertyWithValue('dtend', endDate.toICALString()); calendarComponent.addSubcomponent(newEvent); });