mirror of
https://github.com/we-promise/sure.git
synced 2026-05-30 07:49:01 +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.
67 lines
1.7 KiB
Ruby
67 lines
1.7 KiB
Ruby
class GoalPledgesController < ApplicationController
|
|
before_action :set_goal
|
|
before_action :set_pledge, only: %i[extend destroy]
|
|
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
|
|
|
|
def new
|
|
@pledge = @goal.goal_pledges.new(
|
|
currency: @goal.currency,
|
|
kind: default_kind_for(@goal),
|
|
amount: params[:amount].presence
|
|
)
|
|
end
|
|
|
|
def create
|
|
@pledge = @goal.goal_pledges.new(pledge_params)
|
|
@pledge.kind = default_kind_for(@goal) if @pledge.kind.blank?
|
|
@pledge.currency = @goal.currency
|
|
|
|
if @pledge.save
|
|
flash[:notice] = t(".success")
|
|
respond_to do |format|
|
|
format.html { redirect_to goal_path(@goal) }
|
|
format.turbo_stream do
|
|
render turbo_stream: turbo_stream.action(:redirect, goal_path(@goal))
|
|
end
|
|
end
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def extend
|
|
@pledge.extend!
|
|
redirect_to goal_path(@goal), notice: t(".success")
|
|
rescue ActiveRecord::RecordInvalid
|
|
redirect_to goal_path(@goal), alert: t(".not_open")
|
|
end
|
|
|
|
def destroy
|
|
@pledge.cancel!
|
|
redirect_to goal_path(@goal), notice: t(".success")
|
|
rescue ActiveRecord::RecordInvalid
|
|
redirect_to goal_path(@goal), alert: t(".not_open")
|
|
end
|
|
|
|
private
|
|
def set_goal
|
|
@goal = Current.family.goals.find(params[:goal_id])
|
|
end
|
|
|
|
def set_pledge
|
|
@pledge = @goal.goal_pledges.find(params[:id])
|
|
end
|
|
|
|
def pledge_params
|
|
params.require(:goal_pledge).permit(:amount, :account_id, :kind)
|
|
end
|
|
|
|
def default_kind_for(goal)
|
|
goal.any_connected_account? ? "transfer" : "manual_save"
|
|
end
|
|
|
|
def record_not_found
|
|
redirect_to goals_path, alert: t("goals.errors.not_found")
|
|
end
|
|
end
|