From 6168a71d8fde7bb0015199fdab43dbe6baa75adc Mon Sep 17 00:00:00 2001 From: null Date: Sat, 6 Jun 2026 14:41:27 -0500 Subject: [PATCH] fix(sync): move auto-match into syncDataSource after merchant rules --- services/bankSyncService.js | 4 +++- services/bankSyncWorker.js | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/bankSyncService.js b/services/bankSyncService.js index b4f261b..86bb79f 100644 --- a/services/bankSyncService.js +++ b/services/bankSyncService.js @@ -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 }; } diff --git a/services/bankSyncWorker.js b/services/bankSyncWorker.js index 9d73e92..5b6eebd 100644 --- a/services/bankSyncWorker.js +++ b/services/bankSyncWorker.js @@ -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++;