Skip to content

ADR 0006: M2 admin reference data

Towns, parcels, and capture API keys were seed-only. A firm could not onboard a town, import its parcel list, or issue and revoke a capture key without database access. M2 calls for admin pages for towns, parcels, and API keys; the UI already has role-aware sessions.

Admin writes are session-only and enforced in core

Section titled “Admin writes are session-only and enforced in core”

Town, parcel, CSV import, and API-key routes use requireSession, and writes use requireSameOrigin. Every core use case calls assertAdmin, so direct callers and HTTP callers get the same 403 forbidden behavior for reviewers, submitters, or disabled users. API keys cannot reach admin routes.

A separate admin database or service was rejected: this is the same tenant-scoped reference data with the same constraints.

Town and parcel creation reuse existing identity rules

Section titled “Town and parcel creation reuse existing identity rules”

Town slugs are lowercase kebab-case and unique per tenant. Parcel map/lots are trimmed and unique per tenant/town. Drizzle unique failures are translated to DuplicateTownError / DuplicateParcelError and exposed as 409 town_exists / 409 parcel_exists. Cross-tenant towns return 422 town_not_found.

No D1 schema or migration change is needed; the constraints already exist.

POST /v1/parcels/import accepts a town id and CSV text. The core parser supports quoted commas, escaped quotes, CRLF/LF, and a UTF-8 BOM. Required headers are mapLot and address; ownerName and externalId are optional. Unknown columns are ignored. The limit is 500 data rows and 500,000 characters.

Every row is validated before writing: required fields, length caps, duplicates within the file, and duplicates already in the town. Any failure returns 422 csv_validation_failed with row-level details and stores nothing. A valid file is inserted in one D1 batch; a race on the unique map/lot rolls back the batch and returns 409 parcel_exists.

Importing valid rows while reporting failures was rejected for this slice because partial town data is harder to reason about. Very large imports may need a background job later.

POST /v1/api-keys generates a cd_live_... key, stores id, tenant, name, prefix, SHA-256 hash, status, and timestamps, and returns the raw key once. List responses expose only summary fields and never raw keys or hashes. Revocation sets revokedAt and revoked; authentication immediately rejects the old key. Revoking an already-revoked key is idempotent. Unknown or cross-tenant ids return 404 not_found.

Key scopes, expiry, rotation, and usage dashboards are deferred.

Admin UI uses the existing shell and hash routing

Section titled “Admin UI uses the existing shell and hash routing”

Admin routes are #/admin/towns, #/admin/parcels, and #/admin/api-keys. GET /v1/me decides whether admin navigation appears and whether the routes render; non-admin users see a forbidden state and make no admin data requests.

Towns use a create form and table. Parcels add a town selector, search, create form, CSV file input plus textarea fallback, and row-error display. API keys show the raw key once in a copy-now panel and let admins revoke active keys. TanStack Query owns lists and mutations.

pnpm seed upserts [email protected] as an active admin and writes the email to .local-demo.json. The existing reviewer seed remains unchanged.

  • Admins can onboard towns, import parcels, and manage capture credentials entirely in the browser.
  • Reviewer and capture flows are unchanged.
  • There is still no edit/delete/merge for towns or parcels, no user management, and no GIS/CAMA sync.
  • CSV import is bounded in size and request duration; larger imports need asynchronous processing.
  • The admin pages share the capture shell; a fuller admin information architecture belongs to the post-M4 design/UX pass.
  • CSRF tokens remain a hardening item; SameSite=Lax plus the same-origin check protect these writes.