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
+15 -14
View File
@@ -100,6 +100,20 @@ router.get('/customers', wrap(async (req, res) => {
function customerData(body, { partial = false } = {}) {
const data = {};
for (const f of ['name', 'contactName', 'email', 'phone', 'notes']) if (body?.[f] !== undefined) data[f] = text(body[f], f === 'notes' ? 5000 : 200);
// AI (Hub 2.0: the church's, for all its installs): the monthly cap and an optional markup.
if (body?.aiMonthlyCapCents !== undefined) {
const n = parseInt(body.aiMonthlyCapCents, 10);
if (!Number.isInteger(n) || n < 0) throw bad('The AI cap must be 0 or more.');
data.aiMonthlyCapCents = n;
}
if (body?.aiMarkupPercent !== undefined) {
if (body.aiMarkupPercent === null || body.aiMarkupPercent === '') data.aiMarkupPercent = null;
else {
const n = parseInt(body.aiMarkupPercent, 10);
if (!Number.isInteger(n) || n < 0 || n > 500) throw bad('The AI markup is 0–500 %, or blank for the default.');
data.aiMarkupPercent = n;
}
}
if ((!partial || 'name' in data) && !data.name) throw bad('Give the customer a name.');
return data;
}
@@ -136,19 +150,6 @@ function licenceData(body, { partial = false } = {}) {
if (!Number.isInteger(n) || n < 0 || n > 90) throw bad('Grace period: 0–90 days.');
data.graceDays = n;
}
if (body?.aiMonthlyCapCents !== undefined) {
const n = parseInt(body.aiMonthlyCapCents, 10);
if (!Number.isInteger(n) || n < 0) throw bad('The AI cap must be 0 or more.');
data.aiMonthlyCapCents = n;
}
if (body?.aiMarkupPercent !== undefined) {
if (body.aiMarkupPercent === null || body.aiMarkupPercent === '') data.aiMarkupPercent = null;
else {
const n = parseInt(body.aiMarkupPercent, 10);
if (!Number.isInteger(n) || n < 0 || n > 500) throw bad('The AI markup is 0–500 %, or blank for the default.');
data.aiMarkupPercent = n;
}
}
if (body?.status !== undefined) {
if (!['active', 'suspended', 'revoked'].includes(body.status)) throw bad('Status must be active, suspended or revoked.');
data.status = body.status;
@@ -224,7 +225,7 @@ router.get('/installs/:id', wrap(async (req, res) => {
let licence = null;
if (install.licence) {
// What the install would be told right now, for checking.
licence = { ...install.licence, tokenPreview: licenceToken.buildPayload(install.licence, install, install.customer.name) };
licence = { ...install.licence, tokenPreview: licenceToken.buildPayload({ ...install.licence, aiMonthlyCapCents: install.customer.aiMonthlyCapCents }, install, install.customer.name) };
}
const target = latest[install.product] || null;
const behind = !!(target && install.version && compareVersions(install.version, target) < 0);