caelum0x 70653ae53b feat(mail): mount the dashboard, email event + webhook delivery logs, billing shape, friendly bad magic links
mail-server serves apps/mail/dashboard at /dashboard/ (tower-http fs,
DASHBOARD_DIR, mock/ and MOUNT.md never served). New account-scoped,
Resend-shaped endpoints the dashboard reads: GET /emails/{id}/events
(status history with timestamps and a one-line detail from the stored
payload), GET /emails/{id}/webhook-deliveries and
GET /webhooks/{id}/deliveries (delivery log joined with event + endpoint),
GET /emails?test=true|false and GET /suppressions?q= server filters.
GET /billing now carries description, next_plan, upgrade_label,
current_period_end/renews_at; POST /billing/checkout accepts success_url
(own origin only) and returns url. mail-auth: a bad, used or expired link
opened in a browser redirects to <redirect_to>#error=invalid_token (or a
plain HTML page when the token was never ours); the dashboard shows the
message on the login form. Dashboard uses the server-side test filter,
portal via POST /billing/portal, docs updated.

Verified live against local Postgres mail_e2e + fake relay + webhook
listener: curl of every endpoint (404 for another account's ids, 401
without auth, 422 on test=maybe), and a headless-browser pass through the
real magic-link login, email timeline, webhook delivery log, usage card
and the expired-link redirect.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 13:01:01 +03:00

115 lines
6.7 KiB
Markdown

# Scribase Mail dashboard: mounting and API needs
Static files only: `index.html`, `styles.css`, `config.js`, `js/**`. No build step,
no CDN, no framework. Hash routing (`#/emails`, `#/domains/{id}` ...), so one
`index.html` serves every screen.
## Mount (done: `crates/mail-server/src/dashboard.rs`)
`mail-server` serves this directory at `/dashboard/` out of the box:
`tower-http` has the `fs` feature and `main.rs` does
`build_app(...).nest_service("/dashboard", dashboard::service())`. The
directory comes from `DASHBOARD_DIR` (default `apps/mail/dashboard`, i.e. the
path when the binary runs from the railyard checkout root; set it explicitly in
production). `mock/` and `MOUNT.md` are never served (404), so the whole
directory can be deployed as-is. The server logs a warning at boot when the
directory has no `index.html`.
The dashboard calls the API on the same origin (`config.js` ships with
`apiBase: ""`), so no CORS and no cookie endpoint are needed: the session is the
mail-auth JWT, kept in `localStorage`, sent as `Authorization: Bearer` plus
`X-Account-Id`. `index.html` fixes a missing trailing slash itself
(`/dashboard` -> `/dashboard/`), so no redirect route is required.
## One-time setup (ops / owner)
1. A mail-auth project with slug `dashboard` (or set `window.MAIL_DASHBOARD.project`
in `config.js`), `jwt_audience` = `DASHBOARD_JWT_AUDIENCE` (`scribase-mail`),
`from_address` on the system domain, and `allowed_redirect_origins` containing
the dashboard origin, e.g. `https://mail.scribase.com`. The dashboard sends
`redirect_to = <origin>/dashboard/`.
2. `SYSTEM_FROM_ADDRESS` set, otherwise invitations are created but not emailed
(the Team screen says so).
3. Consider `AUTH_JWT_TTL_SECS` longer than the default 3600 for this project:
the dashboard signs the user out when the JWT expires (no refresh endpoint).
## Local dev against a separate API origin
Edit `config.js` -> `apiBase: "http://127.0.0.1:8790"`. The API then needs a
CORS layer (`tower_http::cors::CorsLayer` allowing the dashboard origin, headers
`authorization, content-type, x-account-id`, methods GET/POST/PATCH/DELETE).
Not needed in production (same origin).
Mock for UI work without Postgres: `python3 mock/mock_server.py` (port 8795,
`MOCK_BILLING=on` to exercise the billing panel). Magic links are not emailed;
`GET /__mock/last-link` returns the verify URL.
## What the dashboard calls
| Screen | Endpoints |
|---|---|
| Login | `POST /auth/magic-link` {email, redirect_to, project}; consumes `#access_token=...` from `GET /auth/verify` |
| Account picker / onboarding | `GET /accounts`, `POST /accounts`, `GET /accounts/{id}` |
| Onboarding | `POST /domains`, `GET /domains/{id}`, `POST /domains/{id}/verify`, `POST /api-keys`, `POST /emails` |
| Emails | `GET /emails?limit&after&test=`, `GET /emails/{id}`, `GET /emails/{id}/events`, `GET /emails/{id}/webhook-deliveries`, `POST /emails/{id}/cancel` |
| Domains | `GET/POST /domains`, `GET/PATCH/DELETE /domains/{id}`, `POST /domains/{id}/verify` (uses `records[]`: record, name, type, value, ttl, status, priority, purpose) |
| API keys | `GET/POST /api-keys` (`mode: live|test`), `DELETE /api-keys/{id}` |
| Webhooks | `GET/POST /webhooks`, `GET/PATCH/DELETE /webhooks/{id}` (`signing_secret` on get), `GET /webhooks/{id}/deliveries` |
| Suppressions | `GET /suppressions?limit=1000&q=`, `POST /suppressions`, `DELETE /suppressions/{address}` |
| Team | `GET/POST /accounts/{id}/members`, `PATCH/DELETE /accounts/{id}/members/{user_id}`, `DELETE /accounts/{id}/invites/{invite_id}` |
| Usage and plan | `GET /usage`, `GET /audit-log?limit=50`, `GET /billing`, `POST /billing/checkout`, `POST /billing/portal` |
Errors are read in Resend shape `{statusCode, name, message}`; 401 on a session
call signs the user out.
## API pieces the dashboard reads (all shipped)
1. **`GET /billing`** returns `plan {code,name,...}`, `description` (one line:
price + included volume), `limits` (`emails_per_day`, `emails_per_month`),
`status`, `next_plan` + `upgrade_label` (next public paid plan up the ladder,
null on the top plan), `current_period_end` / `renews_at` (from the
subscription, null on Free), `checkout_available` (Dodo configured) and
`portal_available`. Portal links are minted on demand by `POST /billing/portal`
(`{link}`), so there is no `portal_url`. **`POST /billing/checkout`** takes
`{plan, success_url, cancel_url}` and returns `{url, checkout_url, session_id}`;
`success_url` is used as Dodo's return URL only when it sits on
`PUBLIC_BASE_URL`. 503 `billing_not_configured` without `DODO_API_KEY`; the
panel then says upgrades are not available yet.
2. **`GET /emails/{id}/events`**: `{data:[{id, email_id, type, message,
created_at}]}`, oldest first. `message` is derived from the stored event
payload: the SMTP reply on a bounce, the relay error on a failure, the
suppression reason, or the recipients.
3. **`GET /emails/{id}/webhook-deliveries`** and **`GET /webhooks/{id}/deliveries?limit=`**:
`{data:[{id, webhook_id, endpoint, event_id, type, email_id, status,
response_status, error, attempts, next_attempt_at, delivered_at, created_at}]}`,
newest first.
4. **`GET /emails?test=true|false`**: server-side live/test filter; the emails
screen sends it so a page is never short in test mode.
5. **`GET /suppressions?q=`**: case-insensitive substring search over address
and reason. The screen filters the loaded page instantly and only asks the
server when the account has more than one page (1000 rows).
6. **Magic-link failure for browsers**: a bad, used or expired link opened in a
browser (`Accept: text/html`) is redirected to the `redirect_to` the link was
issued with plus `#error=invalid_token`; the dashboard consumes the fragment
(`js/session.js`) and shows "That sign-in link expired or was already used."
on the login form. A token we never issued gets a plain HTML page (400). API
clients (`Accept: application/json`) still get the JSON 400.
Still optional: `POST /auth/refresh` (or a longer `AUTH_JWT_TTL_SECS`) so an
open tab does not get signed out after an hour.
## Files
```
dashboard/
index.html shell + trailing-slash fix
config.js apiBase, project slug, docs link (plain script, swap per deploy)
styles.css one file, black/white, system fonts
js/app.js bootstrap, sidebar, account picker, live/test switch, hash router
js/api.js fetch wrapper + every endpoint the UI uses
js/session.js JWT storage, login fragment consumption, account + mode persistence
js/router.js, js/ui.js, js/snippets.js
js/views/*.js login, start (onboarding), emails, domains, keys, webhooks, suppressions, team, usage
mock/mock_server.py stdlib mock of the documented endpoints (dev only)
```