Files
OSIT-AE-App-Svelte/src/lib/ae_journals/ae_journals__journal_entry.ts

582 lines
19 KiB
TypeScript

import type { key_val } from '$lib/ae_stores';
import { api } from '$lib/api';
import { db_journals } from "$lib/ae_journals/db_journals";
import { marked } from 'marked';
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
db_save_ae_obj_li__journal_entry({
obj_type: 'journal_entry',
obj_li: [journal_entry_obj_get_result],
log_lvl: log_lvl
});
}
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,
log_lvl: log_lvl
});
}
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}`);
}
if (!journal_id) {
console.log(`ERROR: Journals - Entry - journal_id required to create`);
return false;
}
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],
log_lvl: log_lvl
});
}
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 = 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) {
// db_save_ae_obj_li__journal_entry({
// obj_type: 'journal_entry',
// obj_li: [journal_entry_obj_update_result],
// log_lvl: log_lvl
// })
// .then(function (result) {
// return result;
// })
// .finally(function () {
// return journal_entry_obj_update_result;
// });
// } else {
// 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 await ae_promises.update__journal_entry_obj;
// }
// Updated 2025-05-09
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);
}
// Perform the API update
const result = 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,
});
// Handle the result
if (result) {
if (try_cache) {
console.log('Saving to DB...');
await db_save_ae_obj_li__journal_entry({
obj_type: 'journal_entry',
obj_li: [result],
log_lvl: log_lvl,
});
console.log('DB save completed.');
}
return result;
} else {
console.error('Failed to update journal entry.');
return null;
}
}
// This function will loop through the journal_entry_obj_li and save each one to the DB.
// Updated 2025-05-09
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() *** obj_type=${obj_type}`, obj_li);
}
if (obj_li && obj_li.length) {
// let obj_li_id = obj_li.map((obj: any) => obj.journal_entry_id_random);
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);
}
let content = obj.content ?? '';
// remove the most common zerowidth characters from the start of the file
let content_cleaned: null|string = null;
let content_md_html: null|string = null; // await marked.parse(content_cleaned ?? '') ?? null;
// let content_md_html_alt: null|string = await marked.parse(content_cleaned ?? '', { gfm: false }) ?? null;
if (obj.content_encrypted) {
// In theory "content" should be null if "content_encrypted" has a value.
content = null; // obj.content_encrypted;
content_cleaned = null;
content_md_html = null;
} else {
content_cleaned = content.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/,"");
content_md_html = await marked.parse(content_cleaned ?? '') ?? null;
}
let history = obj.history ?? '';
let history_cleaned: null|string = null;
let history_md_html: null|string = null; // await marked.parse(history_cleaned ?? '') ?? null;
if (obj.history_encrypted) {
// In theory "history" should be null if "history_encrypted" has a value.
history = null; // obj.history_encrypted;
history_cleaned = null;
history_md_html = null;
} else {
history_cleaned = history.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/,"");
history_md_html = await marked.parse(history_cleaned ?? '') ?? null;
}
let obj_record = {
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,
template: obj.template ?? null, // Allow for a template to be used, otherwise null
activity_code: obj.activity_code,
category_code: obj.category_code,
type_code: obj.type_code,
topic_code: obj.topic_code,
tags: obj.tags,
public: obj.public,
private: obj.private,
personal: obj.personal,
professional: obj.professional,
name: obj.name,
short_name: obj.short_name ?? null,
summary: obj.summary,
outline: obj.outline,
// description: obj.description,
content: obj.content,
content_md_html: content_md_html,
// content_md_html_alt: content_md_html_alt,
content_html: obj.content_html,
content_json: obj.content_json,
content_encrypted: obj.content_encrypted,
history: obj.history,
history_md_html: history_md_html,
history_encrypted: obj.history_encrypted,
passcode_hash: obj.passcode_hash,
// 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,
archive: obj.archive,
archive_on: obj.archive_on,
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.group}_${obj.priority}_${obj.sort}_${obj.updated_on}_${obj.created_on}`,
tmp_sort_2: `${obj.group}_${obj.priority}_${obj.sort}_${obj.updated_on ?? obj.created_on}`,
// tmp_sort_1: `${obj.original_datetime}_${obj.group}_${obj.priority}_${obj.sort}`,
// tmp_sort_2: `${obj.group}_${obj.original_datetime}_${obj.priority}_${obj.sort}`,
// 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,
};
let id_random = null;
try {
id_random = await db_journals.journal_entry.update(obj_record.id, obj_record);
} catch (error) {
console.log(`Error: Failed to update ${obj_record.id}: ${error}`);
}
if (!id_random) {
if (log_lvl) {
console.log(`Failed to update record with ID: ${obj_record.id}. Trying put...`);
}
try {
id_random = await db_journals.journal_entry.put(obj_record);
} catch (error) {
console.log(`Error: Failed to put ${obj.journal_entry_id_random}: ${error}`);
}
} else {
if (log_lvl) {
console.log(`Updated record with ID: ${obj_record.id}`);
}
obj_li_id.push(obj_record.id);
}
if (!id_random) {
console.log(`Failed to save record with ID: ${obj_record.id}`);
} else {
if (log_lvl) {
console.log(`Saved record with ID: ${obj_record.id}`);
}
}
// });
}
return obj_li_id;
}
}