parse calendar data using icalGenerator
This commit is contained in:
parent
f6e0ef8fdf
commit
d62a8cd144
46
server.js
46
server.js
|
@ -67,42 +67,24 @@ app.post('/merge', async (req, res) => {
|
||||||
const calendar = icalGenerator({ name: linkGroupName });
|
const calendar = icalGenerator({ name: linkGroupName });
|
||||||
|
|
||||||
// Parse calendar data
|
// Parse calendar data
|
||||||
const mergedCal = [];
|
|
||||||
validResults.forEach((result) => {
|
validResults.forEach((result) => {
|
||||||
const calendar = ical.parseICS(result.data);
|
const parsedCalendar = ical.parseICS(result.data);
|
||||||
Object.keys(calendar).forEach((key) => {
|
Object.keys(parsedCalendar).forEach((key) => {
|
||||||
const event = calendar[key];
|
const event = parsedCalendar[key];
|
||||||
if (result.override) {
|
const start = new Date(event.start);
|
||||||
mergedCal.push({
|
const end = new Date(event.end);
|
||||||
start: event.start,
|
const summary = result.override ? result.prefix : `${result.prefix} ${event.summary}`;
|
||||||
end: event.end,
|
|
||||||
summary: result.prefix,
|
// Add event to the calendar
|
||||||
});
|
calendar.createEvent({
|
||||||
} else {
|
start: start,
|
||||||
mergedCal.push({
|
end: end,
|
||||||
start: event.start,
|
summary: summary,
|
||||||
end: event.end,
|
});
|
||||||
summary: `${result.prefix} ${event.summary}`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Save merged calendar to .ics file with the sanitized linkGroupName
|
|
||||||
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(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
|
fs.writeFileSync(`${MERGED_CALENDARS_DIR}/${filename}`, icalString);
|
||||||
|
|
||||||
// Save the user input and sanitizedLinkGroupName in a separate JSON file
|
// Save the user input and sanitizedLinkGroupName in a separate JSON file
|
||||||
|
|
Loading…
Reference in New Issue