Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix: Properly compute credit card balance for Enable Banking accounts (#663)

Alessio Cappa6ec03e93f485
diff --git a/app/models/enable_banking_account/processor.rb b/app/models/enable_banking_account/processor.rb
index 3b5741b17c..ada55916f9 100644
--- a/app/models/enable_banking_account/processor.rb
+++ b/app/models/enable_banking_account/processor.rb
@@ -38,8 +38,12 @@ class EnableBankingAccount::Processor
account = enable_banking_account.current_account
balance = enable_banking_account.current_balance || 0
+ # For credit cards, compute balance based on credit limit
+ if account.accountable_type == "CreditCard"
+ available_credit = account.accountable.available_credit || 0
+ balance = available_credit - balance
# For liability accounts, ensure positive balances
- if account.accountable_type == "CreditCard" || account.accountable_type == "Loan"
+ elsif account.accountable_type == "Loan"
balance = -balance
end
diff --git a/app/models/enable_banking_item/importer.rb b/app/models/enable_banking_item/importer.rb
index 60b1e4d041..178d146f29 100644
--- a/app/models/enable_banking_item/importer.rb
+++ b/app/models/enable_banking_item/importer.rb
@@ -134,9 +134,11 @@ class EnableBankingItem::Importer
balances = balance_data[:balances] || []
return if balances.empty?
- # Find the most relevant balance (prefer "closingBooked" or "expected")
- balance = balances.find { |b| b[:balance_type] == "closingBooked" } ||
- balances.find { |b| b[:balance_type] == "expected" } ||
+ # Find the most relevant balance (prefer "ITAV" or "CLAV" types)
+ balance = balances.find { |b| b[:balance_type] == "ITAV" } ||
+ balances.find { |b| b[:balance_type] == "CLAV" } ||
+ balances.find { |b| b[:balance_type] == "ITBD" } ||
+ balances.find { |b| b[:balance_type] == "CLBD" } ||
balances.first
if balance.present?