Hub 2.0 step 6: AI cap and markup per church

The cap and markup move from the licence to the customer (migrated), use
is counted over all the church's installs under one lock per church, and
the licence token carries the church's cap. Settings → AI shows product.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-05 08:55:43 +02:00
co-authored by Claude Opus 5.5
parent 6e51780103
commit 59e6b9f03c
11 changed files with 138 additions and 72 deletions
@@ -0,0 +1,19 @@
-- Hub 2.0: the AI cap and markup are the church's (counted over all its installs), moved from the
-- licence. Each church takes the cap of its open licence with the highest cap, and that licence's
-- markup override (Hub 1.7 had one Events licence per church, so this is that licence's).
ALTER TABLE "Customer" ADD COLUMN "aiMarkupPercent" INTEGER,
ADD COLUMN "aiMonthlyCapCents" INTEGER NOT NULL DEFAULT 0;
UPDATE "Customer" c
SET "aiMonthlyCapCents" = l."aiMonthlyCapCents", "aiMarkupPercent" = l."aiMarkupPercent"
FROM (
SELECT DISTINCT ON ("customerId") "customerId", "aiMonthlyCapCents", "aiMarkupPercent"
FROM "Licence"
WHERE status <> 'revoked'
ORDER BY "customerId", "aiMonthlyCapCents" DESC, "createdAt" ASC
) l
WHERE c.id = l."customerId";
ALTER TABLE "Licence" DROP COLUMN "aiMarkupPercent",
DROP COLUMN "aiMonthlyCapCents";
+5 -4
View File
@@ -62,6 +62,11 @@ model Customer {
// reminded about (30 days before), so he's reminded once per renewal.
domainRenewsOn DateTime? @db.Date
domainRemindedFor DateTime? @db.Date
// Hub 2.0 (moved from Licence): the monthly AI spend cap in cents (rand), counted in charged
// rand over all the church's installs. 0 = AI off. And the markup on the real cost, in %; null =
// the Hub default (Settings → AI).
aiMonthlyCapCents Int @default(0)
aiMarkupPercent Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
licences Licence[]
@@ -102,10 +107,6 @@ model Licence {
// Null = no end date.
expiresAt DateTime?
graceDays Int @default(14)
// Cross Events 2.0: monthly AI spend cap (in cents, rand), counted in charged rand. 0 = AI off.
aiMonthlyCapCents Int @default(0)
// Markup on the real AI cost, in %. Null = the Hub default (Settings → AI).
aiMarkupPercent Int?
notes String?
// Billing (Hub 2.0: the schedule is the customer's). The licence is charged from billedFrom
// (null = not charged: a trial, free, or Accounts); a product added to a customer already