/* Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Backend Copyright (C) 2024 Christian Fraß This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ namespace _espe.sample { /** */ type type_data = { groups : Array< { id : int; name : string; } >; admins : Array< { id : int; name : string; email_address : (null | string); password : string; } >; members : Array< { id : int; name : string; email_address : (null | string); groups : Array; } >; invitations : Array< { id : int; name_changeable : boolean; name_value : string; email_address_changeable : boolean; email_address_value : (null | string); groups_changeable : boolean; groups_value : Array; } >; }; /** */ export async function fill( data : type_data ) : Promise { const track_groups : Map = new Map(); // groups { for (const group_raw of data.groups) { const group_id : _espe.type.group_id = await _espe.service.group.add( { "name": group_raw.name, } ); track_groups.set(group_raw.id, group_id); } } // admins { for (const admin_raw of data.admins) { const admin_id : _espe.type.admin_id = await _espe.service.admin.add( admin_raw.name, admin_raw.email_address, admin_raw.password, ); } } // members { for (const member_raw of data.members) { const member_id : _espe.type.member_id = await _espe.service.member.project( { "name": member_raw.name, "email_address": member_raw.email_address, "groups": member_raw.groups.map(track_groups.get), }, { "silent": true, } ); } /** * @todo passwords */ } // invitations { for (const invitation_raw of data.invitations) { const result : {id : _espe.type.invitation_id; key : _espe.type.invitation_key;} = await _espe.service.invitation.create( { "name_changeable": invitation_raw.name_changeable, "name_value": invitation_raw.name_value, "email_address_changeable": invitation_raw.email_address_changeable, "email_address_value": invitation_raw.email_address_value, "groups_changeable": invitation_raw.groups_changeable, "groups_value": invitation_raw.groups_value.map(track_groups.get), } ); } } } /** */ export async function fill_by_path( path : string ) : Promise { const content : string = await lib_plankton.file.read(path); const data : type_data = (lib_plankton.json.decode(content) as type_data); await fill(data); } }