Open tickets list for the support inbox
Database · PostgreSQL · beginner · modification
Adds the endpoint behind the new support inbox. `queries/open_tickets.sql` (new) selects the workspace's open and pending tickets, latest activity first, capped at 100. `app/tickets.py` maps `GET /open` on the existing tickets router, above the `/{ticket_id}` route so `open` is never read as a ticket id, and returns the rows. `tests/test_open_tickets.py` (new) checks the status filter and the order, and that another workspace's tickets stay out of the list.
Helpdesk API on PostgreSQL 16 and Python 3.12 (FastAPI 0.115, psycopg 3.2 with dict rows, plain SQL, no ORM). Existing and unchanged: `tickets` (`id bigserial` primary key, `workspace_id bigint NOT NULL`, `subject text NOT NULL`, `status text NOT NULL` with a CHECK that it is one of `open`, `pending`, `solved`, `closed`, `updated_at timestamptz NOT NULL`) holds the tickets of every workspace, about 2,000 workspaces and 3 million tickets, with an index on `(workspace_id, updated_at DESC, id DESC)`. `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; queries use psycopg named placeholders (`%(workspace_id)s`), so values are always sent as parameters. `app.auth.require_member` answers 401 or 403 itself unless the signed-in user is a member of the workspace in the path. `queries/ticket_by_id.sql` selects one ticket by `workspace_id` and `id`. Tests run with pytest against a fresh test database built from `migrations/`; `tests/conftest.py` provides `db` (a connection to that database, `tickets` emptied before each test) and `client_for(workspace_id)` (a FastAPI `TestClient` signed in as a member of that workspace only). The inbox page (web app) ships separately and only reads this endpoint.
Requirements
- New endpoint `GET /api/workspaces/{workspace_id}/tickets/open` returns the tickets that still need an answer, for the support inbox: those whose `status` is `open` or `pending`. `solved` and `closed` tickets are left out.
- Only tickets of the workspace in the path are returned. A workspace's tickets are confidential to its members: a ticket's subject is the subject line of the customer's email.
- Each item is `{ "id", "subject", "status", "updated_at" }`. Items are ordered by `updated_at`, latest first, ties broken by `id`, higher first. At most 100 items are returned (the inbox shows the first 100; paging is a separate ticket). A workspace with no such tickets gets `[]`.
- The query lives in `queries/open_tickets.sql` and is loaded with `q()`, like the app's other queries.
- Unchanged: the tickets router's `require_member` dependency answers 401 or 403 unless the caller is a signed-in member of the workspace in the path, and `GET /api/workspaces/{workspace_id}/tickets/{ticket_id}` works as before.
Files touched
- queries/open_tickets.sql
- app/tickets.py
- tests/test_open_tickets.py