use calendarId in updateMergedCalendars function

This commit is contained in:
Ryan Mwangi 2024-10-22 14:29:51 +03:00
parent b4674feabf
commit a3703ef485
1 changed files with 24 additions and 19 deletions

View File

@ -145,27 +145,33 @@ function saveCalendarData(calendarId, linkGroupName, calendars) {
} }
// Function to update the merged calendar // Function to update the merged calendar
async function updateMergedCalendars(){ async function updateMergedCalendars(calendarId){
try { try {
// Load calendars data from calendars.json file // Load calendars data from calendars.json file
const calendarsData = JSON.parse(fs.readFileSync(CALENDARS_FILE, 'utf8')); const calendarsData = JSON.parse(fs.readFileSync(CALENDARS_FILE, 'utf8'));
// Find the merged calendar with the specified ID
const mergedCalendar = calendarsData.mergedCalendars.find((calendar) => calendar.id === calendarId);
if (!mergedCalendar) {
throw new Error(`Merged calendar with ID ${calendarId} not found`);
}
// Fetch calendar data for each merged calendar // Fetch calendar data for each merged calendar
for (const mergedCalendar of calendarsData.mergedCalendars) { const promises = mergedCalendar.calendars.map((calendar) => {
const promises = mergedCalendar.calendars.map((calendar) => { return axios.get(calendar.url)
return axios.get(calendar.url) .then((response) => {
.then((response) => { return {
return { data: response.data,
data: response.data, prefix: calendar.prefix,
prefix: calendar.prefix, override: calendar.override,
override: calendar.override, };
}; })
}) .catch((error) => {
.catch((error) => { console.error(error);
console.error(error); return null;
return null; });
}); });
});
const results = await Promise.all(promises); const results = await Promise.all(promises);
// Filter out any failed requests // Filter out any failed requests
@ -194,7 +200,7 @@ async function updateMergedCalendars(){
}); });
// Save merged calendar to file // Save merged calendar to file
const filename = `${mergedCalendar.id}.ics`; const filename = `${calendarId}.ics`;
let icalString = `BEGIN:VCALENDAR let icalString = `BEGIN:VCALENDAR
VERSION:2.0 VERSION:2.0
CALSCALE:GREGORIAN CALSCALE:GREGORIAN
@ -213,9 +219,8 @@ END:VEVENT
// Store the merged calendar URL in a file // Store the merged calendar URL in a file
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString); fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
console.log(`Merged calendar updated: ${mergedCalendar.id}`); console.log(`Merged calendar updated: ${calendarId}`);
}
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }