- Updated '_refresh_file_li_background' to manually inject 'for_id', 'for_type', and specific object IDs if they are missing from the API response. - This ensures robust indexing and retrieval from Dexie even when the V3 backend fails to populate linking fields. - Verified that these 'fixed' objects are correctly processed and saved to the local cache.
543 lines
14 KiB
TypeScript
543 lines
14 KiB
TypeScript
import type { key_val } from '$lib/stores/ae_stores';
|
|
import { api } from '$lib/api/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 type { ae_EventFile } from '$lib/types/ae_types';
|
|
|
|
const ae_promises: key_val = {};
|
|
|
|
// Updated 2026-01-30: Fixed - Removed aggressive ID overwriting
|
|
export async function load_ae_obj_id__event_file({
|
|
api_cfg,
|
|
event_file_id,
|
|
view = 'default',
|
|
try_cache = false,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any;
|
|
event_file_id: string;
|
|
view?: string;
|
|
try_cache?: boolean;
|
|
log_lvl?: number;
|
|
}): Promise<ae_EventFile | null> {
|
|
if (log_lvl) {
|
|
console.log(
|
|
`*** load_ae_obj_id__event_file() *** [V3] id=${event_file_id} (SWR)`
|
|
);
|
|
}
|
|
|
|
if (try_cache) {
|
|
try {
|
|
const cached = await db_events.file.get(event_file_id);
|
|
if (cached) {
|
|
_refresh_file_id_background({
|
|
api_cfg,
|
|
event_file_id,
|
|
view,
|
|
try_cache,
|
|
log_lvl: 0
|
|
});
|
|
return cached;
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
return await _refresh_file_id_background({
|
|
api_cfg,
|
|
event_file_id,
|
|
view,
|
|
try_cache,
|
|
log_lvl
|
|
});
|
|
}
|
|
|
|
async function _refresh_file_id_background({
|
|
api_cfg,
|
|
event_file_id,
|
|
view,
|
|
try_cache,
|
|
log_lvl
|
|
}: any) {
|
|
if (typeof navigator !== 'undefined' && !navigator.onLine) return null;
|
|
try {
|
|
const result = await api.get_ae_obj_v3({
|
|
api_cfg,
|
|
obj_type: 'event_file',
|
|
obj_id: event_file_id,
|
|
view,
|
|
log_lvl
|
|
});
|
|
if (result) {
|
|
const processed = await process_ae_obj__event_file_props({
|
|
obj_li: [result],
|
|
log_lvl
|
|
});
|
|
const processed_obj = processed[0];
|
|
if (try_cache) {
|
|
await db_save_ae_obj_li__ae_obj({
|
|
db_instance: db_events,
|
|
table_name: 'file',
|
|
obj_li: [processed_obj],
|
|
properties_to_save,
|
|
log_lvl
|
|
});
|
|
}
|
|
return processed_obj;
|
|
}
|
|
} catch (e) {}
|
|
return null;
|
|
}
|
|
|
|
export async function load_ae_obj_li__event_file({
|
|
api_cfg,
|
|
for_obj_type,
|
|
for_obj_id,
|
|
enabled = 'enabled',
|
|
hidden = 'not_hidden',
|
|
view = 'default',
|
|
limit = 100,
|
|
offset = 0,
|
|
order_by_li = [
|
|
{ priority: 'DESC' },
|
|
{ sort: 'DESC' },
|
|
{ updated_on: 'DESC' }
|
|
],
|
|
try_cache = true,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any;
|
|
for_obj_type: string;
|
|
for_obj_id: string;
|
|
enabled?: 'enabled' | 'all' | 'not_enabled';
|
|
hidden?: 'hidden' | 'all' | 'not_hidden';
|
|
view?: string;
|
|
limit?: number;
|
|
offset?: number;
|
|
order_by_li?: any;
|
|
try_cache?: boolean;
|
|
log_lvl?: number;
|
|
}): Promise<ae_EventFile[]> {
|
|
if (log_lvl) {
|
|
console.log(
|
|
`*** load_ae_obj_li__event_file() *** [V3] for=${for_obj_type}:${for_obj_id} (SWR)`
|
|
);
|
|
}
|
|
|
|
if (try_cache) {
|
|
try {
|
|
const cached_li = await db_events.file
|
|
.where('for_id')
|
|
.equals(for_obj_id)
|
|
.toArray();
|
|
if (cached_li && cached_li.length > 0) {
|
|
_refresh_file_li_background({
|
|
api_cfg,
|
|
for_obj_type,
|
|
for_obj_id,
|
|
enabled,
|
|
hidden,
|
|
view,
|
|
limit,
|
|
offset,
|
|
order_by_li,
|
|
try_cache,
|
|
log_lvl: 0
|
|
});
|
|
return cached_li;
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
return await _refresh_file_li_background({
|
|
api_cfg,
|
|
for_obj_type,
|
|
for_obj_id,
|
|
enabled,
|
|
hidden,
|
|
view,
|
|
limit,
|
|
offset,
|
|
order_by_li,
|
|
try_cache,
|
|
log_lvl
|
|
});
|
|
}
|
|
|
|
async function _refresh_file_li_background({
|
|
api_cfg,
|
|
for_obj_type,
|
|
for_obj_id,
|
|
enabled,
|
|
hidden,
|
|
view,
|
|
limit,
|
|
offset,
|
|
order_by_li,
|
|
try_cache,
|
|
log_lvl
|
|
}: any) {
|
|
if (typeof navigator !== 'undefined' && !navigator.onLine) return [];
|
|
try {
|
|
const result_li = await api.get_ae_obj_li_v3({
|
|
api_cfg,
|
|
obj_type: 'event_file',
|
|
for_obj_type,
|
|
for_obj_id,
|
|
enabled,
|
|
hidden,
|
|
view,
|
|
limit,
|
|
offset,
|
|
order_by_li,
|
|
log_lvl
|
|
});
|
|
if (result_li) {
|
|
// SAFETY NET: If API returns null IDs, inject the ones we used for the query
|
|
const fixed_li = result_li.map((obj: any) => {
|
|
const fixed_obj = { ...obj };
|
|
if (!fixed_obj.for_type) fixed_obj.for_type = for_obj_type;
|
|
if (!fixed_obj.for_id) fixed_obj.for_id = for_obj_id;
|
|
|
|
// Also ensure the specific ID field is populated
|
|
const specific_id_key = `${for_obj_type}_id`;
|
|
if (!fixed_obj[specific_id_key])
|
|
fixed_obj[specific_id_key] = for_obj_id;
|
|
|
|
return fixed_obj;
|
|
});
|
|
|
|
const processed = await process_ae_obj__event_file_props({
|
|
obj_li: fixed_li,
|
|
log_lvl
|
|
});
|
|
if (try_cache) {
|
|
await db_save_ae_obj_li__ae_obj({
|
|
db_instance: db_events,
|
|
table_name: 'file',
|
|
obj_li: processed,
|
|
properties_to_save,
|
|
log_lvl
|
|
});
|
|
}
|
|
return processed;
|
|
}
|
|
} catch (e) {}
|
|
return [];
|
|
}
|
|
|
|
export async function create_event_file_obj_from_hosted_file_async({
|
|
api_cfg,
|
|
hosted_file_id,
|
|
params = {},
|
|
data = {},
|
|
return_obj = false,
|
|
inc_hosted_file = false,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any;
|
|
hosted_file_id: string;
|
|
params?: key_val;
|
|
data?: key_val;
|
|
return_obj?: boolean;
|
|
inc_hosted_file?: boolean;
|
|
log_lvl?: number;
|
|
}) {
|
|
if (!hosted_file_id) return false;
|
|
|
|
// Use V3 endpoint for creation from hosted file
|
|
const endpoint = `/v3/action/event_file/from_hosted_file/${hosted_file_id}`;
|
|
const query_params = { ...params };
|
|
if (return_obj) query_params['return_obj'] = true;
|
|
if (inc_hosted_file) query_params['inc_hosted_file'] = true;
|
|
|
|
const result = await api.post_object({
|
|
api_cfg,
|
|
endpoint,
|
|
params: query_params,
|
|
data,
|
|
log_lvl
|
|
});
|
|
|
|
if (return_obj) return result;
|
|
return result?.event_file_id || result?.id || result?.event_file_id_random;
|
|
}
|
|
|
|
export async function delete_ae_obj_id__event_file({
|
|
api_cfg,
|
|
event_file_id,
|
|
params = {},
|
|
try_cache = true,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any;
|
|
event_file_id: string;
|
|
params?: key_val;
|
|
try_cache?: boolean;
|
|
log_lvl?: number;
|
|
}) {
|
|
const result = await api.delete_ae_obj_v3({
|
|
api_cfg,
|
|
obj_type: 'event_file',
|
|
obj_id: event_file_id,
|
|
params: { ...params, delete_hosted_file: true, rm_orphan: true },
|
|
log_lvl
|
|
});
|
|
if (try_cache) await db_events.file.delete(event_file_id);
|
|
return result;
|
|
}
|
|
|
|
export async function update_ae_obj__event_file({
|
|
api_cfg,
|
|
event_file_id,
|
|
data_kv,
|
|
params = {},
|
|
try_cache = true,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any;
|
|
event_file_id: string;
|
|
data_kv: key_val;
|
|
params?: key_val;
|
|
try_cache?: boolean;
|
|
log_lvl?: number;
|
|
}): Promise<ae_EventFile | null> {
|
|
const result = await api.update_ae_obj_v3({
|
|
api_cfg,
|
|
obj_type: 'event_file',
|
|
obj_id: event_file_id,
|
|
fields: data_kv,
|
|
params,
|
|
log_lvl
|
|
});
|
|
if (result && try_cache) {
|
|
const processed = await process_ae_obj__event_file_props({
|
|
obj_li: [result],
|
|
log_lvl
|
|
});
|
|
await db_save_ae_obj_li__ae_obj({
|
|
db_instance: db_events,
|
|
table_name: 'file',
|
|
obj_li: processed,
|
|
properties_to_save,
|
|
log_lvl
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export async function search__event_file({
|
|
api_cfg,
|
|
event_id,
|
|
qry_str = '',
|
|
qry_created_on = null,
|
|
qry_min_file_size = null,
|
|
qry_file_purpose = null,
|
|
enabled = 'enabled',
|
|
hidden = 'not_hidden',
|
|
view = 'default',
|
|
limit = 25,
|
|
offset = 0,
|
|
order_by_li = [
|
|
{ priority: 'DESC' },
|
|
{ sort: 'DESC' },
|
|
{ updated_on: 'DESC' }
|
|
],
|
|
try_cache = true,
|
|
log_lvl = 0
|
|
}: {
|
|
api_cfg: any;
|
|
event_id: string;
|
|
qry_str?: string;
|
|
qry_created_on?: string | null;
|
|
qry_min_file_size?: null | number;
|
|
qry_file_purpose?: string | null;
|
|
enabled?: 'enabled' | 'all' | 'not_enabled';
|
|
hidden?: 'hidden' | 'all' | 'not_hidden';
|
|
view?: string;
|
|
limit?: number;
|
|
offset?: number;
|
|
order_by_li?: any;
|
|
try_cache?: boolean;
|
|
log_lvl?: number;
|
|
}): Promise<ae_EventFile[]> {
|
|
const search_query: any = {
|
|
q: qry_str,
|
|
and: [{ field: 'event_id', op: 'eq', value: event_id }]
|
|
};
|
|
if (qry_min_file_size)
|
|
search_query.and.push({
|
|
field: 'hosted_file_size',
|
|
op: 'gt',
|
|
value: qry_min_file_size
|
|
});
|
|
if (qry_created_on)
|
|
search_query.and.push({
|
|
field: 'created_on',
|
|
op: 'gte',
|
|
value: qry_created_on
|
|
});
|
|
if (qry_file_purpose)
|
|
search_query.and.push({
|
|
field: 'file_purpose',
|
|
op: 'eq',
|
|
value: qry_file_purpose
|
|
});
|
|
const result_li = await api.search_ae_obj_v3({
|
|
api_cfg,
|
|
obj_type: 'event_file',
|
|
search_query,
|
|
enabled,
|
|
hidden,
|
|
view,
|
|
order_by_li,
|
|
limit,
|
|
offset,
|
|
log_lvl
|
|
});
|
|
if (result_li && try_cache) {
|
|
const processed = await process_ae_obj__event_file_props({
|
|
obj_li: result_li,
|
|
log_lvl
|
|
});
|
|
await db_save_ae_obj_li__ae_obj({
|
|
db_instance: db_events,
|
|
table_name: 'file',
|
|
obj_li: processed,
|
|
properties_to_save,
|
|
log_lvl
|
|
});
|
|
}
|
|
return result_li || [];
|
|
}
|
|
|
|
export const qry__event_file = search__event_file;
|
|
|
|
export const properties_to_save = [
|
|
'id',
|
|
'event_file_id',
|
|
'event_file_id_random',
|
|
'hosted_file_id',
|
|
'hosted_file_id_random',
|
|
'hash_sha256',
|
|
'for_type',
|
|
'for_id',
|
|
'for_id_random',
|
|
'event_id',
|
|
'event_id_random',
|
|
'event_session_id',
|
|
'event_presentation_id',
|
|
'event_presenter_id',
|
|
'event_location_id',
|
|
'filename',
|
|
'extension',
|
|
'open_in_os',
|
|
'lu_file_purpose_id',
|
|
'lu_event_file_purpose_name',
|
|
'file_purpose',
|
|
'enable',
|
|
'hide',
|
|
'priority',
|
|
'sort',
|
|
'group',
|
|
'notes',
|
|
'created_on',
|
|
'updated_on',
|
|
'tmp_sort_1',
|
|
'tmp_sort_2',
|
|
'filename_no_ext',
|
|
'filename_w_ext',
|
|
'hosted_file_content_type',
|
|
'file_size',
|
|
'hosted_file_size',
|
|
'event_location_code',
|
|
'event_location_name',
|
|
'event_session_code',
|
|
'event_session_type_code',
|
|
'event_session_name',
|
|
'event_session_start_datetime',
|
|
'event_session_end_datetime',
|
|
'event_presentation_code',
|
|
'event_presentation_type_code',
|
|
'event_presentation_name',
|
|
'event_presentation_start_datetime',
|
|
'event_presentation_end_datetime',
|
|
'event_presenter_given_name',
|
|
'event_presenter_family_name',
|
|
'event_presenter_full_name',
|
|
'event_presenter_email'
|
|
];
|
|
|
|
async function _process_generic_props<T extends Record<string, any>>({
|
|
obj_li,
|
|
obj_type,
|
|
log_lvl = 0,
|
|
specific_processor
|
|
}: {
|
|
obj_li: T[];
|
|
obj_type: string;
|
|
log_lvl?: number;
|
|
specific_processor?: (obj: T) => Promise<T> | T;
|
|
}): Promise<T[]> {
|
|
if (!obj_li || obj_li.length === 0) return [];
|
|
const processed_obj_li: T[] = [];
|
|
for (const original_obj of obj_li) {
|
|
let processed_obj = { ...original_obj };
|
|
for (const key in processed_obj) {
|
|
if (key.endsWith('_random')) {
|
|
const newKey = key.slice(0, -7);
|
|
(processed_obj as any)[newKey] = processed_obj[key];
|
|
}
|
|
}
|
|
const randomIdKey = `${obj_type}_id_random`;
|
|
if (processed_obj[randomIdKey])
|
|
(processed_obj as any).id = processed_obj[randomIdKey];
|
|
const group = processed_obj.group ?? '0';
|
|
const priority = processed_obj.priority ? 1 : 0;
|
|
const sort = processed_obj.sort ?? '0';
|
|
const updated = processed_obj.updated_on ?? processed_obj.created_on;
|
|
const name = processed_obj.name ?? '';
|
|
(processed_obj as any).tmp_sort_1 =
|
|
`${group}_${priority}_${sort}_${updated}`;
|
|
(processed_obj as any).tmp_sort_2 =
|
|
`${group}_${priority}_${sort}_${name}_${updated}`;
|
|
if (specific_processor)
|
|
processed_obj = await Promise.resolve(
|
|
specific_processor(processed_obj)
|
|
);
|
|
processed_obj_li.push(processed_obj as T);
|
|
}
|
|
return processed_obj_li;
|
|
}
|
|
|
|
export async function process_ae_obj__event_file_props({
|
|
obj_li,
|
|
log_lvl = 0
|
|
}: {
|
|
obj_li: any[];
|
|
log_lvl?: number;
|
|
}) {
|
|
return _process_generic_props({
|
|
obj_li,
|
|
obj_type: 'event_file',
|
|
log_lvl,
|
|
specific_processor: (obj) => {
|
|
// SYNC: Ensure for_id matches the specific object ID it was linked to
|
|
if (obj.for_type && !obj.for_id) {
|
|
const specific_id_key = `${obj.for_type}_id`;
|
|
if (obj[specific_id_key]) {
|
|
obj.for_id = obj[specific_id_key];
|
|
}
|
|
}
|
|
// SYNC: Ensure specific object ID is populated if for_id is present
|
|
if (obj.for_type && obj.for_id) {
|
|
const specific_id_key = `${obj.for_type}_id`;
|
|
if (!obj[specific_id_key]) {
|
|
obj[specific_id_key] = obj.for_id;
|
|
}
|
|
}
|
|
return obj;
|
|
}
|
|
});
|
|
}
|