From 4d6c10cbad4032baa82df03490f395d15aa2ee57 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Thu, 13 Nov 2025 14:26:04 -0500 Subject: [PATCH] Quick save before more big changes. --- src/lib/ae_core/core__crud_generic.ts | 206 +++++++++++++++++++++++++ src/lib/ae_core/core__data_store.ts | 115 ++++++++++++++ src/lib/ae_core/core__site_domain.ts | 62 +++++++- src/lib/element_ae_crud.svelte | 2 +- src/lib/element_input_file.svelte | 4 +- src/lib/element_input_files_tbl.svelte | 4 +- src/routes/+layout.ts | 6 +- 7 files changed, 388 insertions(+), 11 deletions(-) create mode 100644 src/lib/ae_core/core__data_store.ts diff --git a/src/lib/ae_core/core__crud_generic.ts b/src/lib/ae_core/core__crud_generic.ts index 09b88fd3..e7683914 100644 --- a/src/lib/ae_core/core__crud_generic.ts +++ b/src/lib/ae_core/core__crud_generic.ts @@ -296,3 +296,209 @@ export async function process_ae_obj__props({ return processed; } + + +// Additional Modules that might be needed for reloads +import { load_ae_obj_id__archive } from "$lib/ae_archives/ae_archives__archive"; +import { load_ae_obj_id__archive_content } from "$lib/ae_archives/ae_archives__archive_content"; +import { load_ae_obj_id__event } from "$lib/ae_events/ae_events__event"; +import { load_ae_obj_id__event_device } from "$lib/ae_events/ae_events__event_device"; +import { load_ae_obj_id__event_file } from "$lib/ae_events/ae_events__event_file"; +import { load_ae_obj_id__event_location } from "$lib/ae_events/ae_events__event_location"; +import { load_ae_obj_id__event_presentation } from "$lib/ae_events/ae_events__event_presentation"; +import { load_ae_obj_id__event_presenter } from "$lib/ae_events/ae_events__event_presenter"; +import { load_ae_obj_id__event_session } from "$lib/ae_events/ae_events__event_session"; +import { load_ae_obj_id__journal } from "$lib/ae_journals/ae_journals__journal"; +import { load_ae_obj_id__journal_entry } from "$lib/ae_journals/ae_journals__journal_entry"; +import { load_ae_obj_id__post } from "$lib/ae_posts/ae_posts__post"; +import { load_ae_obj_id__post_comment } from "$lib/ae_posts/ae_posts__post_comment"; +import { load_ae_obj_id__person } from "$lib/ae_core/core__person"; + + +/** + * @deprecated Use update_ae_obj_id_crud_v2 instead. This is the older version. + */ +export async function update_ae_obj_id_crud( + { + api_cfg, + object_type, + object_id, + field_name, + new_field_value, + log_lvl = 0 + }: { + api_cfg: any, + object_type: string, + object_id: string, + field_name: string, + new_field_value: any, + log_lvl: number + }) { + + try { + const results = await api.update_ae_obj_id_crud({ + api_cfg: api_cfg, + obj_type: object_type, + obj_id: object_id, + field_name: field_name, + field_value: new_field_value, + key: api_cfg.api_crud_super_key, + log_lvl: log_lvl + }); + + if (results) { + console.log(`Patched - Field Name: ${field_name} with new Field Value: ${new_field_value}`); + return true; + } else { + console.log(`Not Patched - Field Name: ${field_name} with new Field Value: ${new_field_value}; Account ID: ${api_cfg.account_id}`); + return false; + } + } catch (error) { + console.log('Something went wrong patching the record.', error); + return false; + } +} + + +export async function update_ae_obj_id_crud_v2( + { + api_cfg, + object_type, + object_id, + object_reload = false, + field_name, + new_field_value, + log_lvl = 0 + }: { + api_cfg: any, + object_type: string, + object_id: string, + object_reload?: boolean, + field_name: string, + new_field_value: any, + log_lvl?: number + }) { + if (log_lvl) { + console.log(`*** update_ae_obj_id_crud_v2() *** object_type=${object_type}, object_id=${object_id}, object_reload=${object_reload}, field_name=${field_name}, new_field_value=`, new_field_value); + } + + try { + const results = await api.update_ae_obj_id_crud({ + api_cfg: api_cfg, + obj_type: object_type, + obj_id: object_id, + field_name: field_name, + field_value: new_field_value, + key: api_cfg.api_crud_super_key, + log_lvl: log_lvl + }); + + if (!results) { + console.log(`Not Patched - Field Name: ${field_name} with new Field Value: ${new_field_value}; Account ID: ${api_cfg.account_id}`); + return false; + } + + console.log(`Patched - Field Name: ${field_name} with new Field Value: ${new_field_value}`); + + if (object_reload) { + if (log_lvl) console.log(`Reloading the object after patching...`); + + const reload_fns: { [key: string]: (args: any) => Promise } = { + person: load_ae_obj_id__person, + archive: load_ae_obj_id__archive, + archive_content: load_ae_obj_id__archive_content, + journal: load_ae_obj_id__journal, + journal_entry: load_ae_obj_id__journal_entry, + event: load_ae_obj_id__event, + event_device: load_ae_obj_id__event_device, + event_file: load_ae_obj_id__event_file, + event_location: load_ae_obj_id__event_location, + event_presentation: load_ae_obj_id__event_presentation, + event_presenter: load_ae_obj_id__event_presenter, + event_session: load_ae_obj_id__event_session, + post: load_ae_obj_id__post, + post_comment: load_ae_obj_id__post_comment, + }; + + const reload_fn = reload_fns[object_type]; + if (reload_fn) { + const id_key = `${object_type}_id`; + return await reload_fn({ api_cfg, [id_key]: object_id, log_lvl }); + } + } + + return true; + + } catch (error) { + console.log('Something went wrong patching the record.', error); + return false; + } +} + + +export async function download_export_li( + { + api_cfg, + get_obj_type, + for_obj_type, + for_obj_id, + exp_alt = null, + file_type = 'CSV', + return_file = true, + filename = 'no_filename.csv', + auto_download = false, + limit = 5000, + params = {}, + log_lvl = 0 + }: { + api_cfg: any, + get_obj_type: string, + for_obj_type: string, + for_obj_id: string, + exp_alt?: null|string, + file_type?: string, + return_file?: boolean, + filename?: string, + auto_download?: boolean, + limit?: number, + params?: key_val, + log_lvl?: number + } + ) { + if (log_lvl) console.log('*** download_export_li() ***'); + + const endpoint = `/v2/crud/${get_obj_type}/list`; + params['for_obj_type'] = for_obj_type; + params['for_obj_id'] = for_obj_id; + + if (file_type === 'CSV' || file_type === 'Excel') { + params['file_type'] = file_type; + } + params['return_file'] = true; + params['mdl_alt'] = 'out'; + + if (exp_alt) { + params['exp_alt'] = exp_alt; + } + + const clean_filename = filename.replace(/[^a-zA-Z0-9\[\]-_.]/gi, '_'); + + if (limit >= 0) { + params['limit'] = limit; + } + + const download_result = await api.get_object({ + api_cfg: api_cfg, + endpoint: endpoint, + params: params, + timeout: 90000, + return_blob: return_file, + filename: clean_filename, + auto_download: auto_download, + task_id: for_obj_id, + log_lvl: log_lvl + }); + + if (log_lvl) console.log('download_result:', download_result); + return download_result; +} diff --git a/src/lib/ae_core/core__data_store.ts b/src/lib/ae_core/core__data_store.ts new file mode 100644 index 00000000..0c2b2fce --- /dev/null +++ b/src/lib/ae_core/core__data_store.ts @@ -0,0 +1,115 @@ +import { browser } from '$app/environment'; +import type { key_val } from '$lib/ae_stores'; +import { api } from '$lib/api'; + +export interface Data_Store { + id: string; + account_id: string; + code: string; + name: string; + type: string; + for_type?: string | null; + for_id?: string | null; + access_read?: string | null; + access_write?: string | null; + access_delete?: string | null; + html?: string | null; + json?: key_val | null; + md?: string | null; + text?: string | null; + updated_on?: string | null; + chk_account_id?: string; + loaded_on?: string; +} + +/** + * Fetches a data_store object by its unique code. + * + * @param api_cfg - The API configuration object. + * @param code - The code of the data store to fetch. + * @param data_type - The expected data type ('text', 'json', 'html', 'md', 'sql'). + * @param save_idb - Whether to save the fetched data to localStorage. + * @param timeout - The request timeout in milliseconds. + * @param log_lvl - The logging level. + * @returns The data from the data store (e.g., text content or JSON object). + */ +export async function load_ae_obj_by_code__data_store( + { + api_cfg, + code, + data_type = 'text', + save_idb = false, + timeout = 9000, + log_lvl = 0 + }: { + api_cfg: any, + code: string, + data_type?: string, + save_idb?: boolean, + timeout?: number, + log_lvl?: number + } + ): Promise { + if (log_lvl) { + console.log(`*** load_ae_obj_by_code__data_store() *** code=${code}`); + } + + if (!code) { + console.log(`*ae_func* No code provided!`); + return null; + } + + if (!api_cfg.account_id) { + console.log(`*ae_func* No account_id found in API config!`); + return null; + } + + try { + const get_ds_result = await api.get_data_store_obj_w_code({ + api_cfg: api_cfg, + data_store_code: code, + data_type: data_type, + timeout: timeout, + log_lvl: log_lvl + }); + + if (!get_ds_result) { + console.log('*ae_func* No results returned.'); + return null; + } + + if (log_lvl) { + console.log(`*ae_func* Got a result for code ${code}`); + } + + if (!get_ds_result.data_store_id_random) { + console.log('*ae_func* Something went wrong? No data store ID found.'); + return null; + } + + let return_this: any = null; + + // Simplified data extraction + if (data_type === 'html') { + return_this = get_ds_result.html; + } else if (data_type === 'json') { + return_this = get_ds_result.json; + } else { + return_this = get_ds_result.text; + } + + if (save_idb && browser) { + const key_prefix = 'ae_ds__'; + if (log_lvl) { + console.log(`*ae_func* localStorage key: ${code}, value:`, get_ds_result); + } + localStorage.setItem(`${key_prefix}${code}`, JSON.stringify(get_ds_result)); + } + + return return_this; + + } catch (error) { + console.log('*ae_func* No results returned or failed.', error); + return null; + } +} diff --git a/src/lib/ae_core/core__site_domain.ts b/src/lib/ae_core/core__site_domain.ts index 964ed989..7aac86fd 100644 --- a/src/lib/ae_core/core__site_domain.ts +++ b/src/lib/ae_core/core__site_domain.ts @@ -15,6 +15,62 @@ export interface Site_Domain { sort?: null|number; group?: null|string; notes?: null|string; - created_on: Date; - updated_on?: null|Date; -} \ No newline at end of file + created_on: Date; + updated_on?: null|Date; + } + + import { api } from '$lib/api'; + + /** + * Fetches a site_domain object by its Fully Qualified Domain Name (FQDN). + * + * @param api_cfg - The API configuration object. + * @param fqdn - The FQDN of the site domain to fetch. + * @param timeout - The request timeout in milliseconds. + * @param log_lvl - The logging level. + * @returns The site domain object or null if not found. + */ + export async function load_ae_obj_by_fqdn__site_domain( + { + api_cfg, + fqdn, + timeout = 7000, + log_lvl = 0 + }: { + api_cfg: any, + fqdn: string, + timeout?: number, + log_lvl?: number + } + ): Promise { + if (log_lvl) { + console.log(`*** load_ae_obj_by_fqdn__site_domain() *** api.base_url=${api_cfg.base_url}, fqdn=${fqdn}, timeout=${timeout}`); + } + + const params = {}; + + try { + const site_domain_obj = await api.get_ae_obj_id_crud({ + api_cfg: api_cfg, + no_account_id: true, // This seems to be a special case for this endpoint + obj_type: 'site_domain', + obj_id: fqdn, // NOTE: This is the FQDN, not the ID. + use_alt_table: true, + use_alt_base: true, + params: params, + timeout: timeout, + log_lvl: log_lvl + }); + + if (site_domain_obj) { + return site_domain_obj; + } else { + console.log('No results returned.'); + return null; + } + } catch (error) { + console.log('No results returned or failed.', error); + return null; + } + } + \ No newline at end of file diff --git a/src/lib/element_ae_crud.svelte b/src/lib/element_ae_crud.svelte index 0b94550f..bb984a13 100644 --- a/src/lib/element_ae_crud.svelte +++ b/src/lib/element_ae_crud.svelte @@ -5,7 +5,7 @@ import { createEventDispatcher, onMount } from 'svelte'; // *** Import Aether core variables and functions import type { key_val } from '$lib/ae_stores'; // import { api } from '$lib/api'; -import { core_func } from '$lib/ae_core/ae_core_functions'; +import { update_ae_obj_id_crud } from '$lib/ae_core/core__crud_generic'; // import { ae_loc, ae_sess, ae_api, ae_trig, slct, slct_trigger } from '$lib/ae_stores'; // *** Import Aether core components diff --git a/src/lib/element_input_file.svelte b/src/lib/element_input_file.svelte index 3d9594eb..6f2ca5c8 100644 --- a/src/lib/element_input_file.svelte +++ b/src/lib/element_input_file.svelte @@ -4,7 +4,7 @@ import { createEventDispatcher, onMount, tick } from 'svelte'; import type { key_val } from '$lib/ae_stores'; import { ae_util } from '$lib/ae_utils/ae_utils'; // import { api } from '$lib/api'; -import { core_func } from '$lib/ae_core/ae_core_functions'; +import { check_hosted_file_obj_w_hash } from '$lib/ae_core/core__check_hosted_file_obj_w_hash'; import { ae_loc, ae_sess, ae_api, ae_trig, slct, slct_trigger } from '$lib/ae_stores'; export let element_id = 'svelte_input_file_element'; @@ -225,7 +225,7 @@ async function process_file_list(file_list) { console.log(`Found file hash to lookup: ${ae_util.shorten_string({string: file_hash})}`); file_data['hash_sha256'] = file_hash; - let check_hosted_file_obj_w_hash_result = await core_func.check_hosted_file_obj_w_hash({ + let check_hosted_file_obj_w_hash_result = await check_hosted_file_obj_w_hash({ api_cfg: $ae_api, hosted_file_hash: file_hash }); diff --git a/src/lib/element_input_files_tbl.svelte b/src/lib/element_input_files_tbl.svelte index b7ecdc7a..fc9143e9 100644 --- a/src/lib/element_input_files_tbl.svelte +++ b/src/lib/element_input_files_tbl.svelte @@ -4,7 +4,7 @@ import { createEventDispatcher, onMount, tick } from 'svelte'; import type { key_val } from '$lib/ae_stores'; import { ae_util } from '$lib/ae_utils/ae_utils'; // import { api } from '$lib/api'; -import { core_func } from '$lib/ae_core/ae_core_functions'; +import { check_hosted_file_obj_w_hash } from '$lib/ae_core/core__check_hosted_file_obj_w_hash'; import { ae_loc, ae_sess, ae_api, ae_trig, slct, slct_trigger } from '$lib/ae_stores'; // export let element_id = 'svelte_input_file_element'; @@ -224,7 +224,7 @@ async function process_file_list(file_list) { console.log(`Found file hash to lookup: ${ae_util.shorten_string({string: file_hash})}`); file_data['hash_sha256'] = file_hash; - let check_hosted_file_obj_w_hash_result = await core_func.check_hosted_file_obj_w_hash({ + let check_hosted_file_obj_w_hash_result = await check_hosted_file_obj_w_hash({ api_cfg: $ae_api, hosted_file_hash: file_hash }); diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts index 5674bc5f..5b9eb42f 100644 --- a/src/routes/+layout.ts +++ b/src/routes/+layout.ts @@ -5,7 +5,7 @@ import { error } from '@sveltejs/kit'; // import { api } from '$lib/api'; // import { ae_loc, ae_sess, ae_api, slct, slct_trigger } from '$lib/ae_stores'; -import { core_func } from '$lib/ae_core/ae_core_functions'; +import { add_url_params, clean_headers } from '$lib/ae_core/core__api_helpers'; import type { key_val } from '$lib/ae_stores'; import { PUBLIC_AE_API_PROTOCOL, PUBLIC_AE_API_SERVER, PUBLIC_AE_API_BAK_SERVER, PUBLIC_AE_API_PORT, PUBLIC_AE_API_PATH, PUBLIC_AE_API_SECRET_KEY, PUBLIC_AE_API_CRUD_SUPER_KEY, PUBLIC_AE_NO_ACCOUNT_ID, PUBLIC_AE_NO_ACCOUNT_ID_TOKEN } from '$env/static/public'; @@ -144,7 +144,7 @@ export async function load({ fetch, params, parent, route, url }) { // params, r 'use_alt_base': true, }; - const api_url_str = core_func.add_url_params({ + const api_url_str = add_url_params({ base_url: ae_api_init['base_url'], endpoint: api_endpoint, params: api_params, @@ -155,7 +155,7 @@ export async function load({ fetch, params, parent, route, url }) { // params, r 'x-no-account-id': 'Nothing to See Here', } - headers = core_func.clean_headers({ + headers = clean_headers({ headers: headers, log_lvl: log_lvl });