feat(GRO-2359): add POST /api/portal/clients-from-auth for OOBE #212

Merged
Flea Flicker merged 1 commits from feature/2357-p2-portal-clients-from-auth into dev 2026-06-11 16:34:34 +00:00
Member

GRO-2359 — companion API change for the OOBE portal-creation flow

The OOBE component (web src/portal/OOBE.tsx) calls POST /api/portal/clients-from-auth to create a fresh clients row bound to the Better Auth user's email. This PR adds that endpoint.

Contract

  • POST /api/portal/clients-from-auth
  • Auth: Better Auth session (uses auth.api.getSession({ headers }) — registered BEFORE the validatePortalSession middleware, same pattern as the existing POST /session-from-auth)
  • Body: { name: string; phone?: string|null; address?: string|null; notes?: string|null } (zod-validated)
  • 201 → { id, name, email }
  • 400 → invalid body
  • 401 → no Better Auth session
  • 409 → a clients row already exists for this email (portal-selection case)
  • 503 → auth not configured
  • 500 → unexpected insert failure

What changed

  • src/routes/portal.ts — new POST /clients-from-auth handler (registered before the validatePortalSession middleware, alongside session-from-auth).
  • src/__tests__/portalClientsFromAuth.test.ts — 7 new tests covering 401, 400, 201 (with persisted values, name trim, optional-field normalisation to null), 409 (existing client), 409 (unique-constraint race via 23505), 503.

Pairing

Companion to the web PR on groombook/web branch feature/2357-p2-sso-to-oobe-routing (PR #75). The web PR is the user-facing fix; this API PR is the necessary backend addition. Both ship in the same release per GRO-2357.

SDLC path

feature/2357-p2-portal-clients-from-authdevuatmain. Tests pass locally (pnpm vitest run src/__tests__/portalClientsFromAuth.test.ts = 7/7 ).

## GRO-2359 — companion API change for the OOBE portal-creation flow The OOBE component (web `src/portal/OOBE.tsx`) calls `POST /api/portal/clients-from-auth` to create a fresh `clients` row bound to the Better Auth user's email. This PR adds that endpoint. ### Contract - `POST /api/portal/clients-from-auth` - Auth: Better Auth session (uses `auth.api.getSession({ headers })` — registered BEFORE the `validatePortalSession` middleware, same pattern as the existing `POST /session-from-auth`) - Body: `{ name: string; phone?: string|null; address?: string|null; notes?: string|null }` (zod-validated) - 201 → `{ id, name, email }` - 400 → invalid body - 401 → no Better Auth session - 409 → a `clients` row already exists for this email (portal-selection case) - 503 → auth not configured - 500 → unexpected insert failure ### What changed - `src/routes/portal.ts` — new `POST /clients-from-auth` handler (registered before the `validatePortalSession` middleware, alongside `session-from-auth`). - `src/__tests__/portalClientsFromAuth.test.ts` — 7 new tests covering 401, 400, 201 (with persisted values, name trim, optional-field normalisation to null), 409 (existing client), 409 (unique-constraint race via 23505), 503. ### Pairing Companion to the web PR on `groombook/web` branch `feature/2357-p2-sso-to-oobe-routing` (PR #75). The web PR is the user-facing fix; this API PR is the necessary backend addition. Both ship in the same release per GRO-2357. ### SDLC path `feature/2357-p2-portal-clients-from-auth` → `dev` → `uat` → `main`. Tests pass locally (`pnpm vitest run src/__tests__/portalClientsFromAuth.test.ts` = 7/7 ✅).
Flea Flicker added 1 commit 2026-06-11 16:31:32 +00:00
feat(GRO-2359): add POST /api/portal/clients-from-auth for OOBE (web)
CI / Test (pull_request) Successful in 29s
CI / Lint & Typecheck (pull_request) Successful in 41s
CI / Build & Push Docker Images (pull_request) Successful in 1m40s
cdeebec021
The OOBE flow on the web portal calls this endpoint to create a fresh
`clients` row bound to the Better Auth user's email when the SSO
bridge returns 404. Returns 201 on success, 409 if a client with that
email already exists (portal-selection case), 401/503 on auth issues,
400 on invalid body.

The OOBE success path navigates the user back to `/` and lets the
existing `session-from-auth` re-bridge; the new client is now
resolvable by email, so the bridge mints a real portal session.

Tests cover: 401 (no session), 400 (zod), 201 + persisted values
(name trimmed, optional fields normalized to null), 409 (existing
client or unique-constraint race), 503 (auth not configured).

Paired with the web PR on `feature/2357-p2-sso-to-oobe-routing`.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
Flea Flicker merged commit 10b78d810d into dev 2026-06-11 16:34:34 +00:00
Flea Flicker deleted branch feature/2357-p2-portal-clients-from-auth 2026-06-11 16:34:35 +00:00
Sign in to join this conversation.