mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 06:21:23 +00:00
collect transaction stats after sync in EnableBanking provider (#1388)
* collect transaction stats after sync in EnableBanking provider * update logic to follow the pattern used by other bank providers * fix n+1 issue * fix N+1 issues
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
class EnableBankingItem::Syncer
|
class EnableBankingItem::Syncer
|
||||||
|
include SyncStats::Collector
|
||||||
|
|
||||||
attr_reader :enable_banking_item
|
attr_reader :enable_banking_item
|
||||||
|
|
||||||
def initialize(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
|
# Phase 2: Check account setup status and collect sync statistics
|
||||||
sync.update!(status_text: "Checking account configuration...") if sync.respond_to?(:status_text)
|
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 })
|
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?
|
if unlinked_accounts.any?
|
||||||
enable_banking_item.update!(pending_account_setup: true)
|
enable_banking_item.update!(pending_account_setup: true)
|
||||||
sync.update!(status_text: "#{unlinked_accounts.count} accounts need setup...") if sync.respond_to?(:status_text)
|
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)
|
enable_banking_item.update!(pending_account_setup: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Phase 3: Process transactions for linked accounts only
|
# Phase 3: Process transactions for linked and visible accounts only
|
||||||
if linked_accounts.any?
|
if linked_accounts.any?
|
||||||
sync.update!(status_text: "Processing transactions...") if sync.respond_to?(:status_text)
|
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
|
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
|
# Phase 4: Schedule balance calculations for linked accounts
|
||||||
sync.update!(status_text: "Calculating balances...") if sync.respond_to?(:status_text)
|
sync.update!(status_text: "Calculating balances...") if sync.respond_to?(:status_text)
|
||||||
enable_banking_item.schedule_account_syncs(
|
enable_banking_item.schedule_account_syncs(
|
||||||
@@ -61,10 +65,6 @@ class EnableBankingItem::Syncer
|
|||||||
window_end_date: sync.window_end_date
|
window_end_date: sync.window_end_date
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if sync.respond_to?(:sync_stats)
|
|
||||||
sync.update!(sync_stats: sync_stats)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform_post_sync
|
def perform_post_sync
|
||||||
|
|||||||
Reference in New Issue
Block a user