fix(sync): move auto-match into syncDataSource after merchant rules
This commit is contained in:
parent
a97d656e92
commit
6168a71d8f
|
|
@ -12,6 +12,7 @@ const { getBankSyncConfig } = require('./bankSyncConfigService');
|
||||||
const { decorateDataSource } = require('./transactionService');
|
const { decorateDataSource } = require('./transactionService');
|
||||||
const { applyMerchantRules } = require('./billMerchantRuleService');
|
const { applyMerchantRules } = require('./billMerchantRuleService');
|
||||||
const { applySpendingCategoryRules } = require('./spendingService');
|
const { applySpendingCategoryRules } = require('./spendingService');
|
||||||
|
const { autoMatchForUser } = require('./matchSuggestionService');
|
||||||
|
|
||||||
const SEED_SYNC_DAYS = 44; // Initial connect / explicit backfill (SimpleFIN Bridge 45-day cap, 1-day buffer)
|
const SEED_SYNC_DAYS = 44; // Initial connect / explicit backfill (SimpleFIN Bridge 45-day cap, 1-day buffer)
|
||||||
const ROUTINE_SYNC_DAYS = 30; // Fallback if admin config is missing
|
const ROUTINE_SYNC_DAYS = 30; // Fallback if admin config is missing
|
||||||
|
|
@ -130,9 +131,10 @@ async function runSync(db, userId, dataSource, { days } = {}) {
|
||||||
WHERE id = ? AND user_id = ?
|
WHERE id = ? AND user_id = ?
|
||||||
`).run(partialError, dataSource.id, userId);
|
`).run(partialError, dataSource.id, userId);
|
||||||
|
|
||||||
// Apply stored merchant→bill rules, then spending category rules
|
// Apply stored merchant→bill rules, then spending category rules, then score-based auto-match
|
||||||
const { matched: autoMatched, matched_bills: matchedBills, late_attributions: lateAttributions } = applyMerchantRules(db, userId);
|
const { matched: autoMatched, matched_bills: matchedBills, late_attributions: lateAttributions } = applyMerchantRules(db, userId);
|
||||||
try { applySpendingCategoryRules(db, userId); } catch { /* non-blocking */ }
|
try { applySpendingCategoryRules(db, userId); } catch { /* non-blocking */ }
|
||||||
|
try { autoMatchForUser(userId); } catch { /* non-blocking */ }
|
||||||
|
|
||||||
return { accountsUpserted, transactionsNew, transactionsSkip, autoMatched, matched_bills: matchedBills || [], late_attributions: lateAttributions || [], errlist: raw._errlistSummary || null };
|
return { accountsUpserted, transactionsNew, transactionsSkip, autoMatched, matched_bills: matchedBills || [], late_attributions: lateAttributions || [], errlist: raw._errlistSummary || null };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
const { getDb } = require('../db/database');
|
const { getDb } = require('../db/database');
|
||||||
const { getBankSyncConfig } = require('./bankSyncConfigService');
|
const { getBankSyncConfig } = require('./bankSyncConfigService');
|
||||||
const { syncDataSource } = require('./bankSyncService');
|
const { syncDataSource } = require('./bankSyncService');
|
||||||
const { autoMatchForUser } = require('./matchSuggestionService');
|
|
||||||
|
|
||||||
// Skip a source if it was synced less than this long ago (catches recent manual syncs)
|
// Skip a source if it was synced less than this long ago (catches recent manual syncs)
|
||||||
const MIN_SYNC_AGE_MS = 60 * 60 * 1000; // 1 hour
|
const MIN_SYNC_AGE_MS = 60 * 60 * 1000; // 1 hour
|
||||||
|
|
@ -69,7 +68,6 @@ async function runCycle() {
|
||||||
try {
|
try {
|
||||||
await syncDataSource(db, source.user_id, source.id);
|
await syncDataSource(db, source.user_id, source.id);
|
||||||
synced++;
|
synced++;
|
||||||
try { autoMatchForUser(source.user_id); } catch { /* non-fatal */ }
|
|
||||||
} catch {
|
} catch {
|
||||||
// syncDataSource already writes last_error to the data_sources row
|
// syncDataSource already writes last_error to the data_sources row
|
||||||
failed++;
|
failed++;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue