Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(enable_banking): keep multi-currency accounts in their own currency (#4077)

Simone603c9d40e3a0
diff --git a/app/models/enable_banking_item/importer.rb b/app/models/enable_banking_item/importer.rb
index a008e44635..32aca4b16f 100644
--- a/app/models/enable_banking_item/importer.rb
+++ b/app/models/enable_banking_item/importer.rb
@@ -1,4 +1,6 @@
class EnableBankingItem::Importer
+ include CurrencyNormalizable
+
# Maximum number of pagination requests to prevent infinite loops
# Enable Banking typically returns ~100 transactions per page, so 100 pages = ~10,000 transactions
MAX_PAGINATION_PAGES = 100
@@ -232,7 +234,8 @@ class EnableBankingItem::Importer
return false
end
- balance = select_current_balance(balances)
+ currency, kept, skipped = split_balances_by_currency(balances, enable_banking_account)
+ balance = select_current_balance(kept)
unless balance.present?
mark_balance_unavailable(enable_banking_account)
@@ -240,7 +243,6 @@ class EnableBankingItem::Importer
end
amount = balance.dig(:balance_amount, :amount) || balance[:amount]
- currency = balance.dig(:balance_amount, :currency) || balance[:currency]
unless amount.present?
mark_balance_unavailable(enable_banking_account)
@@ -256,8 +258,10 @@ class EnableBankingItem::Importer
enable_banking_account.update!(
current_balance: parsed_amount,
- currency: currency.presence || enable_banking_account.currency
+ currency: balance_currency(balance) || enable_banking_account.currency
)
+ # Only now is the chosen-currency balance known to be the one in use.
+ capture_skipped_currency_balances(enable_banking_account, currency, skipped)
true
rescue Provider::EnableBanking::EnableBankingError => e
@@ -274,6 +278,65 @@ class EnableBankingItem::Importer
false
end
+ # Multi-currency wallets such as PayPal report one balance per currency
+ # under the same balance type (EUR, CAD, USD, ...). Keep the balances in
+ # the currency the accounts endpoint reports for the account, else in the
+ # first currency listed, which is the wallet's primary one. Otherwise
+ # select_current_balance takes the last entry of each type, so an empty
+ # USD balance replaced the EUR one and switched the account to USD.
+ # Returns the chosen currency, the balances in it and the ones left out.
+ def split_balances_by_currency(balances, enable_banking_account)
+ currencies = balances.filter_map { |balance| balance_currency(balance) }.uniq
+ return [ currencies.first, balances, [] ] if currencies.size <= 1
+
+ account_currency = snapshot_currency(enable_banking_account)
+ currency = currencies.include?(account_currency) ? account_currency : currencies.first
+ kept, skipped = balances.partition { |balance| balance_currency(balance) == currency }
+ [ currency, kept, skipped ]
+ end
+
+ def balance_currency(balance)
+ parse_currency(balance.dig(:balance_amount, :currency) || balance[:currency])
+ end
+
+ # Read from the raw snapshot, not the stored column: the stored currency
+ # may already be the wrong one picked from a multi-currency response.
+ # PayPal reports XXX (no currency) for the wallet; parse_currency turns
+ # that into nil, so the first listed currency is used.
+ def snapshot_currency(enable_banking_account)
+ payload = enable_banking_account.raw_payload
+ parse_currency(payload.with_indifferent_access[:currency]) if payload.is_a?(Hash)
+ end
+
+ # A non-zero balance in another currency is money the account leaves out;
+ # make that visible to support instead of dropping it silently.
+ def capture_skipped_currency_balances(enable_banking_account, currency, skipped)
+ funded = skipped.filter_map do |balance|
+ amount = balance.dig(:balance_amount, :amount) || balance[:amount]
+ next if amount.blank? || amount.to_d.zero?
+
+ { currency: balance_currency(balance), balance_type: balance[:balance_type], amount: amount.to_s }
+ end
+ return if funded.empty?
+
+ DebugLogEntry.capture(
+ category: "provider_sync",
+ level: "info",
+ message: "Enable Banking account holds balances in other currencies; only the #{currency} balance is used",
+ source: self.class.name,
+ provider_key: "enable_banking",
+ family: enable_banking_item.family,
+ account_provider: enable_banking_account.account_provider,
+ metadata: {
+ enable_banking_item_id: enable_banking_item.id,
+ enable_banking_account_id: enable_banking_account.id,
+ uid: enable_banking_account.uid,
+ currency: currency,
+ other_balances: funded
+ }
+ )
+ end
+
def select_current_balance(balances)
by_type = balances.index_by { |balance| normalize_balance_type(balance[:balance_type]) }
diff --git a/test/models/enable_banking_item/importer_balance_test.rb b/test/models/enable_banking_item/importer_balance_test.rb
index 618f6c1b8c..ec9f6a53db 100644
--- a/test/models/enable_banking_item/importer_balance_test.rb
+++ b/test/models/enable_banking_item/importer_balance_test.rb
@@ -267,6 +267,120 @@ class EnableBankingItem::ImporterBalanceTest < ActiveSupport::TestCase
assert_equal BigDecimal("10.00"), @enable_banking_account.reload.current_balance
end
+ test "fetch_and_update_balance uses the account currency's balance from a multi-currency wallet" do
+ # PayPal reports one balance per currency under the same type. The stored
+ # USD currency is what the old last-entry pick left behind.
+ @enable_banking_account.update!(currency: "USD", raw_payload: { "uid" => "paypal", "currency" => "EUR" })
+ @mock_provider.stubs(:get_account_balances).returns(balances: paypal_balances)
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ @enable_banking_account.reload
+ assert_equal BigDecimal("392.04"), @enable_banking_account.current_balance
+ assert_equal "EUR", @enable_banking_account.currency
+ end
+
+ test "fetch_and_update_balance uses the first listed currency when the snapshot has none" do
+ @enable_banking_account.update!(raw_payload: { "uid" => "paypal" })
+ @mock_provider.stubs(:get_account_balances).returns(balances: paypal_balances)
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ @enable_banking_account.reload
+ assert_equal BigDecimal("392.04"), @enable_banking_account.current_balance
+ assert_equal "EUR", @enable_banking_account.currency
+ end
+
+ test "fetch_and_update_balance follows the snapshot currency even when it is not listed first" do
+ @enable_banking_account.update!(raw_payload: { "uid" => "paypal", "currency" => "USD" })
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ { balance_type: "CLBD", balance_amount: { amount: "10.00", currency: "EUR" }, credit_debit_indicator: "CRDT" },
+ { balance_type: "ITAV", balance_amount: { amount: "30.00", currency: "USD" }, credit_debit_indicator: "CRDT" },
+ { balance_type: "ITBD", balance_amount: { amount: "25.00", currency: "USD" }, credit_debit_indicator: "CRDT" }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ @enable_banking_account.reload
+ assert_equal BigDecimal("25.00"), @enable_banking_account.current_balance
+ assert_equal "USD", @enable_banking_account.currency
+ end
+
+ test "fetch_and_update_balance uses the first listed currency when the snapshot reports XXX" do
+ # PayPal's accounts endpoint reports the wallet's currency as XXX.
+ @enable_banking_account.update!(currency: "USD", raw_payload: { "uid" => "paypal", "currency" => "XXX" })
+ @mock_provider.stubs(:get_account_balances).returns(balances: paypal_balances)
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ @enable_banking_account.reload
+ assert_equal BigDecimal("392.04"), @enable_banking_account.current_balance
+ assert_equal "EUR", @enable_banking_account.currency
+ end
+
+ test "fetch_and_update_balance keeps the stored currency when the balance has no valid currency" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [ { balance_type: "CLBD", balance_amount: { amount: "12.00", currency: "XXX" }, credit_debit_indicator: "CRDT" } ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ @enable_banking_account.reload
+ assert_equal BigDecimal("12.00"), @enable_banking_account.current_balance
+ assert_equal "EUR", @enable_banking_account.currency
+ end
+
+ test "fetch_and_update_balance stores the normalized balance currency" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [ { balance_type: "CLBD", balance_amount: { amount: "12.00", currency: " usd " }, credit_debit_indicator: "CRDT" } ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal "USD", @enable_banking_account.reload.currency
+ end
+
+ test "fetch_and_update_balance logs funded balances left out in other currencies" do
+ @enable_banking_account.update!(raw_payload: { "uid" => "paypal", "currency" => "EUR" })
+ balances = paypal_balances
+ balances[2] = balances[2].merge(balance_amount: { amount: "15.50", currency: "USD" })
+ @mock_provider.stubs(:get_account_balances).returns(balances: balances)
+
+ assert_difference -> { DebugLogEntry.where(category: "provider_sync").count }, 1 do
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+ end
+
+ entry = DebugLogEntry.where(category: "provider_sync").order(:created_at).last
+ assert_equal "EUR", entry.metadata["currency"]
+ assert_equal [ { "currency" => "USD", "balance_type" => "CLBD", "amount" => "15.50" } ], entry.metadata["other_balances"]
+ end
+
+ test "fetch_and_update_balance does not log other currencies when the chosen balance has no amount" do
+ @enable_banking_account.update!(raw_payload: { "uid" => "paypal", "currency" => "EUR" })
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ { balance_type: "CLBD", balance_amount: { currency: "EUR" }, credit_debit_indicator: "CRDT" },
+ { balance_type: "CLBD", balance_amount: { amount: "15.50", currency: "USD" }, credit_debit_indicator: "CRDT" }
+ ]
+ )
+
+ assert_no_difference -> { DebugLogEntry.where(category: "provider_sync").count } do
+ assert_not @importer.send(:fetch_and_update_balance, @enable_banking_account)
+ end
+ assert_nil @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance does not log empty balances in other currencies" do
+ @enable_banking_account.update!(raw_payload: { "uid" => "paypal", "currency" => "EUR" })
+ @mock_provider.stubs(:get_account_balances).returns(balances: paypal_balances)
+
+ assert_no_difference -> { DebugLogEntry.where(category: "provider_sync").count } do
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+ end
+ end
+
test "fetch_and_update_balance handles descriptive booked balance types" do
@mock_provider.stubs(:get_account_balances).returns(
balances: [
@@ -390,4 +504,14 @@ class EnableBankingItem::ImporterBalanceTest < ActiveSupport::TestCase
assert_nil unsaved_account.current_balance
end
+
+ private
+
+ # Shaped like PayPal's response: the primary EUR balance first, then empty
+ # CAD and USD balances under the same type.
+ def paypal_balances
+ %w[EUR CAD USD].zip(%w[392.04 0.00 0.00]).map do |currency, amount|
+ { balance_type: "CLBD", balance_amount: { amount: amount, currency: currency }, credit_debit_indicator: "CRDT" }
+ end
+ end
end