Monthly revenue on the admin reports page

Database · PostgreSQL · beginner · modification

Finance asked for a monthly revenue figure on the admin reports page. `app/sql/monthly_revenue.sql` (new) counts the orders paid in a month and sums their totals, taking the month's first day as its only parameter. `app/reports.py` adds `GET /api/admin/reports/revenue`, which validates `month`, turns it into that first day and returns the count and the total next to the existing top-products report. `tests/test_revenue_report.py` (new) covers the endpoint against the test database.

Shop backend: FastAPI 0.115 on Python 3.12 with psycopg 3.2 (plain SQL, no ORM) against PostgreSQL 16. Report queries live in `app/sql/<name>.sql` and are run with psycopg's named parameters through `load_sql` (existing). Existing and unchanged: `orders` (migration `0001_init.sql`) has `id bigserial`, `customer_id bigint`, `total_cents bigint NOT NULL CHECK (total_cents >= 0)` (at most 1,000,000,000 per order, at most 2,000,000 orders a month, so a month's sum fits in a `bigint`), `created_at timestamptz` and `paid_at timestamptz` (null until the payment is captured, then set once to the capture time with microsecond precision and never changed), with an index on `paid_at`. `get_conn` hands out an autocommit connection from the app's psycopg pool, which opens every connection with `options='-c TimeZone=UTC'`, so the session time zone is UTC in production and in tests. `require_admin` (the router dependency) answers 401 without a session and 403 for a non-admin. `top_products.sql` and its endpoint are unchanged. Tests run with pytest against a fresh test database built from the migrations: `tests/conftest.py` (existing) provides `client`, a `TestClient` signed in as an admin, and `db`, an autocommit connection to the test database whose tables are truncated after each test. The reports page (web app) ships separately and only reads this endpoint.

Requirements

Files touched

Review this PR

Database practice