backend/source/api/actions/events.ts

162 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-09-12 19:35:31 +02:00
namespace _zeitbild.api
{
/**
*/
export function register_events(
rest_subject : lib_plankton.rest.type_rest
) : void
{
register<
{
from : int;
to : int;
calendar_ids : (
null
|
2024-09-21 11:05:24 +02:00
Array<_zeitbild.type_calendar_id>
2024-09-12 19:35:31 +02:00
);
},
Array<
{
calendar_id : int;
2024-09-12 20:42:06 +02:00
calendar_name : string;
2024-09-21 11:05:24 +02:00
event : _zeitbild.type_event_object;
2024-09-12 19:35:31 +02:00
}
>
>(
rest_subject,
2024-09-12 20:42:06 +02:00
lib_plankton.http.enum_method.get,
2024-09-12 19:35:31 +02:00
"/events",
{
"description": "stellt Veranstaltungen aus verschiedenen Kalendern zusammen",
2024-09-19 13:34:07 +02:00
"query_parameters": () => ([
2024-09-12 20:42:06 +02:00
{
"name": "from",
"required": true,
"description": "UNIX timestamp",
2024-09-12 19:35:31 +02:00
},
2024-09-12 20:42:06 +02:00
{
"name": "to",
"required": true,
"description": "UNIX timestamp",
},
{
"name": "calendar_ids",
"required": false,
"description": "comma separated",
},
2024-09-19 13:34:07 +02:00
]),
2024-09-12 19:35:31 +02:00
"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": [
2024-09-12 20:42:06 +02:00
"calendar_ids",
"calendar_name",
2024-09-12 19:35:31 +02:00
"event",
],
}
}),
"restriction": restriction_none, // TODO
2024-09-12 20:42:06 +02:00
"execution": async (stuff) => {
2024-09-18 18:17:25 +02:00
const session : {key : string; value : lib_plankton.session.type_session;} = await session_from_stuff(stuff);
2024-09-21 11:05:24 +02:00
const user_id : _zeitbild.type_user_id = await _zeitbild.service.user.identify(session.value.name);
2024-09-18 18:17:25 +02:00
2024-09-12 20:42:06 +02:00
const from : _zeitbild.helpers.type_pit = parseInt(stuff.query_parameters["from"]);
const to : _zeitbild.helpers.type_pit = parseInt(stuff.query_parameters["to"]);
2024-09-21 11:05:24 +02:00
const calendar_ids_wanted : Array<_zeitbild.type_calendar_id> = (
2024-09-12 20:42:06 +02:00
(
("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
);
2024-09-21 11:05:24 +02:00
const calendar_ids_allowed : Array<_zeitbild.type_calendar_id> = (
2024-09-18 18:17:25 +02:00
(await _zeitbild.service.calendar.overview(user_id))
2024-09-12 20:42:06 +02:00
.map((x : any) => x.id)
);
2024-09-21 11:05:24 +02:00
const calendar_ids : Array<_zeitbild.type_calendar_id> = (
2024-09-12 20:42:06 +02:00
(
2024-09-19 01:40:12 +02:00
(calendar_ids_wanted === null)
?
calendar_ids_allowed
:
(
calendar_ids_wanted
.filter(
(calendar_id) => calendar_ids_allowed.includes(calendar_id)
)
2024-09-12 19:35:31 +02:00
)
2024-09-12 20:42:06 +02:00
)
);
2024-09-21 10:56:00 +02:00
calendar_ids.sort();
2024-09-12 20:42:06 +02:00
const data = await _zeitbild.service.calendar.gather_events(
calendar_ids,
from,
to
);
return Promise.resolve({
"status_code": 200,
"data": data,
});
2024-09-12 19:35:31 +02:00
}
}
);
}
}