#20-calendar-api #21

Merged
jkeffects merged 3 commits from #20-calendar-api into main 2024-12-12 14:04:56 +00:00
Showing only changes of commit 597feef9ef - Show all commits

View file

@ -4,6 +4,8 @@ import CalendarTypeService from "../service/calendarTypeService";
import { calendar } from "../entity/calendar"; import { calendar } from "../entity/calendar";
import { createEvents } from "ics"; import { createEvents } from "ics";
import moment from "moment"; import moment from "moment";
import InternalException from "../exceptions/internalException";
import CalendarFactory from "../factory/admin/calendar";
/** /**
* @description get all calendar items by types or nscdr * @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<any> { export async function getCalendarItemsByTypes(req: Request, res: Response): Promise<any> {
let types = Array.isArray(req.query.types) ? req.query.types : [req.query.types]; 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<calendar> = []; let items: Array<calendar> = [];
if (types.length == 0) { if (types.length == 0) {
@ -22,11 +29,26 @@ export async function getCalendarItemsByTypes(req: Request, res: Response): Prom
items = await CalendarService.getByTypeNSCDR(); items = await CalendarService.getByTypeNSCDR();
} }
if (output == "json") {
res.json(CalendarFactory.mapToBase(items));
} else {
let events = createEvents( let events = createEvents(
items.map((i) => ({ items.map((i) => ({
calName: process.env.CLUB_NAME, calName: process.env.CLUB_NAME,
uid: i.id, uid: i.id,
sequence: 1, 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) start: moment(i.starttime)
.format("YYYY-M-D-H-m") .format("YYYY-M-D-H-m")
.split("-") .split("-")
@ -35,6 +57,7 @@ export async function getCalendarItemsByTypes(req: Request, res: Response): Prom
.format("YYYY-M-D-H-m") .format("YYYY-M-D-H-m")
.split("-") .split("-")
.map((a) => parseInt(a)) as [number, number, number, number, number], .map((a) => parseInt(a)) as [number, number, number, number, number],
}),
title: i.title, title: i.title,
description: i.content, description: i.content,
location: i.location, location: i.location,
@ -64,3 +87,4 @@ export async function getCalendarItemsByTypes(req: Request, res: Response): Prom
res.type("ics").send(events.value); res.type("ics").send(events.value);
} }
}