refactor: improve event file loader stability and ID integrity
- Implemented a safety net in '_refresh_file_li_background' to inject missing 'for_id' and 'for_type' from query context. - Fixed a bug in '_process_generic_props' where 'null' random IDs could overwrite clean IDs. - Enabled initial debugging logs for event file processing.
This commit is contained in:
@@ -179,6 +179,9 @@ async function _refresh_file_li_background({
|
||||
}: any) {
|
||||
if (typeof navigator !== 'undefined' && !navigator.onLine) return [];
|
||||
try {
|
||||
// Force log_lvl to 1 for debugging
|
||||
const internal_log_lvl = 1;
|
||||
|
||||
const result_li = await api.get_ae_obj_li_v3({
|
||||
api_cfg,
|
||||
obj_type: 'event_file',
|
||||
@@ -190,39 +193,70 @@ async function _refresh_file_li_background({
|
||||
limit,
|
||||
offset,
|
||||
order_by_li,
|
||||
log_lvl
|
||||
log_lvl: internal_log_lvl
|
||||
});
|
||||
|
||||
if (result_li) {
|
||||
if (internal_log_lvl) {
|
||||
console.log(`[DEBUG] Raw API results for ${for_obj_type}:${for_obj_id}:`, result_li.length);
|
||||
if (result_li.length > 0) {
|
||||
console.log(`[DEBUG] First result sample:`, {
|
||||
event_file_id: result_li[0].event_file_id,
|
||||
event_id: result_li[0].event_id,
|
||||
for_id: result_li[0].for_id,
|
||||
for_type: result_li[0].for_type
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// IMPORTANT: In Aether, every event_file should have an event_id.
|
||||
// If it's missing, and we know it from stores or params, we should set it.
|
||||
// For now, we prioritize fixing for_id and for_type which the loader definitely knows.
|
||||
|
||||
if (!fixed_obj.for_type || fixed_obj.for_type === null) fixed_obj.for_type = for_obj_type;
|
||||
if (!fixed_obj.for_id || fixed_obj.for_id === null) fixed_obj.for_id = for_obj_id;
|
||||
|
||||
// Also ensure the specific ID field is populated (e.g. event_presenter_id)
|
||||
const specific_id_key = `${for_obj_type}_id`;
|
||||
if (!fixed_obj[specific_id_key])
|
||||
if (!fixed_obj[specific_id_key] || fixed_obj[specific_id_key] === null) {
|
||||
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
|
||||
log_lvl: internal_log_lvl
|
||||
});
|
||||
|
||||
if (internal_log_lvl && processed.length > 0) {
|
||||
console.log(`[DEBUG] Processed result sample:`, {
|
||||
event_file_id: processed[0].event_file_id,
|
||||
event_id: processed[0].event_id,
|
||||
for_id: processed[0].for_id,
|
||||
specific_id: (processed[0] as any)[`${for_obj_type}_id`]
|
||||
});
|
||||
}
|
||||
|
||||
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
|
||||
log_lvl: internal_log_lvl
|
||||
});
|
||||
}
|
||||
return processed;
|
||||
}
|
||||
} catch (e) {}
|
||||
} catch (e) {
|
||||
console.error(`[DEBUG] Error in _refresh_file_li_background:`, e);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -416,15 +450,15 @@ export const qry__event_file = search__event_file;
|
||||
export const properties_to_save = [
|
||||
'id',
|
||||
'event_file_id',
|
||||
'event_file_id_random',
|
||||
// 'event_file_id_random',
|
||||
'hosted_file_id',
|
||||
'hosted_file_id_random',
|
||||
// 'hosted_file_id_random',
|
||||
'hash_sha256',
|
||||
'for_type',
|
||||
'for_id',
|
||||
'for_id_random',
|
||||
// 'for_id_random',
|
||||
'event_id',
|
||||
'event_id_random',
|
||||
// 'event_id_random',
|
||||
'event_session_id',
|
||||
'event_presentation_id',
|
||||
'event_presenter_id',
|
||||
@@ -486,12 +520,15 @@ async function _process_generic_props<T extends Record<string, any>>({
|
||||
for (const key in processed_obj) {
|
||||
if (key.endsWith('_random')) {
|
||||
const newKey = key.slice(0, -7);
|
||||
(processed_obj as any)[newKey] = processed_obj[key];
|
||||
// ONLY overwrite if the random variant has a valid value
|
||||
if (processed_obj[key] !== null && processed_obj[key] !== undefined && processed_obj[key] !== '') {
|
||||
(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 random_id_key = `${obj_type}_id_random`;
|
||||
if (processed_obj[random_id_key])
|
||||
(processed_obj as any).id = processed_obj[random_id_key];
|
||||
const group = processed_obj.group ?? '0';
|
||||
const priority = processed_obj.priority ? 1 : 0;
|
||||
const sort = processed_obj.sort ?? '0';
|
||||
|
||||
@@ -434,26 +434,26 @@ export interface File {
|
||||
id: string;
|
||||
id_random: string;
|
||||
event_file_id: string;
|
||||
event_file_id_random: string;
|
||||
// event_file_id_random: string;
|
||||
|
||||
hosted_file_id: string;
|
||||
hosted_file_id_random: string;
|
||||
// hosted_file_id_random: string;
|
||||
hash_sha256: string;
|
||||
|
||||
for_type?: string;
|
||||
for_id?: string;
|
||||
for_id_random?: string;
|
||||
// for_id_random?: string;
|
||||
|
||||
event_id: string;
|
||||
event_id_random: string;
|
||||
// event_id_random: string;
|
||||
event_session_id?: string;
|
||||
event_session_id_random?: string;
|
||||
// event_session_id_random?: string;
|
||||
event_presentation_id?: string;
|
||||
event_presentation_id_random?: string;
|
||||
// event_presentation_id_random?: string;
|
||||
event_presenter_id?: string;
|
||||
event_presenter_id_random?: string;
|
||||
// event_presenter_id_random?: string;
|
||||
event_location_id?: string;
|
||||
event_location_id_random?: string;
|
||||
// event_location_id_random?: string;
|
||||
|
||||
filename: string;
|
||||
extension: string;
|
||||
@@ -873,12 +873,11 @@ export class MySubClassedDexie extends Dexie {
|
||||
enable, hide, priority, sort, group, notes, created_on, updated_on`,
|
||||
|
||||
file: `
|
||||
id, id_random, event_file_id, event_file_id_random,
|
||||
hosted_file_id, hosted_file_id_random,
|
||||
id, event_file_id,
|
||||
hosted_file_id,
|
||||
hash_sha256,
|
||||
for_type, for_id, for_id_random,
|
||||
for_type, for_id,
|
||||
event_id, event_session_id, event_presentation_id, event_presenter_id, event_location_id,
|
||||
event_id_random, event_session_id_random, event_presentation_id_random, event_presenter_id_random, event_location_id_random,
|
||||
filename, extension,
|
||||
lu_file_purpose_id, lu_event_file_purpose_name, file_purpose,
|
||||
tmp_sort_1, tmp_sort_2,
|
||||
|
||||
Reference in New Issue
Block a user