Add customer names on the ledger, staff session login, and exclusive jobs.events.
Some checks are pending
offline / test (push) Waiting to run

Account-balance stores display names and looks up by name. Edge writes
names on customer create/edit; staff UIs join from edge when needed.
New verae-staff-session issues a host cookie; department HTML redirects
when STAFF_AUTH=1. JOBS_EVENTS_EXCLUSIVE lets jobs-events own the durable
consumer. Catalog index is cards; disabled fleet machines are grey.
This commit is contained in:
George Lambert 2026-09-11 18:11:21 -04:00
parent cb07f5b321
commit 9cc0018708
38 changed files with 533 additions and 68 deletions

View file

@ -7,21 +7,24 @@ export class AccountBooks {
this.prepaid = {};
/** @type {Record<string, string>} */
this.veraeUserIds = {};
/** @type {Record<string, string>} */
this.names = {};
this.credits = [];
this.usage = [];
this.payments = [];
}
remember(customerId, veraeUserId) {
remember(customerId, veraeUserId, name) {
if (customerId && veraeUserId) this.veraeUserIds[customerId] = veraeUserId;
if (customerId && name) this.names[customerId] = name;
}
prepaidCents(customerId) {
return this.prepaid[customerId] ?? 0;
}
adjust({ customerId, cents, reason, agent, kind = 'credit', veraeUserId }) {
this.remember(customerId, veraeUserId);
adjust({ customerId, cents, reason, agent, kind = 'credit', veraeUserId, name }) {
this.remember(customerId, veraeUserId, name);
const delta = Math.trunc(Number(cents) || 0);
const next = this.prepaidCents(customerId) + delta;
this.prepaid[customerId] = next;
@ -41,7 +44,7 @@ export class AccountBooks {
}
recordUsage(entry) {
this.remember(entry.customerId, entry.veraeUserId);
this.remember(entry.customerId, entry.veraeUserId, entry.name);
const cents = Number(entry.cents) || 0;
const customerId = entry.customerId;
const next = this.prepaidCents(customerId) - cents;
@ -67,10 +70,20 @@ export class AccountBooks {
});
}
lookup(idOrName) {
if (this.names[idOrName] || this.prepaid[idOrName] != null || this.veraeUserIds[idOrName]) {
return this.statement(idOrName);
}
const want = String(idOrName || '').toLowerCase();
const hit = Object.entries(this.names).find(([, n]) => String(n).toLowerCase() === want);
return this.statement(hit ? hit[0] : idOrName);
}
statement(customerId) {
const match = (rows) => rows.filter((r) => r.customerId === customerId).slice(0, 100);
return {
customerId,
name: this.names[customerId],
veraeUserId: this.veraeUserIds[customerId],
prepaidCents: this.prepaidCents(customerId),
credits: match(this.credits),
@ -83,6 +96,7 @@ export class AccountBooks {
return {
prepaid: this.prepaid,
veraeUserIds: this.veraeUserIds,
names: this.names,
credits: this.credits,
usage: this.usage,
payments: this.payments,
@ -93,6 +107,7 @@ export class AccountBooks {
if (!raw || typeof raw !== 'object') return this;
this.prepaid = raw.prepaid || {};
this.veraeUserIds = raw.veraeUserIds || {};
this.names = raw.names || {};
this.credits = Array.isArray(raw.credits) ? raw.credits : [];
this.usage = Array.isArray(raw.usage) ? raw.usage : [];
this.payments = Array.isArray(raw.payments) ? raw.payments : [];
@ -102,9 +117,13 @@ export class AccountBooks {
export function handle(subject, payload, books) {
const p = payload || {};
if (subject.endsWith('customer.put')) {
books.remember(p.customerId, p.veraeUserId, p.name);
return books.lookup(p.customerId);
}
if (subject.endsWith('balance.get') || subject.endsWith('statement.get')) {
books.remember(p.customerId, p.veraeUserId);
return books.statement(p.customerId);
books.remember(p.customerId, p.veraeUserId, p.name);
return books.lookup(p.customerId);
}
if (subject.endsWith('balance.adjust') || subject.endsWith('credit.applied')) {
return books.adjust(p);

View file

@ -43,6 +43,9 @@ async function startNats() {
reply(nc.subscribe(SUBJECTS.BALANCE_ADJUST, { queue: SUBJECTS.QUEUE }), (p) =>
apply(SUBJECTS.BALANCE_ADJUST, p),
);
reply(nc.subscribe(SUBJECTS.CUSTOMER_PUT, { queue: SUBJECTS.QUEUE }), (p) =>
apply(SUBJECTS.CUSTOMER_PUT, p),
);
(async () => {
for await (const m of nc.subscribe(SUBJECTS.USAGE_RECORDED)) {
apply(SUBJECTS.USAGE_RECORDED, JSON.parse(sc.decode(m.data) || '{}'));
@ -80,7 +83,11 @@ const server = http.createServer(async (req, res) => {
}
const st = url.pathname.match(/^\/statement\/([^/]+)$/);
if (req.method === 'GET' && st) {
return json(200, books.statement(st[1]));
return json(200, books.lookup(decodeURIComponent(st[1])));
}
if (req.method === 'POST' && url.pathname === '/customer') {
const body = await readBody(req);
return json(200, apply(SUBJECTS.CUSTOMER_PUT, body));
}
if (req.method === 'POST' && url.pathname === '/adjust') {
const body = await readBody(req);

View file

@ -6,5 +6,6 @@ export const SUBJECTS = {
USAGE_RECORDED: 'verae.billing.usage.recorded',
PAYMENT_RECORDED: 'verae.billing.payment.recorded',
CREDIT_APPLIED: 'verae.billing.credit.applied',
CUSTOMER_PUT: 'verae.billing.customer.put',
QUEUE: 'account-balance',
};