Skip to content

Commit

Permalink
✨ add ical API back (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildmodeOne authored Oct 14, 2024
1 parent 16bfe15 commit 73a36af
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions rogue-thi-app/pages/api/cl-events.js
Original file line number Diff line number Diff line change
@@ -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,
},
])
}

0 comments on commit 73a36af

Please sign in to comment.