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

Merged
SW
4mo ago
+178-14

Implements item 2 of recoupable/chat#1938.

Merge order: docs#289 → this PR. And the read half (docs#288 → api#815) should land first — creating an org-owned catalog is useless until the read path can surface it.

Problem

Both creation paths hardcoded the calling account as owner — createCatalogHandler and createSnapshotCatalog each called insertAccountCatalog({ account: accountId, … }) where accountId came from credentials. There was no way to create a catalog that an organization's members could all see, and no endpoint anywhere to link an existing catalog to another account.

Change

Optional organization_id on createCatalogBodySchema.

This is not an account_id override. The caller is still resolved from credentials; organization_id only selects an owner, and it is authorized rather than trusted — validateAuthContext(request, { organizationId }) runs the existing validateOrganizationAccess and 403s a caller who is not a member. Absent, behaviour is byte-identical to today.

createSnapshotCatalog gains an optional ownerId, which splits two things that were previously the same value:

Goes to
account_catalogs link (who owns the catalog)ownerId ?? accountId — the organization
attachCanonicalArtistToAccount (roster)always accountId — whoever ran the claim

That split is deliberate and documented in docs#289: the catalog belongs to the org, but the artist belongs on the roster of the person who ran it.

Tests — TDD, RED before GREEN

Three units, each RED first:

UnitREDGREEN
validateCreateCatalogBody3 failed / 7 passed10/10
createSnapshotCatalog1 failed / 8 passed9/9
createCatalogHandler4 failed / 10 passed14/14

New coverage: organization_id accepted with name and with snapshot; non-uuid rejected 400; organization_id alone still 400 (name or snapshot still required); passed through to validateAuthContext; name-only and snapshot paths both link the org; the 403 propagates and nothing is written; and with organization_id absent the owner is still the caller.

One existing assertion was updated rather than broken: createSnapshotCatalog is now called with ownerId, and the received value equalled accountId — the correct default — so the assertion now pins that explicitly.

Full suites green: lib/catalog, lib/auth, lib/organizations, lib/supabase/account_catalogs — 31 files, 215 tests. eslint and prettier clean.

One out-of-scope fix, called out

createCatalogHandler.test.ts had a pre-existing tsc error — a createSnapshotCatalog mock missing attachedArtistId. Verified byte-identical on a clean origin/main checkout before touching it. Fixed here because this PR already edits that file; tsc --noEmit is now clean for lib/catalog.

Scope

Org catalogs are a new concept — no existing catalog changes. On prod 0 of 117 account_catalogs rows are org-owned, and nothing here migrates or re-links any of them.

No schema change. An organization is a row in accounts, so account_catalogs.account already accepts an organization id.

Preview verification to follow on this PR once the deployment is up.


Summary by cubic

POST /api/catalogs now accepts an optional organization_id to create org-owned catalogs that all members can see (chat#1938). The caller is still resolved from credentials; org membership is authorized; behavior is unchanged when organization_id is omitted.

  • New Features

    • Accepts organization_id (UUID) with name or snapshot (one required); rejects non-UUID with 400.
    • Authorizes membership via validateAuthContext(request, { organizationId }); returns 403 for non-members.
    • Links ownership to the org in account_catalogs for both name-only and snapshot paths; roster attach still targets the caller; createSnapshotCatalog now accepts optional ownerId.
    • No schema changes; backward compatible. Org-owned catalogs will appear in reads once the read path supports organization owners.
  • Bug Fixes

    • Fixed a pre-existing TypeScript error in createCatalogHandler.test.ts (missing attachedArtistId in mock).