veza/veza-backend-api/internal/api
senke 921889840f feat(marketplace): multi-creator royalty splits with audit ledger
v1.0.10 légal item 4. Marketplace products can now have a per-recipient
payout structure ; each purchase fans out the net (post-platform-fee)
amount across the recipients per their basis_points share. Audit ledger
captures every change for legal-evidence purposes.

Without this, a co-produced track gets paid to the registered seller
only and the contributors must chase reimbursement off-platform =
contentieux risk. F250 in the ORIGIN spec called this out as a v2.0.0
blocker ; this commit closes the gap.

Schema (migrations/992_royalty_splits.sql)
  * royalty_splits        : (product_id, recipient_user_id, basis_points, role_label).
                            UNIQUE on (product_id, recipient_user_id).
                            CHECK : basis_points in (0, 10000]. Sum-to-10000
                            invariant lives in the service layer (cross-row).
  * royalty_splits_audit  : append-only history. action ∈ {set, replace,
                            remove}. previous_splits + new_splits as
                            JSONB snapshots. Never deleted.
  ON DELETE :
    products  → CASCADE   (a deleted product takes its splits with it)
    users     → RESTRICT  (a recipient must be removed from splits before
                            their account can be deleted ; preserves payment
                            history coherence)

Service (internal/core/marketplace/royalty_splits.go)
  * GetRoyaltySplits(productID)                — public read.
  * SetRoyaltySplits(actor, productID, inputs, reason)
      Validations : seller-owned, sum == 10000 bps, no duplicate
      recipients, all recipients exist, each bp in (0, 10000].
      Single transaction : delete old rows + bulk insert new + audit
      entry. action='set' on first write, 'replace' afterwards.
  * RemoveRoyaltySplits(actor, productID, reason)
      Idempotent. action='remove'. Reverts the product to single-seller
      payout on the next purchase.
  * distributePerProductSplits(productID) → recipient → bps map. Used
    by processSellerTransfers ; nil result triggers the legacy path.
  Sentinel errors :
      ErrSplitsForbidden / ErrSplitsSumInvalid / ErrSplitsRecipientDup /
      ErrSplitsRecipientNF / ErrSplitsBPRange.

Hook (service.go::processSellerTransfers)
  Per-item resolution : if the product has splits, fan the net out
  across recipients (rounding remainder absorbed by the dominant
  recipient so the total stays exact) ; otherwise the legacy
  single-seller path runs. SellerTransfer rows still get one per
  recipient, with the originating seller's commission rate carried
  through for audit. Mixed orders (some products with splits, some
  without) are handled correctly.

Handler (internal/handlers/royalty_splits_handler.go)
  * GET    /api/v1/marketplace/products/:id/royalty-splits   public
  * PUT    /api/v1/marketplace/products/:id/royalty-splits   seller-only
  * DELETE /api/v1/marketplace/products/:id/royalty-splits   seller-only
  Error mapping : sentinel → AppError code so the SPA can render the
  right toast without parsing messages. Both PUT and DELETE go through
  the existing RequireOwnershipOrAdmin middleware (defense in depth ;
  service layer also checks).

What v1.0.10 leaves to v2.1
  * UI for managing splits (product editor) — backend-complete here ;
    UI follows. Operators can already configure splits via the API.
  * Dispute workflow (third-party arbitration when a recipient
    contests their share). For v2.0.0 the legal coverage is "splits
    are visible publicly, audit log is append-only, contentieux goes
    through legal channels with the audit log as evidence."
  * Tax allocation (each recipient may be in a different tax
    jurisdiction). Splits today distribute the gross-minus-fee evenly
    by share ; per-jurisdiction tax math comes later.

Tests pass : go test ./internal/core/marketplace ./internal/handlers
              -short → ok.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 20:53:22 +02:00
..
admin chore: consolidate CI, E2E, backend and frontend updates 2026-02-17 16:43:21 +01:00
chat adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
collaboration adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
grpc adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
listing adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
message adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
offer adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
room adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
search adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
shared_resources adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
tag adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
track adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
user fix(v0.12.6): apply all pentest remediations — 36 findings across 36 files 2026-03-14 00:44:46 +01:00
websocket adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
router.go feat(embed): /embed/track/:id widget + /oembed envelope + per-track OG tags (W3 Day 15) 2026-04-28 15:49:54 +02:00
routes_admin_platform.go feat(v0.12.6.2): enforce MFA for admin/moderator + align refresh token TTL to 7 days 2026-03-12 06:53:27 +01:00
routes_analytics.go style(backend): gofmt -w on 85 files (whitespace only) 2026-04-14 12:22:14 +02:00
routes_auth.go feat(v0.13.3): complete - Polish Sécurité Avancée 2026-03-13 10:09:01 +01:00
routes_cloud.go feat(cloud): file versioning, restore, and sharing 2026-02-25 13:33:08 +01:00
routes_co_listening.go feat(v0.10.7): Collaboration Temps Réel F481-F483 2026-03-10 13:34:16 +01:00
routes_core.go feat(cdn): Bunny.net signed URLs + HLS cache headers + metric collision fix (W3 Day 13) 2026-04-28 14:07:20 +02:00
routes_developer.go feat(developer): add API keys backend (Lot C) 2026-02-20 00:18:36 +01:00
routes_discover.go feat(v0.10.4): Playlists collaboratives - F136, F140, F141, F143, F145 2026-03-09 16:49:05 +01:00
routes_distribution.go feat: backend, stream server & infra improvements 2026-03-18 11:36:06 +01:00
routes_education.go feat(v0.12.3): F276-F305 video upload, HLS transcoding, education tests 2026-03-11 19:20:48 +01:00
routes_embed.go feat(embed): /embed/track/:id widget + /oembed envelope + per-track OG tags (W3 Day 15) 2026-04-28 15:49:54 +02:00
routes_feed.go feat(v0.10.1): Tags & Genres discover - F351-F355 2026-03-09 01:52:56 +01:00
routes_gear.go feat(v0.802): frontend Cloud/Gear, MSW, docs, scope v0.803, archive 2026-02-25 14:00:58 +01:00
routes_legal.go feat(legal): versioned terms acceptance ledger (CGU/CGV/mentions) 2026-05-01 20:47:07 +02:00
routes_live.go feat(backend,web): surface RTMP ingest health on the Go Live page 2026-04-16 23:52:36 +02:00
routes_marketplace.go feat(marketplace): multi-creator royalty splits with audit ledger 2026-05-01 20:53:22 +02:00
routes_moderation.go feat(v0.12.6.2): enforce MFA for admin/moderator + align refresh token TTL to 7 days 2026-03-12 06:53:27 +01:00
routes_playlists.go feat(v0.10.4): Playlists collaboratives - F136, F140, F141, F143, F145 2026-03-09 16:49:05 +01:00
routes_queue.go feat(queue): add queue session API (create, get, delete, add/remove items) 2026-02-20 18:41:12 +01:00
routes_search.go feat(v0.10.2): Recherche fulltext Elasticsearch - F361-F365 2026-03-09 10:13:18 +01:00
routes_social.go feat(groups): S2 frontend - request join, invite, roles, my groups, MSW handlers 2026-02-21 05:51:29 +01:00
routes_subscription.go feat(subscription): recovery endpoint + distribution gate (v1.0.9 item G — Phase 3) 2026-04-27 11:33:40 +02:00
routes_tag.go feat(upload): tags auto-suggest endpoint and additional audio formats 2026-02-25 13:39:59 +01:00
routes_tracks.go feat(cdn): Bunny.net signed URLs + HLS cache headers + metric collision fix (W3 Day 13) 2026-04-28 14:07:20 +02:00
routes_users.go feat(search): faceted filters (genre/key/BPM/year) + FacetSidebar UI (W4 Day 18) 2026-04-29 10:33:35 +02:00
routes_webhooks.go feat(subscription): webhook handler closes pending_payment state machine (v1.0.9 item G — Phase 2) 2026-04-27 05:39:59 +02:00
routes_webhooks_test.go v0.9.4 2026-03-05 23:03:43 +01:00
versioning.go api-versioning: add X-API-Deprecated header and frontend deprecation warning 2026-01-15 16:56:21 +01:00
versioning_test.go api-versioning: add X-API-Deprecated header and frontend deprecation warning 2026-01-15 16:56:21 +01:00