Code clean up. Journal IDB save has been updated to properly await.

This commit is contained in:
Scott Idem
2025-05-09 13:44:19 -04:00
parent 8c81f6f65e
commit 8f1f7bfa7e
5 changed files with 87 additions and 68 deletions

View File

@@ -115,6 +115,7 @@ export async function load_ae_obj_li__journal(
api_cfg,
for_obj_type = 'account',
for_obj_id,
qry_person_id = null,
inc_entry_li = false,
enabled = 'enabled',
hidden = 'not_hidden',
@@ -128,6 +129,7 @@ export async function load_ae_obj_li__journal(
api_cfg: any,
for_obj_type: string,
for_obj_id: string,
qry_person_id?: string|null,
inc_entry_li?: boolean,
enabled?: string, // all, disabled, enabled
hidden?: string, // all, hidden, not_hidden
@@ -145,7 +147,20 @@ export async function load_ae_obj_li__journal(
let params_json: key_val = {};
// console.log('params_json:', params_json);
if (qry_person_id) {
let qry_param =
{
type: "AND",
field: "person_id_random",
operator: "=",
value: qry_person_id
};
params_json['qry'].push(qry_param);
}
if (log_lvl) {
console.log('params_json:', params_json);
}
ae_promises.load__journal_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({
api_cfg: api_cfg,
@@ -341,28 +356,28 @@ export async function delete_ae_obj_id__journal(
}
// Updated 2025-03-15
export async function update_ae_obj__journal(
{
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
}
) {
// Updated 2025-05-09
export async function update_ae_obj__journal({
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(`*** update_ae_obj__journal() *** journal_id=${journal_id}`, data_kv);
}
ae_promises.update__journal_obj = await api.update_ae_obj_id_crud({
// Perform the API update
const result = await api.update_ae_obj_id_crud({
api_cfg: api_cfg,
obj_type: 'journal',
obj_id: journal_id,
@@ -370,31 +385,25 @@ export async function update_ae_obj__journal(
key: api_cfg.api_crud_super_key,
params: params,
return_obj: true,
log_lvl: log_lvl
})
.then(function (journal_obj_update_result) {
if (journal_obj_update_result) {
if (try_cache) {
db_save_ae_obj_li__journal({
obj_type: 'journal', obj_li: [journal_obj_update_result],
log_lvl: log_lvl
});
}
return journal_obj_update_result;
} else {
return null;
}
})
.catch(function (error) {
console.log('No results returned or failed.', error);
})
.finally(function () {
log_lvl: log_lvl,
});
if (log_lvl) {
console.log('ae_promises.update__journal_obj:', ae_promises.update__journal_obj);
// Handle the result
if (result) {
if (try_cache) {
console.log('Saving to DB...');
await db_save_ae_obj_li__journal({
obj_type: 'journal',
obj_li: [result],
log_lvl: log_lvl,
});
console.log('DB save completed.');
}
return result;
} else {
console.error('Failed to update journal.');
return null;
}
return ae_promises.update__journal_obj;
}
@@ -517,8 +526,8 @@ export async function qry__journal(
// This function will loop through the journal_obj_li and save each one to the DB.
// Updated 2025-03-15
export function db_save_ae_obj_li__journal(
// Updated 2025-05-09
export async function db_save_ae_obj_li__journal(
{
obj_type,
obj_li,
@@ -530,11 +539,13 @@ export function db_save_ae_obj_li__journal(
}
) {
if (log_lvl) {
console.log(`*** db_save_ae_obj_li__journal() ***`);
console.log(`*** db_save_ae_obj_li__journal() *** obj_type=${obj_type}`, obj_li);
}
if (obj_li && obj_li.length) {
obj_li.forEach(async function (obj: any) {
let obj_li_id: string[] = [];
for (const obj of obj_li) {
// obj_li.forEach(async function (obj: any) {
if (log_lvl) {
console.log(`ae_obj ${obj_type}:`, obj);
}
@@ -648,8 +659,8 @@ export function db_save_ae_obj_li__journal(
console.log(`Saved record with ID: ${obj_record.id}`);
}
}
});
}
return true;
return obj_li_id;
}
}

View File

@@ -324,6 +324,8 @@ export async function delete_ae_obj_id__journal_entry(
// return await ae_promises.update__journal_entry_obj;
// }
// Updated 2025-05-09
export async function update_ae_obj__journal_entry(
{
api_cfg,
@@ -344,6 +346,7 @@ export async function update_ae_obj__journal_entry(
console.log(`*** update_ae_obj__journal_entry() *** journal_entry_id=${journal_entry_id}`, data_kv);
}
// Perform the API update
const result = await api.update_ae_obj_id_crud({
api_cfg: api_cfg,
obj_type: 'journal_entry',
@@ -355,6 +358,7 @@ export async function update_ae_obj__journal_entry(
log_lvl: log_lvl,
});
// Handle the result
if (result) {
if (try_cache) {
console.log('Saving to DB...');
@@ -374,7 +378,7 @@ export async function update_ae_obj__journal_entry(
// This function will loop through the journal_entry_obj_li and save each one to the DB.
// Updated 2025-03-15
// Updated 2025-05-09
export async function db_save_ae_obj_li__journal_entry(
{
obj_type,

View File

@@ -36,10 +36,16 @@ export async function load({ fetch, parent }) {
ae_acct.slct.journal_obj_li = load_journal_obj_li;
}
// console.log(`ae_acct = `, ae_acct);
// WARNING: This does not currently work because the person_id has not been set yet.
let person_id = ae_acct.loc.person_id;
// console.log(`person_id = `, person_id);
let load_journal_obj_li = await journals_func.load_ae_obj_li__journal({
api_cfg: ae_acct.api,
for_obj_type: 'account',
for_obj_id: account_id,
qry_person_id: person_id,
inc_entry_li: true,
hidden: 'all', // 'not_hidden'
enabled: 'enabled',
@@ -47,7 +53,7 @@ export async function load({ fetch, parent }) {
limit: 25,
// params: ae_params,
try_cache: true,
log_lvl: log_lvl
log_lvl: 2
});
ae_acct.slct.journal_obj_li = load_journal_obj_li;

View File

@@ -101,25 +101,13 @@ let lq__journal_entry_obj = $derived(liveQuery(async () => {
"
>
<!-- <h1>Journals {$lq__journal_entry_obj?.name} - {$lq__journal_entry_obj_li?.length}</h1> -->
<!-- <a href="/journals/{$lq__journal_entry_obj?.journal_id}" class="novi_btn btn btn-secondary btn-sm
variant-ghost-tertiary
hover:variant-filled-tertiary
transition
">
<span class="fas fa-arrow-left m-1"></span>
Back to Journal Entries
</a> -->
{#if $lq__journal_entry_obj}
<!-- {#if $lq__journal_entry_obj} -->
<Journal_entry_view
lq__journal_obj={lq__journal_obj}
lq__journal_obj_li={lq__journal_obj_li}
lq__journal_entry_obj={lq__journal_entry_obj}
/>
{/if}
<!-- {/if} -->
</section>

View File

@@ -796,7 +796,14 @@ function handle_marked(text_string: string) {
</script>
<section class="svelte_component ae_section ae_view journal_entry_obj view__journal_entry_obj bg-white flex flex-col flex-grow items-center justify-center rounded-lg w-full h-full p-2 m-2 space-y-2" bind:clientHeight={$ae_loc.iframe_height_modal_body}>
<section
class="
svelte_component ae_section ae_view journal_entry_obj view__journal_entry_obj bg-white
flex flex-col flex-grow items-center justify-start
w-full h-full p-2 m-2 space-y-2
"
bind:clientHeight={$ae_loc.iframe_height_modal_body}
>
{#if $lq__journal_entry_obj}
@@ -2027,11 +2034,14 @@ zzzz
</span>
</section>
{:else}
<section class="ae_meta flex flex-row flex-wrap gap-1 items-center justify-center w-full">
<span class="text-lg text-orange-900 dark:text-orange-100">
The Journal Entry was not found or is not available to show.
</span>
</section>
{/if}
</section>