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;
|
|
|
|
} = {}
|
|
|
|
) : Promise<type_output>
|
|
|
|
{
|
|
|
|
options = Object.assign(
|
|
|
|
{
|
|
|
|
"data": null,
|
|
|
|
},
|
|
|
|
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 => {
|
|
|
|
if ((x.status >= 200) && (x.status < 300)) {
|
|
|
|
return x.json();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Promise.reject<type_output>(new Error("unexpected response status code"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
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(
|
|
|
|
) : Promise<Array<any>>
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
abstract_call(
|
|
|
|
"GET",
|
|
|
|
"/member"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
export async function member_get(
|
|
|
|
id : int
|
|
|
|
) : Promise<any>
|
|
|
|
{
|
|
|
|
return abstract_call(
|
|
|
|
"GET",
|
|
|
|
"/member/" + id.toFixed(0)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
export async function member_add(
|
|
|
|
data : Record<string, any>
|
|
|
|
) : Promise<int>
|
|
|
|
{
|
|
|
|
return abstract_call(
|
|
|
|
"POST",
|
|
|
|
"/member",
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"membership_number": data["membership_number"],
|
|
|
|
"enabled": data["enabled"],
|
|
|
|
"name_real_value": data["name_real_value"],
|
|
|
|
"name_real_extension": data["name_real_extension"],
|
|
|
|
"name_display": data["name_display"],
|
|
|
|
"name_login": data["name_login"],
|
|
|
|
"password_image": null,
|
|
|
|
"email_address_private_value": data["email_address_private_value"],
|
|
|
|
"email_address_numberbased_use": data["email_address_numberbased_use"],
|
|
|
|
"email_address_namebased_use": data["email_address_namebased_use"],
|
|
|
|
"email_redirect_to_private": data["email_redirect_to_private"],
|
|
|
|
"salutation": data["salutation"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
export async function member_modify(
|
|
|
|
id : int,
|
|
|
|
data : Record<string, any>
|
|
|
|
) : Promise<void>
|
|
|
|
{
|
|
|
|
return abstract_call(
|
|
|
|
"PATCH",
|
|
|
|
"/member/" + id.toFixed(0),
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"membership_number": data["membership_number"],
|
|
|
|
"enabled": data["enabled"],
|
|
|
|
"name_real_value": data["name_real_value"],
|
|
|
|
"name_real_extension": data["name_real_extension"],
|
|
|
|
"name_display": data["name_display"],
|
|
|
|
"name_login": data["name_login"],
|
|
|
|
"password_image": null,
|
|
|
|
"email_address_private_value": data["email_address_private_value"],
|
|
|
|
"email_address_numberbased_use": data["email_address_numberbased_use"],
|
|
|
|
"email_address_namebased_use": data["email_address_namebased_use"],
|
|
|
|
"email_redirect_to_private": data["email_redirect_to_private"],
|
|
|
|
"salutation": data["salutation"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-23 17:44:55 +02:00
|
|
|
/**
|
|
|
|
*/
|
|
|
|
export async function member_urge_for_registration(
|
|
|
|
id : int,
|
|
|
|
url : string
|
|
|
|
) : Promise<null>
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
abstract_call(
|
|
|
|
"POST",
|
|
|
|
"/member/urge_for_registration",
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"id": id,
|
|
|
|
"url": url,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-22 10:02:43 +02:00
|
|
|
/**
|
|
|
|
*/
|
|
|
|
export async function member_register(
|
|
|
|
id : int,
|
|
|
|
verification : string,
|
|
|
|
data : {
|
|
|
|
name_login : string;
|
|
|
|
name_display : string;
|
|
|
|
salutation : string;
|
|
|
|
email_mode : ("none" | "number" | "number_and_name");
|
|
|
|
email_redirect : boolean;
|
|
|
|
password : string;
|
|
|
|
}
|
|
|
|
) : Promise<void>
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
abstract_call(
|
|
|
|
"POST",
|
|
|
|
"/member/register/" + id.toFixed(0) + "?verification=" + verification,
|
|
|
|
{
|
|
|
|
"data": data,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|