mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-07 06:25:19 +02:00
Balance sheet cache layer, non-blocking sync UI (#2356)
* Balance sheet cache layer with cache-busting * Update family cache timestamps during Sync * Less blocking sync loaders * Consolidate family data caching key logic * Fix turbo stream broadcasts * Remove dev delay * Add back account group sorting
This commit is contained in:
parent
dab693d74f
commit
10ce2c8e23
35 changed files with 529 additions and 466 deletions
63
app/models/balance_sheet/account_totals.rb
Normal file
63
app/models/balance_sheet/account_totals.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
class BalanceSheet::AccountTotals
|
||||
def initialize(family, sync_status_monitor:)
|
||||
@family = family
|
||||
@sync_status_monitor = sync_status_monitor
|
||||
end
|
||||
|
||||
def asset_accounts
|
||||
@asset_accounts ||= account_rows.filter { |t| t.classification == "asset" }
|
||||
end
|
||||
|
||||
def liability_accounts
|
||||
@liability_accounts ||= account_rows.filter { |t| t.classification == "liability" }
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :family, :sync_status_monitor
|
||||
|
||||
AccountRow = Data.define(:account, :converted_balance, :is_syncing) do
|
||||
def syncing? = is_syncing
|
||||
|
||||
# Allows Rails path helpers to generate URLs from the wrapper
|
||||
def to_param = account.to_param
|
||||
delegate_missing_to :account
|
||||
end
|
||||
|
||||
def active_accounts
|
||||
@active_accounts ||= family.accounts.active.with_attached_logo
|
||||
end
|
||||
|
||||
def account_rows
|
||||
@account_rows ||= query.map do |account_row|
|
||||
AccountRow.new(
|
||||
account: account_row,
|
||||
converted_balance: account_row.converted_balance,
|
||||
is_syncing: sync_status_monitor.account_syncing?(account_row)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def cache_key
|
||||
family.build_cache_key(
|
||||
"balance_sheet_account_rows",
|
||||
invalidate_on_data_updates: true
|
||||
)
|
||||
end
|
||||
|
||||
def query
|
||||
@query ||= Rails.cache.fetch(cache_key) do
|
||||
active_accounts
|
||||
.joins(ActiveRecord::Base.sanitize_sql_array([
|
||||
"LEFT JOIN exchange_rates ON exchange_rates.date = ? AND accounts.currency = exchange_rates.from_currency AND exchange_rates.to_currency = ?",
|
||||
Date.current,
|
||||
family.currency
|
||||
]))
|
||||
.select(
|
||||
"accounts.*",
|
||||
"SUM(accounts.balance * COALESCE(exchange_rates.rate, 1)) as converted_balance"
|
||||
)
|
||||
.group(:classification, :accountable_type, :id)
|
||||
.to_a
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue