Files
sure/app/models/balance_sheet/net_worth_series_builder.rb
ghost 683f518780 fix(balance-sheet): preserve disabled-account net worth history (#1730)
* fix(balance-sheet): preserve disabled account history

* fix(balance-sheet): tighten historical account scope

* fix(balance-sheet): stop disabled account carry-forward

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-06-04 22:11:51 +02:00

61 lines
1.6 KiB
Ruby

class BalanceSheet::NetWorthSeriesBuilder
def initialize(family, user: nil)
@family = family
@user = user
end
def net_worth_series(period: Period.last_30_days)
Rails.cache.fetch(cache_key(period)) do
builder = Balance::ChartSeriesBuilder.new(
account_ids: historical_account_ids,
account_active_until_dates: disabled_account_active_until_dates,
currency: family.currency,
period: period,
favorable_direction: "up"
)
builder.balance_series
end
end
private
attr_reader :family, :user
def historical_accounts
@historical_accounts ||= historical_account_scope.relation.to_a
end
def historical_account_ids
@historical_account_ids ||= historical_accounts.map(&:id)
end
def disabled_account_active_until_dates
@disabled_account_active_until_dates ||= historical_accounts.each_with_object({}) do |account, dates|
next unless account.disabled?
disabled_on = (account.disabled_at || account.updated_at).to_date
dates[account.id] = disabled_on - 1.day
end
end
def historical_account_scope
@historical_account_scope ||= BalanceSheet::HistoricalAccountScope.new(family, user: user)
end
def cache_key(period)
shares_version = user ? AccountShare.where(user: user).maximum(:updated_at)&.to_i : nil
key = [
"balance_sheet_net_worth_series_historical",
user&.id,
shares_version,
period.start_date,
period.end_date
].compact.join("_")
family.build_cache_key(
key,
invalidate_on_data_updates: true
)
end
end