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
- New endpoint `GET /api/admin/reports/revenue?month=YYYY-MM` returns `{"month", "orders", "revenue_cents"}` for that month: the number of orders paid during the month and the sum of their `total_cents`. A month with no paid orders answers `orders: 0`, `revenue_cents: 0`.
- An order is paid during a month when its `paid_at` falls from the first instant of the month's first day (00:00:00 UTC, included) up to the first instant of the next month (excluded). Report months are UTC calendar months, so every paid order belongs to exactly one month, and the twelve months of a year add up to every order paid that year. Unpaid orders (`paid_at` is `null`) are never counted.
- `month` is required and must be `YYYY-MM` with a year from 2000 to 2099 and a month from 01 to 12; anything else answers 422 (FastAPI's validation error).
- Unchanged: the reports router answers 401 or 403 itself unless the caller is a signed-in admin, and `GET /api/admin/reports/top-products` keeps working as before.
Files touched
- app/sql/monthly_revenue.sql
- app/reports.py
- tests/test_revenue_report.py