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:
@@ -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'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>
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ type Detail = {
|
||||
adminContact: { name?: string; email?: string } | null; server: { node?: string; postgres?: string; os?: string } | null;
|
||||
features: Record<string, unknown> | null; registeredAt: string; lastHeartbeatAt: string | null; releasedAt: string | null;
|
||||
health: Health | null; stats: Stats | null;
|
||||
customer: { id: string; name: string };
|
||||
customer: { id: string; name: string; aiMonthlyCapCents: number; aiMarkupPercent: number | null };
|
||||
licence: (Licence & { tokenPreview: { status: string; validUntil: string; licenceExpiresAt: string | null } }) | null;
|
||||
summary: InstallSummary;
|
||||
events: InstallEvent[];
|
||||
@@ -137,8 +137,8 @@ export default function InstallPage() {
|
||||
v={<>{tier.name} · {tierPrice(tier)}{d.licence.plan !== tier.name && d.licence.plan !== "Trial" && <span className="ml-1 text-amber-700">(plan is {d.licence.plan})</span>}</>}
|
||||
/>
|
||||
)}
|
||||
<Row k="AI cap" v={d.licence.aiMonthlyCapCents ? `${rand(d.licence.aiMonthlyCapCents)} / month` : "off"} />
|
||||
<Row k="AI markup" v={d.licence.aiMarkupPercent == null ? "default" : `${d.licence.aiMarkupPercent}%`} />
|
||||
<Row k="AI cap (church)" v={d.customer.aiMonthlyCapCents ? `${rand(d.customer.aiMonthlyCapCents)} / month` : "off"} />
|
||||
<Row k="AI markup" v={d.customer.aiMarkupPercent == null ? "default" : `${d.customer.aiMarkupPercent}%`} />
|
||||
<Link href={`/customers/${d.customer.id}`} className="mt-3 inline-block text-sm text-indigo-600 hover:underline">Change the licence →</Link>
|
||||
</>
|
||||
) : <p className="text-sm text-slate-500">No licence (released).</p>}
|
||||
@@ -162,7 +162,7 @@ export default function InstallPage() {
|
||||
<div className="card mt-6">
|
||||
<h2 className="mb-2 font-semibold">AI</h2>
|
||||
{!d.ai.status.enabled && d.ai.months.length === 0 ? (
|
||||
<p className="text-sm text-slate-500">Off for this site{d.licence?.aiMonthlyCapCents ? " (AI isn't set up in Settings → AI, or the licence isn't active)" : " (cap R0)"}.</p>
|
||||
<p className="text-sm text-slate-500">Off for this site{d.customer.aiMonthlyCapCents ? " (AI isn't set up in Settings → AI, or the licence isn't active)" : " (cap R0)"}.</p>
|
||||
) : (
|
||||
<>
|
||||
<Row k="This month (charged)" v={`${randFine(d.ai.status.usedCents)} of ${rand(d.ai.status.capCents)} (${d.ai.status.usedPercent}%)${d.ai.status.capReached ? " — used up" : ""}`} />
|
||||
|
||||
@@ -8,6 +8,7 @@ import { matches, usePaged } from "@/lib/paging";
|
||||
import type { AiInstallUse } from "@/lib/types";
|
||||
import { Loading, Message, PageTitle, Pager, SearchInput, Tabs } from "@/components/ui";
|
||||
import { BillingSettings } from "@/components/billing/BillingSettings";
|
||||
import { PRODUCT_NAMES } from "@/lib/products";
|
||||
|
||||
type Settings = Record<string, string>;
|
||||
type AuditRow = { id: string; action: string; target: string | null; metadata: unknown; createdAt: string; actor: { name: string } | null };
|
||||
@@ -145,11 +146,11 @@ function AiTab({ settings, models, onSaved, setMsg }: { settings: Settings; mode
|
||||
{!usage ? <Loading /> : usage.installs.length === 0 ? <p className="px-5 pb-5 text-sm text-slate-500">No AI use in {usage.month}.</p> : (
|
||||
<div className="overflow-x-auto px-5 pb-5">
|
||||
<table className="w-full text-sm">
|
||||
<thead><tr className="text-left text-xs text-slate-500"><th className="py-1">Site</th><th>Cap</th><th>Uses</th><th className="text-right">Real cost</th><th className="text-right">Charged</th><th className="text-right">Profit</th></tr></thead>
|
||||
<thead><tr className="text-left text-xs text-slate-500"><th className="py-1">Site</th><th>Church cap</th><th>Uses</th><th className="text-right">Real cost</th><th className="text-right">Charged</th><th className="text-right">Profit</th></tr></thead>
|
||||
<tbody>
|
||||
{usage.installs.map(i => (
|
||||
<tr key={i.installId} className="border-t border-slate-100">
|
||||
<td className="py-1"><Link href={`/installs/${i.installId}`} className="text-indigo-700 hover:underline">{i.name}</Link></td>
|
||||
<td className="py-1"><Link href={`/installs/${i.installId}`} className="text-indigo-700 hover:underline">{i.customer?.name || i.name}</Link> <span className="text-xs text-slate-500">{PRODUCT_NAMES[i.product]}</span></td>
|
||||
<td>{i.capCents ? rand(i.capCents) : "off"}</td>
|
||||
<td>{i.calls}</td>
|
||||
<td className="text-right">{randFine(i.realCents)}</td>
|
||||
|
||||
Reference in New Issue
Block a user