fix(data_store): implement hierarchical priority logic in liveQuery; update project plans
This commit is contained in:
@@ -72,41 +72,36 @@
|
||||
if (log_lvl) console.log(`ae_e_data_store [${current_code}]: LQ Lookup...`, { account_id, current_for_type, current_for_id });
|
||||
|
||||
// Hierarchical Local Lookup (Specific -> Account -> Global)
|
||||
let result = null;
|
||||
|
||||
// 0. Code Only Lookup
|
||||
// NOTE: Why this works. There should only be one record per code in the *local* Dexie DB. This is correct in 99.9% of cases.
|
||||
if (log_lvl) console.log(`ae_e_data_store [${current_code}]: Trying Specific Code Lookup...`);
|
||||
result = await db_core.data_store
|
||||
// Mimics backend SQL priority: WHERE code = :code ORDER BY for_id DESC, account_id DESC
|
||||
if (log_lvl) console.log(`ae_e_data_store [${current_code}]: Fetching all matching codes for priority sorting...`);
|
||||
|
||||
const results = await db_core.data_store
|
||||
.where('code')
|
||||
.equals(current_code)
|
||||
.first();
|
||||
.toArray();
|
||||
|
||||
// // 1. Specific Lookup
|
||||
// if (current_for_type && current_for_id) {
|
||||
// result = await db_core.data_store
|
||||
// .where('[code+for_type+for_id]')
|
||||
// .equals([current_code, current_for_type, current_for_id])
|
||||
// .first();
|
||||
// }
|
||||
if (!results || results.length === 0) return null;
|
||||
|
||||
// // 2. Account Lookup
|
||||
// if (!result && account_id) {
|
||||
// result = await db_core.data_store
|
||||
// .where('[code+account_id+for_type]')
|
||||
// .equals([current_code, account_id, null])
|
||||
// .first();
|
||||
// }
|
||||
// Sort by specificity
|
||||
results.sort((a, b) => {
|
||||
// 1. Priority: Specific Context match (for_type + for_id)
|
||||
const a_context = (current_for_id && a.for_id === current_for_id && a.for_type === current_for_type) ? 1 : 0;
|
||||
const b_context = (current_for_id && b.for_id === current_for_id && b.for_type === current_for_type) ? 1 : 0;
|
||||
if (a_context !== b_context) return b_context - a_context;
|
||||
|
||||
// // 3. Global Lookup
|
||||
// if (!result) {
|
||||
// result = await db_core.data_store
|
||||
// .where('[code+account_id+for_type]')
|
||||
// .equals([current_code, null, null])
|
||||
// .first();
|
||||
// }
|
||||
// 2. Priority: Account-specific match
|
||||
const a_account = (account_id && a.account_id === account_id) ? 1 : 0;
|
||||
const b_account = (account_id && b.account_id === account_id) ? 1 : 0;
|
||||
if (a_account !== b_account) return b_account - a_account;
|
||||
|
||||
return result;
|
||||
// 3. Tie-breaker: Newest updated
|
||||
const a_time = new Date(a.updated_on || a.created_on || 0).getTime();
|
||||
const b_time = new Date(b.updated_on || b.created_on || 0).getTime();
|
||||
return b_time - a_time;
|
||||
});
|
||||
|
||||
if (log_lvl) console.log(`ae_e_data_store [${current_code}]: Best match found (ID: ${results[0].id}, Account: ${results[0].account_id})`);
|
||||
return results[0];
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user