backend/source/repositories/user.ts

207 lines
3.9 KiB
TypeScript
Raw Normal View History

2025-09-25 17:18:16 +02:00
/*
This file is part of »zeitbild«.
Copyright 2025 'kcf' <fenris@folksprak.org>
»zeitbild« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»zeitbild« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with »zeitbild«. If not, see <http://www.gnu.org/licenses/>.
*/
2024-09-18 18:17:25 +02:00
namespace _zeitbild.repository.user
{
2025-10-17 13:16:56 +02:00
/**
*/
type type_row = {
name : string;
email_address : (null | string);
dav_token : (null | string);
};
/**
*/
type type_preview = {
name : string;
};
2024-09-18 18:17:25 +02:00
/**
*/
var _store : (
null
|
lib_plankton.storage.type_store<
2024-09-21 11:05:24 +02:00
_zeitbild.type_user_id,
2025-10-17 13:16:56 +02:00
/*type_row*/Record<string, any>,
2024-09-18 18:17:25 +02:00
{},
lib_plankton.storage.type_sql_table_autokey_search_term,
2025-10-17 13:16:56 +02:00
/*type_preview*/Record<string, any>
2024-09-18 18:17:25 +02:00
>
) = null;
/**
*/
function get_store(
2025-10-17 13:16:56 +02:00
)
: lib_plankton.storage.type_store<
2024-09-21 11:05:24 +02:00
_zeitbild.type_user_id,
2025-10-17 13:16:56 +02:00
/*type_row*/Record<string, any>,
2024-09-18 18:17:25 +02:00
{},
lib_plankton.storage.type_sql_table_autokey_search_term,
2025-10-17 13:16:56 +02:00
/*type_preview*/Record<string, any>
2024-09-18 18:17:25 +02:00
>
{
2025-10-17 13:16:56 +02:00
if (_store === null)
{
2024-09-18 18:17:25 +02:00
_store = lib_plankton.storage.sql_table_autokey_store(
{
"database_implementation": _zeitbild.database.get_implementation(),
"table_name": "users",
"key_name": "id",
}
);
}
2025-10-17 13:16:56 +02:00
else
{
2024-09-18 18:17:25 +02:00
// do nothing
}
return _store;
}
/**
*/
function encode(
2024-09-21 11:05:24 +02:00
user_object : _zeitbild.type_user_object
2025-10-17 13:16:56 +02:00
)
: type_row
2024-09-18 18:17:25 +02:00
{
return {
"name": user_object.name,
"email_address": user_object.email_address,
"dav_token": user_object.dav_token,
2024-09-18 18:17:25 +02:00
};
}
/**
*/
function decode(
2025-10-17 13:16:56 +02:00
row : type_row
)
: _zeitbild.type_user_object
2024-09-18 18:17:25 +02:00
{
return {
2025-10-17 13:16:56 +02:00
"name": row.name,
"email_address": row.email_address,
"dav_token": row.dav_token,
2024-09-18 18:17:25 +02:00
};
}
/**
*/
export async function list(
2025-10-17 13:16:56 +02:00
)
: Promise<
Array<
{
id : _zeitbild.type_user_id;
name : string;
}
>
>
{
2025-10-17 13:16:56 +02:00
const hits : Array<{key : int; preview : /*type_preview*/Record<string, any>;}> = await get_store().search({"expression": "TRUE", "arguments": {}});
return Promise.resolve(
hits
.map(
(hit) => ({
"id": hit.key,
"name": hit.preview.name,
})
)
);
}
2024-09-18 18:17:25 +02:00
/**
*/
export async function read(
2024-09-21 11:05:24 +02:00
user_id : _zeitbild.type_user_id
2025-10-17 13:16:56 +02:00
)
: Promise<_zeitbild.type_user_object>
2024-09-18 18:17:25 +02:00
{
2025-10-17 13:16:56 +02:00
const row : type_row = ((await get_store().read(user_id)) as type_row);
2024-09-21 11:05:24 +02:00
const user_object : _zeitbild.type_user_object = decode(row);
return Promise.resolve<_zeitbild.type_user_object>(user_object);
2024-09-18 18:17:25 +02:00
}
/**
*/
export async function create(
2024-09-21 11:05:24 +02:00
user_object : _zeitbild.type_user_object
2025-10-17 13:16:56 +02:00
)
: Promise<_zeitbild.type_user_id>
2024-09-18 18:17:25 +02:00
{
2025-10-17 13:16:56 +02:00
const row : type_row = encode(user_object);
2024-09-21 11:05:24 +02:00
const user_id : _zeitbild.type_user_id = await get_store().create(row);
return Promise.resolve<_zeitbild.type_user_id>(user_id);
2024-09-18 18:17:25 +02:00
}
/**
*/
export async function update(
user_id : _zeitbild.type_user_id,
user_object : _zeitbild.type_user_object
2025-10-17 13:16:56 +02:00
)
: Promise<void>
{
2025-10-17 13:16:56 +02:00
const row : type_row = encode(user_object);
await get_store().update(user_id, row);
return Promise.resolve<void>(undefined);
}
2024-09-18 18:17:25 +02:00
/**
*/
export async function identify(
name : string
2025-10-17 13:16:56 +02:00
)
: Promise<_zeitbild.type_user_id>
2024-09-18 18:17:25 +02:00
{
2025-10-17 13:16:56 +02:00
const hits : Array<{key : _zeitbild.type_user_id; preview : /*type_preview*/Record<string, any>;}> = await get_store().search(
2024-09-18 18:17:25 +02:00
{
"expression": "(name = $name)",
"arguments": {
"name": name,
}
}
);
2025-10-17 13:16:56 +02:00
if (hits.length <= 0)
{
2024-09-21 11:05:24 +02:00
return Promise.reject<_zeitbild.type_user_id>(new Error("not found"));
2024-09-18 18:17:25 +02:00
}
2025-10-17 13:16:56 +02:00
else
{
2024-09-21 11:05:24 +02:00
return Promise.resolve<_zeitbild.type_user_id>(hits[0].key);
2024-09-18 18:17:25 +02:00
}
}
}