feat(artists): POST /api/artists resolve-or-create by spotify_artist_id (chat#1889 row 8)

Merged
SW
4mo ago
+258-27

Row 8 of the canonical-artist trilogy on chat#1889 — dedup at creation. Contract: docs#278. Merge order: docs#278 → this → chat#TBD.

Why

The chat add path (POST /api/artists name-only, then a client PATCH for the social) creates a new artist row per signup, so multiple rows exist per Spotify id and every canonical lookup downstream is ambiguous — the root cause of the roster doubles. api#791 deduped the valuation fallback (necessary, insufficient); api#792 tried account-scoped preference and was closed unmerged on the 2026-07-29 decision: canonical artists exist across all accounts; a canonical lookup should not need an accountId. Creation is the only place dedup can be enforced.

What changed

  • validateCreateArtistBody: optional spotify_artist_id (min 1), threaded as spotifyArtistId.
  • New resolveOrCreateArtist (SRP): canonical exists → insertAccountArtistId (skip if rostered) + return it, created: false; else createArtistInDb + attach the SPOTIFY social server-side (non-fatal, same contract as chat#1892's guard), created: true.
  • createArtistPostHandler: 200 on reuse, 201 on create; response shape { artist } unchanged.

Verification

TDD red → green, three units (validator 2 new tests, resolver 5, handler rewritten + 2 new — all RED first: two files failed collection, validator failed assertions). Full api suite 786 files / 4262 tests pass; tsc at the 236 baseline; eslint clean.

Preview verification to follow on this PR (200-reuse against a known canonical, 201-create for a fresh artist, cross-account link assertion via SQL).

Reviewer notes

  • Org linking on the reuse path links the account only — an existing canonical is not added to artist_organization_ids when organization_id is passed alongside a resolved spotify_artist_id. Flagging rather than silently deciding; happy to add if you want org parity.
  • Until the row-9 cleanup migration lands, the canonical picked among existing duplicates is newest-first and can drift (row 10 makes it deterministic). New adds stop creating duplicates as of this PR regardless.

Tracked in chat#1889 (matrix row 8).


Summary by cubic

POST /api/artists now resolves or creates by spotify_artist_id to prevent duplicate artist rows and keep one canonical per Spotify id. If a canonical exists it’s linked to the account (200); otherwise a new artist is created and enriched with the SPOTIFY social (best‑effort) (201).

  • New Features
    • Accepts optional spotify_artist_id (must be non-empty).
    • When a canonical exists, link it via account_artist_ids and skip if already rostered.
    • On create, attach the SPOTIFY URL server-side; failures don’t block creation.
    • Response shape is unchanged: { artist }.

Summary by CodeRabbit

  • New Features

    • Artist creation now accepts an optional Spotify artist ID.
    • Existing canonical artists are reused and linked instead of creating duplicates.
    • Spotify information is attached automatically when available.
  • Bug Fixes

    • Non-critical Spotify information failures no longer prevent artist creation.
    • Responses now distinguish between newly created artists and existing artists that were linked.