feat: financial scenario simulator — what-if planning (#94)#477
Open
sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
Open
feat: financial scenario simulator — what-if planning (#94)#477sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 simulationGET /scenarios/presets— list 5 common scenario templatesBoth JWT-protected.
Adjustment types
category_changecategory_id,pct_changeoramount_changeincome_changepct_changeoramount_changefixed_cost_changelabel,pct_changeoramount_changeMultiple adjustments can be combined in a single request (max 20). Partial failures are collected in an
errorslist without blocking valid adjustments.Example request
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 engineapp/routes/scenarios.py— Flask blueprint + 5 preset templatesapp/routes/__init__.py— blueprint registrationtests/test_scenario_simulator.py— 42 tests (unit + integration)/claim #94
Closes #94