mirror of
https://github.com/we-promise/sure.git
synced 2026-05-30 15:59:02 +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.
79 lines
2.5 KiB
Ruby
79 lines
2.5 KiB
Ruby
require "test_helper"
|
|
|
|
class GoalPledgesControllerTest < ActionDispatch::IntegrationTest
|
|
setup do
|
|
sign_in users(:family_admin)
|
|
@goal = goals(:vacation_italy)
|
|
@account = accounts(:depository)
|
|
@pledge = goal_pledges(:open_transfer)
|
|
ensure_tailwind_build
|
|
end
|
|
|
|
test "new renders the pledge form" do
|
|
get new_goal_pledge_url(@goal)
|
|
assert_response :success
|
|
end
|
|
|
|
test "create opens a pledge with default kind" do
|
|
assert_difference -> { GoalPledge.count } => 1 do
|
|
post goal_pledges_url(@goal), params: {
|
|
goal_pledge: {
|
|
amount: "150",
|
|
account_id: @account.id
|
|
}
|
|
}
|
|
end
|
|
pledge = GoalPledge.order(created_at: :desc).first
|
|
assert_equal "open", pledge.status
|
|
assert_equal @goal.id, pledge.goal_id
|
|
assert_redirected_to goal_path(@goal)
|
|
end
|
|
|
|
test "create rejects amount <= 0" do
|
|
assert_no_difference "GoalPledge.count" do
|
|
post goal_pledges_url(@goal), params: {
|
|
goal_pledge: { amount: "0", account_id: @account.id }
|
|
}
|
|
end
|
|
assert_response :unprocessable_entity
|
|
end
|
|
|
|
test "extend pushes expires_at forward" do
|
|
before = @pledge.expires_at
|
|
patch extend_goal_pledge_url(@goal, @pledge)
|
|
assert_redirected_to goal_path(@goal)
|
|
assert @pledge.reload.expires_at > before
|
|
end
|
|
|
|
test "extend on non-open pledge flashes alert" do
|
|
pledge = goal_pledges(:matched_transfer)
|
|
patch extend_goal_pledge_url(@goal, pledge)
|
|
assert_redirected_to goal_path(@goal)
|
|
assert flash[:alert].present?
|
|
end
|
|
|
|
test "destroy cancels an open pledge" do
|
|
delete goal_pledge_url(@goal, @pledge)
|
|
assert_redirected_to goal_path(@goal)
|
|
assert @pledge.reload.status_cancelled?
|
|
end
|
|
|
|
test "destroy on non-open pledge flashes alert" do
|
|
pledge = goal_pledges(:matched_transfer)
|
|
delete goal_pledge_url(@goal, pledge)
|
|
assert_redirected_to goal_path(@goal)
|
|
assert flash[:alert].present?
|
|
end
|
|
|
|
test "another family's goal returns redirect" do
|
|
other_family = Family.create!(name: "Other", currency: "USD", locale: "en", country: "US", timezone: "UTC")
|
|
other_account = Account.create!(family: other_family, accountable: Depository.new, name: "Foreign", currency: "USD", balance: 100)
|
|
other_goal = other_family.goals.new(name: "Foreign goal", target_amount: 100, currency: "USD")
|
|
other_goal.goal_accounts.build(account: other_account)
|
|
other_goal.save!
|
|
|
|
get new_goal_pledge_url(other_goal)
|
|
assert_redirected_to goals_path
|
|
end
|
|
end
|