diff options
author | Raphael Kabo <raphaelkabo@hey.com> | 2023-05-12 16:54:06 +0100 |
---|---|---|
committer | Raphael Kabo <raphaelkabo@hey.com> | 2023-05-12 16:54:06 +0100 |
commit | bfe708d48f603998a1f2c4cad4a6f9f8683dc18f (patch) | |
tree | bc0402abb6fd999f00e2b180144a34c851e36abf /src/helpers.js | |
parent | 69b4c854b399554ed413cc7ff9d625aee5053927 (diff) |
Migrate to Typescript
Diffstat (limited to 'src/helpers.js')
-rw-r--r-- | src/helpers.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/helpers.js b/src/helpers.js new file mode 100644 index 0000000..bf95e27 --- /dev/null +++ b/src/helpers.js @@ -0,0 +1,57 @@ +const domain = require('./config/domain.js').domain; +const siteName = require('./config/domain.js').sitename; + +const mongoose = require('mongoose'); +const Log = mongoose.model('Log'); +var moment = require('moment-timezone'); +const icalGenerator = require('ical-generator'); + +// LOGGING + +function addToLog(process, status, message) { + let logEntry = new Log({ + status: status, + process: process, + message: message, + timestamp: moment() + }); + logEntry.save().catch(() => { console.log("Error saving log entry!") }); +} + +function exportIcal(events, calendarName) { + // Create a new icalGenerator... generator + const cal = icalGenerator({ + name: calendarName || siteName, + x: { + 'X-WR-CALNAME': calendarName || siteName, + }, + }); + if (events instanceof Array === false) { + events = [ events ]; + } + events.forEach(event => { + // Add the event to the generator + cal.createEvent({ + start: moment.tz(event.start, event.timezone), + end: moment.tz(event.end, event.timezone), + timezone: event.timezone, + timestamp: moment(), + summary: event.name, + description: event.description, + organizer: { + name: event.hostName || "Anonymous", + email: event.creatorEmail || 'anonymous@anonymous.com', + }, + location: event.location, + url: 'https://' + domain + '/' + event.id + }); + }); + // Stringify it! + const string = cal.toString(); + return string; +} + +module.exports = { + addToLog, + exportIcal, +} |