refactor: replace event addition with ical.js
- transition event processing from ical-generator to ical.js - add logging for event addition to calendar component - preserve event details (uid, summary, start, end) in new event components
This commit is contained in:
parent
1c241c0738
commit
8e143df754
39
server.js
39
server.js
|
@ -3,7 +3,6 @@ import ICAL from 'ical.js';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import icalGenerator from 'ical-generator';
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
@ -26,38 +25,50 @@ const fetchCalendarData = async (calendar) => {
|
||||||
const isFilePath = !calendar.url.startsWith('http');
|
const isFilePath = !calendar.url.startsWith('http');
|
||||||
try {
|
try {
|
||||||
if (isFilePath) {
|
if (isFilePath) {
|
||||||
|
console.log(`Fetching calendar data from file: ${calendar.url}`);
|
||||||
return {
|
return {
|
||||||
data: fs.readFileSync(path.resolve(calendar.url), 'utf-8'),
|
data: fs.readFileSync(path.resolve(calendar.url), 'utf-8'),
|
||||||
...calendar
|
...calendar
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
console.log(`Fetching calendar data from URL: ${calendar.url}`);
|
||||||
const response = await axios.get(calendar.url);
|
const response = await axios.get(calendar.url);
|
||||||
return { data: response.data, ...calendar };
|
return { data: response.data, ...calendar };
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error retrieving calendar from ${calendar.url}:`, error);
|
console.error(`Error retrieving calendar from ${calendar.url}:`, error.message);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Merge calendar events
|
// Create a top-level VCALENDAR component
|
||||||
const mergeCalendarEvents = (calendarInstance, results) => {
|
const 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) => {
|
||||||
|
console.log(`Adding events to calendar component.`);
|
||||||
results.forEach((result) => {
|
results.forEach((result) => {
|
||||||
const parsed = ICAL.parse(result.data);
|
const parsed = ICAL.parse(result.data);
|
||||||
const component = new ICAL.Component(parsed);
|
const component = new ICAL.Component(parsed);
|
||||||
|
|
||||||
component.getAllSubcomponents('vevent').forEach((event) => {
|
component.getAllSubcomponents('vevent').forEach((event) => {
|
||||||
const vevent = new ICAL.Event(event);
|
const vevent = new ICAL.Event(event);
|
||||||
const start = vevent.startDate.toJSDate();
|
const newEvent = new ICAL.Component('vevent');
|
||||||
const end = vevent.endDate.toJSDate();
|
|
||||||
const summary = result.override ? result.prefix : `${result.prefix} ${vevent.summary}`;
|
|
||||||
|
|
||||||
const eventOptions = {
|
console.log(`Adding event: ${vevent.summary} to calendar.`);
|
||||||
start: start,
|
newEvent.updatePropertyWithValue('uid', vevent.uid);
|
||||||
end: end,
|
newEvent.updatePropertyWithValue('summary', result.override ? result.prefix : `${result.prefix} ${vevent.summary}`);
|
||||||
summary: summary,
|
newEvent.updatePropertyWithValue('dtstart', vevent.startDate.toICALString());
|
||||||
allDay: vevent.startDate.isDate
|
newEvent.updatePropertyWithValue('dtend', vevent.endDate.toICALString());
|
||||||
};
|
|
||||||
calendarInstance.createEvent(eventOptions);
|
calendarComponent.addSubcomponent(newEvent);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue