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:
@@ -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';
|
||||
@@ -56,12 +56,25 @@ enum LicenceStatus {
|
||||
revoked
|
||||
}
|
||||
|
||||
// Hub 2.0: the Cross Code products a church can have (ROADMAP-v3 §5). Accounts comes free with
|
||||
// any other product.
|
||||
enum Product {
|
||||
events
|
||||
accounts
|
||||
website
|
||||
app
|
||||
}
|
||||
|
||||
model Licence {
|
||||
id String @id @default(uuid())
|
||||
customerId String
|
||||
customer Customer @relation(fields: [customerId], references: [id], onDelete: Restrict)
|
||||
// CEH-XXXX-XXXX-XXXX-XXXX, generated by the Hub.
|
||||
// CEH-XXXX-XXXX-XXXX-XXXX (Events), CCA- (Accounts), CCW- (Website), CCP- (App).
|
||||
key String @unique
|
||||
// Hub 2.0. A customer has at most one licence per product that isn't revoked: enforced by the
|
||||
// partial unique index "Licence_customerId_product_open_key" (migration 20261104…_products),
|
||||
// which Prisma can't express here.
|
||||
product Product @default(events)
|
||||
plan String @default("standard")
|
||||
status LicenceStatus @default(active)
|
||||
// Null = no end date.
|
||||
@@ -90,6 +103,8 @@ model Licence {
|
||||
install Install?
|
||||
invoices Invoice[]
|
||||
licenceChanges LicenceChange[]
|
||||
|
||||
@@index([customerId, product])
|
||||
}
|
||||
|
||||
enum BillingCycle {
|
||||
@@ -104,8 +119,12 @@ model Install {
|
||||
customer Customer @relation(fields: [customerId], references: [id], onDelete: Restrict)
|
||||
licenceId String? @unique
|
||||
licence Licence? @relation(fields: [licenceId], references: [id], onDelete: SetNull)
|
||||
// Hub 2.0: the licence's product, set when the install registers.
|
||||
product Product @default(events)
|
||||
name String
|
||||
url String
|
||||
// Protocol v2: the install's Ed25519 public key (PEM), shared with its linked products.
|
||||
publicKey String?
|
||||
// The install's HMAC secret, AES-GCM encrypted: the Hub needs it to check signatures.
|
||||
secret String
|
||||
version String?
|
||||
|
||||
Reference in New Issue
Block a user