feat(savings): add savings goals

Adds a standalone Savings goals feature: a piggy-bank style tracker that
lets a family set a target, link one or more Depository accounts as
funding sources, and log manual contributions over time. Supersedes #1569
(closed) — same intent, redesigned per reviewer + Discord feedback.

What this adds:

- New `/savings_goals` sidebar entry (piggy-bank icon) with index, show,
  state-filtered tabs (all/active/paused/completed/archived), and a
  2-step modal stepper for creation (Identity → Review).
- Multi-account funding via a `SavingsGoalAccount` join: a goal requires
  ≥1 linked Depository account (checking/savings/HSA/CD/money-market),
  and all linked accounts must share the goal's currency.
- Tracker balance model: goal balance = SUM(contributions.amount). No
  auto-flow from account balances. Contributions are pure logical
  records and don't move money between accounts.
- Manual contributions modal scoped to the goal's linked accounts.
  Initial contributions seeded at creation can't be deleted; manual
  ones can.
- AASM lifecycle: active / paused / completed / archived.
  Hard-delete only after archive.
- Status pills (On track / Behind / Reached / No date) derived from
  pace vs target_date.
- AI Assistant tool `create_savings_goal` lets the sidebar chat create
  a goal end-to-end from a natural-language prompt; soft errors carry
  the available-accounts list back to the LLM (mirrors the existing
  `import_bank_statement` pattern).
- Family-scoped throughout (`Current.family`-only access, account
  family-scoping enforced both in controllers and the AI tool).
- Demo data seed wires up 4 sample goals across the Depository accounts.

Intentionally out of scope (separate PRs / v1.1):

- Auto-fund from budget surplus + Sidekiq cron + budget-show card.
- Dashboard "Savings goals" widget.
- "Behind pace" projection chart on the detail page.
- `evaluate_savings_goal_feasibility` LLM tool (level-setting before
  create_savings_goal).
- Spend-less goals inside Budgets.
- Family-member-private goals (deferred investigation).
This commit is contained in:
Guillem Arias
2026-05-11 11:20:37 +02:00
parent 36960fe058
commit 77660d2ee4
49 changed files with 2419 additions and 5 deletions

View File

@@ -0,0 +1,124 @@
import { Controller } from "@hotwired/stimulus";
// 2-step modal stepper for creating a savings goal.
//
// Single <form> with two panels. Step 1 collects identity (name, amount,
// date, color, notes). Step 2 collects ≥1 linked depository accounts and
// optionally an initial contribution. Submit button stays disabled until at
// least one linked account is selected. Step state lives entirely in the
// DOM — no half-records.
export default class extends Controller {
static targets = [
"step1Panel",
"step2Panel",
"step1Indicator",
"step2Indicator",
"step1Field",
"nameField",
"targetAmountField",
"linkedAccountCheckbox",
"initialContributionAmount",
"initialContributionAccountSelect",
"reviewPanel",
"reviewName",
"reviewAmount",
"reviewDate",
"reviewAccounts",
"submitButton",
];
next(event) {
event?.preventDefault?.();
if (!this.validateStep1()) return;
this.step1PanelTarget.classList.add("hidden");
this.step2PanelTarget.classList.remove("hidden");
this.markStepActive(2);
this.updateReview();
this.refreshSubmitState();
}
back(event) {
event?.preventDefault?.();
this.step2PanelTarget.classList.add("hidden");
this.step1PanelTarget.classList.remove("hidden");
this.markStepActive(1);
}
linkedAccountChanged() {
this.refreshAccountSelect();
this.refreshSubmitState();
this.updateReview();
}
validateStep1() {
let ok = true;
this.step1FieldTargets.forEach((field) => {
if (!field.checkValidity()) {
field.reportValidity();
ok = false;
}
});
return ok;
}
refreshSubmitState() {
const anyChecked = this.linkedAccountCheckboxTargets.some((cb) => cb.checked);
this.submitButtonTarget.disabled = !anyChecked;
}
refreshAccountSelect() {
if (!this.hasInitialContributionAccountSelectTarget) return;
const select = this.initialContributionAccountSelectTarget;
const previous = select.value;
select.innerHTML = "";
const blank = document.createElement("option");
blank.value = "";
blank.textContent = select.dataset.blankLabel || "—";
select.appendChild(blank);
this.linkedAccountCheckboxTargets
.filter((cb) => cb.checked)
.forEach((cb) => {
const opt = document.createElement("option");
opt.value = cb.value;
opt.textContent = cb.dataset.accountName || cb.value;
select.appendChild(opt);
});
if ([...select.options].some((o) => o.value === previous)) {
select.value = previous;
}
}
updateReview() {
if (!this.hasReviewPanelTarget) return;
if (this.hasReviewNameTarget && this.hasNameFieldTarget) {
this.reviewNameTarget.textContent = this.nameFieldTarget.value || "—";
}
if (this.hasReviewAmountTarget && this.hasTargetAmountFieldTarget) {
this.reviewAmountTarget.textContent = this.targetAmountFieldTarget.value || "—";
}
if (this.hasReviewDateTarget) {
const dateInput = this.element.querySelector('input[type="date"][name="savings_goal[target_date]"]');
this.reviewDateTarget.textContent = dateInput?.value || "—";
}
if (this.hasReviewAccountsTarget) {
const names = this.linkedAccountCheckboxTargets
.filter((cb) => cb.checked)
.map((cb) => cb.dataset.accountName || cb.value);
this.reviewAccountsTarget.textContent = names.length ? names.join(", ") : "—";
}
}
markStepActive(stepNumber) {
if (this.hasStep1IndicatorTarget) {
this.step1IndicatorTarget.classList.toggle("text-primary", stepNumber === 1);
}
if (this.hasStep2IndicatorTarget) {
this.step2IndicatorTarget.classList.toggle("text-primary", stepNumber === 2);
}
}
}