summaryrefslogtreecommitdiff
path: root/src/helpers.ts
blob: 72bbd177dc6736e9a5b53c4fdf299a5f53959d02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import moment from "moment-timezone";
import icalGenerator from "ical-generator";
import Log, { ILog } from "./models/Log.js";
import { getConfig } from "./lib/config.js";
import { IEvent } from "./models/Event.js";

const config = getConfig();
const domain = config.general.domain;
const siteName = config.general.site_name;

// LOGGING
export function addToLog(process: string, status: string, message: string) {
    const logEntry = {
        status,
        process,
        message,
        timestamp: new Date(),
    };
    new Log(logEntry).save().catch(() => {
        console.log("Error saving log entry!");
    });
}

export function exportIcal(events: IEvent[], calendarName: string) {
    if (!events || events.length < 1) return;

    // Create a new icalGenerator... generator
    const cal = icalGenerator({
        name: calendarName || siteName,
    });
    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,
            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;
}