Hub 2.0 step 1: products foundation

- Product enum (events, accounts, website, app) on Licence and Install,
  Install.publicKey; existing rows are events
- One open licence per product per customer: partial unique index, with a
  migration guard that names customers with duplicates (npm run check:hub2
  lists them beforehand)
- Licence keys per product (CEH-, CCA-, CCW-, CCP-); free Accounts licence
  created with the first other product licence
- Protocol v1 registers Events licences only (409 WRONG_PRODUCT); only Events
  licences can be invoiced until billing per customer
- Cross Code Hub in the UI, emails and PDFs
- @crosscode/contracts 1.0.0 vendored (backend/vendor)
- Steps merge into release/2.0, not main (plan updated)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-04 15:28:06 +02:00
co-authored by Claude Opus 5.5
parent fe0f2638c6
commit ad602b2846
30 changed files with 544 additions and 49 deletions
@@ -0,0 +1,29 @@
-- CreateEnum
CREATE TYPE "Product" AS ENUM ('events', 'accounts', 'website', 'app');
-- AlterTable
ALTER TABLE "Install" ADD COLUMN "product" "Product" NOT NULL DEFAULT 'events',
ADD COLUMN "publicKey" TEXT;
-- AlterTable
ALTER TABLE "Licence" ADD COLUMN "product" "Product" NOT NULL DEFAULT 'events';
-- CreateIndex
CREATE INDEX "Licence_customerId_product_idx" ON "Licence"("customerId", "product");
-- One licence per product per customer that isn't revoked (Hub 2.0; a revoked licence can be
-- replaced). Prisma can't express a partial index, so it's only here. Every licence before 2.0 is
-- an Events licence: a customer with two that aren't revoked stops the migration, named, so it can
-- be sorted out (revoke one) before deploying — `npm run check:hub2` lists them in advance.
DO $$
DECLARE dupes TEXT;
BEGIN
SELECT string_agg(format('%s (%s licences)', c.name, n), ', ') INTO dupes
FROM (SELECT "customerId", count(*) AS n FROM "Licence" WHERE status <> 'revoked' GROUP BY "customerId" HAVING count(*) > 1) d
JOIN "Customer" c ON c.id = d."customerId";
IF dupes IS NOT NULL THEN
RAISE EXCEPTION 'Hub 2.0 allows one Events licence per customer that is not revoked. Revoke the extra licences first: %', dupes;
END IF;
END $$;
CREATE UNIQUE INDEX "Licence_customerId_product_open_key" ON "Licence"("customerId", "product") WHERE status <> 'revoked';