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_events } from "$lib/ae_events/db_events"; import { load_ae_obj_li__event_device } from "$lib/ae_events/ae_events__event_device"; import { load_ae_obj_li__event_file } from "$lib/ae_events/ae_events__event_file"; import { load_ae_obj_li__event_session } from './ae_events__event_session'; let ae_promises: key_val = {}; // Updated 2025-05-23 export async function load_ae_obj_id__event_location( { api_cfg, event_location_id, inc_file_li = false, inc_session_li = false, try_cache = true, log_lvl = 0 }: { api_cfg: any, event_location_id: string, inc_file_li?: boolean, inc_session_li?: boolean, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** load_ae_obj_id__event_location() *** event_location_id=${event_location_id}`); } let params = {}; ae_promises.load__event_location_obj = await api.get_ae_obj_id_crud({ api_cfg: api_cfg, obj_type: 'event_location', obj_id: event_location_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(async function (event_location_obj_get_result) { if (event_location_obj_get_result) { if (try_cache) { let processed_obj_li = await process_ae_obj__event_location_props({ obj_li: [event_location_obj_get_result], log_lvl }); // 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_events, table_name: 'locations', obj_li: processed_obj_li, properties_to_save, log_lvl, }); if (log_lvl) { console.log('DB save completed.'); } // // This is expecting a list // db_save_ae_obj_li__event_location({ // obj_type: 'event_location', // obj_li: [event_location_obj_get_result] // }); } return event_location_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__event_location_obj:', ae_promises.load__event_location_obj); } if (ae_promises?.load__event_location_obj === null) { console.log('No results returned.'); return null; } if (inc_file_li) { // Load the files for the location if (log_lvl) { console.log(`Need to load the file list for the location now`); } let load_event_file_obj_li = load_ae_obj_li__event_file({ api_cfg: api_cfg, for_obj_type: 'event_location', for_obj_id: event_location_id, enabled: 'all', limit: 15, try_cache: try_cache, log_lvl: log_lvl }) .then((event_file_obj_li) => { if (log_lvl) { console.log(`event_file_obj_li = `, event_file_obj_li); } return event_file_obj_li; }); if (log_lvl) { console.log(`load_event_file_obj_li = `, load_event_file_obj_li); } ae_promises.load__event_location_obj.event_file_li = load_event_file_obj_li; } if (inc_session_li) { // Load the sessions for the location if (log_lvl) { console.log(`Need to load the session list for the location now`); } let load_event_session_obj_li = load_ae_obj_li__event_session({ api_cfg: api_cfg, for_obj_type: 'event_location', for_obj_id: event_location_id, enabled: 'all', limit: 15, try_cache: try_cache, log_lvl: log_lvl }); if (log_lvl) { console.log(`load_event_session_obj_li = `, load_event_session_obj_li); } ae_promises.load__event_location_obj.event_session_li = load_event_session_obj_li; } return ae_promises.load__event_location_obj; } // Updated 2025-05-23 export async function load_ae_obj_li__event_location( { api_cfg, for_obj_type, for_obj_id, inc_device_li = false, inc_file_li = false, inc_session_li = false, enabled = 'enabled', hidden = 'not_hidden', limit = 29, offset = 0, order_by_li = {'priority': 'DESC', 'sort': 'DESC', 'name': 'ASC', 'code': '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_device_li?: boolean, inc_file_li?: boolean, inc_session_li?: boolean, enabled?: string, // all, disabled, enabled hidden?: string, // all, hidden, not_hidden limit?: number, // 99 offset?: number, // 0 order_by_li?: key_val, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** load_ae_obj_li__event_location() *** 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__event_location_obj_li = await api.get_ae_obj_li_for_obj_id_crud({ ae_promises.load__event_location_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({ api_cfg: api_cfg, obj_type: 'event_location', for_obj_type: for_obj_type, for_obj_id: for_obj_id, use_alt_tbl: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config. // use_alt_mdl: false, // NOTE: This will use the base_name_alt value instead of the base_name value 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 (event_location_obj_li_get_result) { if (event_location_obj_li_get_result) { if (try_cache) { // Process the results first let processed_obj_li = await process_ae_obj__event_location_props({ obj_li: event_location_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...'); } await db_save_ae_obj_li__ae_obj({ db_instance: db_events, table_name: 'locations', 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__event_location({ // obj_type: 'event_location', // obj_li: event_location_obj_li_get_result // }); } return event_location_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__event_location_obj_li:', ae_promises.load__event_location_obj_li); } if (inc_device_li) { // Load the devices for the locations if (log_lvl) { console.log(`Need to load the device list for each location now`); } for (let i = 0; i < ae_promises.load__event_location_obj_li.length; i++) { let event_location_obj = ae_promises.load__event_location_obj_li[i]; let event_location_id = event_location_obj.event_location_id_random; let load_event_device_obj_li = load_ae_obj_li__event_device({ api_cfg: api_cfg, for_obj_type: 'event_location', for_obj_id: event_location_id, params: {qry__enabled: enabled, qry__limit: limit}, try_cache: try_cache, log_lvl: log_lvl }) .then((event_device_obj_li) => { if (log_lvl) { console.log(`event_device_obj_li = `, event_device_obj_li); } return event_device_obj_li; }); if (log_lvl) { console.log(`load_event_device_obj_li = `, load_event_device_obj_li); } } } if (inc_file_li) { // Load the files for the locations if (log_lvl) { console.log(`Need to load the file list for each location now`); } for (let i = 0; i < ae_promises.load__event_location_obj_li.length; i++) { let event_location_obj = ae_promises.load__event_location_obj_li[i]; let event_location_id = event_location_obj.event_location_id_random; let load_event_file_obj_li = load_ae_obj_li__event_file({ api_cfg: api_cfg, for_obj_type: 'event_location', for_obj_id: event_location_id, enabled: enabled, limit: limit, try_cache: try_cache, log_lvl: log_lvl }) .then((event_file_obj_li) => { if (log_lvl) { console.log(`event_file_obj_li = `, event_file_obj_li); } return event_file_obj_li; }); if (log_lvl) { console.log(`load_event_file_obj_li = `, load_event_file_obj_li); } } } if (inc_session_li) { // Load the sessions for the locations if (log_lvl) { console.log(`Need to load the session list for each location now`); } for (let i = 0; i < ae_promises.load__event_location_obj_li.length; i++) { let event_location_obj = ae_promises.load__event_location_obj_li[i]; let event_location_id = event_location_obj.event_location_id_random; let load_event_session_obj_li = load_ae_obj_li__event_session({ api_cfg: api_cfg, for_obj_type: 'event_location', for_obj_id: event_location_id, enabled: enabled, limit: limit, try_cache: try_cache, log_lvl: log_lvl }) .then((event_session_obj_li) => { if (log_lvl) { console.log(`event_session_obj_li = `, event_session_obj_li); } return event_session_obj_li; }); if (log_lvl) { console.log(`load_event_session_obj_li = `, load_event_session_obj_li); } } } return ae_promises.load__event_location_obj_li; } // Updated 2025-05-23 export async function create_ae_obj__event_location( { api_cfg, event_id, data_kv, params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, event_id: string, data_kv: key_val, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** create_ae_obj__event_location() *** event_id=${event_id}`); } ae_promises.create__event_location = await api.create_ae_obj_crud({ api_cfg: api_cfg, obj_type: 'event_location', fields: { event_id_random: event_id, ...data_kv }, key: api_cfg.api_crud_super_key, params: params, return_obj: true, log_lvl: log_lvl }) .then(async function (event_location_obj_create_result) { if (event_location_obj_create_result) { if (try_cache) { // Process the results first let processed_obj_li = await process_ae_obj__event_location_props({ obj_li: [event_location_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...'); } db_save_ae_obj_li__ae_obj({ db_instance: db_events, table_name: 'locations', 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__event_location( // { // obj_type: 'event_location', // obj_li: [event_location_obj_create_result] // }); } return event_location_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__event_location:', ae_promises.create__event_location); } return ae_promises.create__event_location; } // Updated 2025-05-23 export async function delete_ae_obj_id__event_location( { api_cfg, event_location_id, method = 'delete', // 'delete', 'disable', 'hide' params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, event_location_id: string, method?: string, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** delete_ae_obj_id__event_location() *** event_location_id=${event_location_id}`); } ae_promises.delete__event_location_obj = await api.delete_ae_obj_id_crud({ api_cfg: api_cfg, obj_type: 'event_location', obj_id: event_location_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 event_location_id=${event_location_id}`); } db_events.locations.delete(event_location_id); } }); if (log_lvl) { console.log('ae_promises.delete__event_location_obj:', ae_promises.delete__event_location_obj); } return ae_promises.delete__event_location_obj; } // Updated 2025-05-23 export async function update_ae_obj__event_location( { api_cfg, event_location_id, data_kv, params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, event_location_id: string, data_kv: key_val, params?: key_val, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** update_ae_obj__event_location() *** event_location_id=${event_location_id}`, data_kv); } ae_promises.update__event_location_obj = await api.update_ae_obj_id_crud({ api_cfg: api_cfg, obj_type: 'event_location', obj_id: event_location_id, fields: data_kv, key: api_cfg.api_crud_super_key, params: params, return_obj: true, log_lvl: log_lvl }) .then(async function (event_location_obj_update_result) { if (event_location_obj_update_result) { if (try_cache) { // Process the results first let processed_obj_li = await process_ae_obj__event_location_props({ obj_li: [event_location_obj_update_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_events, table_name: 'locations', 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__event_location({ // obj_type: 'event_location', obj_li: [event_location_obj_update_result] // }); } return event_location_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__event_location_obj:', ae_promises.update__event_location_obj); } return ae_promises.update__event_location_obj; } // Updated 2025-05-23 export async function search__event_location( { api_cfg, event_id, fulltext_search_qry_str, ft_presenter_search_qry_str, like_search_qry_str = null, like_presentation_search_qry_str = null, like_presenter_search_qry_str = null, params = {}, try_cache = true, log_lvl = 0 }: { api_cfg: any, event_id: any, fulltext_search_qry_str?: null|string, ft_presenter_search_qry_str?: null|string, like_search_qry_str?: null|string, like_presentation_search_qry_str?: null|string, like_presenter_search_qry_str?: null|string, params?: any, try_cache?: boolean, log_lvl?: number } ) { if (log_lvl) { console.log(`*** search__event_location() *** event_id=${event_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 ?? 25); // 99 let offset: number = (params.qry__offset ?? 0); // 0 let params_json: key_val = {}; if (!fulltext_search_qry_str && !like_search_qry_str) { console.log('No search string provided!!!'); return false; // Returning false instead of [] because no search was performed. } if (fulltext_search_qry_str || ft_presenter_search_qry_str) { params_json['ft_qry'] = {}; if (fulltext_search_qry_str && fulltext_search_qry_str.length > 2) { params_json['ft_qry']['default_qry_str'] = fulltext_search_qry_str; } if (ft_presenter_search_qry_str && ft_presenter_search_qry_str.length > 2) { params_json['ft_qry']['event_presenter_li_qry_str'] = ft_presenter_search_qry_str; } } // Use the AND (AND LIKE) query // if (like_search_qry_str || like_presenter_search_qry_str) { // params_json['and_like'] = {}; // if (like_search_qry_str && like_search_qry_str.length > 2) { // params_json['and_like']['default_qry_str'] = like_search_qry_str; // } // if (like_presenter_search_qry_str && like_presenter_search_qry_str.length > 2) { // params_json['and_like']['event_presenter_li_qry_str'] = like_presenter_search_qry_str; // } // } // Use the AND (OR LIKE) query if (like_search_qry_str || like_presentation_search_qry_str || like_presenter_search_qry_str) { params_json['or_like'] = {}; if (like_search_qry_str && like_search_qry_str.length > 2) { params_json['or_like']['default_qry_str'] = like_search_qry_str; } if (like_presentation_search_qry_str && like_presentation_search_qry_str.length > 2) { params_json['or_like']['event_presentation_li_qry_str'] = like_presentation_search_qry_str; } if (like_presenter_search_qry_str && like_presenter_search_qry_str.length > 2) { params_json['or_like']['event_presenter_li_qry_str'] = like_presenter_search_qry_str; } } params_json['and_qry'] = {}; // if (location_type_code) { // params_json['and_qry']['location_type_code'] = location_type_code; // } let order_by_li = {'priority': 'DESC', 'sort': 'DESC', 'start_datetime': 'ASC', 'name': 'ASC', 'updated_on': 'DESC', 'created_on': 'DESC'}; // ae_promises.load__event_location_obj_li = await api.get_ae_obj_li_for_obj_id_crud({ ae_promises.load__event_location_obj_li = await api.get_ae_obj_li_for_obj_id_crud_v2({ api_cfg: api_cfg, obj_type: 'event_location', for_obj_type: 'event', for_obj_id: event_id, use_alt_tbl: true, // NOTE: We want to use the alt table for location searching // use_alt_mdl: 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 (event_location_obj_li_get_result) { if (event_location_obj_li_get_result) { if (try_cache) { db_save_ae_obj_li__event_location({obj_type: 'event_location', obj_li: event_location_obj_li_get_result}); } return event_location_obj_li_get_result; } else { return []; } }) .catch(function (error) { console.log('No results returned or failed.', error); }) .finally(function () { }); if (log_lvl) { console.log('ae_promises.load__event_location_obj_li:', ae_promises.load__event_location_obj_li); } return ae_promises.load__event_location_obj_li; } // This function will loop through the event_location_obj_li and save each one to the DB. // Updated 2024-06-25 export function db_save_ae_obj_li__event_location( { 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__event_location() ***`); } if (obj_li && obj_li.length) { obj_li.forEach(async function (obj: any) { if (log_lvl) { console.log(`ae_obj ${obj_type}:`, obj); } try { const id_random = await db_events.locations.put({ id: obj.event_location_id_random, event_location_id: obj.event_location_id_random, event_location_id_random: obj.event_location_id_random, external_id: obj.external_id, code: obj.code, type_code: obj.type_code, event_id: obj.event_id_random, event_id_random: obj.event_id_random, name: obj.name, description: obj.description, passcode: obj.passcode, hide_event_launcher: obj.hide_event_launcher, alert: obj.alert, alert_msg: obj.alert_msg, data_json: obj.data_json, 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, // From SQL view file_count: obj.file_count, file_count_all: obj.file_count_all, internal_use_count: obj.internal_use_count, event_file_id_li_json: obj.event_file_id_li_json, // poc_person_given_name: obj.poc_person_given_name, // poc_person_family_name: obj.poc_person_family_name, // poc_person_full_name: obj.poc_person_full_name, // poc_person_primary_email: obj.poc_person_primary_email, // poc_person_passcode: obj.poc_person_passcode, // poc_kv_json: obj.poc_kv_json, event_name: obj.event_name, }); // console.log(`Put obj with ID: ${obj.event_location_id_random} or ${id_random}`); } catch (error) { let status = `Failed to put ${obj.event_location_id_random}: ${error}`; console.log(status); } // const id_random = await db_events.locations.put(obj); // console.log(`Put obj with ID: ${obj.event_location_id_random}`); }); return true; } } // Updated 2025-05-23 export const properties_to_save = [ 'id', 'event_location_id', 'event_location_id_random', 'external_id', 'code', 'type_code', 'event_id', 'event_id_random', 'name', 'description', 'passcode', 'hide_event_launcher', 'alert', 'alert_msg', 'data_json', 'enable', 'hide', 'priority', 'sort', 'group', 'notes', 'created_on', 'updated_on', // Generated fields for sorting locally only 'tmp_sort_1', 'tmp_sort_2', // 'tmp_sort_a', // 'tmp_sort_b', // From SQL view 'file_count', 'file_count_all', 'internal_use_count', 'event_file_id_li_json', 'event_name', ]; // Updated 2025-05-23 export async function process_ae_obj__event_location_props({ obj_li, log_lvl = 0, }: { obj_li: any[]; log_lvl?: number; }) { if (log_lvl) { console.log(`*** process_ae_obj__event_location_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 event_location:`, obj); let processed_obj = { id: obj.event_location_id_random, event_location_id: obj.event_location_id_random, event_location_id_random: obj.event_location_id_random, external_id: obj.external_id, code: obj.code, type_code: obj.type_code, event_id: obj.event_id_random, event_id_random: obj.event_id_random, name: obj.name, description: obj.description, passcode: obj.passcode, hide_event_launcher: obj.hide_event_launcher, alert: obj.alert, alert_msg: obj.alert_msg, data_json: obj.data_json, 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}`, // From SQL view file_count: obj.file_count, file_count_all: obj.file_count_all, internal_use_count: obj.internal_use_count, event_file_id_li_json: obj.event_file_id_li_json, event_name: obj.event_name, }; processed_obj_li.push(processed_obj); } return processed_obj_li; }