mirror of
https://github.com/we-promise/sure.git
synced 2026-08-05 16:42:18 +00:00
* 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>
61 lines
1.6 KiB
Ruby
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
|