1
0
Fork 0

function to add calendarData to calendars,json

This commit is contained in:
Ryan Mwangi 2024-10-18 15:43:04 +03:00
parent 2d3c90aa38
commit 4b27d84f92
1 changed files with 15 additions and 1 deletions

View File

@ -95,6 +95,9 @@ END:VEVENT
// Generate URL for the merged calendar
const mergedCalendarUrl = `${req.protocol}://${req.get('host')}/calendar/${calendarId}`;
// Save the user input and generated ID in calendars.json file
saveCalendarData(calendarId, calendars);
// Save the user input in a calendars.json file
const calendarsFile = 'calendars.json';
let calendarsData = {};
@ -126,7 +129,18 @@ END:VEVENT
res.status(500).json({ error: 'Failed to merge calendars' });
}
});
//function to save CalendarData to calendars.json
function saveCalendarData(calendarId, calendars) {
let calendarsData = { mergedCalendars: [] };
if (fs.existsSync(CALENDARS_FILE)) {
calendarsData = JSON.parse(fs.readFileSync(CALENDARS_FILE, 'utf8'));
}
calendarsData.mergedCalendars.push({
id: calendarId,
calendars: calendars
});
fs.writeFileSync(CALENDARS_FILE, JSON.stringify(calendarsData, null, 2));
}
// Serve the merged calendar file
app.get('/:filename', (req, res) => {
const filename = req.params.filename;