backend/source/api/actions/events.ts

204 lines
4.4 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;
calendar_name : string;
access_level : string;
event_id : (null | int);
event_object : _zeitbild.type_event_object;
}
>
|
string
)
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": {
"nullable": false,
"type": "object",
2024-09-12 19:35:31 +02:00
"additionalProperties": false,
"properties": {
"calendar_id": {
"nullable": false,
2024-09-12 19:35:31 +02:00
"type": "number",
},
"calendar_name": {
2024-09-12 19:35:31 +02:00
"nullable": false,
"type": "string",
2024-09-12 19:35:31 +02:00
},
"access_level": {
"nullable": false,
"type": "string",
"enum": ["none","view","edit","admin"],
},
"event_id": {
"nullable": true,
"type": "number",
},
"event_object": {
"nullable": false,
2024-09-12 19:35:31 +02:00
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"nullable": false,
},
"begin": _zeitbild.api.datetime_type(
{
"nullable": false,
}
),
"end": _zeitbild.api.datetime_type(
{
"nullable": true,
}
),
2024-09-12 19:35:31 +02:00
"location": {
"type": "string",
"nullable": true,
},
"link": {
"type": "string",
"nullable": true,
},
2024-09-12 19:35:31 +02:00
"description": {
"type": "string",
"nullable": true,
},
},
"required": [
"name",
"begin",
"end",
"location",
"description",
]
}
},
"required": [
"calendar_id",
"event_id",
2024-09-12 20:42:06 +02:00
"calendar_name",
"event_object",
2024-09-12 19:35:31 +02:00
],
}
}),
2024-10-10 23:00:29 +02:00
"restriction": restriction_none,
2024-09-12 20:42:06 +02:00
"execution": async (stuff) => {
2024-10-10 23:00:29 +02:00
const user_id : (null | _zeitbild.type_user_id) = await (
session_from_stuff(stuff)
.then(
(session : {key : string; value : lib_plankton.session.type_session;}) => (
_zeitbild.service.user.identify(session.value.name)
.catch(x => Promise.resolve(null))
)
)
.catch(x => Promise.resolve(null))
);
2024-09-18 18:17:25 +02:00
2024-09-26 16:47:38 +02:00
const from : lib_plankton.pit.type_pit = parseInt(stuff.query_parameters["from"]);
const to : lib_plankton.pit.type_pit = parseInt(stuff.query_parameters["to"]);
const calendar_ids_wanted : (null | 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
);
return (
_zeitbild.service.calendar.gather_events(
calendar_ids_wanted,
from,
to,
user_id
)
.then(
(data) => Promise.resolve(
{
"status_code": 200,
"data": (
data
.map(
(entry) => ({
"calendar_id": entry.calendar_id,
"calendar_name": entry.calendar_name,
"access_level": _zeitbild.api.access_level_encode(entry.access_level),
"event_id": entry.event_id,
"event_object": entry.event_object,
})
)
),
}
)
)
.catch(
(reason) => Promise.resolve(
{
"status_code": 403,
"data": String(reason),
}
)
2024-09-12 20:42:06 +02:00
)
);
2024-09-12 19:35:31 +02:00
}
}
);
}
}