mirror of
https://github.com/we-promise/sure.git
synced 2026-05-31 16:29:03 +00:00
Reshape the goals feature to live on top of linked-account balances. A goal's balance is now the live balance of every depository account linked to it — no parallel ledger, no "log a contribution" step. The "Add contribution" affordance is replaced by a 7-day GoalPledge (kind: transfer | manual_save). GoalPledge::Reconciler matches incoming Transactions (via Account::ProviderImportAdapter) and Valuations (via Account::ReconciliationManager) against open pledges within ±5 days, ±$0.50, or ±1% — single hook covers every provider (Plaid, SimpleFIN, Lunchflow, Enable Banking, Brex, IBKR, Kraken, SnapTrade) plus manual balance edits. A 15-minute Sidekiq cron sweeps expired pledges. Goal model: balance derived from linked_accounts.sum(&:balance), new pace (90-day net non-transfer inflow), months_of_runway, last_matched_pledge_*, pledge_action_label_key (the "I just transferred…" vs "I just saved…" verb switch). UI: - Index gets a 3-card KPI strip (Contributed last 30d / Needs this month / On track) plus a pending-pledges callout. - Show page swaps the "Add contribution" CTA for the pledge modal, replaces the contribution list with a pending-pledge banner, and rebuilds the funding widget into per-account rows with a 12-bucket weekly sparkline and last-30 inflow. - Projection chart adds a required-line (dashed light from today → target) and a translucent pending-pledge bump at today's X. Schema (3 migrations): 1. goal_pledges table with PG enums (goal_pledge_kind, goal_pledge_status), open-by-expiry index, and unique-when-not-null matched_transaction_id. 2. Drop goal_contributions. 3. Partial unique index on transactions ((extra -> 'goal' ->> 'pledge_id')) built CONCURRENTLY so it doesn't block prod. After pulling: run bin/rails db:migrate, then commit the schema.rb sync separately (or let CI regenerate). Deferred to v1.1: allocation columns, contention/archived banners, "why is this behind?" diagnostic, reallocate flow, refresh-sync + Plaid throttle, unallocated-cash chip, joint-account approval, goal_activities log, polymorphic matched_entry_id/type for manual pledge audit.
91 lines
3.3 KiB
Ruby
91 lines
3.3 KiB
Ruby
class Account::ReconciliationManager
|
|
attr_reader :account
|
|
|
|
def initialize(account)
|
|
@account = account
|
|
end
|
|
|
|
# Reconciles balance by creating a Valuation entry. If existing valuation is provided, it will be updated instead of creating a new one.
|
|
def reconcile_balance(balance:, date: Date.current, dry_run: false, existing_valuation_entry: nil)
|
|
old_balance_components = old_balance_components(reconciliation_date: date, existing_valuation_entry: existing_valuation_entry)
|
|
prepared_valuation = prepare_reconciliation(balance, date, existing_valuation_entry)
|
|
|
|
unless dry_run
|
|
prepared_valuation.save!
|
|
GoalPledge::Reconciler.new(prepared_valuation).run
|
|
end
|
|
|
|
ReconciliationResult.new(
|
|
success?: true,
|
|
old_cash_balance: old_balance_components[:cash_balance],
|
|
old_balance: old_balance_components[:balance],
|
|
new_cash_balance: derived_cash_balance(date: date, total_balance: prepared_valuation.amount),
|
|
new_balance: prepared_valuation.amount,
|
|
error_message: nil
|
|
)
|
|
rescue => e
|
|
ReconciliationResult.new(
|
|
success?: false,
|
|
error_message: e.message
|
|
)
|
|
end
|
|
|
|
private
|
|
# Returns before -> after OR error message
|
|
ReconciliationResult = Struct.new(
|
|
:success?,
|
|
:old_cash_balance,
|
|
:old_balance,
|
|
:new_cash_balance,
|
|
:new_balance,
|
|
:error_message,
|
|
keyword_init: true
|
|
)
|
|
|
|
def prepare_reconciliation(balance, date, existing_valuation)
|
|
valuation_record = existing_valuation ||
|
|
account.entries.valuations.find_by(date: date) || # In case of conflict, where existing valuation is not passed as arg, but one exists
|
|
account.entries.build(
|
|
name: Valuation.build_reconciliation_name(account.accountable_type),
|
|
entryable: Valuation.new(kind: "reconciliation")
|
|
)
|
|
|
|
valuation_record.assign_attributes(
|
|
date: date,
|
|
amount: balance,
|
|
currency: account.currency
|
|
)
|
|
|
|
valuation_record
|
|
end
|
|
|
|
def derived_cash_balance(date:, total_balance:)
|
|
balance_components_for_reconciliation_date = get_balance_components_for_date(date)
|
|
|
|
return nil unless balance_components_for_reconciliation_date[:balance] && balance_components_for_reconciliation_date[:cash_balance]
|
|
|
|
# We calculate the existing non-cash balance, which for investments would represents "holdings" for the date of reconciliation
|
|
# Since the user is setting "total balance", we have to subtract the existing non-cash balance from the total balance to get the new cash balance
|
|
existing_non_cash_balance = balance_components_for_reconciliation_date[:balance] - balance_components_for_reconciliation_date[:cash_balance]
|
|
|
|
total_balance - existing_non_cash_balance
|
|
end
|
|
|
|
def old_balance_components(reconciliation_date:, existing_valuation_entry: nil)
|
|
if existing_valuation_entry
|
|
get_balance_components_for_date(existing_valuation_entry.date)
|
|
else
|
|
get_balance_components_for_date(reconciliation_date)
|
|
end
|
|
end
|
|
|
|
def get_balance_components_for_date(date)
|
|
balance_record = account.balances.find_by(date: date, currency: account.currency)
|
|
|
|
{
|
|
cash_balance: balance_record&.end_cash_balance,
|
|
balance: balance_record&.end_balance
|
|
}
|
|
end
|
|
end
|