Add iCal converter
Some checks are pending
Jenkins Production Deployment

This commit is contained in:
Patrick Müller 2022-12-24 17:44:28 +01:00
parent 96f04c6de4
commit b00a37eb17
Signed by: Paddy
GPG Key ID: 37ABC11275CAABCE
4 changed files with 172 additions and 4 deletions

2
app.ts
View File

@ -29,7 +29,7 @@ app.use(express.json());
// Configure CORS // Configure CORS
let allowedHosts = [ let allowedHosts = [
'https://www.nachklang.art', 'https://www.nachklang.art',
'https://calendar.nachklang.art', 'https://calendar.nachklang.art'
]; ];
app.use(cors({ app.use(cors({
origin: function (origin: any, callback: any) { origin: function (origin: any, callback: any) {

View File

@ -1,3 +1,13 @@
export interface Event { export interface Event {
event_id: number; event_id: number;
calendar_id: number;
uuid: string;
name: string;
description: string;
start_datetime: Date;
end_datetime: Date;
created_date: Date;
location: string;
created_by: string;
url: string;
} }

View File

@ -4,7 +4,7 @@
import express, {Request, Response} from 'express'; import express, {Request, Response} from 'express';
import * as EventService from './events.service'; import * as EventService from './events.service';
import {Event} from './event.interface'; import * as iCalService from './icalgenerator.service';
/** /**
@ -13,13 +13,18 @@ import {Event} from './event.interface';
export const eventsRouter = express.Router(); export const eventsRouter = express.Router();
/**
* Constants
*/
const fileNames = ['Nachklang_calendar', 'Nachklang_internal_calendar', 'Nachklang_management_calendar'];
/** /**
* Controller Definitions * Controller Definitions
*/ */
// POST users/register // POST users/register
eventsRouter.get('/all', async (req: Request, res: Response) => { eventsRouter.get('/json', async (req: Request, res: Response) => {
try { try {
// Get request params // Get request params
let calendarId: number = parseInt(req.query.calendar as string ?? '', 10); let calendarId: number = parseInt(req.query.calendar as string ?? '', 10);
@ -27,10 +32,31 @@ eventsRouter.get('/all', async (req: Request, res: Response) => {
// Get events // Get events
let events = await EventService.getAllEvents(calendarId); let events = await EventService.getAllEvents(calendarId);
// Send the session details back to the user // Send the events back
res.status(200).send(events); res.status(200).send(events);
} catch (e: any) { } catch (e: any) {
console.log('Error handling a request: ' + e.message); console.log('Error handling a request: ' + e.message);
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
} }
}); });
eventsRouter.get('/ical', async (req: Request, res: Response) => {
try {
// Get request params
let calendarId: number = parseInt(req.query.calendar as string ?? '', 10);
// Get events
let events = await EventService.getAllEvents(calendarId);
// generate file name
let fileName = fileNames[calendarId];
let file = await iCalService.convertToIcal(events);
// Send the ical file back
res.set({'Content-Disposition': 'attachment; filename=' + fileName + '.ics'});
res.status(200).send(file);
} catch (e: any) {
console.log('Error handling a request: ' + e.message);
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
}
});

View File

@ -0,0 +1,132 @@
import {Event} from './event.interface';
export const convertToIcal = async (events: Event[]): Promise<string> => {
try {
let ical: iCalFile = {body: []};
generateHeaderInfo(ical);
generateFooterInfo(ical);
for (let event of events) {
addEventToFile(ical, event);
}
return serializeIcalFile(ical);
} catch (err) {
throw err;
}
};
const serializeIcalFile = (ical: iCalFile): string => {
let returnString = '';
returnString += ical.header;
for (let event of ical.body) {
returnString += serializeIcalEvent(event);
}
returnString += ical.footer;
return returnString;
};
const serializeIcalEvent = (icalevent: iCalEvent): string => {
let returnString = '';
returnString += icalevent.header;
returnString += 'UID:' + icalevent.uid;
returnString += 'DTSTAMP:' + icalevent.created;
returnString += 'ORGANIZER:' + icalevent.organizer;
returnString += 'DTSTART;TZID=Europe/Berlin:' + icalevent.start;
returnString += 'DTEND;TZID=Europe/Berlin:' + icalevent.end;
returnString += 'SUMMARY:' + icalevent.summary;
returnString += 'DESCRIPTION:' + icalevent.description;
returnString += 'LOCATION:' + icalevent.location;
returnString += 'URL:' + icalevent.url;
returnString += icalevent.footer;
return returnString;
};
const generateHeaderInfo = (ical: iCalFile) => {
ical.header = 'BEGIN:VCALENDAR\n' +
'VERSION:2.0\n' +
'PRODID:-//hacksw/handcal//NONSGML v1.0//EN\n' +
'CALSCALE:GREGORIAN\n' +
'BEGIN:VTIMEZONE\n' +
'TZID:Europe/Berlin\n' +
'LAST-MODIFIED:20201011T015911Z\n' +
'TZURL:http://tzurl.org/zoneinfo-outlook/Europe/Berlin\n' +
'X-LIC-LOCATION:Europe/Berlin\n' +
'BEGIN:DAYLIGHT\n' +
'TZNAME:CEST\n' +
'TZOFFSETFROM:+0100\n' +
'TZOFFSETTO:+0200\n' +
'DTSTART:19700329T020000\n' +
'RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\n' +
'END:DAYLIGHT\n' +
'BEGIN:STANDARD\n' +
'TZNAME:CET\n' +
'TZOFFSETFROM:+0200\n' +
'TZOFFSETTO:+0100\n' +
'DTSTART:19701025T030000\n' +
'RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\n' +
'END:STANDARD\n' +
'END:VTIMEZONE\n';
};
const generateFooterInfo = (ical: iCalFile) => {
ical.footer = 'END:VCALENDAR';
};
const addEventToFile = (ical: iCalFile, event: Event) => {
ical.body.push(createIcalEvent(event));
};
const createIcalEvent = (event: Event): iCalEvent => {
return {
header: 'BEGIN:VEVENT\n',
uid: event.uuid + '\n',
created: formatDate(event.created_date) + 'Z\n',
organizer: event.created_by + '\n',
start: formatDate(event.start_datetime) + '\n',
end: formatDate(event.end_datetime) + '\n',
summary: event.name + '\n',
description: event.description + '\n',
location: event.location + '\n',
url: event.url + '\n',
footer: 'END:VEVENT\n'
};
};
const formatDate = (date: Date): string => {
let returnString = '';
returnString += date.getFullYear();
returnString += (date.getMonth() + 1).toString().padStart(2, '0'); // +1 Because JS sucks
returnString += date.getDate().toString().padStart(2, '0');
returnString += 'T';
returnString += date.getHours().toString().padStart(2, '0');
returnString += date.getMinutes().toString().padStart(2, '0');
returnString += date.getSeconds().toString().padStart(2, '0');
return returnString;
};
export interface iCalFile {
header?: string;
body: iCalEvent[];
footer?: string;
}
export interface iCalEvent {
header: string;
uid: string;
created: string;
organizer: string;
start: string;
end: string;
summary: string;
description: string;
location: string;
url: string;
footer: string;
}