diff --git a/app/models/enable_banking_item/syncer.rb b/app/models/enable_banking_item/syncer.rb index 627d03de6..7ab6e8279 100644 --- a/app/models/enable_banking_item/syncer.rb +++ b/app/models/enable_banking_item/syncer.rb @@ -1,4 +1,6 @@ class EnableBankingItem::Syncer + include SyncStats::Collector + attr_reader :enable_banking_item def initialize(enable_banking_item) @@ -30,17 +32,15 @@ class EnableBankingItem::Syncer # Phase 2: Check account setup status and collect sync statistics sync.update!(status_text: "Checking account configuration...") if sync.respond_to?(:status_text) - total_accounts = enable_banking_item.enable_banking_accounts.count + collect_setup_stats(sync, provider_accounts: enable_banking_item.enable_banking_accounts.includes(:account_provider, :account)) - linked_accounts = enable_banking_item.enable_banking_accounts.joins(:account_provider).joins(:account).merge(Account.visible) + linked_accounts = enable_banking_item.enable_banking_accounts + .joins(:account_provider) + .includes(:account_provider, :account) + .joins(:account) + .merge(Account.visible) unlinked_accounts = enable_banking_item.enable_banking_accounts.left_joins(:account_provider).where(account_providers: { id: nil }) - sync_stats = { - total_accounts: total_accounts, - linked_accounts: linked_accounts.count, - unlinked_accounts: unlinked_accounts.count - } - if unlinked_accounts.any? enable_banking_item.update!(pending_account_setup: true) sync.update!(status_text: "#{unlinked_accounts.count} accounts need setup...") if sync.respond_to?(:status_text) @@ -48,11 +48,15 @@ class EnableBankingItem::Syncer enable_banking_item.update!(pending_account_setup: false) end - # Phase 3: Process transactions for linked accounts only + # Phase 3: Process transactions for linked and visible accounts only if linked_accounts.any? sync.update!(status_text: "Processing transactions...") if sync.respond_to?(:status_text) + account_ids = linked_accounts.filter_map { |a| a.current_account&.id } enable_banking_item.process_accounts + # Collect transaction statistics + collect_transaction_stats(sync, account_ids: account_ids, source: "enable_banking") + # Phase 4: Schedule balance calculations for linked accounts sync.update!(status_text: "Calculating balances...") if sync.respond_to?(:status_text) enable_banking_item.schedule_account_syncs( @@ -61,10 +65,6 @@ class EnableBankingItem::Syncer window_end_date: sync.window_end_date ) end - - if sync.respond_to?(:sync_stats) - sync.update!(sync_stats: sync_stats) - end end def perform_post_sync