From 242eb5cea1f216dcba76ebda185f99e6ecb01af4 Mon Sep 17 00:00:00 2001 From: Nico Date: Fri, 22 Nov 2024 11:38:41 -0300 Subject: [PATCH 1/3] Calculates balance based on previous transaction on the same date (#1483) --- app/models/account/entry.rb | 10 +++++++++- test/models/account/entry_test.rb | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/account/entry.rb b/app/models/account/entry.rb index 09d19618..2e77b19c 100644 --- a/app/models/account/entry.rb +++ b/app/models/account/entry.rb @@ -15,6 +15,7 @@ class Account::Entry < ApplicationRecord validates :date, comparison: { greater_than: -> { min_supported_date } } scope :chronological, -> { order(:date, :created_at) } + scope :not_account_valuations, -> { where.not(entryable_type: "Account::Valuation") } scope :reverse_chronological, -> { order(date: :desc, created_at: :desc) } scope :without_transfers, -> { where(marked_as_transfer: false) } scope :with_converted_amount, ->(currency) { @@ -54,6 +55,13 @@ class Account::Entry < ApplicationRecord account.balances.find_by(date: date - 1)&.balance || 0 end + def prior_entry_balance + entries_on_entry_date + .not_account_valuations + .last + &.balance_after_entry || 0 + end + def balance_after_entry if account_valuation? Money.new(amount, currency) @@ -75,7 +83,7 @@ class Account::Entry < ApplicationRecord def trend TimeSeries::Trend.new( current: balance_after_entry, - previous: Money.new(prior_balance, currency), + previous: Money.new(prior_entry_balance, currency), favorable_direction: account.favorable_direction ) end diff --git a/test/models/account/entry_test.rb b/test/models/account/entry_test.rb index e99f8db0..b5711d29 100644 --- a/test/models/account/entry_test.rb +++ b/test/models/account/entry_test.rb @@ -110,4 +110,15 @@ class Account::EntryTest < ActiveSupport::TestCase assert_equal Money.new(100), transaction.balance_after_entry end + + test "prior_entry_balance returns last transaction entry balance" do + family = families(:empty) + account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new + + new_valuation = create_valuation(account: account, amount: 1) + transaction = create_transaction(date: new_valuation.date, account: account, amount: -100) + + + assert_equal Money.new(100), transaction.prior_entry_balance + end end -- 2.53.0 From c309c8abf89469fc2b91d5ef54fdc86d35965510 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Fri, 22 Nov 2024 10:41:16 -0500 Subject: [PATCH 2/3] Safely call liability object when syncing --- app/models/plaid_item.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/plaid_item.rb b/app/models/plaid_item.rb index 5492edaf..5abcff8f 100644 --- a/app/models/plaid_item.rb +++ b/app/models/plaid_item.rb @@ -107,9 +107,9 @@ class PlaidItem < ApplicationRecord if fetched_liabilities transaction do internal_plaid_accounts.each do |internal_plaid_account| - credit = fetched_liabilities.credit.find { |l| l.account_id == internal_plaid_account.plaid_id } - mortgage = fetched_liabilities.mortgage.find { |l| l.account_id == internal_plaid_account.plaid_id } - student = fetched_liabilities.student.find { |l| l.account_id == internal_plaid_account.plaid_id } + credit = fetched_liabilities.credit&.find { |l| l.account_id == internal_plaid_account.plaid_id } + mortgage = fetched_liabilities.mortgage&.find { |l| l.account_id == internal_plaid_account.plaid_id } + student = fetched_liabilities.student&.find { |l| l.account_id == internal_plaid_account.plaid_id } internal_plaid_account.sync_credit_data!(credit) if credit internal_plaid_account.sync_mortgage_data!(mortgage) if mortgage -- 2.53.0 From 9ece9b093a21a3e2967b365645001cb1c63f10a1 Mon Sep 17 00:00:00 2001 From: Evlos <4tyle8@gmail.com> Date: Fri, 22 Nov 2024 14:17:09 +0800 Subject: [PATCH 3/3] [#] base_url on synth.rb --- app/models/provider/synth.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/provider/synth.rb b/app/models/provider/synth.rb index 0e367f1e..8247ab34 100644 --- a/app/models/provider/synth.rb +++ b/app/models/provider/synth.rb @@ -174,7 +174,7 @@ class Provider::Synth SecurityInfoResponse = Struct.new :info, :success?, :error, :raw_response, keyword_init: true def base_url - "https://api.synthfinance.com" + ENV["SYNTH_URL"] || "https://api.synthfinance.com" end def app_name -- 2.53.0