ff-admin-server/src/controller/publicController.ts

54 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-10-27 11:47:13 +01:00
import { Request, Response } from "express";
2025-01-05 14:14:00 +01:00
import CalendarService from "../service/club/calendarService";
2025-02-15 10:59:54 +01:00
import CalendarTypeService from "../service/configuration/calendarTypeService";
2025-01-05 14:14:00 +01:00
import { calendar } from "../entity/club/calendar";
2024-11-07 10:49:34 +01:00
import { createEvents } from "ics";
import moment from "moment";
import InternalException from "../exceptions/internalException";
2025-01-05 14:14:00 +01:00
import CalendarFactory from "../factory/admin/club/calendar";
import { CalendarHelper } from "../helpers/calendarHelper";
2024-10-27 11:47:13 +01:00
/**
* @description get all calendar items by types or nscdr
2024-12-03 19:29:38 +01:00
* @summary passphrase is passed as value pair like `type:passphrase`
2024-10-27 11:47:13 +01:00
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getCalendarItemsByTypes(req: Request, res: Response): Promise<any> {
let types = Array.isArray(req.query.types) ? req.query.types : [req.query.types];
2024-12-24 14:06:48 +01:00
let nscdr = req.query.nscdr == "true";
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`)");
}
2024-10-27 11:47:13 +01:00
2024-12-04 18:58:31 +01:00
types = types.filter((t) => t);
2024-10-27 11:47:13 +01:00
let items: Array<calendar> = [];
2024-12-03 19:29:38 +01:00
if (types.length != 0) {
let typeIds = await CalendarTypeService.getByTypes((types as Array<string>).map((t) => t.split(":")[0]));
typeIds = typeIds.filter(
(ti) =>
ti.passphrase == null ||
ti.passphrase == "" ||
ti.passphrase == (types as Array<string>).find((t) => t.includes(ti.type)).split(":")[1]
);
2024-12-24 14:06:48 +01:00
items = await CalendarService.getByTypes(
typeIds.map((t) => t.id),
nscdr
);
2024-10-27 11:47:13 +01:00
} else {
items = await CalendarService.getByTypeNSCDR();
}
if (output == "json") {
res.json(CalendarFactory.mapToBase(items));
} else {
let { error, value } = CalendarHelper.buildICS(items);
2024-11-07 10:49:34 +01:00
res.type("ics").send(value);
}
2024-10-27 11:47:13 +01:00
}