diff --git a/src/lib/ae_core/ae_core_functions.ts b/src/lib/ae_core/ae_core_functions.ts index ba5160fb..204b8750 100644 --- a/src/lib/ae_core/ae_core_functions.ts +++ b/src/lib/ae_core/ae_core_functions.ts @@ -59,6 +59,11 @@ import { db_update_ae_obj_id__hosted_file } from "$lib/ae_core/core__hosted_files"; +// This has more generic general purpose functions that can eventually replace the custom ones per object type. +import { + db_save_ae_obj_li__ae_obj, +} from "$lib/ae_core/core__idb_dexie"; + import { add_url_params, clean_headers, @@ -431,6 +436,7 @@ let export_obj = { delete_ae_obj_id__hosted_file: delete_ae_obj_id__hosted_file, db_save_ae_obj_li__hosted_file: db_save_ae_obj_li__hosted_file, db_update_ae_obj_id__hosted_file: db_update_ae_obj_id__hosted_file, + db_save_ae_obj_li__ae_obj: db_save_ae_obj_li__ae_obj, add_url_params: add_url_params, clean_headers: clean_headers, handle_load_ae_obj_id__site_domain: handle_load_ae_obj_id__site_domain, diff --git a/src/lib/ae_core/core__idb_dexie.ts b/src/lib/ae_core/core__idb_dexie.ts new file mode 100644 index 00000000..cbaaff55 --- /dev/null +++ b/src/lib/ae_core/core__idb_dexie.ts @@ -0,0 +1,91 @@ + +// This function will save an array of objects to a Dexie database table. +// It will first attempt to update existing records using bulkUpdate. +// If that fails, it will fall back to bulkPut. +// The function takes a database instance, table name, array of objects, and properties to save. +// It also accepts a log level for debugging purposes. +// Updated 2025-05-09 +export async function db_save_ae_obj_li__ae_obj({ + db_instance, + table_name, + obj_li, + properties_to_save, + log_lvl = 0, + }: { + db_instance: any; // The Dexie database instance + table_name: string; // The name of the table in the database + obj_li: any[]; + properties_to_save: string[]; + log_lvl?: number; + }) { + log_lvl = 1; + if (log_lvl) { + console.log(`*** db_save_ae_obj_li__ae_obj() *** table_name=${table_name}`, obj_li); + } + + if (!obj_li || obj_li.length === 0) { + if (log_lvl) { + console.log('No objects to save.'); + } + return []; + } + + const db_table = db_instance[table_name]; + if (!db_table) { + console.error(`Table not found: ${table_name}`); + return []; + } + + const bulkUpdateData = []; + const bulkPutData = []; + + for (const obj of obj_li) { + const obj_record: Record = {}; + + // Extract only the specified properties to save + for (const prop of properties_to_save) { + obj_record[prop] = obj[prop]; + } + + // Ensure the `id` field is included + obj_record.id = obj_record.id || obj.id || obj[`${table_name}_id`] || obj[`${table_name}_id_random`]; + + if (!obj_record.id) { + console.error(`Object is missing an ID:`, obj); + continue; + } + + // Prepare data for bulkUpdate or bulkPut + bulkUpdateData.push({ + key: obj_record.id, + changes: obj_record, + }); + bulkPutData.push(obj_record); + } + + // Attempt bulkUpdate first + try { + const updatedKeys = await db_table.bulkUpdate(bulkUpdateData); + if (log_lvl) { + console.log(`Bulk update completed. Updated keys:`, updatedKeys); + } + } catch (error) { + // This is fairly common and normal if the object is new + if (log_lvl) { + console.error(`Bulk update failed. Falling back to bulkPut.`, error); + } + } + + // Use bulkPut for any records that couldn't be updated + try { + const putKeys = await db_table.bulkPut(bulkPutData); + if (log_lvl) { + console.log(`Bulk put completed. Put keys:`, putKeys); + } + return putKeys; + } catch (error) { + // This should not happen if the object is new + console.error(`Bulk put failed.`, error); + return []; + } +} \ No newline at end of file diff --git a/src/lib/ae_journals/ae_journals__journal.ts b/src/lib/ae_journals/ae_journals__journal.ts index 42aa91c5..dddc378a 100644 --- a/src/lib/ae_journals/ae_journals__journal.ts +++ b/src/lib/ae_journals/ae_journals__journal.ts @@ -1,8 +1,10 @@ +import { marked } from 'marked'; + import type { key_val } from '$lib/ae_stores'; import { api } from '$lib/api'; +import { db_save_ae_obj_li__ae_obj } from "$lib/ae_core/core__idb_dexie"; 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"; @@ -50,15 +52,37 @@ export async function load_ae_obj_id__journal( params: params, log_lvl: log_lvl }) - .then(function (journal_obj_get_result) { + .then(async 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', + // Process the results first + let processed_obj_li = await process_ae_obj__journal_props({ obj_li: [journal_obj_get_result], - log_lvl: log_lvl + log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('Processed object list:', processed_obj_li); + } + // Save the updated results list to the database + if (log_lvl) { + console.log('Saving to DB...'); + } + await db_save_ae_obj_li__ae_obj({ + db_instance: db_journals, + table_name: 'journal', + obj_li: processed_obj_li, + properties_to_save: properties_to_save, + log_lvl: log_lvl, }); + if (log_lvl) { + console.log('DB save completed.'); + } + // // 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 { @@ -179,14 +203,36 @@ export async function load_ae_obj_li__journal( params: params, log_lvl: log_lvl }) - .then(function (journal_obj_li_get_result) { + .then(async function (journal_obj_li_get_result) { if (journal_obj_li_get_result) { if (try_cache) { - db_save_ae_obj_li__journal({ - obj_type: 'journal', + // Process the results first + let processed_obj_li = await process_ae_obj__journal_props({ obj_li: journal_obj_li_get_result, - log_lvl: log_lvl + log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('Processed object list:', processed_obj_li); + } + // Save the updated results list to the database + if (log_lvl) { + console.log('Saving to DB...'); + } + await db_save_ae_obj_li__ae_obj({ + db_instance: db_journals, + table_name: 'journal', + obj_li: processed_obj_li, + properties_to_save: properties_to_save, + log_lvl: log_lvl, }); + if (log_lvl) { + console.log('DB save completed.'); + } + // 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 { @@ -277,15 +323,37 @@ export async function create_ae_obj__journal( return_obj: true, log_lvl: log_lvl }) - .then(function (journal_obj_create_result) { + .then(async 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], - log_lvl: log_lvl + // Process the results first + let processed_obj_li = await process_ae_obj__journal_props({ + obj_li: [journal_obj_create_result], + log_lvl: log_lvl, }); + if (log_lvl) { + console.log('Processed object list:', processed_obj_li); + } + // Save the updated results list to the database + if (log_lvl) { + console.log('Saving to DB...'); + } + await db_save_ae_obj_li__ae_obj({ + db_instance: db_journals, + table_name: 'journal', + obj_li: processed_obj_li, + properties_to_save: properties_to_save, + log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('DB save completed.'); + } + // db_save_ae_obj_li__journal( + // { + // obj_type: 'journal', + // obj_li: [journal_obj_create_result], + // log_lvl: log_lvl + // }); } return journal_obj_create_result; } else { @@ -339,12 +407,12 @@ export async function delete_ae_obj_id__journal( .catch(function (error) { console.log('No results returned or failed.', error); }) - .finally(function () { + .finally(async 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); + await db_journals.journal.delete(journal_id); } }); @@ -376,6 +444,8 @@ export async function update_ae_obj__journal({ console.log(`*** update_ae_obj__journal() *** journal_id=${journal_id}`, data_kv); } + log_lvl = 1; + // Perform the API update const result = await api.update_ae_obj_id_crud({ api_cfg: api_cfg, @@ -391,13 +461,33 @@ export async function update_ae_obj__journal({ // Handle the result if (result) { if (try_cache) { - console.log('Saving to DB...'); - await db_save_ae_obj_li__journal({ - obj_type: 'journal', + // Process the results first + let processed_obj_li = await process_ae_obj__journal_props({ obj_li: [result], log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('Processed object list:', processed_obj_li); + } + // Save the updated results list to the database + if (log_lvl) { + console.log('Saving to DB...'); + } + await db_save_ae_obj_li__ae_obj({ + db_instance: db_journals, + table_name: 'journal', + obj_li: processed_obj_li, + properties_to_save: properties_to_save, + log_lvl: log_lvl, }); - console.log('DB save completed.'); + if (log_lvl) { + console.log('DB save completed.'); + } + // await db_save_ae_obj_li__journal({ + // obj_type: 'journal', + // obj_li: [result], + // log_lvl: log_lvl, + // }); } return result; } else { @@ -506,12 +596,36 @@ export async function qry__journal( params: params, log_lvl: log_lvl }) - .then(function (journal_obj_li_get_result) { + .then(async 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 - }); + if (try_cache) { + // Process the results first + let processed_obj_li = await process_ae_obj__journal_props({ + obj_li: journal_obj_li_get_result, + log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('Processed object list:', processed_obj_li); + } + // Save the updated results list to the database + if (log_lvl) { + console.log('Saving to DB...'); + } + db_save_ae_obj_li__ae_obj({ + db_instance: db_journals, + table_name: 'journal', + obj_li: processed_obj_li, + properties_to_save: properties_to_save, + log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('DB save completed.'); + } + } + // db_save_ae_obj_li__journal({ + // obj_type: 'journal', + // obj_li: journal_obj_li_get_result + // }); return journal_obj_li_get_result; } else { return []; @@ -670,3 +784,195 @@ export async function db_save_ae_obj_li__journal( return []; } } + + +// Updated 2025-05-09 +let properties_to_save = [ + + 'id', + 'journal_id', + + 'code', + + 'for_type', + 'for_id', + 'type_code', + + 'account_id', + 'person_id', + + 'name', + 'short_name', + 'summary', + 'outline', + + 'description', + 'description_md_html', // Use the markdown parser to generate HTML + 'description_html', + 'description_json', + + // start_datetime: obj.start_datetime, + // end_datetime: obj.end_datetime, + 'timezone', + + 'alert', + 'alert_msg', + + 'sort_by', + 'sort_by_desc', + + 'cfg_json', + + // ux_mode: obj.ux_mode, + + // This only allows for basic access to the data. + 'passcode_read', // For LLM (AI) generated summary...??? + 'passcode_read_expire', + 'passcode_write', + 'passcode_write_expire', + + 'passcode', // For Journal Entry encryption password + 'passcode_timeout', + + 'auth_key', // For Journal authorization without sign in + + 'enable', + 'hide', + 'priority', + 'sort', + 'group', + 'notes', + 'created_on', + 'updated_on', + + // Generated fields for sorting locally only + 'tmp_sort_1', + 'tmp_sort_2', + 'tmp_sort_3', + // 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', + + // A key value list of the others + // journal_other_kv + // journal_other_li +]; + + +// Updated 2025-05-09 +export async function process_ae_obj__journal_props( + { + // obj_type, + obj_li, + log_lvl = 0, + }: { + // obj_type: string; + obj_li: any[]; + log_lvl?: number; + } + ) { + if (log_lvl) { + console.log(`*** process_ae_obj__journal_props() ***`, obj_li); + } + + if (!obj_li || obj_li.length === 0) { + if (log_lvl) { + console.log('No objects to process.'); + } + return []; + } + + const processed_obj_li = []; + + for (const obj of obj_li) { + // const processed_obj = { ...obj }; + + // Process the properties as needed + 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; + + let processed_obj = { + 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, // For Journal Entry encryption password + passcode_timeout: obj.passcode_timeout, + + auth_key: obj.auth_key, // For Journal authorization without sign in + + 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 ?? '0'}_${obj.priority ? 1 : 0}_${obj.sort ?? '0'}_${obj.updated_on}_${obj.created_on}`, + tmp_sort_2: `${obj.group ?? '0'}_${obj.priority ? 1 : 0}_${obj.sort ?? '0'}_${obj.updated_on ?? obj.created_on}`, + tmp_sort_3: `${obj.group ?? '0'}_${obj.priority ? 1 : 0}_${obj.sort ?? '0'}_${obj.name}_${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, + }; + + processed_obj_li.push(processed_obj); + } + + return processed_obj_li; +} diff --git a/src/lib/ae_journals/ae_journals__journal_entry.ts b/src/lib/ae_journals/ae_journals__journal_entry.ts index 80c284dd..1b57d613 100644 --- a/src/lib/ae_journals/ae_journals__journal_entry.ts +++ b/src/lib/ae_journals/ae_journals__journal_entry.ts @@ -1,8 +1,11 @@ +import { marked } from 'marked'; + import type { key_val } from '$lib/ae_stores'; import { api } from '$lib/api'; +import { db_save_ae_obj_li__ae_obj } from "$lib/ae_core/core__idb_dexie"; import { db_journals } from "$lib/ae_journals/db_journals"; -import { marked } from 'marked'; + let ae_promises: key_val = {}; @@ -39,12 +42,34 @@ export async function load_ae_obj_id__journal_entry( .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', + // Process the results first + let processed_obj_li = await process_ae_obj__journal_entry_props({ obj_li: [journal_entry_obj_get_result], - log_lvl: log_lvl + log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('Processed object list:', processed_obj_li); + } + // Save the updated results list to the database + if (log_lvl) { + console.log('Saving to DB...'); + } + await db_save_ae_obj_li__ae_obj({ + db_instance: db_journals, + table_name: 'journal_entry', + obj_li: processed_obj_li, + properties_to_save: properties_to_save, + log_lvl: log_lvl, }); + if (log_lvl) { + console.log('DB save completed.'); + } + // // 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 { @@ -121,11 +146,33 @@ export async function load_ae_obj_li__journal_entry( .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', + // Process the results first + let processed_obj_li = await process_ae_obj__journal_entry_props({ obj_li: journal_entry_obj_li_get_result, - log_lvl: log_lvl + log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('Processed object list:', processed_obj_li); + } + // Save the updated results list to the database + if (log_lvl) { + console.log('Saving to DB...'); + } + await db_save_ae_obj_li__ae_obj({ + db_instance: db_journals, + table_name: 'journal_entry', + obj_li: processed_obj_li, + properties_to_save: properties_to_save, + log_lvl: log_lvl, }); + if (log_lvl) { + console.log('DB save completed.'); + } + // 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 { @@ -186,12 +233,34 @@ export async function create_ae_obj__journal_entry( .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 + // Process the results first + let processed_obj_li = await process_ae_obj__journal_entry_props({ + obj_li: [journal_entry_obj_create_result], + log_lvl: log_lvl, }); + if (log_lvl) { + console.log('Processed object list:', processed_obj_li); + } + // Save the updated results list to the database + if (log_lvl) { + console.log('Saving to DB...'); + } + await db_save_ae_obj_li__ae_obj({ + db_instance: db_journals, + table_name: 'journal_entry', + obj_li: processed_obj_li, + properties_to_save: properties_to_save, + log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('DB save completed.'); + } + // 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 { @@ -346,6 +415,8 @@ export async function update_ae_obj__journal_entry( console.log(`*** update_ae_obj__journal_entry() *** journal_entry_id=${journal_entry_id}`, data_kv); } + log_lvl = 1; + // Perform the API update const result = await api.update_ae_obj_id_crud({ api_cfg: api_cfg, @@ -361,13 +432,33 @@ export async function update_ae_obj__journal_entry( // 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', + // Process the results first + let processed_obj_li = await process_ae_obj__journal_entry_props({ obj_li: [result], log_lvl: log_lvl, + }); + if (log_lvl) { + console.log('Processed object list:', processed_obj_li); + } + // Save the updated results list to the database + if (log_lvl) { + console.log('Saving to DB...'); + } + await db_save_ae_obj_li__ae_obj({ + db_instance: db_journals, + table_name: 'journal_entry', + obj_li: processed_obj_li, + properties_to_save: properties_to_save, + log_lvl: log_lvl, }); - console.log('DB save completed.'); + // await db_save_ae_obj_li__journal_entry({ + // obj_type: 'journal_entry', + // obj_li: [result], + // log_lvl: log_lvl, + // }); + if (log_lvl) { + console.log('DB save completed.'); + } } return result; } else { @@ -390,6 +481,7 @@ export async function db_save_ae_obj_li__journal_entry( log_lvl?: number } ) { + log_lvl = 1; if (log_lvl) { console.log(`*** db_save_ae_obj_li__journal_entry() *** obj_type=${obj_type}`, obj_li); } @@ -585,3 +677,259 @@ export async function db_save_ae_obj_li__journal_entry( return []; } } + + +// Updated 2025-05-09 +let properties_to_save = [ + 'id', + 'journal_entry_id', + + 'journal_id', + + 'code', + + 'for_type', + 'for_id', + + 'journal_entry_type', + + 'person_id', + + 'template', // Allow for a template to be used, otherwise null + + 'activity_code', + 'category_code', + 'type_code', + 'topic_code', + 'tags', + + 'public', + 'private', + 'personal', + 'professional', + + 'name', + 'short_name', + 'summary', + 'outline', + // 'description', + + 'content', + 'content_md_html', + 'content_html', + 'content_json', + 'content_encrypted', + + 'history', + 'history_md_html', + 'history_encrypted', + + 'passcode_hash', + + 'alert', + 'alert_msg', + + // 'cfg_json', + 'data_json', + + 'enable', + 'hide', + 'archive', + 'archive_on', + 'priority', + 'sort', + 'group', + 'notes', + 'created_on', + 'updated_on', + + // Generated fields for sorting locally only + 'tmp_sort_1', + 'tmp_sort_2', + + // From SQL view + 'journal_code', + 'journal_name', + + // A key value list of the others + // 'journal_other_kv', + // 'journal_other_li', +]; + + +// Updated 2025-05-09 +export async function process_ae_obj__journal_entry_props( + { + // obj_type, + obj_li, + log_lvl = 0, + }: { + // obj_type: string; + obj_li: any[]; + log_lvl?: number; + } + ) { + if (log_lvl) { + console.log(`*** process_ae_obj__journal_entry_props() ***`, obj_li); + } + + if (!obj_li || obj_li.length === 0) { + if (log_lvl) { + console.log('No objects to process.'); + } + return []; + } + + const processed_obj_li = []; + + for (const obj of obj_li) { + if (log_lvl) { + console.log(`Processing ae_obj journal_entry:`, 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 processed_obj = { + 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: 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: 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, + }; + + processed_obj_li.push(processed_obj); + } + + return processed_obj_li; +}