Add customer names on the ledger, staff session login, and exclusive jobs.events.
Some checks are pending
offline / test (push) Waiting to run
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:
parent
cb07f5b321
commit
9cc0018708
38 changed files with 533 additions and 68 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
};
|
||||
|
|
|
|||
|
|
@ -24,6 +24,14 @@ test('adjust credits prepaid and statement lists credits usage payments', () =>
|
|||
assert.equal(st.payments[0].kind, 'payment');
|
||||
});
|
||||
|
||||
test('customer.put stores display name and lookup by name', () => {
|
||||
const books = new AccountBooks();
|
||||
handle(SUBJECTS.CUSTOMER_PUT, { customerId: 'cust_1', name: 'Ada (free)' }, books);
|
||||
const st = handle(SUBJECTS.STATEMENT_GET, { customerId: 'Ada (free)' }, books);
|
||||
assert.equal(st.customerId, 'cust_1');
|
||||
assert.equal(st.name, 'Ada (free)');
|
||||
});
|
||||
|
||||
test('persist round-trip keeps prepaid', () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'books-'));
|
||||
process.env.BOOKS_PATH = path.join(dir, 'books.json');
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawn } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
|
|
@ -8,9 +10,10 @@ const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|||
|
||||
test('account-balance health and statement', async () => {
|
||||
const port = 18010;
|
||||
const books = path.join(os.tmpdir(), `books-health-${Date.now()}.json`);
|
||||
const child = spawn(process.execPath, ['src/server.js'], {
|
||||
cwd: root,
|
||||
env: { ...process.env, PORT: String(port) },
|
||||
env: { ...process.env, PORT: String(port), BOOKS_PATH: books },
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
await new Promise((r) => setTimeout(r, 400));
|
||||
|
|
@ -26,5 +29,6 @@ test('account-balance health and statement', async () => {
|
|||
assert.equal(st.prepaidCents, 200);
|
||||
} finally {
|
||||
child.kill('SIGTERM');
|
||||
fs.rmSync(books, { force: true });
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue