refactor: streamline calendar refresh logic with refreshCalendarData helper

This commit is contained in:
Ryan Mwangi 2024-11-07 14:35:25 +03:00
parent 1dde451f9d
commit cdc25981b5
1 changed files with 13 additions and 0 deletions

View File

@ -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;