mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Basic Account Balance Sync Algorithm (#501)
* Sketch out sync interface * Add basic account sync algorithm * Update logic for final balance in series * Remove start_date concept * Clean up tests * Improve clarity of test * Update app/models/account.rb Co-authored-by: Rob Zolkos <rob@zolkos.com> Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> * Update app/models/transaction.rb Co-authored-by: Rob Zolkos <rob@zolkos.com> Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> * Update app/models/valuation.rb Co-authored-by: Rob Zolkos <rob@zolkos.com> Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> * Re-organize code, simplify job interface * Consolidate balance calculations * More cleanup --------- Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> Co-authored-by: Rob Zolkos <rob@zolkos.com>
This commit is contained in:
parent
fb657856a5
commit
dbf575c02a
14 changed files with 207 additions and 175 deletions
38
app/models/account/balance_calculator.rb
Normal file
38
app/models/account/balance_calculator.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
class Account::BalanceCalculator
|
||||
def initialize(account)
|
||||
@account = account
|
||||
end
|
||||
|
||||
def daily_balances(start_date = nil)
|
||||
calc_start_date = [ start_date, @account.effective_start_date ].compact.max
|
||||
|
||||
valuations = @account.valuations.where("date >= ?", calc_start_date).order(:date).select(:date, :value)
|
||||
transactions = @account.transactions.where("date > ?", calc_start_date).order(:date).select(:date, :amount)
|
||||
oldest_entry = [ valuations.first, transactions.first ].compact.min_by(&:date)
|
||||
|
||||
net_transaction_flows = transactions.sum(&:amount)
|
||||
implied_start_balance = oldest_entry.is_a?(Valuation) ? oldest_entry.value : @account.balance + net_transaction_flows
|
||||
|
||||
prior_balance = implied_start_balance
|
||||
calculated_balances = ((calc_start_date + 1.day)...Date.current).map do |date|
|
||||
valuation = valuations.find { |v| v.date == date }
|
||||
|
||||
if valuation
|
||||
current_balance = valuation.value
|
||||
else
|
||||
current_day_net_transaction_flows = transactions.select { |t| t.date == date }.sum(&:amount)
|
||||
current_balance = prior_balance - current_day_net_transaction_flows
|
||||
end
|
||||
|
||||
prior_balance = current_balance
|
||||
|
||||
{ date: date, balance: current_balance, updated_at: Time.current }
|
||||
end
|
||||
|
||||
[
|
||||
{ date: calc_start_date, balance: implied_start_balance, updated_at: Time.current },
|
||||
*calculated_balances,
|
||||
{ date: Date.current, balance: @account.balance, updated_at: Time.current } # Last balance must always match "source of truth"
|
||||
]
|
||||
end
|
||||
end
|
26
app/models/account/syncable.rb
Normal file
26
app/models/account/syncable.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Account::Syncable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def sync_later
|
||||
AccountSyncJob.perform_later self
|
||||
end
|
||||
|
||||
def sync
|
||||
update!(status: "SYNCING")
|
||||
synced_daily_balances = Account::BalanceCalculator.new(self).daily_balances
|
||||
self.balances.upsert_all(synced_daily_balances, unique_by: :index_account_balances_on_account_id_and_date)
|
||||
self.balances.where("date < ?", effective_start_date).delete_all
|
||||
update!(status: "OK")
|
||||
rescue => e
|
||||
update!(status: "ERROR")
|
||||
Rails.logger.error("Failed to sync account #{id}: #{e.message}")
|
||||
end
|
||||
|
||||
# The earliest date we can calculate a balance for
|
||||
def effective_start_date
|
||||
first_valuation_date = self.valuations.order(:date).pluck(:date).first
|
||||
first_transaction_date = self.transactions.order(:date).pluck(:date).first
|
||||
|
||||
[ first_valuation_date, first_transaction_date&.prev_day ].compact.min || Date.current
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue