fix(catalog): link the caller's account on catalog re-claim (chat#1867)
Prerequisite for the post-valuation catalog report (recoupable/chat#1867, the row above chat#1873 in that tracker). Fixes the confirmed onboarding break where a real signup runs a valuation, clicks "Get the full report", and the report renders "No valuation found for this catalog."
Root cause
GET /api/catalogs/{id}/measurements returns 404 only when the caller has no account_catalogs row for the catalog (lib/catalog/getCatalogMeasurementsHandler.ts — missing measurement data returns 200-with-zeros, not 404). The measurement rows themselves are persisted by the funnel snapshot workflow.
The gap: createCatalogHandler's re-claim branch (snapshot.catalog already set) returned the existing catalog without calling insertAccountCatalog. The fresh-claim path (createSnapshotCatalog) links the caller; the re-claim path did not. So whenever the reader's resolved account differs from the account that first claimed the snapshot — account divergence, or a prior double-account race (chat#1875) — the reader is never linked and the report 404s.
What changed
lib/catalog/createCatalogHandler.ts— the re-claim branch now callsinsertAccountCatalog({ account: accountId, catalog: existing.id })before returning, guaranteeing the caller owns the catalog (mirrors the fresh-claim path).lib/supabase/account_catalogs/insertAccountCatalog.ts—insert→ idempotentupsert(onConflict: "account,catalog", ignoreDuplicates: true, matching theaccount_catalogs_account_catalog_uniqueconstraint), so linking an already-linked account is a no-op instead of a unique-violation. Mirrors the siblinginsertCatalogSongsupsert pattern.
No API contract change (response shapes unchanged) → no docs PR needed.
TDD / verification
- RED first: added
lib/catalog/__tests__/createCatalogHandler.test.ts"re-claims link the caller's account…" (assertsinsertAccountCatalogis called on re-claim) and newlib/supabase/account_catalogs/__tests__/insertAccountCatalog.test.ts(idempotent upsert args + error path). Confirmed both fail against the old code. - GREEN: full api suite 4123 pass (754 files);
tsc --noEmitclean on touched files (repo has a large pre-existing tsc baseline in unrelated files; CI runspnpm test);eslint+prettierclean on touched files.
Preview verification — pending (honest)
Not yet exercised end-to-end on a preview. The clean way to verify: authenticated POST /api/catalogs { snapshot } re-claim → account_catalogs row exists for the caller → GET /api/catalogs/{id}/measurements returns 200 with rows (was 404). Also recommend a one-time DB spot-check that song_measurements rows exist for a stuck catalog (e.g. a0a4a769-…) to confirm the fix addresses the observed repro rather than a data gap.
Note on the chat report preview: chat previews read test-recoup-api, so verifying chat#1873's happy path against a chat preview needs this fix live on the api deployment that preview reads. Merging here lands it on prod api; getting it onto test-recoup-api is a separate step.
Part of recoupable/chat#1867 (does not close it).
🤖 Generated with Claude Code
Summary by cubic
Links the caller’s account when re-claiming a catalog so the measurements endpoint stops 404ing and the post-valuation report shows data instead of “No valuation found for this catalog.” Makes account–catalog linking idempotent to handle repeated claims safely.
-
Bug Fixes
- Re-claim path calls upsertAccountCatalog before returning to ensure the caller is linked.
- upsertAccountCatalog upserts with onConflict "account,catalog" and ignoreDuplicates: true.
- Added tests for re-claim linking and upsert error handling.
-
Refactors
- Renamed insertAccountCatalog → upsertAccountCatalog across code and tests; no behavior change.
Summary by CodeRabbit
- Bug Fixes
- Improved catalog snapshot re-claims so the current account is reliably linked to an existing catalog.
- Prevented duplicate catalog-account links from causing errors during repeated operations.
- Preserved existing catalog processing while correctly reporting when no new songs are added.