From 1150778f4cb36736c46ff8b2e10ecc98e8a3c321 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Thu, 24 Oct 2024 15:33:58 +0300 Subject: [PATCH] save merged calendar to file --- server.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/server.js b/server.js index ebb8c01..8ed5e45 100644 --- a/server.js +++ b/server.js @@ -168,8 +168,29 @@ app.get('/calendar/:name', async (req, res) => { }); }); + //save merged calendar to file + let icalString = `BEGIN:VCALENDAR + VERSION:2.0 + CALSCALE:GREGORIAN + METHOD:PUBLISH + `; + mergedCal.forEach((event) => { + icalString += `BEGIN:VEVENT + DTSTART;VALUE=DATETIME:${event.start.toISOString().replace(/-|:|\.\d{3}/g, '')} + DTEND;VALUE=DATETIME:${event.end.toISOString().replace(/-|:|\.\d{3}/g, '')} + SUMMARY:${event.summary} + END:VEVENT + `; + }); + icalString += `END:VCALENDAR`; + fs.writeFileSync(icsFilePath, icalString); + + console.log('Calendar data refreshed.'); } + } else { + return res.status(404).json({ error: 'Calendar not found.' }); } + } catch (error) { console.error(error); res.status(500).json({ error: 'Failed to retrieve calendar data.' });