backend/source/api/actions/events.ts
2024-09-21 11:05:24 +02:00

161 lines
3.6 KiB
TypeScript

namespace _zeitbild.api
{
/**
*/
export function register_events(
rest_subject : lib_plankton.rest.type_rest
) : void
{
register<
{
from : int;
to : int;
calendar_ids : (
null
|
Array<_zeitbild.type_calendar_id>
);
},
Array<
{
calendar_id : int;
calendar_name : string;
event : _zeitbild.type_event_object;
}
>
>(
rest_subject,
lib_plankton.http.enum_method.get,
"/events",
{
"description": "stellt Veranstaltungen aus verschiedenen Kalendern zusammen",
"query_parameters": () => ([
{
"name": "from",
"required": true,
"description": "UNIX timestamp",
},
{
"name": "to",
"required": true,
"description": "UNIX timestamp",
},
{
"name": "calendar_ids",
"required": false,
"description": "comma separated",
},
]),
"output_schema": () => ({
"type": "array",
"items": {
"type": "object",
"nullable": false,
"additionalProperties": false,
"properties": {
"calendar_id": {
"type": "number",
"nullable": false,
},
"event": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"nullable": false,
},
"begin": {
"type": "int",
"nullable": false,
},
"end": {
"type": "int",
"nullable": true,
},
"location": {
"type": "string",
"nullable": true,
},
"description": {
"type": "string",
"nullable": true,
},
},
"required": [
"name",
"begin",
"end",
"location",
"description",
]
}
},
"required": [
"calendar_ids",
"calendar_name",
"event",
],
}
}),
"restriction": restriction_none, // TODO
"execution": async (stuff) => {
const session : {key : string; value : lib_plankton.session.type_session;} = await session_from_stuff(stuff);
const user_id : _zeitbild.type_user_id = await _zeitbild.service.user.identify(session.value.name);
const from : _zeitbild.helpers.type_pit = parseInt(stuff.query_parameters["from"]);
const to : _zeitbild.helpers.type_pit = parseInt(stuff.query_parameters["to"]);
const calendar_ids_wanted : Array<_zeitbild.type_calendar_id> = (
(
("calendar_ids" in stuff.query_parameters)
&&
(stuff.query_parameters["calendar_ids"] !== null)
)
?
lib_plankton.call.convey(
stuff.query_parameters["calendar_ids"],
[
(x : string) => x.split(","),
(x : Array<string>) => x.map(parseInt),
(x : Array<int>) => x.filter(y => (! isNaN(y)))
]
)
:
null
);
const calendar_ids_allowed : Array<_zeitbild.type_calendar_id> = (
(await _zeitbild.service.calendar.overview(user_id))
.map((x : any) => x.id)
);
const calendar_ids : Array<_zeitbild.type_calendar_id> = (
(
(calendar_ids_wanted === null)
?
calendar_ids_allowed
:
(
calendar_ids_wanted
.filter(
(calendar_id) => calendar_ids_allowed.includes(calendar_id)
)
)
)
);
calendar_ids.sort();
const data = await _zeitbild.service.calendar.gather_events(
calendar_ids,
from,
to
);
return Promise.resolve({
"status_code": 200,
"data": data,
});
}
}
);
}
}