Fix demo seed: compute account balances from transactions, fix VWRP cost basis

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 <noreply@anthropic.com>
This commit is contained in:
megaproxy 2026-04-23 22:38:54 +00:00
parent 520a5a7d96
commit 48c9e1acab

View file

@ -6,7 +6,7 @@ import uuid
from datetime import date, datetime, timezone from datetime import date, datetime, timezone
from decimal import Decimal from decimal import Decimal
from sqlalchemy import select from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.core.security import encrypt_field, hash_password from app.core.security import encrypt_field, hash_password
@ -80,9 +80,9 @@ async def seed_demo(db: AsyncSession) -> None:
updated_at=now, updated_at=now,
) )
monzo = mk_acc("Monzo Current Account", "Monzo", "checking", "2847.32", "#ff6b35") monzo = mk_acc("Monzo Current Account", "Monzo", "checking", "0", "#ff6b35")
marcus = mk_acc("Marcus Savings", "Goldman Sachs", "savings", "6234.50", "#22c55e") marcus = mk_acc("Marcus Savings", "Goldman Sachs", "savings", "0", "#22c55e")
amex = mk_acc("Amex Gold", "American Express", "credit_card", "-342.18", "#f59e0b", credit_limit=5000) 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") freetrade = mk_acc("Freetrade Stocks & Shares ISA", "Freetrade", "investment", "0", "#6366f1")
for acc in [monzo, marcus, amex, freetrade]: for acc in [monzo, marcus, amex, freetrade]:
@ -322,6 +322,17 @@ async def seed_demo(db: AsyncSession) -> None:
db.add(t) db.add(t)
await db.flush() 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 ─────────────────────────────────────────────────────────── # ── Budgets ───────────────────────────────────────────────────────────
budget_defs = [ budget_defs = [
("Groceries", 300.00, "Groceries"), ("Groceries", 300.00, "Groceries"),
@ -384,7 +395,7 @@ async def seed_demo(db: AsyncSession) -> None:
# Quantities match investment transactions below — do not double-count. # Quantities match investment transactions below — do not double-count.
vwrp_h = InvestmentHolding( vwrp_h = InvestmentHolding(
id=uuid.uuid4(), user_id=uid, account_id=freetrade.id, asset_id=vwrp.id, 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, currency="GBP", created_at=now, updated_at=now,
) )
aapl_h = InvestmentHolding( aapl_h = InvestmentHolding(