From 73a36afa4ae2f43e1185a71334049531d7bc2873 Mon Sep 17 00:00:00 2001 From: Philipp Opheys Date: Mon, 14 Oct 2024 11:48:28 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20ical=20API=20back=20(#423)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rogue-thi-app/pages/api/cl-events.js | 71 ++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 rogue-thi-app/pages/api/cl-events.js diff --git a/rogue-thi-app/pages/api/cl-events.js b/rogue-thi-app/pages/api/cl-events.js new file mode 100644 index 00000000..48541fc8 --- /dev/null +++ b/rogue-thi-app/pages/api/cl-events.js @@ -0,0 +1,71 @@ +import NeulandAPI from '../../lib/backend/neuland-api' +import ical from 'ical-generator' + +/** + * Sends a HTTP response as JSON. + * @param {object} res Next.js response object + * @param {number} status HTTP status code + * @param {object} body Response body + */ +function sendJson(res, status, body) { + res.statusCode = status + res.setHeader('Content-Type', 'application/json') + res.end(JSON.stringify(body)) +} + +/** + * Sends a HTTP response as iCal. + * @param {object} res Next.js response object + * @param {number} status HTTP status code + * @param {object} body Response body + */ +function sendCalendar(res, status, body) { + res.statusCode = status + res.setHeader('Content-Type', 'text/calendar') + res.end(body.toString()) +} + +export default async function handler(req, res) { + function isCalRequest() { + const accept = req.headers.accept || '' + const format = req.query.format || 'json' + return accept.includes('text/calendar') || format === 'ical' + } + + const plan = await NeulandAPI.getCampusLifeEvents() + + if (isCalRequest()) { + const cal = ical({ name: 'Campus Life' }) + .timezone('Europe/Berlin') + .ttl(60 * 60 * 24) + for (const event of plan.clEvents) { + const start = new Date(Number(event.begin)) + + // discard the end if it is before the start + const end = + event.end > event.begin ? new Date(Number(event.end)) : undefined + + cal.createEvent({ + id: event.id, + summary: event.title, + description: `Veranstalter: ${event.organizer}`, + location: event.location, + start, + end, + }) + } + sendCalendar(res, 200, cal) + return + } + + sendJson(res, 200, [ + { + id: 'deprecation', + organizer: 'Neuland e.V.', + title: 'Neuland REST API is deprecated', + begin: '2024-01-01T00:00:00.000Z', + end: '2026-01-01T00:00:00.000Z', + location: null, + }, + ]) +}