Top customers card on the admin dashboard

Database · PostgreSQL · beginner · modification

Adds the data for the new "Top customers" card. `queries/top_customers.sql` (new) sums each customer's paid orders and returns the ten biggest spenders. `app/dashboard.py` maps `GET /top-customers` on the existing dashboard router and returns the rows in the card's shape. `tests/test_top_customers.py` (new) covers the endpoint.

Shop admin API on PostgreSQL 16 and Python 3.12 (FastAPI 0.115, psycopg 3.2 with dict rows, plain SQL, no ORM). Existing and unchanged: `customers` (`id bigserial` primary key, `name text NOT NULL`, `created_at timestamptz NOT NULL DEFAULT now()`) and `orders` (`id bigserial` primary key, `customer_id bigint NOT NULL` referencing `customers`, `status text NOT NULL` with a CHECK that it is one of `pending`, `paid`, `refunded`, `cancelled`, `total_cents integer NOT NULL` with a CHECK that it is `>= 0`, `created_at timestamptz NOT NULL DEFAULT now()`, an index on `customer_id`). The store has about 20,000 customers and 150,000 orders; about 6,000 customers have at least one paid order, since many sign up and never buy. `app.db.get_conn` yields a connection from the app's pool for the request. `app.queries.q(name)` returns the text of `queries/<name>.sql`, read once and cached. `app.auth.require_admin` answers 401 or 403 itself unless the signed-in user is an admin. `queries/order_counts.sql` counts orders per status. Tests run with pytest against a fresh test database built from `migrations/`; `tests/conftest.py` provides `db` (a connection to that database, `customers` and `orders` emptied before each test) and `admin_client` (a FastAPI `TestClient` signed in as an admin). The dashboard card (web app) ships separately and only reads this endpoint.

Requirements

Files touched

Review this PR

Database practice