feat(catalogs): PATCH /api/catalogs/{catalogId} renames a catalog (chat#1942)

Merged
SW
4mo ago
+385-0

Row 3 of chat#1942. Implements the contract in docs#292, which merges first.

Why

app/api/catalogs/route.ts exported only OPTIONS and POST. Nothing wrote catalogs.name after creation, and re-creating is not a workaround: createCatalogHandler L70-L82 is idempotent on a claimed snapshot — it returns the existing catalog and ignores the supplied name. So the 82 production catalogs called "Valuation Catalog" had no path to a real name. This is that path.

Change

FileRole
lib/supabase/catalogs/updateCatalog.tsupdate + return the row; null when no row has that id. updated_at is left to the table's set_updated_at trigger.
lib/catalog/validateUpdateCatalogRequest.tsauth first (401 precedes body handling, matching the catalog validator family), then catalogId uuid + { name }.
lib/catalog/updateCatalogHandler.tsvisibility, then the rename.
app/api/catalogs/[catalogId]/route.tsOPTIONS + PATCH.

Authorization is the catalog read path's, verbatim — getCatalogOwnerIds(accountId) → selectAccountCatalog({ accountIds, catalogId }), exactly what getCatalogMeasurementsHandler does. So a member of the owning organization can rename an org-owned catalog (chat#1938), and a catalog the caller cannot see is a 404, never a 403 — a deviation from the issue's original acceptance criteria, argued in docs#292 and recorded as a dated decision on the issue: a 403 would confirm the existence of a catalog you cannot see.

Two small judgment calls worth a reviewer's eye:

  • A whitespace-only name is a 400, not a stored value. A catalog named " " is as unidentifiable as the generic default this endpoint exists to replace.
  • name is the only mutable field. Membership stays with the catalog-songs endpoints; there is no reason for a rename to be able to move songs.

Tests — RED first

Both suites failed to resolve their module before the implementation existed:

FAIL lib/catalog/__tests__/updateCatalogHandler.test.ts
  Error: Cannot find module '../updateCatalogHandler'
FAIL lib/catalog/__tests__/validateUpdateCatalogRequest.test.ts
  Error: Cannot find module '../validateUpdateCatalogRequest'
Test Files  2 failed (2)

GREEN after, 11 new tests covering: validator short-circuit (no DB touched), owner rename, org-member rename, 404 for an invisible catalog (and updateCatalog never called), 404 when the row disappears between check and update, 500 on throw; plus 401-before-body, non-uuid catalogId, missing name, whitespace-only name, and name trimming.

Whole domain green alongside:

pnpm exec vitest run lib/catalog lib/supabase
Test Files  62 passed (62)
     Tests  291 passed (291)

tsc --noEmit: 202 pre-existing errors, unchanged, none in lib/catalog, lib/supabase, or app/api/catalogs. eslint + prettier --check clean on all six files.

One fixture note for anyone copying these tests: zod's .uuid() rejects aaaaaaaa-bbbb-cccc-dddd-… (invalid version nibble), which silently made a body-validation test pass for the wrong reason. Fixtures are now v4-shaped.

Verification plan

Preview-verified once the deployment builds: rename a real generic catalog and re-read it via GET /api/accounts/{id}/catalogs; 404 an unknown id; 404 a catalog owned by another account; 400 a non-uuid and an empty name; 401 with no key. Results posted here.

Merge order

api#820 (naming fix) → docs#292 (contract) → this → the DELETE pair.

🤖 Generated with Claude Code


Summary by cubic

Adds PATCH /api/catalogs/{catalogId} to rename a catalog, fixing the inability to change generic names. Implements the contract in docs#292 and aligns with chat#1942 by using read-path visibility (hidden catalogs return 404).

  • New Features
    • Adds OPTIONS and PATCH /api/catalogs/{catalogId}; body expects { name }.
    • Auth runs first (Privy bearer or x-api-key); org members can rename; non-visible catalogs return 404.
    • Validation: catalogId must be a UUID; trims name; rejects whitespace-only names with 400.
    • Only name is mutable; updates via updateCatalog; 404 if the row disappears; 500 on update errors.
    • Tests cover validator short-circuit, owner/org rename, 404 visibility, and error cases; also asserts the full 400 error envelope for the missing-name case.