feat(catalogs): DELETE /api/catalogs/{catalogId} keeps the paid measurement (chat#1942)
Row 5 (last) of chat#1942. Implements the contract in docs#293, which merges first.
Stacked on api#821 (branch feat/catalog-rename), which creates app/api/catalogs/[catalogId]/route.ts. Both target main; merge #821 first and this diff reduces to the DELETE alone.
Why
A duplicate catalog was permanent. It is also easy to create by accident: a client timeout on the ~15s valuation call that has already succeeded server-side produces a second identical catalog on retry — which is exactly how the duplicate that prompted the issue appeared.
The one thing this must not do
Cascade into the measurement. The schema already draws that line, so the handler leans on it instead of reimplementing cascade logic in application code:
| Table | FK | Result |
|---|---|---|
catalog_songs | ON DELETE CASCADE | removed |
account_catalogs | ON DELETE CASCADE | removed |
catalog_valuations | ON DELETE CASCADE (database#52) | removed — derived, recomputable from the snapshot |
playcount_snapshots | ON DELETE SET NULL | survives, reference cleared |
A catalog is a label over songs, rebuildable from its snapshot in one call. A snapshot is a metered capture that spent the customer's credits and cannot be re-taken for a past date. Clearing the reference also re-opens the idempotent re-claim path in createCatalogHandler, which is the recovery story for the 82 generic catalogs: delete the duplicate, re-claim the released snapshot with the name you wanted.
That is why the handler reads the snapshot ids before the delete clears them and returns them as released_snapshot_ids — otherwise the recovery handle disappears with the row. A test pins that ordering.
Change
| File | Role |
|---|---|
lib/supabase/playcount_snapshots/selectPlaycountSnapshotIdsByCatalog.ts | returns null on error. selectPlaycountSnapshots swallows errors as [], which would report a recoverable snapshot as none. |
lib/supabase/catalogs/deleteCatalogById.ts | delete + return the id (null when the row is already gone). |
lib/catalog/validateDeleteCatalogRequest.ts | auth, then catalogId uuid. No body. |
lib/catalog/deleteCatalogHandler.ts | visibility → released ids → delete. |
app/api/catalogs/[catalogId]/route.ts | DELETE export. |
Fails closed, mirroring deleteArtist's treatment of an unknown dependency state: a failed snapshot lookup is a 500 and nothing is deleted, because deleting without knowing what was released would strand the paid-for measurement. Ownership fails closed too — selectAccountCatalog returns null on error, which is a 404.
Same authorization as the rename: getCatalogOwnerIds + selectAccountCatalog, so an org member can delete an org-owned catalog, and a catalog the caller cannot see is a 404, never a 403 (dated decision on the issue).
Tests — RED first
FAIL lib/catalog/__tests__/deleteCatalogHandler.test.ts
Error: Cannot find module '../deleteCatalogHandler'
FAIL lib/catalog/__tests__/validateDeleteCatalogRequest.test.ts
Error: Cannot find module '../validateDeleteCatalogRequest'
Test Files 2 failed (2)
GREEN after, 11 new tests: validator short-circuit; 404 for an invisible catalog with no delete attempted; delete + released_snapshot_ids; snapshot ids read before the delete (invocation order); org-member delete; fail-closed 500 on snapshot lookup error, nothing deleted; 404 when the row is already gone; 500 on throw; plus 401, non-uuid, and the validated shape.
pnpm exec vitest run lib/catalog lib/supabase
Test Files 64 passed (64)
Tests 302 passed (302)
tsc --noEmit: 202 pre-existing errors, unchanged, none in the touched paths. eslint + prettier --check clean on all seven files.
Verification plan
Preview-verified once the deployment builds, on a throwaway catalog created for the purpose — never a customer's: delete it, then confirm (a) it is gone from GET /api/accounts/{id}/catalogs, (b) its catalog_songs are gone, (c) the playcount_snapshots row still exists with catalog NULL, and (d) re-claiming that snapshot via POST /api/catalogs { snapshot, name } yields a fresh, correctly named catalog. Plus 404 on an unknown id, 404 on another account's catalog, 400 on a non-uuid, 401 without a key. Results posted here.
🤖 Generated with Claude Code
Summary by cubic
Adds DELETE /api/catalogs/{catalogId} to remove a catalog while keeping the paid measurement. Keeps snapshot rows and returns released_snapshot_ids so the run can be re-claimed (addresses chat#1942).
- New Features
- Deletes the catalog; cascades
catalog_songs,account_catalogs,catalog_valuations; leavesplaycount_snapshotsby settingcatalogto NULL and returnsreleased_snapshot_ids. - Reads snapshot ids before deletion using
selectPlaycountSnapshots({ catalog })and maps to ids. - Auth + UUID validation; org members can delete; invisible or unknown catalogs return 404 (never 403).
- Adds
validateDeleteCatalogRequest,deleteCatalogHandler; usesgetCatalogOwnerIds+selectAccountCatalogfor visibility anddeleteCatalogById; API route exports the DELETE handler.
- Deletes the catalog; cascades