From 1bc7119b08ea2f39d6d1bac1a651b444ca6a8f2c Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Thu, 7 Nov 2024 14:23:24 +0300 Subject: [PATCH] refactor: modularize event merging with mergeCalendarEvents helper --- server.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/server.js b/server.js index 247e1c9..88ed731 100644 --- a/server.js +++ b/server.js @@ -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 app.post('/merge', async (req, res) => { const { linkGroupName, calendars } = req.body;