feat(api): CRM parity mutations and recent-errors filters (BIT-726)

Add POST /api/error-monitoring/resolve, bulk-resolve, and purge-resolved with
resolve key or admin JWT (errors:triage / errors:delete). Extend GET
recent-errors with status, severity, project_slug, q, offset, and stable
ordering. Document contracts in api/docs/ERROR_MONITORING_PUBLIC_API.md;
add vitest for public auth helpers. Proxy new routes and widen admin-ui
middleware for /api/error-monitoring/*.

Paperclip-Issue: BIT-726
Paperclip-Run: 3fbf92be-736e-4b11-8e90-cf993d8cd5ca
Paperclip-Agent: Pedro Alves (id d706f4e6-5d05-4cec-b6e8-f8fb1f203aae)
Made-with: Cursor
This commit is contained in:
David Santos committed 2026-04-02 10:50:06 +00:00
1 parent e0af92eda1
commit 8b0044be82
10 files changed
+919 -49

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
# Public error-monitoring API (hub)
CRM-shaped JSON endpoints served by the Fastify API (`@errors-bitreport/api`). Same paths as the CRM `ErrorMonitoring` module where noted.
## Authentication
### Read (`GET …/recent-errors`)
- Query: `api_key` must match `ERROR_RESOLVE_API_KEY` or `ERROR_REPORTING_API_KEY`, **or**
- Query: `view_errors` must match `DEBUG_VIEW_ERRORS_KEY`.
- If neither server secret is set → **503**.
### Mutations (`POST …/resolve`, `bulk-resolve`, `purge-resolved`)
- Header `X-Error-Resolve-Key` or JSON body field `api_key` must match `ERROR_RESOLVE_API_KEY` or `ERROR_REPORTING_API_KEY` when that secret is set, **or**
- Admin session: HTTP-only cookie `__Host-access` (JWT) with:
- `errors:triage` for resolve and bulk-resolve
- `errors:delete` for purge-resolved
- If no resolve key is configured → only session auth is accepted (token-only automation is disabled).
## `GET /api/error-monitoring/recent-errors`
Alias: `GET /v1/paperclip/recent-errors`
| Query | Description |
|--------|-------------|
| `limit` | Max rows (default large cap; invalid → **400**) |
| `offset` | Pagination offset (default `0`) |
| `status` | Comma-separated: `open`, `investigating`, `resolved`, `ignored` (default `open,investigating`) |
| `severity` | `critical` \| `high` \| `medium` \| `low` \| `info` |
| `project_slug` | Project slug (lowercase) |
| `q` | Search incident title, occurrence message, or fingerprint (`LIKE`) |
| `order` | `occurred_at_desc` (default) or `occurred_at_asc` (tie-break: `occurrence id`) |
**Response:** `{ "success": true, "message": "ok", "data": [ … ] }`
Each row includes CRM-compatible fields. For non-resolved incidents, `status` is always `"open"` (including `investigating`) so Paperclip `error_loop` continues to treat rows as open.
## `POST /api/error-monitoring/resolve`
**Body (JSON):**
| Field | Required | Description |
|--------|-----------|-------------|
| `unique_id` | yes | Latest occurrence id as returned in `recent-errors`, or incident id |
| `resolution_notes` | no | Stored in state history note (default message if omitted) |
| `assigned_to` | no | Appended to the change note for traceability |
| `api_key` | no | Resolve key if not using header / session |
**Responses:** **200** success envelope; **400** missing id; **401** auth; **404** not found or not resolvable; **503** if read keys not applicable (mutations use resolve key / session only).
## `POST /api/error-monitoring/bulk-resolve`
**Body (JSON):**
| Field | Required | Description |
|--------|-----------|-------------|
| `unique_ids` | one of | Non-empty array of numeric string ids (max 1000); maps to incidents via occurrence or incident id |
| `message_pattern` | one of | String or array of strings (max 20); resolves incidents with any open occurrence whose `message` contains the substring (`LIKE %pattern%`) |
| `resolution_notes` | no | Default bulk message if omitted |
| `assigned_to` | no | Appended to note |
| `api_key` | no | Resolve key |
If both `unique_ids` and `message_pattern` are sent, **`unique_ids` wins** (CRM behaviour).
## `POST /api/error-monitoring/purge-resolved`
**Body (JSON):**
| Field | Description |
|--------|-------------|
| `dry_run` | If true, returns `would_delete` count only |
| `api_key` | Optional resolve key |
**Effect:** `DELETE` from `er_incidents` where `current_state = 'resolved'` (cascades occurrences and related rows per schema).
## Admin UI proxy
When the Next admin UI is deployed with `ERROR_REPORTING_ADMIN_API_BASE`, the same paths under `/api/error-monitoring/*` are reverse-proxied to Fastify (see `admin-ui/app/api/error-monitoring/*/route.ts`).