From cdc25981b5d26169d930dcee277e259c407518b9 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Thu, 7 Nov 2024 14:35:25 +0300 Subject: [PATCH] refactor: streamline calendar refresh logic with refreshCalendarData helper --- server.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/server.js b/server.js index 25a1406..dfe1e30 100644 --- a/server.js +++ b/server.js @@ -105,6 +105,19 @@ app.post('/merge', async (req, res) => { } }); +// Refresh calendar if outdated +const refreshCalendarData = async (calendarName) => { + const jsonFilePath = path.join(MERGED_CALENDARS_DIR, `${calendarName}.json`); + const { calendars } = JSON.parse(fs.readFileSync(jsonFilePath, 'utf-8')); + + const results = await Promise.all(calendars.map(fetchCalendarData)); + const calendarInstance = icalGenerator({ name: calendarName }); + mergeCalendarEvents(calendarInstance, results); + + saveCalendarFile(`${calendarName}.ics`, calendarInstance.toString()); + console.log('Calendar data refreshed.'); +}; + // Serve the merged calendar file and refresh if older than an hour app.get('/calendar/:name', async (req, res) => { const calendarName = req.params.name;