feat(catalogs): the catalogs list returns a valuation and an owner (chat#1943)
Rows 2 of chat#1943. Implements the contract in docs#294, which merges first. The chat PR that renders these fields follows.
Why
GET /api/accounts/{id}/catalogs returned id, name, created_at, updated_at — so a card on /catalogs had nothing to show but a name. The value already existed one click away on the report page, and account_catalogs already knew whether a catalog was yours or an organization's; neither reached the list.
Valuation — batched, and identical to the report
lib/catalog/getCatalogValuations.ts uses the same computeValuationBand model as GET /api/catalogs/{id}/measurements, so a card and the report it opens cannot disagree. The naive implementation — getCatalogEarliestReleaseDate per catalog — is a snapshots query plus a Spotify round trip each, which for a 43-catalog account is 43 sequential Spotify calls. Instead:
| Work | Cost |
|---|---|
| Album ids for every catalog | 1 snapshots query (selectPlaycountSnapshots({ catalogs })) |
| Release dates | 1 getAlbums call over the deduped union (it already chunks 20 ids per request) |
| Stream aggregate | 1 RPC per catalog, run concurrently — get_catalog_measurements_aggregate has no batched form |
So a page of N catalogs costs 2 + N calls instead of 3N, and the only part that scales is the one that can't be batched without a database function. Worth a follow-up if N grows.
valuation is null, never $0, when a catalog has no measured songs — that is 34 of the 70 catalogs on prod. Best-effort on the age input exactly like the single-catalog path: no snapshot, no album ids, or an unavailable Spotify falls back to the model's default age rather than dropping the number.
Owner — three batched reads, organization wins
lib/catalog/resolveCatalogOwners.ts. When a catalog is owned both directly and through an organization, the organization is reported: showing a member their own avatar on a catalog the whole org can edit is the misleading half of the truth. On prod that tie-break fires for 2 catalogs today.
is_organization is derived from the caller's own organization set (chat#1938), not a column — there is no account type. name and image stay nullable because account_info.image genuinely is null for some accounts (the Recoup organization's is), and the client is expected to fall back to initials.
New files, and one that isn't
lib/supabase/account_catalogs/selectCatalogOwnerLinks.ts— catalogs → owners, the inverse of the existing owners → catalogs selector.lib/supabase/account_info/selectAccountInfos.ts— plural sibling ofselectAccountInfo, which is single-account and would mean a query per row.selectPlaycountSnapshotsgained an optionalcatalogsfilter instead of a new selector — the same call the api#822 review asked for, applied before review this time.selectAccountCatalogsis deliberately unchanged:authorizeCatalogAccess, the MCPget_catalogstool and an eval all call it and none of them want this payload.
Tests — RED first
FAIL lib/catalog/__tests__/getCatalogValuations.test.ts
Error: Cannot find module '../getCatalogValuations'
FAIL lib/catalog/__tests__/resolveCatalogOwners.test.ts
Error: Cannot find module '../resolveCatalogOwners'
and on the handler, 4 of 6 red before the change (returns each catalog with its valuation and owner, the null-valuation case, the organization-ids case, the empty-account case).
GREEN after — 13 new tests including "asks Spotify once for the union of album ids, not once per catalog" and "uses each catalog's own earliest release date" (asserting two catalogs get different bands from the same stream count), plus the org tie-break and the null-image case.
pnpm exec vitest run lib/catalog lib/supabase lib/songs lib/mcp
Test Files 90 passed (90)
Tests 462 passed (462)
tsc --noEmit: 202 pre-existing errors, unchanged, none in the touched paths. eslint + prettier clean.
Verification plan
Preview-verified once it builds: GET /api/accounts/{id}/catalogs for an account with both personal and org-owned catalogs — every item carries valuation, measured_song_count and owner; the band for a measured catalog matches GET /api/catalogs/{id}/measurements for the same catalog to the cent; an unmeasured catalog reports null; an org-owned catalog reports is_organization: true. Results posted here.
🤖 Generated with Claude Code
Summary by cubic
Adds valuation bands and owners to GET /api/accounts/{id}/catalogs so cards show value and clearly indicate personal vs organization ownership, per chat#1943. Keeps the list fast with batched snapshots, concurrent Spotify album fetches, and a retry on aggregates.
-
New Features
- Response includes
measured_song_count,valuation(band), andownerper catalog. - Valuation uses the same
computeValuationBandasGET /api/catalogs/{id}/measurements; returnsnullwhen nothing is measured. Spotify album ids are deduped and fetched in parallel batches (20 per request, up to 8 in flight). - Snapshot reads are batched across catalogs (50 ids per query);
selectPlaycountSnapshotssupports a multi-catalog filter.selectAccountCatalogsnow returns each catalog once with authorized owner links inownersand can narrow by{ catalogIds }. Owner resolution prefers the organization when both exist and only considers the caller’s owner set;is_organizationis derived andname/imagecan benull. API andmcptool responses build payloads field-by-field to avoid leaking the internalownerslist.
- Response includes
-
Bug Fixes
- Retries the measurements aggregate once before treating a catalog as unmeasured; logs and falls back to
nullonly after two failures.
- Retries the measurements aggregate once before treating a catalog as unmeasured; logs and falls back to
Summary by CodeRabbit
-
New Features
- Catalog listings now include measured song counts, valuation bands, and resolved ownership details.
- Added earliest release-date information for catalogs when available.
- Catalog selection now supports filtering across multiple catalogs.
- Catalog tools return streamlined catalog metadata without internal ownership details.
- Catalog owners display names, images, and organization status when available.
-
Bug Fixes
- Improved reliability when retrieving catalog measurement data through automatic retry handling.
- Added safer handling for missing valuation, owner, account, and release information.