feat(valuation): POST /api/valuation accepts an organization_id owner (chat#1938)

Merged
SW
4mo ago
+95-6

Resolves the open design question on recoupable/chat#1938 — decision recorded on the issue 2026-08-05.

Stacked PR. Base is feat/org-owned-catalogs-create (api#816), not main — this depends on the ownerId parameter added to createSnapshotCatalog there. Review the diff on top of that branch; it retargets to main automatically once api#816 merges.

Merge order: docs#288 → api#815 → docs#289 → api#816 → docs#290 → this PR.

Why

chat#1938 asked whether POST /api/valuation should take organization_id too. Yes — a valuation run is how catalogs actually get created, so shipping org ownership on POST /api/catalogs alone would leave every valuation-created catalog account-owned and reproduce the exact bug one layer up.

The related work on this endpoint was tracked on chat#1937, closed 2026-08-05 with its valuation items deliberately dropped — nothing else is picking this up.

Change

Optional organization_id on runValuationBodySchema, passed to createSnapshotCatalog as ownerId. The ownership boundary, as documented in docs#290:

Account
Owns the resulting catalogthe organization
Spends the creditsthe calling account
Gets the searched artist on its rosterthe calling account

A sequencing detail worth reviewing

The house pattern is validateAuthContext(request, { organizationId: body.organization_id }). I did not use it here, deliberately.

validateRunValuationRequest authenticates before reading the body, and there is an existing test asserting exactly that — "returns the auth response (401) when auth fails, before touching the body". Passing organizationId into validateAuthContext requires parsing the body first, which would flip the 401/400 precedence and break that contract.

So membership is checked with validateOrganizationAccess after the body parses. That is the same function validateAuthContext delegates to, so the authorization is identical — only the ordering differs, and the reason is in the JSDoc.

Tests — TDD, RED before GREEN

validateRunValuationRequest: RED 3 failed / 5 passed → GREEN 8/8.

TestAsserts
memberreturns organizationId, calls validateOrganizationAccess with the right pair
non-member403
non-uuid organization_id400, and membership is never checked
absentorganizationId undefined, membership never checked — unchanged behaviour

Existing precedence tests still pass untouched, including the 401-before-body one.

Full suites green across the stack: lib/valuation, lib/catalog, lib/auth, lib/organizations, lib/supabase/account_catalogs — 41 files, 260 tests. tsc --noEmit clean; eslint and prettier clean.

Scope

Org catalogs are a new concept; no existing catalog changes, and omitting organization_id is byte-identical to today. No schema change — an organization is a row in accounts, so account_catalogs.account already accepts an organization id.

Preview verification to follow once the deployment is up.


Summary by cubic

POST /api/valuation now accepts an optional organization_id to create the resulting catalog under that organization. Credits and the roster attach still apply to the calling account (chat#1938).

  • New Features
    • Accept organization_id in the body; passed as ownerId to createSnapshotCatalog so the catalog is owned by that organization. Absent, behavior is unchanged.
    • Auth: membership is checked with validateOrganizationAccess after parsing the body to preserve 401-before-body; returns 403 for non-members and 400 for an invalid UUID.

Summary by CodeRabbit

  • New Features
    • Valuation requests can optionally specify an organization for catalog ownership.
    • Organization identifiers are validated before processing.
  • Bug Fixes
    • Unauthorized organization access is now rejected with a clear 403 response.
    • The requesting account remains responsible for attaching the resulting catalog to its roster.