return json or ics and provide all day entries
This commit is contained in:
parent
8b7bbf8edd
commit
597feef9ef
1 changed files with 63 additions and 39 deletions
|
@ -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<any> {
|
||||
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> = [];
|
||||
if (types.length == 0) {
|
||||
|
@ -22,11 +29,26 @@ export async function getCalendarItemsByTypes(req: Request, res: Response): Prom
|
|||
items = await CalendarService.getByTypeNSCDR();
|
||||
}
|
||||
|
||||
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("-")
|
||||
|
@ -35,6 +57,7 @@ export async function getCalendarItemsByTypes(req: Request, res: Response): Prom
|
|||
.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,
|
||||
|
@ -63,4 +86,5 @@ export async function getCalendarItemsByTypes(req: Request, res: Response): Prom
|
|||
);
|
||||
|
||||
res.type("ics").send(events.value);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue