import { Request, Response } from "express"; import CalendarService from "../service/club/calendarService"; import CalendarTypeService from "../service/configuration/calendarTypeService"; import { calendar } from "../entity/club/calendar"; import { createEvents } from "ics"; import moment from "moment"; import InternalException from "../exceptions/internalException"; import CalendarFactory from "../factory/admin/club/calendar"; import { CalendarHelper } from "../helpers/calendarHelper"; /** * @description get all calendar items by types or nscdr * @summary passphrase is passed as value pair like `type:passphrase` * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function getCalendarItemsByTypes(req: Request, res: Response): Promise { let types = Array.isArray(req.query.types) ? req.query.types : [req.query.types]; 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`)"); } types = types.filter((t) => t); let items: Array = []; if (types.length != 0) { let typeIds = await CalendarTypeService.getByTypes((types as Array).map((t) => t.split(":")[0])); typeIds = typeIds.filter( (ti) => ti.passphrase == null || ti.passphrase == "" || ti.passphrase == (types as Array).find((t) => t.includes(ti.type)).split(":")[1] ); items = await CalendarService.getByTypes( typeIds.map((t) => t.id), nscdr ); } else { items = await CalendarService.getByTypeNSCDR(); } if (output == "json") { res.json(CalendarFactory.mapToBase(items)); } else { let { error, value } = CalendarHelper.buildICS(items); res.type("ics").send(value); } }