From 48c9e1acab51da9f990ad38cf7438492d87b8e1e Mon Sep 17 00:00:00 2001 From: megaproxy Date: Thu, 23 Apr 2026 22:38:54 +0000 Subject: [PATCH] Fix demo seed: compute account balances from transactions, fix VWRP cost basis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Account balances are now summed from the actual transaction data after insert rather than hardcoded estimates. VWRP avg cost basis corrected to £102.21. Co-Authored-By: Claude Sonnet 4.6 --- backend/app/demo/seed.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/backend/app/demo/seed.py b/backend/app/demo/seed.py index be8f7ea..b8c107f 100644 --- a/backend/app/demo/seed.py +++ b/backend/app/demo/seed.py @@ -6,7 +6,7 @@ import uuid from datetime import date, datetime, timezone from decimal import Decimal -from sqlalchemy import select +from sqlalchemy import func, select from sqlalchemy.ext.asyncio import AsyncSession from app.core.security import encrypt_field, hash_password @@ -80,10 +80,10 @@ async def seed_demo(db: AsyncSession) -> None: updated_at=now, ) - monzo = mk_acc("Monzo Current Account", "Monzo", "checking", "2847.32", "#ff6b35") - marcus = mk_acc("Marcus Savings", "Goldman Sachs", "savings", "6234.50", "#22c55e") - amex = mk_acc("Amex Gold", "American Express", "credit_card", "-342.18", "#f59e0b", credit_limit=5000) - freetrade = mk_acc("Freetrade Stocks & Shares ISA", "Freetrade", "investment", "0", "#6366f1") + monzo = mk_acc("Monzo Current Account", "Monzo", "checking", "0", "#ff6b35") + marcus = mk_acc("Marcus Savings", "Goldman Sachs", "savings", "0", "#22c55e") + amex = mk_acc("Amex Gold", "American Express", "credit_card", "0", "#f59e0b", credit_limit=5000) + freetrade = mk_acc("Freetrade Stocks & Shares ISA", "Freetrade", "investment", "0", "#6366f1") for acc in [monzo, marcus, amex, freetrade]: db.add(acc) @@ -322,6 +322,17 @@ async def seed_demo(db: AsyncSession) -> None: db.add(t) await db.flush() + # ── Compute account balances from actual transactions ───────────────── + for acc in [monzo, marcus, amex]: + result = await db.execute( + select(func.sum(Transaction.amount)).where( + Transaction.account_id == acc.id, + Transaction.deleted_at.is_(None), + ) + ) + acc.current_balance = result.scalar() or Decimal("0") + # Freetrade balance stays 0 — investment account value tracked via holdings + # ── Budgets ─────────────────────────────────────────────────────────── budget_defs = [ ("Groceries", 300.00, "Groceries"), @@ -384,7 +395,7 @@ async def seed_demo(db: AsyncSession) -> None: # Quantities match investment transactions below — do not double-count. vwrp_h = InvestmentHolding( id=uuid.uuid4(), user_id=uid, account_id=freetrade.id, asset_id=vwrp.id, - quantity=Decimal("50"), avg_cost_basis=Decimal("101.21"), + quantity=Decimal("50"), avg_cost_basis=Decimal("102.21"), currency="GBP", created_at=now, updated_at=now, ) aapl_h = InvestmentHolding(