frontend-zackeneule/source/logic/backend.ts

476 lines
7.9 KiB
TypeScript
Raw Normal View History

2024-04-22 10:24:38 +02:00
namespace _espe.backend
2024-04-22 10:02:43 +02:00
{
/**
*/
var _session_key_chest : lib_plankton.storage.type_chest<string, string, void, string, string>;
/**
* @todo use backend call
*/
export async function logged_in(
) : Promise<boolean>
{
let session_key : (null | string);
try {
session_key = (await _session_key_chest.read("session_key"));
}
catch (error) {
session_key = null;
}
return Promise.resolve<boolean>(session_key !== null);
}
/**
*/
export function init(
) : Promise<void>
{
_session_key_chest = lib_plankton.storage.localstorage.implementation_chest(
{
"corner": "aum",
}
);
return Promise.resolve<void>(undefined);
}
/**
*/
async function abstract_call<type_input, type_output>(
http_method : string,
path : string,
options : {
data ?: type_input;
2024-04-30 14:05:40 +02:00
custom_response_handlers ?: Record<int, ((output_data_raw : any) => Promise<type_output>)>;
2024-04-22 10:02:43 +02:00
} = {}
) : Promise<type_output>
{
options = Object.assign(
{
"data": null,
2024-04-30 14:05:40 +02:00
"custom_response_handlers": [],
2024-04-22 10:02:43 +02:00
},
options
);
let session_key : (null | string);
try {
session_key = (await _session_key_chest.read("session_key"));
}
catch (error) {
session_key = null;
}
const without_content : boolean = ["HEAD","OPTIONS","GET"].includes(http_method);
return (
fetch(
lib_plankton.string.coin(
("{{scheme}}://{{host}}:{{port}}{{path_base}}{{path_action}}"),
{
2024-04-22 10:24:38 +02:00
"scheme": _espe.conf.get().backend.scheme,
"host": _espe.conf.get().backend.host,
"port": _espe.conf.get().backend.port.toFixed(0),
"path_base": _espe.conf.get().backend.path_base,
2024-04-22 10:02:43 +02:00
"path_action": path,
}
),
{
"method": http_method,
"headers": Object.assign(
{},
((! without_content) ? {"Content-Type": "application/json"} : {}),
((session_key !== null) ? {"X-Session-Key": session_key} : {})
),
"body": (
without_content
? null
: JSON.stringify(options.data)
),
}
)
.then(
x => {
2024-04-30 14:05:40 +02:00
if (x.status in options.custom_response_handlers) {
return x.json().then(output_data_raw => options.custom_response_handlers[x.status](output_data_raw));
2024-04-22 10:02:43 +02:00
}
else {
2024-04-30 14:05:40 +02:00
if ((x.status >= 200) && (x.status < 300)) {
return x.json();
}
else {
return Promise.reject<type_output>(new Error("irregular response status code"));
}
2024-04-22 10:02:43 +02:00
}
}
)
);
}
/**
*/
export async function login(
name : string,
password : string
) : Promise<void>
{
const session_key : string = await abstract_call<{name : string; password : string;}, string>(
"POST",
"/session/begin",
{
"data": {
"name": name,
"password": password,
}
}
);
await _session_key_chest.write("session_key", session_key);
}
/**
*/
export async function logout(
) : Promise<void>
{
try {
await abstract_call<void, null>(
"DELETE",
"/session/end"
);
}
catch (error) {
// do nothing
}
await _session_key_chest.delete("session_key");
}
/**
*/
export async function email(
receivers : Array<string>,
subject : string,
content : string
) : Promise<void>
{
return (
abstract_call(
"POST",
"/email",
{
"data": {
"receivers": receivers,
"subject": subject,
"content": content,
},
}
)
);
}
/**
*/
export async function verification_get(
data : any
) : Promise<string>
{
return (
abstract_call(
"POST",
"/verification/get",
{
"data": {
"data": data,
},
}
)
);
}
/**
*/
export async function verification_check(
data : any,
verification : string
) : Promise<boolean>
{
return (
abstract_call(
"POST",
"/verification/check",
{
"data": {
"data": data,
"verification": verification,
}
}
)
);
}
/**
*/
export async function member_list(
2024-04-30 01:32:40 +02:00
search_term : (null | string)
) : Promise<
Array<
{
id : int;
preview : {
membership_number : string;
name_real_value : string;
name_real_index : int;
};
}
>
>
2024-04-22 10:02:43 +02:00
{
return (
abstract_call(
"GET",
2024-04-30 01:32:40 +02:00
("/member/list" + ((search_term === null) ? "" : ("?search_term=" + search_term)))
2024-04-22 10:02:43 +02:00
)
);
}
/**
*/
export async function member_get(
id : int
2024-04-29 00:15:48 +02:00
) : Promise<
{
2024-04-30 01:32:40 +02:00
membership_number : string;
name_real_value : string;
name_real_index : int;
email_address_private : (null | string);
registered : boolean;
enabled : boolean;
email_use_veiled_address : boolean;
email_use_nominal_address : boolean;
email_redirect_to_private_address : boolean;
email_allow_sending : boolean;
password_set : boolean;
2024-04-30 08:46:19 +02:00
email_address_veiled : string;
email_address_nominal : string;
name_login : string;
2024-04-29 00:15:48 +02:00
}
>
{
return abstract_call(
"GET",
2024-04-30 01:32:40 +02:00
("/member/read/" + id.toFixed(0))
2024-04-29 00:15:48 +02:00
);
}
2024-04-22 10:02:43 +02:00
/**
*/
2024-04-30 01:32:40 +02:00
export async function member_project(
data : {
membership_number : string;
name_real_value : string;
email_address_private : (null | string);
}
2024-04-22 10:02:43 +02:00
) : Promise<int>
{
return abstract_call(
"POST",
2024-04-30 01:32:40 +02:00
"/member/project",
2024-04-22 10:02:43 +02:00
{
"data": {
2024-04-30 01:32:40 +02:00
"membership_number": data.membership_number,
"name_real_value": data.name_real_value,
"email_address_private": data.email_address_private,
2024-04-22 10:02:43 +02:00
}
}
);
}
/**
*/
2024-04-30 08:46:19 +02:00
export async function member_info(
2024-04-22 10:02:43 +02:00
id : int,
2024-04-30 01:32:40 +02:00
key : string
) : Promise<
{
name_real_value : string;
name_real_index : int;
name_login : string;
email_address_veiled : string;
email_address_nominal : string;
}
>
2024-04-22 10:02:43 +02:00
{
return abstract_call(
2024-04-30 01:32:40 +02:00
"GET",
lib_plankton.string.coin(
"/member/info/{{id}}?key={{key}}",
{
"id": id.toFixed(0),
"key": key,
2024-04-22 10:02:43 +02:00
}
2024-04-30 01:32:40 +02:00
)
2024-04-22 10:02:43 +02:00
);
}
2024-04-23 17:44:55 +02:00
/**
*/
2024-04-30 01:32:40 +02:00
export async function member_register(
2024-04-23 17:44:55 +02:00
id : int,
2024-04-30 01:32:40 +02:00
verification : string,
data : {
email_use_veiled_address : boolean;
email_use_nominal_address : boolean;
email_redirect_to_private_address : boolean;
password : (null | string);
}
2024-04-30 14:05:40 +02:00
) : Promise<
Array<
{
incident : string;
details : Record<string, any>;
}
>
>
2024-04-23 17:44:55 +02:00
{
return (
abstract_call(
"POST",
2024-04-30 01:32:40 +02:00
lib_plankton.string.coin(
"/member/register/{{id}}?key={{key}}",
{
"id": id.toFixed(0),
"key": verification,
}
),
2024-04-23 17:44:55 +02:00
{
2024-04-30 01:32:40 +02:00
"data": data,
2024-04-30 14:05:40 +02:00
"custom_response_handlers": {
409: (output_data_raw) => output_data_raw,
},
2024-04-23 17:44:55 +02:00
}
)
);
}
2024-04-22 10:02:43 +02:00
/**
*/
2024-04-30 01:32:40 +02:00
export async function member_modify(
2024-04-22 10:02:43 +02:00
id : int,
data : {
2024-04-30 01:32:40 +02:00
email_address_private : (null | string);
registered : boolean;
enabled : boolean;
2024-04-22 10:02:43 +02:00
}
) : Promise<void>
2024-04-30 01:32:40 +02:00
{
return abstract_call(
"PATCH",
("/member/modify/" + id.toFixed(0)),
{
"data": data
}
);
}
/**
*/
export async function member_summon(
id : int,
2024-05-01 09:53:59 +02:00
url_template : string
) : Promise<string>
2024-04-22 10:02:43 +02:00
{
return (
abstract_call(
"POST",
2024-04-30 01:32:40 +02:00
("/member/summon/" + id.toFixed(0)),
2024-04-22 10:02:43 +02:00
{
2024-04-30 01:32:40 +02:00
"data": {
2024-05-01 09:53:59 +02:00
"url_template": url_template,
2024-04-30 01:32:40 +02:00
},
2024-04-22 10:02:43 +02:00
}
)
);
}
/**
*/
export async function member_password_change_initialize(
identifier : string
) : Promise<string>
{
const url_template : string = (
"/"
+
lib_plankton.zoo_page.encode(
{
"name": "password_change_exec",
"parameters": {
"id": "{{id}}",
"token": "{{token}}",
}
}
)
);
return (
abstract_call(
"POST",
"/member/password_change/initialize",
{
"data": {
"identifier": identifier,
"url_template": url_template,
},
}
)
);
}
/**
*/
export async function member_password_change_execute(
id : int,
token : string,
password_new : string
) : Promise<
Array<
{
incident : string;
details : Record<string, any>;
}
>
>
{
return (
abstract_call(
"PATCH",
("/member/password_change/execute/" + id.toFixed(0)),
{
"data": {
"token": token,
"password_new": password_new,
},
"custom_response_handlers": {
409: (output_data_raw) => output_data_raw,
},
}
)
);
}
2024-04-22 10:02:43 +02:00
}