320 lines
9.9 KiB
TypeScript
320 lines
9.9 KiB
TypeScript
import type { key_val } from '$lib/ae_stores';
|
|
import { api } from '$lib/api';
|
|
|
|
import { db_core } from "$lib/db_core";
|
|
|
|
let ae_promises: key_val = {};
|
|
|
|
|
|
// Updated 2024-07-17
|
|
export async function handle_load_ae_obj_id__person(
|
|
{
|
|
api_cfg,
|
|
person_id,
|
|
try_cache=false,
|
|
log_lvl=0
|
|
}: {
|
|
api_cfg: any,
|
|
person_id: string,
|
|
try_cache?: boolean,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
console.log(`*** handle_load_ae_obj_id__person() *** person_id=${person_id}`);
|
|
|
|
let params = {};
|
|
|
|
ae_promises.load__person_obj = await api.get_ae_obj_id_crud({
|
|
api_cfg: api_cfg,
|
|
obj_type: 'person',
|
|
obj_id: person_id, // NOTE: This is the FQDN, not normally the ID.
|
|
use_alt_table: false, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
|
|
use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value
|
|
params: params,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then(function (person_obj_get_result) {
|
|
if (person_obj_get_result) {
|
|
// This is expecting a list
|
|
handle_db_save_ae_obj_li__person({obj_type: 'person', obj_li: [person_obj_get_result]});
|
|
return person_obj_get_result;
|
|
} else {
|
|
console.log('No results returned.');
|
|
return null;
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
console.log('No results returned or failed.', error);
|
|
});
|
|
|
|
return ae_promises.load__person_obj;
|
|
}
|
|
|
|
|
|
// Updated 2024-07-17
|
|
export async function handle_load_ae_obj_li__person(
|
|
{
|
|
api_cfg,
|
|
account_id,
|
|
params={},
|
|
try_cache=true,
|
|
log_lvl=0
|
|
}: {
|
|
api_cfg: any,
|
|
account_id: string,
|
|
params?: key_val,
|
|
try_cache?: boolean,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
console.log(`*** handle_load_ae_obj_li__person() *** account_id=${account_id}`);
|
|
|
|
let enabled: string = (params.qry__enabled ?? 'enabled'); // all, disabled, enabled
|
|
let hidden: string = (params.qry__hidden ?? 'not_hidden'); // all, hidden, not_hidden
|
|
let limit: number = (params.qry__limit ?? 99); // 99
|
|
let offset: number = (params.qry__offset ?? 0); // 0
|
|
|
|
|
|
let params_json: key_val = {};
|
|
|
|
// console.log('params_json:', params_json);
|
|
|
|
ae_promises.load__person_obj_li = await api.get_ae_obj_li_for_obj_id_crud({
|
|
api_cfg: api_cfg,
|
|
obj_type: 'person',
|
|
for_obj_type: 'account',
|
|
for_obj_id: account_id,
|
|
use_alt_table: false, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
|
|
use_alt_base: false, // NOTE: This will use the base_name_alt value instead of the base_name value
|
|
enabled: enabled,
|
|
hidden: hidden,
|
|
order_by_li: {'given_name': 'ASC', 'family_name': 'ASC', 'updated_on': 'DESC', 'created_on': 'DESC'},
|
|
limit: limit,
|
|
offset: offset,
|
|
params_json: params_json,
|
|
params: params,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then(function (person_obj_li_get_result) {
|
|
if (person_obj_li_get_result) {
|
|
handle_db_save_ae_obj_li__person({obj_type: 'person', obj_li: person_obj_li_get_result});
|
|
return person_obj_li_get_result;
|
|
} else {
|
|
return [];
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
console.log('No results returned or failed.', error);
|
|
});
|
|
|
|
console.log('ae_promises.load__person_obj_li:', ae_promises.load__person_obj_li);
|
|
return ae_promises.load__person_obj_li;
|
|
}
|
|
|
|
|
|
// Updated 2024-06-24
|
|
export async function handle_create_ae_obj__person(
|
|
{
|
|
api_cfg,
|
|
user_id,
|
|
data_kv,
|
|
params={},
|
|
log_lvl=0
|
|
}: {
|
|
api_cfg: any,
|
|
user_id?: string,
|
|
data_kv: key_val,
|
|
params?: key_val,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
console.log(`*** handle_create_ae_obj__person() *** user_id=${user_id}`);
|
|
|
|
ae_promises.create__person = await api.create_ae_obj_crud({
|
|
api_cfg: api_cfg,
|
|
obj_type: 'person',
|
|
fields: {
|
|
user_id_random: user_id,
|
|
...data_kv
|
|
},
|
|
key: api_cfg.api_crud_super_key,
|
|
params: params,
|
|
return_obj: true,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then(function (person_obj_create_result) {
|
|
if (person_obj_create_result) {
|
|
handle_db_save_ae_obj_li__person(
|
|
{
|
|
obj_type: 'person', obj_li: [person_obj_create_result]
|
|
});
|
|
return person_obj_create_result;
|
|
} else {
|
|
return null;
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
console.log('No results returned or failed.', error);
|
|
})
|
|
.finally(function () {
|
|
});
|
|
|
|
if (log_lvl) {
|
|
console.log('ae_promises.create__person:', ae_promises.create__person);
|
|
}
|
|
return ae_promises.create__person;
|
|
}
|
|
|
|
|
|
// Updated 2024-07-17
|
|
export async function handle_update_ae_obj__person(
|
|
{
|
|
api_cfg,
|
|
person_id,
|
|
data_kv,
|
|
params={},
|
|
log_lvl=0
|
|
}: {
|
|
api_cfg: any,
|
|
person_id: string,
|
|
data_kv: key_val,
|
|
params?: key_val,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
console.log(`*** handle_update_ae_obj__person() *** person_id=${person_id}`);
|
|
|
|
ae_promises.update__person_obj = await api.update_ae_obj_id_crud({
|
|
api_cfg: api_cfg,
|
|
obj_type: 'person',
|
|
obj_id: person_id, // NOTE: This is the FQDN, not normally the ID.
|
|
fields: data_kv,
|
|
key: api_cfg.api_crud_super_key,
|
|
params: params,
|
|
return_obj: true,
|
|
log_lvl: log_lvl
|
|
})
|
|
.then(function (person_obj_update_result) {
|
|
if (person_obj_update_result) {
|
|
handle_db_save_ae_obj_li__person({obj_type: 'person', obj_li: [person_obj_update_result]});
|
|
return person_obj_update_result;
|
|
} else {
|
|
return null;
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
console.log('No results returned or failed.', error);
|
|
})
|
|
.finally(function () {
|
|
});
|
|
|
|
if (log_lvl) {
|
|
console.log('ae_promises.update__person_obj:', ae_promises.update__person_obj);
|
|
}
|
|
return ae_promises.update__person_obj;
|
|
}
|
|
|
|
|
|
|
|
// Updated 2024-06-10
|
|
export function handle_db_save_ae_obj_li__person(
|
|
{
|
|
obj_type,
|
|
obj_li,
|
|
log_lvl=0
|
|
}: {
|
|
obj_type: string,
|
|
obj_li: any,
|
|
log_lvl?: number
|
|
}
|
|
) {
|
|
if (log_lvl) {
|
|
console.log(`*** handle_db_save_ae_obj_li__person() ***`);
|
|
}
|
|
|
|
if (obj_li && obj_li.length) {
|
|
obj_li.forEach(async function (obj: any) {
|
|
if (log_lvl) {
|
|
console.log(`ae_obj ${obj_type}:`, obj);
|
|
}
|
|
|
|
try {
|
|
const id_random = await db_core.person.put({
|
|
id: obj.person_id_random,
|
|
// id_random: obj.person_id_random,
|
|
person_id: obj.person_id_random,
|
|
person_id_random: obj.person_id_random,
|
|
|
|
external_id: obj.external_id,
|
|
external_sys_id: obj.external_sys_id,
|
|
code: obj.code,
|
|
|
|
account_id: obj.account_id_random,
|
|
account_id_random: obj.account_id_random,
|
|
|
|
person_profile_id: obj.person_profile_id_random,
|
|
person_profile_id_random: obj.person_profile_id_random, // The new table person_profile will be used soon...
|
|
|
|
user_id: obj.user_id_random,
|
|
user_id_random: obj.user_id_random,
|
|
|
|
pronouns: obj.pronouns,
|
|
informal_name: obj.informal_name,
|
|
title_names: obj.title_names,
|
|
given_name: obj.given_name,
|
|
middle_name: obj.middle_name,
|
|
family_name: obj.family_name,
|
|
designations: obj.designations,
|
|
|
|
professional_title: obj.professional_title,
|
|
|
|
full_name: obj.full_name,
|
|
|
|
affiliations: obj.affiliations,
|
|
|
|
primary_email: obj.primary_email,
|
|
|
|
biography: obj.biography,
|
|
|
|
agree: obj.agree,
|
|
comments: obj.comments,
|
|
|
|
allow_auth_key: obj.allow_auth_key, // For sign in without password
|
|
// auth_key: obj.auth_key,
|
|
passcode: obj.passcode,
|
|
|
|
data_json: obj.data_json,
|
|
|
|
enable: obj.enable,
|
|
hide: obj.hide,
|
|
priority: obj.priority,
|
|
sort: obj.sort,
|
|
group: obj.group,
|
|
notes: obj.notes,
|
|
created_on: obj.created_on,
|
|
updated_on: obj.updated_on,
|
|
|
|
// From SQL view
|
|
username: obj.username,
|
|
user_name: obj.user_name,
|
|
user_email: obj.user_email,
|
|
user_allow_auth_key: obj.user_allow_auth_key, // For sign in without password
|
|
user_super: obj.user_super,
|
|
user_manager: obj.user_manager,
|
|
user_administrator: obj.user_administrator,
|
|
user_public: obj.user_public,
|
|
});
|
|
// console.log(`Put obj with ID: ${obj.person_id_random} or ${id_random}`);
|
|
} catch (error) {
|
|
let status = `Failed to put ${obj.person_id_random}: ${error}`;
|
|
console.log(status);
|
|
}
|
|
|
|
// const id_random = await db_core.person.put(obj);
|
|
// console.log(`Put obj with ID: ${obj.person_id_random}`);
|
|
});
|
|
|
|
return true;
|
|
}
|
|
}
|