From 597feef9ef53837974c0c5efdb4df32cf507bd77 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Tue, 3 Dec 2024 19:11:09 +0100 Subject: [PATCH] return json or ics and provide all day entries --- src/controller/publicController.ts | 102 ++++++++++++++++++----------- 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/src/controller/publicController.ts b/src/controller/publicController.ts index ef675fa..1ba5134 100644 --- a/src/controller/publicController.ts +++ b/src/controller/publicController.ts @@ -4,6 +4,8 @@ import CalendarTypeService from "../service/calendarTypeService"; import { calendar } from "../entity/calendar"; import { createEvents } from "ics"; import moment from "moment"; +import InternalException from "../exceptions/internalException"; +import CalendarFactory from "../factory/admin/calendar"; /** * @description get all calendar items by types or nscdr @@ -13,6 +15,11 @@ import moment from "moment"; */ export async function getCalendarItemsByTypes(req: Request, res: Response): Promise { let types = Array.isArray(req.query.types) ? req.query.types : [req.query.types]; + let output = (req.query.output as "ics" | "json") ?? "ics"; + + if (output != "ics" && output != "json") { + throw new InternalException("set output query value to `ics` or `json` (defaults to `ics`)"); + } let items: Array = []; if (types.length == 0) { @@ -22,45 +29,62 @@ export async function getCalendarItemsByTypes(req: Request, res: Response): Prom items = await CalendarService.getByTypeNSCDR(); } - let events = createEvents( - items.map((i) => ({ - calName: process.env.CLUB_NAME, - uid: i.id, - sequence: 1, - start: moment(i.starttime) - .format("YYYY-M-D-H-m") - .split("-") - .map((a) => parseInt(a)) as [number, number, number, number, number], - end: moment(i.endtime) - .format("YYYY-M-D-H-m") - .split("-") - .map((a) => parseInt(a)) as [number, number, number, number, number], - title: i.title, - description: i.content, - location: i.location, - categories: [i.type.type], - created: moment(i.createdAt) - .format("YYYY-M-D-H-m") - .split("-") - .map((a) => parseInt(a)) as [number, number, number, number, number], - lastModified: moment(i.updatedAt) - .format("YYYY-M-D-H-m") - .split("-") - .map((a) => parseInt(a)) as [number, number, number, number, number], - transp: "OPAQUE" as "OPAQUE", - url: "https://www.ff-merching.de", - alarms: [ - { - action: "display", - description: "Erinnerung", - trigger: { - minutes: 30, - before: true, + if (output == "json") { + res.json(CalendarFactory.mapToBase(items)); + } else { + let events = createEvents( + items.map((i) => ({ + calName: process.env.CLUB_NAME, + uid: i.id, + sequence: 1, + ...(i.allDay + ? { + start: moment(i.starttime) + .format("YYYY-M-D") + .split("-") + .map((a) => parseInt(a)) as [number, number, number], + end: moment(i.endtime) + .format("YYYY-M-D") + .split("-") + .map((a) => parseInt(a)) as [number, number, number], + } + : { + start: moment(i.starttime) + .format("YYYY-M-D-H-m") + .split("-") + .map((a) => parseInt(a)) as [number, number, number, number, number], + end: moment(i.endtime) + .format("YYYY-M-D-H-m") + .split("-") + .map((a) => parseInt(a)) as [number, number, number, number, number], + }), + title: i.title, + description: i.content, + location: i.location, + categories: [i.type.type], + created: moment(i.createdAt) + .format("YYYY-M-D-H-m") + .split("-") + .map((a) => parseInt(a)) as [number, number, number, number, number], + lastModified: moment(i.updatedAt) + .format("YYYY-M-D-H-m") + .split("-") + .map((a) => parseInt(a)) as [number, number, number, number, number], + transp: "OPAQUE" as "OPAQUE", + url: "https://www.ff-merching.de", + alarms: [ + { + action: "display", + description: "Erinnerung", + trigger: { + minutes: 30, + before: true, + }, }, - }, - ], - })) - ); + ], + })) + ); - res.type("ics").send(events.value); + res.type("ics").send(events.value); + } }