Wrapping up for the night at 4 AM. Made lots of progress with the Journals module. Should have saved more often.

This commit is contained in:
Scott Idem
2025-03-16 04:04:58 -04:00
parent d8020b3d77
commit b62a267ee8
23 changed files with 2340 additions and 1182 deletions

View File

@@ -0,0 +1,425 @@
import type { key_val } from '$lib/ae_stores';
import { api } from '$lib/api';
import { db_journals } from "$lib/ae_journals/db_journals";
let ae_promises: key_val = {};
// Updated 2025-03-15
export async function load_ae_obj_id__journal_entry(
{
api_cfg,
journal_entry_id,
try_cache = true,
log_lvl = 0
}: {
api_cfg: any,
journal_entry_id: string,
try_cache?: boolean,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** load_ae_obj_id__journal_entry() *** journal_entry_id=${journal_entry_id}`);
}
let params = {};
ae_promises.load__journal_entry_obj = await api.get_ae_obj_id_crud({
api_cfg: api_cfg,
obj_type: 'journal_entry',
obj_id: journal_entry_id, // NOTE: This is the FQDN, not normally the ID.
use_alt_table: true, // 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(async function (journal_entry_obj_get_result) {
if (journal_entry_obj_get_result) {
if (try_cache) {
// This is expecting a list
await db_save_ae_obj_li__journal_entry({
obj_type: 'journal_entry',
obj_li: [journal_entry_obj_get_result]
});
}
return journal_entry_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__journal_entry_obj;
}
// Updated 2025-03-15
export async function load_ae_obj_li__journal_entry(
{
api_cfg,
for_obj_type = 'journal',
for_obj_id,
enabled = 'enabled',
hidden = 'not_hidden',
limit = 99,
offset = 0,
order_by_li = {'priority': 'DESC', 'sort': 'DESC', 'name': 'ASC', 'updated_on': 'DESC', 'created_on': 'DESC'},
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any,
for_obj_type: string,
for_obj_id: string,
enabled?: string,
hidden?: string,
limit?: number,
offset?: number,
order_by_li?: key_val,
params?: key_val,
try_cache?: boolean,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** load_ae_obj_li__journal_entry() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_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('params_json:', params_json);
ae_promises.load__journal_entry_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({
api_cfg: api_cfg,
obj_type: 'journal_entry',
for_obj_type: for_obj_type,
for_obj_id: for_obj_id,
use_alt_tbl: false,
use_alt_mdl: false,
use_alt_exp: false,
enabled: enabled,
hidden: hidden,
order_by_li: order_by_li,
limit: limit,
offset: offset,
params_json: params_json,
params: params,
log_lvl: log_lvl
})
.then(async function (journal_entry_obj_li_get_result) {
if (journal_entry_obj_li_get_result) {
if (try_cache) {
await db_save_ae_obj_li__journal_entry({
obj_type: 'journal_entry', obj_li: journal_entry_obj_li_get_result
});
}
return journal_entry_obj_li_get_result;
} else {
return [];
}
})
.catch(function (error) {
console.log('No results returned or failed.', error);
});
if (log_lvl) {
console.log('ae_promises.load__journal_entry_obj_li:', ae_promises.load__journal_entry_obj_li);
}
return ae_promises.load__journal_entry_obj_li;
}
// Updated 2025-03-15
export async function create_ae_obj__journal_entry(
{
api_cfg,
journal_id,
data_kv,
params={},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any,
journal_id: string,
data_kv: key_val,
params?: key_val,
try_cache?: boolean,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** create_ae_obj__journal_entry() *** journal_id=${journal_id}`);
}
ae_promises.create__journal_entry = await api.create_ae_obj_crud({
api_cfg: api_cfg,
obj_type: 'journal_entry',
fields: {
journal_id_random: journal_id,
...data_kv
},
key: api_cfg.api_crud_super_key,
params: params,
return_obj: true,
log_lvl: log_lvl
})
.then(async function (journal_entry_obj_create_result) {
if (journal_entry_obj_create_result) {
if (try_cache) {
await db_save_ae_obj_li__journal_entry(
{
obj_type: 'journal_entry',
obj_li: [journal_entry_obj_create_result]
});
}
return journal_entry_obj_create_result;
} else {
return null;
}
})
.catch(function (error) {
console.log('No results returned or failed.', error);
});
if (log_lvl) {
console.log('ae_promises.create__journal_entry:', ae_promises.create__journal_entry);
}
return ae_promises.create__journal_entry;
}
// Updated 2025-03-15
export async function delete_ae_obj_id__journal_entry(
{
api_cfg,
journal_entry_id,
method = 'delete', // 'delete', 'disable', 'hide'
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any,
journal_entry_id: string,
method?: string,
params?: key_val,
try_cache?: boolean,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** delete_ae_obj_id__journal_entry() *** journal_entry_id=${journal_entry_id}`);
}
ae_promises.delete__journal_entry_obj = await api.delete_ae_obj_id_crud({
api_cfg: api_cfg,
obj_type: 'journal_entry',
obj_id: journal_entry_id,
key: api_cfg.api_crud_super_key,
params: params,
method: method,
log_lvl: log_lvl
})
.catch(function (error) {
console.log('No results returned or failed.', error);
})
.finally(function () {
if (try_cache) {
if (log_lvl) {
console.log(`Attempting to remove IDB entry for journal_entry_id=${journal_entry_id}`);
}
db_journals.journal_entry.delete(journal_entry_id); // Delete from the DB no matter what.
}
});
if (log_lvl) {
console.log('ae_promises.delete__journal_entry_obj:', ae_promises.delete__journal_entry_obj);
}
return ae_promises.delete__journal_entry_obj;
}
// Updated 2025-03-15
export async function update_ae_obj__journal_entry(
{
api_cfg,
journal_entry_id,
data_kv,
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any,
journal_entry_id: string,
data_kv: key_val,
params?: key_val,
try_cache?: boolean,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** update_ae_obj__journal_entry() *** journal_entry_id=${journal_entry_id}`, data_kv);
}
ae_promises.update__journal_entry_obj = await api.update_ae_obj_id_crud({
api_cfg: api_cfg,
obj_type: 'journal_entry',
obj_id: journal_entry_id,
fields: data_kv,
key: api_cfg.api_crud_super_key,
params: params,
return_obj: true,
log_lvl: log_lvl
})
.then(async function (journal_entry_obj_update_result) {
if (journal_entry_obj_update_result) {
if (try_cache) {
await db_save_ae_obj_li__journal_entry({
obj_type: 'journal_entry', obj_li: [journal_entry_obj_update_result]
});
}
return journal_entry_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__journal_entry_obj:', ae_promises.update__journal_entry_obj);
}
return ae_promises.update__journal_entry_obj;
}
// This function will loop through the journal_entry_obj_li and save each one to the DB.
// Updated 2025-03-15
export async function db_save_ae_obj_li__journal_entry(
{
obj_type,
obj_li,
log_lvl = 0
}: {
obj_type: string,
obj_li: any,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** db_save_ae_obj_li__journal_entry() ***`);
}
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_journals.journal_entry.put({
id: obj.journal_entry_id_random,
journal_entry_id: obj.journal_entry_id_random,
journal_id: obj.journal_id_random,
code: obj.code,
for_type: obj.for_type,
for_id: obj.for_id,
journal_entry_type: obj.journal_entry_type,
person_id: obj.person_id_random,
name: obj.name,
summary: obj.summary,
outline: obj.outline,
// description: obj.description,
content: obj.content,
content_html: obj.content_html,
content_json: obj.content_json,
// url: obj.url,
// url_text: obj.url_text,
// hosted_file_id: obj.hosted_file_id_random,
// file_path: obj.file_path,
// filename: obj.filename,
// file_extension: obj.file_extension,
// start_datetime: obj.start_datetime,
// end_datetime: obj.end_datetime,
// timezone: obj.timezone,
// original_datetime: obj.original_datetime,
// original_timezone: obj.original_timezone,
// original_location: obj.original_location,
// original_url: obj.original_url,
// original_url_text: obj.original_url_text,
// enable_for_public: obj.enable_for_public,
alert: obj.alert,
alert_msg: obj.alert_msg,
// cfg_json: obj.cfg_json,
data_json: obj.data_json,
// This only allows for basic access to the data.
// passcode_read: obj.passcode_read, // For LLM (AI) generated summary...???
// passcode_read_expire: obj.passcode_read_expire,
// passcode_write: obj.passcode_write,
// passcode_write_expire: obj.passcode_write_expire,
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,
// Generated fields for sorting locally only
// tmp_sort_1: `${obj.original_datetime}_${obj.group}_${obj.priority}_${obj.sort}`,
// tmp_sort_2: `${obj.group}_${obj.original_datetime}_${obj.priority}_${obj.sort}`,
// From SQL view
journal_code: obj.journal_code,
journal_name: obj.journal_name,
// A key value list of the others
// journal_other_kv: obj.journal_other_kv,
// journal_other_li: obj.journal_other_li,
});
// console.log(`Put obj with ID: ${obj.journal_entry_id_random} or ${id_random}`);
} catch (error) {
let status = `Failed to put ${obj.journal_entry_id_random}: ${error}`;
console.log(status);
}
// const id_random = await db_journals.journal_entry.put(obj);
// console.log(`Put obj with ID: ${obj.journal_entry_id_random}`);
});
return true;
}
}