Compare commits

..

3 commits

Author SHA1 Message Date
be6d378890 style: refactor async functions to use traditional function declaration syntax
- Converted async arrow functions to traditional function declarations for consistency
- Updated  and  to use  syntax
- Maintained overall functionality while improving readability and alignment with preferred style
2024-11-08 15:49:33 +03:00
11a4efe30b fix: ensure valid date-time handling in event components
- Resolved calendar merging error where ICAL.js rejected formatted date-time strings by refactoring  and  initialization to use  for accurate timezone conversion.
- Updated  and  to receive ICAL.Time objects directly, ensuring proper format consistency and timezone handling in event components.
2024-11-08 14:23:30 +03:00
db78a0121e 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.
2024-11-08 02:21:22 +03:00

View file

@ -21,7 +21,7 @@ app.get('/', (req, res) => res.sendFile('index.html', { root: '.' }));
const sanitizeFilename = (filename) => filename.replace(/[<>:"/\\|?* ]/g, '_');
// Fetch calendar data from URL or file
const fetchCalendarData = async (calendar) => {
async function fetchCalendarData(calendar) {
const isFilePath = !calendar.url.startsWith('http');
try {
if (isFilePath) {
@ -39,23 +39,23 @@ const fetchCalendarData = async (calendar) => {
console.error(`Error retrieving calendar from ${calendar.url}:`, error.message);
throw error;
}
};
}
// Create a top-level VCALENDAR component
const createCalendarComponent = (name) => {
function createCalendarComponent(name) {
console.log(`Creating calendar component for: ${name}`);
const calendarComponent = new ICAL.Component(['vcalendar', [], []]);
calendarComponent.updatePropertyWithValue('prodid', '-//Your Product ID//EN');
calendarComponent.updatePropertyWithValue('version', '2.0');
calendarComponent.updatePropertyWithValue('name', name); // calendar name
return calendarComponent;
};
}
// Add events to the calendar component
const addEventsToCalendar = (calendarComponent, results) => {
function addEventsToCalendar(calendarComponent, results) {
console.log(`Adding events to calendar component.`);
results.forEach((result) => {
console.log(result.data);
// console.log(result.data);
const parsed = ICAL.parse(result.data);
const component = new ICAL.Component(parsed);
@ -64,23 +64,38 @@ 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 && ICAL.Time.fromJSDate(vevent.startDate.toJSDate());
const endDate = vevent.endDate && ICAL.Time.fromJSDate(vevent.endDate.toJSDate());
// Log the start and end dates
console.log(`Start Date: ${startDate}`);
console.log(`End Date: ${endDate}`);
// // Check if the dates are valid
// if (!startDate || !endDate || !startDate.isValid() || !endDate.isValid()) {
// console.warn(`Invalid date for event: ${vevent.summary}`);
// return; // Skip or handle 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);
newEvent.updatePropertyWithValue('dtend', endDate);
calendarComponent.addSubcomponent(newEvent);
});
});
};
}
// Save calendar data to file
const saveCalendarFile = (filename, content) => {
function saveCalendarFile(filename, content) {
const filePath = path.join(MERGED_CALENDARS_DIR, filename);
console.log(`Saving calendar data to file: ${filePath}`);
fs.writeFileSync(filePath, content);
return filePath;
};
}
// Merge calendars endpoint
app.post('/merge', async (req, res) => {
@ -120,7 +135,7 @@ app.post('/merge', async (req, res) => {
});
// Refresh calendar if outdated
const refreshCalendarData = async (calendarName) => {
async function refreshCalendarData(calendarName) {
const jsonFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.json`);
console.log(`Refreshing calendar data for: ${calendarName}`);
@ -133,7 +148,7 @@ const refreshCalendarData = async (calendarName) => {
saveCalendarFile(`${calendarName}.ics`, calendarComponent.toString());
console.log('Calendar data refreshed and saved.');
};
}
// Serve the merged calendar file and refresh if older than an hour
app.get('/calendar/:name', async (req, res) => {