refactor: modularize event merging with mergeCalendarEvents helper

This commit is contained in:
Ryan Mwangi 2024-11-07 14:23:24 +03:00
parent 0c61cb0e7c
commit 1bc7119b08
1 changed files with 22 additions and 0 deletions

View File

@ -40,6 +40,28 @@ const fetchCalendarData = async (calendar) => {
} }
}; };
// Merge calendar events
const mergeCalendarEvents = (calendarInstance, results) => {
results.forEach((result) => {
const parsed = ICAL.parse(result.data);
const component = new ICAL.Component(parsed);
component.getAllSubcomponents('vevent').forEach((event) => {
const vevent = new ICAL.Event(event);
const start = vevent.startDate.toJSDate();
const end = vevent.endDate.toJSDate();
const summary = result.override ? result.prefix : `${result.prefix} ${vevent.summary}`;
const eventOptions = {
start: start,
end: end,
summary: summary,
allDay: vevent.startDate.isDate
};
calendarInstance.createEvent(eventOptions);
});
});
};
// Merge calendars endpoint // Merge calendars endpoint
app.post('/merge', async (req, res) => { app.post('/merge', async (req, res) => {
const { linkGroupName, calendars } = req.body; const { linkGroupName, calendars } = req.body;