Skip to content

Commit

Permalink
fix: consider only settled balances
Browse files Browse the repository at this point in the history
Resolves: #75
  • Loading branch information
EduardoSimon committed Dec 29, 2023
1 parent 8f56a8e commit 71d9bbe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
12 changes: 10 additions & 2 deletions app/services/open_banking_connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ def fetch_movements(account:)

raise StandardError.new("balances object is nil in account") if balances.nil?

main_balance = balances.first[:balanceAmount]
main_balance = settled_amount_from(balances)

raise StandardError.new({message: "balances object was empty", params: main_balance}) if main_balance.nil?
Rails.logger.info({balances:})

raise StandardError.new({message: "balances object was empty", params: balances}) if main_balance.nil?

transactions_response = get_transactions_response(external_account:, account:)

Expand All @@ -76,6 +78,12 @@ def fetch_movements(account:)
AccountResponse.new(transactions: transactions, balance: main_balance[:amount].to_f, currency: main_balance[:currency])
end

SETTLED_BALANCE_TYPES = ["interimBooked"]

def settled_amount_from(balances)
balances.find { |b| SETTLED_BALANCE_TYPES.include?(b[:balanceType]) }[:balanceAmount]
end

def client
@client ||= ::Nordigen::NordigenClient.new(
secret_id: ENV.fetch("OB_CLIENT_ID"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,25 @@ class FetchMovementsTest < ActiveSupport::TestCase
end
end

test "When no movement is present, then returns the main balance of the given existent account as a float number, the currency and the transactions for last 4 months" do
test "When no movement is present, then returns the main settled balance of the given existent account as a float number, the currency and the transactions for last 4 months" do
date_from = 4.months.ago.strftime("%F")
date_to = @current_date

balances_response = {"balances" => [{"balanceAmount" => {"amount" => "896.13", "currency" => "EUR"}, "balanceType" => "information", "lastChangeDateTime" => "2023-10-12T00:00:00Z"}, {"balanceAmount" => {"amount" => "935.37", "currency" => "EUR"}, "balanceType" => "interimBooked", "lastChangeDateTime" => "2023-10-12T00:00:00Z"}]}
balances_response = {"balances" =>
[
{"balanceAmount" =>
{"amount" => "896.13",
"currency" => "EUR"},
"balanceType" => "information",
"lastChangeDateTime" => "2023-10-12T00:00:00Z"},
{
"balanceAmount" =>
{"amount" => "935.37",
"currency" => "EUR"},
"balanceType" => "interimBooked",
"lastChangeDateTime" => "2023-10-12T00:00:00Z"
}
]}
account_mock = Minitest::Mock.new
account_mock.expect(:get_balances, balances_response.deep_symbolize_keys)
account_mock.expect(:get_transactions, {"transactions" => {"booked" => MockClient::Stubs.transactions}}, date_from:, date_to:)
Expand All @@ -45,8 +59,8 @@ class FetchMovementsTest < ActiveSupport::TestCase
response = OpenBankingConnector.new.fetch_movements(account: @account_instance)

assert_equal @account_instance.movements.exists?, false
assert_equal response.balance, 896.13
assert_equal response.currency, "EUR"
assert_equal 935.37, response.balance
assert_equal "EUR", response.currency

assert_equal response.transactions.first[:id], 0
assert_equal response.transactions.first[:amount], 100.0
Expand Down Expand Up @@ -76,8 +90,6 @@ class FetchMovementsTest < ActiveSupport::TestCase
response = OpenBankingConnector.new.fetch_movements(account: @account_instance)

assert_equal @account_instance.movements.exists?, true
assert_equal response.balance, 896.13
assert_equal response.currency, "EUR"

assert_equal response.transactions.first[:id], 0
assert_equal response.transactions.first[:amount], 100.0
Expand Down

0 comments on commit 71d9bbe

Please sign in to comment.