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

631 lines
20 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';
import { load_ae_obj_li__journal_entry } from "$lib/ae_journals/ae_journals__journal_entry";
let ae_promises: key_val = {};
// Updated 2025-03-15
export async function load_ae_obj_id__journal(
{
api_cfg,
journal_id,
inc_entry_li = false,
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,
journal_id: string,
inc_entry_li?: boolean,
enabled?: string, // all, disabled, enabled
hidden?: string, // all, hidden, not_hidden
limit?: number,
offset?: number,
order_by_li?: key_val, // Order by fields for the journal entries
params?: key_val,
try_cache?: boolean,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** load_ae_obj_id__journal() *** journal_id=${journal_id}`);
}
ae_promises.load__journal_obj = await api.get_ae_obj_id_crud({
api_cfg: api_cfg,
obj_type: 'journal',
obj_id: journal_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 in the API config.
params: params,
log_lvl: log_lvl
})
.then(function (journal_obj_get_result) {
if (journal_obj_get_result) {
if (try_cache) {
// This is expecting a list
db_save_ae_obj_li__journal({
obj_type: 'journal',
obj_li: [journal_obj_get_result],
log_lvl: log_lvl
});
}
return journal_obj_get_result;
} else {
console.log('No results returned.');
return null;
}
})
.catch(function (error) {
console.log('No results returned or failed.', error);
});
if (log_lvl) {
console.log('ae_promises.load__journal_obj:', ae_promises.load__journal_obj);
}
if (inc_entry_li) {
// Load the entries for the journal
if (log_lvl) {
console.log(`Need to load the entry list for the journal now`);
}
let load_journal_entry_obj_li = load_ae_obj_li__journal_entry({
api_cfg: api_cfg,
for_obj_type: 'journal',
for_obj_id: journal_id,
enabled: enabled, // all, disabled, enabled
hidden: hidden, // all, hidden, not_hidden
limit: limit, // Limit for the entries
offset: offset,
order_by_li: order_by_li,
params: params,
try_cache: try_cache,
log_lvl: log_lvl
})
.then((journal_entry_obj_li) => {
if (log_lvl) {
console.log(`journal_entry_obj_li = `, journal_entry_obj_li);
}
return journal_entry_obj_li;
});
if (log_lvl) {
console.log(`journal_entry_obj_li = `, load_journal_entry_obj_li);
}
ae_promises.load__journal_obj.journal_entry_li = load_journal_entry_obj_li;
}
return ae_promises.load__journal_obj;
}
// Updated 2025-03-15
export async function load_ae_obj_li__journal(
{
api_cfg,
for_obj_type = 'account',
for_obj_id,
inc_entry_li = false,
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,
inc_entry_li?: boolean,
enabled?: string, // all, disabled, enabled
hidden?: string, // all, hidden, not_hidden
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() *** for_obj_type=${for_obj_type} for_obj_id=${for_obj_id}`);
}
let params_json: key_val = {};
// 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,
obj_type: 'journal',
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(function (journal_obj_li_get_result) {
if (journal_obj_li_get_result) {
if (try_cache) {
db_save_ae_obj_li__journal({
obj_type: 'journal',
obj_li: journal_obj_li_get_result,
log_lvl: log_lvl
});
}
return journal_obj_li_get_result;
} else {
return [];
}
});
if (log_lvl) {
console.log('ae_promises.load__journal_obj_li:', ae_promises.load__journal_obj_li);
}
if (inc_entry_li) {
// Load the entries for the journals
if (log_lvl) {
console.log(`Need to load the entry list for each journal now`);
}
for (let i = 0; i < ae_promises.load__journal_obj_li.length; i++) {
let journal_obj = ae_promises.load__journal_obj_li[i];
let journal_id = journal_obj.journal_id_random;
let load_journal_entry_obj_li = load_ae_obj_li__journal_entry({
api_cfg: api_cfg,
for_obj_type: 'journal',
for_obj_id: journal_id,
enabled: enabled,
hidden: hidden,
limit: limit,
offset: offset,
order_by_li: order_by_li,
params: params,
try_cache: try_cache,
log_lvl: log_lvl
})
.then((journal_entry_obj_li) => {
if (log_lvl) {
console.log(`journal_entry_obj_li = `, journal_entry_obj_li);
}
return journal_entry_obj_li;
});
if (log_lvl) {
console.log(`load_journal_entry_obj_li = `, load_journal_entry_obj_li);
}
}
}
return ae_promises.load__journal_obj_li;
}
// Updated 2025-03-24
export async function create_ae_obj__journal(
{
api_cfg,
account_id,
data_kv,
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any,
account_id: string,
data_kv: key_val,
params?: key_val,
try_cache?: boolean,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** create_ae_obj__journal() *** account_id=${account_id}`);
}
if (!account_id) {
console.log(`ERROR: Journals - Journal - account_id required to create`);
return false;
}
ae_promises.create__journal = await api.create_ae_obj_crud({
api_cfg: api_cfg,
obj_type: 'journal',
fields: {
account_id_random: account_id,
...data_kv
},
key: api_cfg.api_crud_super_key,
params: params,
return_obj: true,
log_lvl: log_lvl
})
.then(function (journal_obj_create_result) {
if (journal_obj_create_result) {
if (try_cache) {
db_save_ae_obj_li__journal(
{
obj_type: 'journal',
obj_li: [journal_obj_create_result]
});
}
return journal_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__journal:', ae_promises.create__journal);
}
return ae_promises.create__journal;
}
// Updated 2025-03-15
export async function delete_ae_obj_id__journal(
{
api_cfg,
journal_id,
method = 'delete', // 'delete', 'disable', 'hide'
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any,
journal_id: string,
method?: string,
params?: key_val,
try_cache?: boolean,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** delete_ae_obj_id__journal() *** journal_id=${journal_id}`);
}
ae_promises.delete__journal_obj = await api.delete_ae_obj_id_crud({
api_cfg: api_cfg,
obj_type: 'journal',
obj_id: journal_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_id=${journal_id}`);
}
db_journals.journal.delete(journal_id);
}
});
if (log_lvl) {
console.log('ae_promises.delete__journal_obj:', ae_promises.delete__journal_obj);
}
return ae_promises.delete__journal_obj;
}
// 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
}
) {
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({
api_cfg: api_cfg,
obj_type: 'journal',
obj_id: journal_id,
fields: data_kv,
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]
});
}
return journal_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_obj:', ae_promises.update__journal_obj);
}
return ae_promises.update__journal_obj;
}
// This new function is using CRUD v2. This should allow for more flexibility in the queries.
// Updated 2025-03-15
export async function qry__journal(
{
api_cfg,
journal_id,
qry_str,
qry_files,
qry_start_datetime, // Example greater than: '2024-10-24'
enabled = 'enabled',
hidden = 'not_hidden',
limit = 50,
offset = 0,
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any,
journal_id: any,
qry_str?: string,
qry_files?: null|boolean,
qry_start_datetime?: null|string, // Greater than this datetime
enabled?: string, // all, disabled, enabled
hidden?: string, // all, hidden, not_hidden
limit?: number,
offset?: number,
params?: any,
try_cache?: boolean,
log_lvl?: number
}
) {
if (log_lvl) {
console.log(`*** qry__journal() *** journal_id=${journal_id} enabled=${enabled} hidden=${hidden} limit=${limit} offset=${offset}`);
}
// 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 ?? 25); // 99
// let offset: number = (params.qry__offset ?? 0); // 0
let params_json: key_val = {};
// if (qry_str && qry_str.length > 2) {
// params_json['ft_qry'] = {};
// params_json['ft_qry']['default_qry_str'] = qry_str;
// }
params_json['qry'] = [];
if (qry_files === true) {
let qry_param =
{
type: "AND",
field: "file_count_all",
operator: ">",
value: 0
};
params_json['qry'].push(qry_param);
} else if (qry_files === false) {
let qry_param =
{
type: "AND",
field: "file_count_all",
operator: "IS",
value: null
};
params_json['qry'].push(qry_param);
}
if (qry_start_datetime) {
let qry_param =
{
type: "AND",
field: "start_datetime",
operator: ">",
value: qry_start_datetime
};
params_json['qry'].push(qry_param);
}
let order_by_li = {'priority': 'DESC', 'sort': 'DESC', 'start_datetime': 'ASC', 'name': 'ASC', 'updated_on': 'DESC', 'created_on': 'DESC'};
ae_promises.load__journal_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({
api_cfg: api_cfg,
obj_type: 'journal',
for_obj_type: 'account',
for_obj_id: journal_id,
use_alt_tbl: true, // NOTE: We want to use the alt table for journal searching
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(function (journal_obj_li_get_result) {
if (journal_obj_li_get_result) {
db_save_ae_obj_li__journal({
obj_type: 'journal',
obj_li: journal_obj_li_get_result
});
return journal_obj_li_get_result;
} else {
return [];
}
});
if (log_lvl) {
console.log('ae_promises.load__journal_obj_li:', ae_promises.load__journal_obj_li);
}
return ae_promises.load__journal_obj_li;
}
// 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(
{
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() ***`);
}
if (obj_li && obj_li.length) {
obj_li.forEach(async function (obj: any) {
if (log_lvl) {
console.log(`ae_obj ${obj_type}:`, obj);
}
let description = obj.description ?? '';
// remove the most common zerowidth characters from the start of the file
let description_cleaned: string = description.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/,"");
let description_md_html: null|string = await marked.parse(description_cleaned ?? '') ?? null;
// let description_md_html_alt: null|string = await marked.parse(description_cleaned ?? '', { gfm: false }) ?? null;
try {
const id_random = await db_journals.journal.put({
id: obj.journal_id_random,
journal_id: obj.journal_id_random,
code: obj.code,
for_type: obj.for_type,
for_id: obj.for_id,
type_code: obj.type_code,
account_id: obj.account_id_random,
person_id: obj.person_id_random,
name: obj.name,
short_name: obj.short_name,
summary: obj.summary,
outline: obj.outline,
description: obj.description,
description_md_html: description_md_html, // Use the markdown parser to generate HTML
description_html: obj.description_html,
description_json: obj.description_json,
// start_datetime: obj.start_datetime,
// end_datetime: obj.end_datetime,
timezone: obj.timezone,
alert: obj.alert,
alert_msg: obj.alert_msg,
sort_by: obj.sort_by,
sort_by_desc: obj.sort_by_desc,
cfg_json: obj.cfg_json ?? {},
data_json: obj.data_json ?? {},
// ux_mode: obj.ux_mode,
// 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,
passcode: obj.passcode,
passcode_timeout: obj.passcode_timeout,
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.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}`,
// From SQL view
journal_entry_count: obj.journal_entry_count,
// 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_id_random} or ${id_random}`);
} catch (error) {
let status = `Failed to put ${obj.journal_id_random}: ${error}`;
console.log(status);
}
// const id_random = await db_journals.journal.put(obj);
// console.log(`Put obj with ID: ${obj.journal_id_random}`);
});
return true;
}
}