Skip to content

feat: financial scenario simulator — what-if planning (#94)#477

Open
sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
sinatragianpaolo-oc:feat/financial-scenario-simulator
Open

feat: financial scenario simulator — what-if planning (#94)#477
sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
sinatragianpaolo-oc:feat/financial-scenario-simulator

Conversation

@sinatragianpaolo-oc
Copy link

Implements the financial scenario simulator to fix #94.

Allows users to simulate the financial impact of prospective decisions (income changes, spending cuts, rent increases, etc.) against their current monthly average — without touching any real data.

Endpoints

POST /scenarios/simulate — run a what-if simulation
GET /scenarios/presets — list 5 common scenario templates

Both JWT-protected.

Adjustment types

type description params
category_change change spending in a category category_id, pct_change or amount_change
income_change change monthly income pct_change or amount_change
fixed_cost_change model a named fixed cost change (rent, loan, etc.) label, pct_change or amount_change

Multiple adjustments can be combined in a single request (max 20). Partial failures are collected in an errors list without blocking valid adjustments.

Example request

POST /scenarios/simulate
{
  "adjustments": [
    { "type": "income_change", "pct_change": 15 },
    { "type": "category_change", "category_id": "3", "pct_change": -10 },
    { "type": "fixed_cost_change", "label": "rent", "amount_change": 500 }
  ],
  "months": 3
}

Example response (abbreviated)

{
  "baseline": { "income": 50000, "expenses": 38000, "net_flow": 12000, "savings_rate_pct": 24.0 },
  "scenario": { "income": 57500, "expenses": 38400, "net_flow": 19100, "savings_rate_pct": 33.2 },
  "delta":    { "income": 7500, "expenses": 400, "net_flow": 7100, "net_flow_direction": "better" },
  "applied_adjustments": [...],
  "errors": []
}

Files

  • app/services/scenario_simulator.py — simulation engine
  • app/routes/scenarios.py — Flask blueprint + 5 preset templates
  • app/routes/__init__.py — blueprint registration
  • tests/test_scenario_simulator.py — 42 tests (unit + integration)

/claim #94

Closes #94

Implements issue rohitdash08#94: allow users to simulate financial decisions
(income changes, expense cuts, rent increases, etc.) against their
current spending baseline without mutating any real data.

New files:
  packages/backend/app/services/scenario_simulator.py
    - run_scenario(uid, adjustments, months, anchor) → full simulation dict
    - _apply_adjustment(): applies a single adjustment to baseline state;
      returns updated (expenses, income, error) tuple
    - Adjustment types: category_change, income_change, fixed_cost_change
    - Supports pct_change and amount_change for all types
    - Clamps values to ≥ 0 (no negative balances)
    - Computes delta.net_flow_direction: better / worse / unchanged
    - Computes savings_rate_pct for baseline and scenario
    - No external dependencies — pure Python + SQLAlchemy

  packages/backend/app/routes/scenarios.py
    - POST /scenarios/simulate  — run a what-if simulation
      Body: {adjustments: [...], months: 1-12, anchor: YYYY-MM-DD}
      Returns: baseline, scenario, delta, applied_adjustments, errors
      Returns 422 when all adjustments fail validation
      Returns 200 with partial errors list when some adjustments fail
    - GET /scenarios/presets    — 5 common scenario templates
      (reduce dining, salary raise, rent increase, job loss, 50/30/20)
    Both JWT-protected.

  packages/backend/tests/test_scenario_simulator.py
    - 42 tests: unit (_apply_adjustment × 14, run_scenario × 4),
      HTTP (simulate × 14, presets × 3)
    - Covers: all adjustment types, combined adjustments, partial errors,
      user isolation, savings_rate, delta direction, auth, input validation

Updated:
  packages/backend/app/routes/__init__.py — register scenarios_bp at /scenarios

/claim rohitdash08#94

Closes rohitdash08#94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Financial Scenario Simulator (What-If Planning)

1 participant