Add multi-currency to the spending summary
Python · FastAPI · intermediate · modification
Extends the spending-summary endpoint for the new multi-currency billing. Narrows the total from all-time to the current calendar month and reports the period alongside it, so the dashboard can label the figure. Keeps the response model tight.
The billing dashboard's headline number; the same endpoint now serves accounts on USD, EUR, and GBP plans.
Requirements
- GET /accounts/{account_id}/spending-summary returns the account's total spend for the current calendar month, in cents.
- Return 404 if the account does not exist.
- Accounts now bill in USD, EUR, or GBP. The response must include the account's `currency` (available as `account.currency`) so the web client can render the amount with the correct symbol.
Files touched
- app/routes/summary.py
--- app/routes/summary.py
from pydantic import BaseModel
from fastapi import APIRouter, HTTPException
-from app.db import get_account, sum_all_charges
+from app.db import get_account, sum_charges_for_month
router = APIRouter()
class SpendingSummary(BaseModel):
account_id: int
+ period: str
total_cents: int
raise HTTPException(status_code=404, detail="account not found")
- total_cents = await sum_all_charges(account_id)
+ today = date.today()
+ period = f"{today.year:04d}-{today.month:02d}"
+ total_cents = await sum_charges_for_month(account_id, today.year, today.month)
return SpendingSummary(
account_id=account_id,
+ period=period,
total_cents=total_cents,
)