Add partial account sync support (#653)

* Add partial account sync support

* Fix indentation

* Use historical balance as base when doing partial sync

* Simplify controller crud tests

* Fix linter errors
This commit is contained in:
Jakub Kottnauer
2024-04-24 21:55:52 +02:00
committed by GitHub
parent b3f8ab78d9
commit ad4de99f1a
9 changed files with 260 additions and 111 deletions

View File

@@ -25,7 +25,7 @@ class Account::Balance::Calculator
prior_balance = current_balance
{ date: date, balance: current_balance, currency: @account.currency, updated_at: Time.current }
{ date:, balance: current_balance, currency: @account.currency, updated_at: Time.current }
end
@daily_balances = [
@@ -92,11 +92,15 @@ class Account::Balance::Calculator
end
def implied_start_balance
if @calc_start_date > @account.effective_start_date
return @account.balance_on(@calc_start_date)
end
oldest_valuation_date = normalized_valuations.first&.dig("date")
oldest_transaction_date = normalized_transactions.first&.dig("date")
oldest_entry_date = [ oldest_valuation_date, oldest_transaction_date ].compact.min
if oldest_entry_date == oldest_valuation_date
if oldest_entry_date.present? && oldest_entry_date == oldest_valuation_date
oldest_valuation = normalized_valuations.find { |v| v["date"] == oldest_valuation_date }
oldest_valuation["value"].to_d
else

View File

@@ -1,16 +1,18 @@
module Account::Syncable
extend ActiveSupport::Concern
def sync_later
AccountSyncJob.perform_later self
def sync_later(start_date = nil)
AccountSyncJob.perform_later(self, start_date)
end
def sync
def sync(start_date = nil)
update!(status: "syncing")
sync_exchange_rates
calculator = Account::Balance::Calculator.new(self)
calc_start_date = start_date - 1.day if start_date.present? && self.balance_on(start_date - 1.day).present?
calculator = Account::Balance::Calculator.new(self, { calc_start_date: })
calculator.calculate
self.balances.upsert_all(calculator.daily_balances, unique_by: :index_account_balances_on_account_id_date_currency_unique)
self.balances.where("date < ?", effective_start_date).delete_all