Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Don't persist zero balances when a provider balance fetch fails (#2617)

Stephen Jollyba0e169f6b19
diff --git a/app/models/enable_banking_account.rb b/app/models/enable_banking_account.rb
index 91bf07539a..c211bb9316 100644
--- a/app/models/enable_banking_account.rb
+++ b/app/models/enable_banking_account.rb
@@ -105,7 +105,12 @@ class EnableBankingAccount < ApplicationRecord
update!(
current_balance: nil,
- currency: parse_currency(snapshot[:currency]) || "EUR",
+ # Preserve an established currency when the snapshot omits or mangles it
+ # (normalized, so blank/invalid stored values still fall through) —
+ # EUR only for records that never had a valid currency. Mirrors the
+ # equivalent Lunch Flow fix; PSD2 payloads usually carry currency, so
+ # this is parity/safety rather than an observed failure.
+ currency: parse_currency(snapshot[:currency]) || parse_currency(currency) || "EUR",
name: build_account_name(snapshot),
account_id: snapshot[:uid],
uid: snapshot[:identification_hash] || snapshot[:uid],
diff --git a/app/models/enable_banking_account/processor.rb b/app/models/enable_banking_account/processor.rb
index 1a7f965390..ef7a1cf4b0 100644
--- a/app/models/enable_banking_account/processor.rb
+++ b/app/models/enable_banking_account/processor.rb
@@ -37,7 +37,19 @@ class EnableBankingAccount::Processor
end
account = enable_banking_account.current_account
- balance = enable_banking_account.current_balance || 0
+ balance = enable_banking_account.current_balance
+
+ # A nil current_balance means this sync's balance fetch did not succeed:
+ # upsert_enable_banking_snapshot! clears it and only a successful
+ # balance fetch repopulates it. Coercing nil to 0 here would persist a
+ # zero balance (and a zero current_anchor valuation) onto a healthy
+ # account whenever the provider has a transient failure, so leave the
+ # account untouched instead.
+ if balance.nil?
+ Rails.logger.warn("EnableBankingAccount::Processor - No balance available for enable_banking_account #{enable_banking_account.id} (balance fetch failed or not yet run), skipping account update")
+ return
+ end
+
available_credit = nil
# For liability accounts, ensure balance sign is correct.
diff --git a/app/models/lunchflow_account.rb b/app/models/lunchflow_account.rb
index c38ae50827..dcc0a04e25 100644
--- a/app/models/lunchflow_account.rb
+++ b/app/models/lunchflow_account.rb
@@ -37,7 +37,12 @@ class LunchflowAccount < ApplicationRecord
assign_attributes(
current_balance: nil, # Balance not provided by accounts endpoint
- currency: parse_currency(snapshot[:currency]) || "USD",
+ # The accounts endpoint usually omits currency (the balance endpoint is
+ # the authoritative source) — preserve what we already know rather than
+ # resetting an established account to USD. Normalize the preserved value
+ # too: a blank/invalid stored currency must fall through to USD, not
+ # propagate (blank would fail the presence validation and break import).
+ currency: parse_currency(snapshot[:currency]) || parse_currency(currency) || "USD",
name: display_name,
account_id: snapshot[:id].to_s,
account_status: snapshot[:status],
diff --git a/app/models/lunchflow_account/processor.rb b/app/models/lunchflow_account/processor.rb
index b9c6b21842..f6d8c3ba45 100644
--- a/app/models/lunchflow_account/processor.rb
+++ b/app/models/lunchflow_account/processor.rb
@@ -38,7 +38,18 @@ class LunchflowAccount::Processor
# Update account balance from latest Lunchflow data
account = lunchflow_account.current_account
- balance = lunchflow_account.current_balance || 0
+ balance = lunchflow_account.current_balance
+
+ # A nil current_balance means this sync's balance fetch did not succeed:
+ # upsert_lunchflow_snapshot! clears it (the accounts endpoint carries no
+ # balance) and only a successful balance fetch repopulates it. Coercing
+ # nil to 0 here would persist a zero balance (and the USD currency
+ # fallback) onto a healthy account whenever the provider has a
+ # transient failure, so leave the account untouched instead.
+ if balance.nil?
+ Rails.logger.warn("LunchflowAccount::Processor - No balance available for lunchflow_account #{lunchflow_account.id} (balance fetch failed or not yet run), skipping account update")
+ return
+ end
# LunchFlow balance convention matches our app convention:
# - Positive balance = debt (you owe money)
diff --git a/app/models/lunchflow_item/importer.rb b/app/models/lunchflow_item/importer.rb
index 21e06314c3..cdfd604333 100644
--- a/app/models/lunchflow_item/importer.rb
+++ b/app/models/lunchflow_item/importer.rb
@@ -269,7 +269,19 @@ class LunchflowItem::Importer
begin
fetch_and_update_balance(lunchflow_account)
rescue => e
- # Log but don't fail transaction import if balance fetch fails
+ # Log but don't fail transaction import if balance fetch fails.
+ # current_balance stays nil, so the processor will skip the balance
+ # update for this account — capture the failure, because the sync
+ # still reports success and this is otherwise invisible to support.
+ DebugLogEntry.capture(
+ category: "provider_sync",
+ level: "warn",
+ message: "Balance fetch failed for account #{lunchflow_account.account_id}; keeping previous balance",
+ source: self.class.name,
+ provider_key: "lunchflow",
+ family: lunchflow_item.family,
+ metadata: { account_id: lunchflow_account.account_id, error_class: e.class.name, error: e.message }
+ )
Rails.logger.warn "LunchflowItem::Importer - Failed to update balance for account #{lunchflow_account.account_id}: #{e.message}"
end
diff --git a/test/models/enable_banking_account_processor_test.rb b/test/models/enable_banking_account_processor_test.rb
new file mode 100644
index 0000000000..320e7f7e1a
--- /dev/null
+++ b/test/models/enable_banking_account_processor_test.rb
@@ -0,0 +1,61 @@
+require "test_helper"
+
+class EnableBankingAccountProcessorTest < ActiveSupport::TestCase
+ setup do
+ @family = families(:dylan_family)
+ @item = EnableBankingItem.new(family: @family, name: "Enable Banking",
+ country_code: "GB", application_id: "test-app")
+ @item.save!(validate: false)
+ end
+
+ test "skips the account update when current_balance is nil (failed balance fetch)" do
+ eb_acct = @item.enable_banking_accounts.create!(
+ name: "Checking",
+ uid: "eb_1",
+ currency: "GBP",
+ current_balance: nil
+ )
+
+ acct = accounts(:depository)
+ acct.update!(balance: 500, cash_balance: 500, currency: "GBP")
+ AccountProvider.create!(account: acct, provider: eb_acct)
+
+ EnableBankingAccount::Processor.new(eb_acct).send(:process_account!)
+
+ acct.reload
+ assert_equal BigDecimal("500"), acct.cash_balance,
+ "a sync whose balance fetch failed must not zero the account"
+ assert_equal BigDecimal("500"), acct.balance
+ # Parity/invariant check, not regression coverage: unlike Lunch Flow, the
+ # EB *processor* never had a currency-reset defect (its fallback chain
+ # already preferred the stored value), so this assertion also passes on
+ # main. The EB currency regression test lives at the model layer below.
+ assert_equal "GBP", acct.currency,
+ "a sync whose balance fetch failed must not change the account currency"
+ end
+
+ test "still updates the account when current_balance is present" do
+ eb_acct = @item.enable_banking_accounts.create!(
+ name: "Checking",
+ uid: "eb_2",
+ currency: "GBP",
+ current_balance: BigDecimal("250")
+ )
+
+ acct = accounts(:depository)
+ acct.update!(balance: 500, cash_balance: 500, currency: "GBP")
+ AccountProvider.create!(account: acct, provider: eb_acct)
+
+ EnableBankingAccount::Processor.new(eb_acct).send(:process_account!)
+
+ assert_equal BigDecimal("250"), acct.reload.cash_balance
+ end
+ test "snapshot upsert preserves an established currency when the payload omits it" do
+ eb_acct = @item.enable_banking_accounts.create!(name: "Checking", uid: "eb_3", currency: "GBP")
+
+ eb_acct.upsert_enable_banking_snapshot!({ uid: "eb_3", name: "Checking" })
+
+ assert_equal "GBP", eb_acct.reload.currency,
+ "an omitted payload currency must not reset an established account to EUR"
+ end
+end
diff --git a/test/models/lunchflow_account_processor_test.rb b/test/models/lunchflow_account_processor_test.rb
new file mode 100644
index 0000000000..b156c857b4
--- /dev/null
+++ b/test/models/lunchflow_account_processor_test.rb
@@ -0,0 +1,78 @@
+require "test_helper"
+
+class LunchflowAccountProcessorTest < ActiveSupport::TestCase
+ setup do
+ @family = families(:dylan_family)
+ @item = LunchflowItem.new(family: @family, name: "Lunch Flow", api_key: "test_key")
+ @item.save!(validate: false)
+ end
+
+ test "skips the account update when current_balance is nil (failed balance fetch)" do
+ lf_acct = @item.lunchflow_accounts.create!(
+ name: "Checking",
+ account_id: "lf_1",
+ currency: "USD",
+ current_balance: nil
+ )
+
+ acct = accounts(:depository)
+ acct.update!(balance: 500, cash_balance: 500, currency: "GBP")
+ AccountProvider.create!(account: acct, provider: lf_acct)
+
+ LunchflowAccount::Processor.new(lf_acct).send(:process_account!)
+
+ acct.reload
+ assert_equal BigDecimal("500"), acct.balance,
+ "a sync whose balance fetch failed must not zero the account"
+ assert_equal "GBP", acct.currency,
+ "a sync whose balance fetch failed must not change the account currency"
+ end
+
+ test "still updates the account when current_balance is present" do
+ lf_acct = @item.lunchflow_accounts.create!(
+ name: "Checking",
+ account_id: "lf_2",
+ currency: "GBP",
+ current_balance: BigDecimal("250")
+ )
+
+ acct = accounts(:depository)
+ acct.update!(balance: 500, cash_balance: 500, currency: "GBP")
+ AccountProvider.create!(account: acct, provider: lf_acct)
+
+ LunchflowAccount::Processor.new(lf_acct).send(:process_account!)
+
+ assert_equal BigDecimal("250"), acct.reload.balance
+ end
+
+ test "snapshot upsert preserves the existing currency when the payload omits it" do
+ lf_acct = @item.lunchflow_accounts.create!(
+ name: "Checking",
+ account_id: "lf_3",
+ currency: "GBP"
+ )
+
+ # The real accounts endpoint carries neither balance nor currency.
+ lf_acct.upsert_lunchflow_snapshot!({ id: "lf_3", name: "Checking", status: "active" })
+
+ lf_acct.reload
+ assert_equal "GBP", lf_acct.currency,
+ "an established account's currency must survive a currency-less snapshot"
+ assert_nil lf_acct.current_balance
+ end
+
+ test "snapshot upsert falls back to USD when neither payload nor record has a valid currency" do
+ lf_acct = @item.lunchflow_accounts.create!(
+ name: "Checking",
+ account_id: "lf_4",
+ currency: "GBP"
+ )
+ # Simulate a record built from bad provider data (bypasses validation)
+ lf_acct.update_column(:currency, "")
+
+ lf_acct.upsert_lunchflow_snapshot!({ id: "lf_4", name: "Checking", status: "active" })
+
+ assert_equal "USD", lf_acct.reload.currency,
+ "a blank stored currency must fall through to USD, not fail validation"
+ end
+end