Add NATS account-balance SoT and statement review for customers, CS, and sales
Some checks are pending
offline / test (push) Waiting to run
Some checks are pending
offline / test (push) Waiting to run
Internal billing now uses verae.billing.* request-reply and pubs. zappier-account-balance tracks prepaid, credits, usage, and payments. Portal, admin, CS, and sales all review the same statement. Independent Forgejo repos stay split via push-module-repos.
This commit is contained in:
parent
a1a5b957fd
commit
ac38676645
135 changed files with 3078 additions and 130 deletions
17
packages/zappier-sales-pricing/src/nats-billing.js
Normal file
17
packages/zappier-sales-pricing/src/nats-billing.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
export const SUBJECTS = { STATEMENT_GET: 'verae.billing.statement.get' };
|
||||
|
||||
export async function billingRequest(subject, payload) {
|
||||
const url = process.env.NATS_URL;
|
||||
if (!url) return null;
|
||||
try {
|
||||
const { connect, StringCodec } = await import('nats');
|
||||
const nc = await connect({ servers: url.split(','), name: 'zappier-sales-pricing' });
|
||||
const sc = StringCodec();
|
||||
const m = await nc.request(subject, sc.encode(JSON.stringify(payload)), { timeout: 2000 });
|
||||
const out = JSON.parse(sc.decode(m.data) || '{}');
|
||||
await nc.close();
|
||||
return out;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,18 @@
|
|||
#!/usr/bin/env node
|
||||
/** Sales department: per-customer multiplier / tier. Writes through zappier-edge. */
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
import { SUBJECTS, billingRequest } from './nats-billing.js';
|
||||
|
||||
const PUBLIC = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'public');
|
||||
|
||||
const PORT = Number(process.env.PORT || 3012);
|
||||
const EDGE = (process.env.ZAPPIER_ADMIN_URL || 'http://127.0.0.1:3000').replace(/\/$/, '');
|
||||
const KEY = process.env.ZAPPIER_ADMIN_KEY || 'admin-dev-key';
|
||||
const BOOKS = (process.env.ACCOUNT_BALANCE_URL || 'http://127.0.0.1:3010').replace(/\/$/, '');
|
||||
|
||||
async function edge(pathname, { method = 'GET', body } = {}) {
|
||||
const r = await fetch(`${EDGE}${pathname}`, {
|
||||
|
|
@ -27,9 +35,23 @@ const server = http.createServer(async (req, res) => {
|
|||
res.end(JSON.stringify(obj));
|
||||
};
|
||||
try {
|
||||
if (req.method === 'GET' && (url.pathname === '/' || url.pathname === '/index.html')) {
|
||||
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
||||
res.end(fs.readFileSync(path.join(PUBLIC, 'index.html')));
|
||||
return;
|
||||
}
|
||||
if (req.method === 'GET' && url.pathname === '/health') {
|
||||
return json(200, { ok: true, role: 'zappier-sales-pricing' });
|
||||
}
|
||||
const review = url.pathname.match(/^\/review\/([^/]+)$/);
|
||||
if (req.method === 'GET' && review) {
|
||||
const nats = await billingRequest(SUBJECTS.STATEMENT_GET, { customerId: review[1] });
|
||||
if (nats) return json(200, { ...nats, source: 'nats' });
|
||||
const r = await fetch(`${BOOKS}/statement/${review[1]}`);
|
||||
if (r.ok) return json(200, { ...(await r.json()), source: 'account-balance' });
|
||||
const e = await edge(`/admin/api/statement/${review[1]}`);
|
||||
return json(e.status, e.body);
|
||||
}
|
||||
const quote = url.pathname.match(/^\/quotes\/([^/]+)$/);
|
||||
if (req.method === 'GET' && quote) {
|
||||
const forwarded = await edge(`/admin/api/sales/quote/${quote[1]}`);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue