fix(sync): move auto-match into syncDataSource after merchant rules

This commit is contained in:
null 2026-06-06 14:41:27 -05:00
parent a97d656e92
commit 6168a71d8f
2 changed files with 3 additions and 3 deletions

View File

@ -12,6 +12,7 @@ const { getBankSyncConfig } = require('./bankSyncConfigService');
const { decorateDataSource } = require('./transactionService');
const { applyMerchantRules } = require('./billMerchantRuleService');
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 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 = ?
`).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);
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 };
}

View File

@ -3,7 +3,6 @@
const { getDb } = require('../db/database');
const { getBankSyncConfig } = require('./bankSyncConfigService');
const { syncDataSource } = require('./bankSyncService');
const { autoMatchForUser } = require('./matchSuggestionService');
// 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
@ -69,7 +68,6 @@ async function runCycle() {
try {
await syncDataSource(db, source.user_id, source.id);
synced++;
try { autoMatchForUser(source.user_id); } catch { /* non-fatal */ }
} catch {
// syncDataSource already writes last_error to the data_sources row
failed++;