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
+22 -18
View File
@@ -22,8 +22,6 @@ function LicenceCard({ licence, onSaved, setMsg }: { licence: Licence; onSaved:
plan: licence.plan,
expiresAt: dateInput(licence.expiresAt),
graceDays: String(licence.graceDays),
aiRand: String(licence.aiMonthlyCapCents / 100),
aiMarkup: licence.aiMarkupPercent == null ? "" : String(licence.aiMarkupPercent),
notes: licence.notes || "",
});
const [busy, setBusy] = useState(false);
@@ -40,8 +38,6 @@ function LicenceCard({ licence, onSaved, setMsg }: { licence: Licence; onSaved:
// End of the chosen day, South African time.
expiresAt: form.expiresAt ? `${form.expiresAt}T23:59:59+02:00` : null,
graceDays: form.graceDays,
aiMonthlyCapCents: Math.round((parseFloat(form.aiRand) || 0) * 100),
aiMarkupPercent: form.aiMarkup.trim() === "" ? null : form.aiMarkup,
notes: form.notes,
},
});
@@ -107,16 +103,6 @@ function LicenceCard({ licence, onSaved, setMsg }: { licence: Licence; onSaved:
<label className="label">Grace (days)</label>
<input type="number" min={0} max={90} className="input" value={form.graceDays} onChange={e => setForm({ ...form, graceDays: e.target.value })} />
</div>
<div>
<label className="label">AI cap (R / month)</label>
<input type="number" min={0} step="1" className="input" value={form.aiRand} onChange={e => setForm({ ...form, aiRand: e.target.value })} />
<p className="mt-1 text-xs text-slate-500">0 = off (no AI buttons). Counted in charged rand.</p>
</div>
<div>
<label className="label" htmlFor={`markup-${licence.id}`}>AI markup (%)</label>
<input id={`markup-${licence.id}`} type="number" min={0} max={500} step="1" className="input" placeholder="Default" value={form.aiMarkup} onChange={e => setForm({ ...form, aiMarkup: e.target.value })} />
<p className="mt-1 text-xs text-slate-500">Blank uses the default (Settings → AI).</p>
</div>
</div>
<div>
<label className="label">Notes</label>
@@ -134,13 +120,16 @@ function LicenceCard({ licence, onSaved, setMsg }: { licence: Licence; onSaved:
export default function CustomerPage() {
const { id } = useParams<{ id: string }>();
const [c, setC] = useState<Customer | null>(null);
const [form, setForm] = useState({ name: "", contactName: "", email: "", phone: "", notes: "" });
const [form, setForm] = useState({ name: "", contactName: "", email: "", phone: "", notes: "", aiRand: "0", aiMarkup: "" });
const [newLicence, setNewLicence] = useState<{ product: Product | ""; expiresAt: string; graceDays: string }>({ product: "", expiresAt: "", graceDays: "14" });
const [msg, setMsg] = useState<Msg>(null);
const load = useCallback(() => api<Customer>(`/api/admin/customers/${id}`).then(x => {
setC(x);
setForm({ name: x.name, contactName: x.contactName || "", email: x.email || "", phone: x.phone || "", notes: x.notes || "" });
setForm({
name: x.name, contactName: x.contactName || "", email: x.email || "", phone: x.phone || "", notes: x.notes || "",
aiRand: String(x.aiMonthlyCapCents / 100), aiMarkup: x.aiMarkupPercent == null ? "" : String(x.aiMarkupPercent),
});
}).catch(e => setMsg({ type: "err", text: errorText(e) })), [id]);
useEffect(() => { load(); }, [load]);
@@ -155,8 +144,12 @@ export default function CustomerPage() {
const saveDetails = async (e: React.FormEvent) => {
e.preventDefault();
try {
await api(`/api/admin/customers/${id}`, { method: "PUT", body: form });
setMsg({ type: "ok", text: "Saved." });
const { aiRand, aiMarkup, ...details } = form;
await api(`/api/admin/customers/${id}`, {
method: "PUT",
body: { ...details, aiMonthlyCapCents: Math.round((parseFloat(aiRand) || 0) * 100), aiMarkupPercent: aiMarkup.trim() === "" ? null : aiMarkup },
});
setMsg({ type: "ok", text: "Saved. The church's sites pick up an AI change with their next heartbeat (within 15 minutes)." });
load();
} catch (err) { setMsg({ type: "err", text: errorText(err) }); }
};
@@ -191,6 +184,17 @@ export default function CustomerPage() {
<div><label className="label">Email</label><input type="email" className="input" value={form.email} onChange={e => setForm({ ...form, email: e.target.value })} /></div>
<div><label className="label">Phone</label><input className="input" value={form.phone} onChange={e => setForm({ ...form, phone: e.target.value })} /></div>
<div><label className="label">Notes</label><textarea className="input" rows={4} value={form.notes} onChange={e => setForm({ ...form, notes: e.target.value })} placeholder="Plan, billing, anything worth remembering" /></div>
<div className="grid gap-3 sm:grid-cols-2">
<div>
<label className="label" htmlFor="ai-cap">AI cap (R / month)</label>
<input id="ai-cap" type="number" min={0} step="1" className="input" value={form.aiRand} onChange={e => setForm({ ...form, aiRand: e.target.value })} />
</div>
<div>
<label className="label" htmlFor="ai-markup">AI markup (%)</label>
<input id="ai-markup" type="number" min={0} max={500} step="1" className="input" placeholder="Default" value={form.aiMarkup} onChange={e => setForm({ ...form, aiMarkup: e.target.value })} />
</div>
</div>
<p className="text-xs text-slate-500">The AI allowance is the church&apos;s, shared by all its sites: 0 = off (no AI buttons). Counted in charged rand. A blank markup uses the default (Settings → AI).</p>
<button className="btn-secondary">Save details</button>
</form>