master-zapier-plan-draft/packages/zappier-accounting-export/src/server.js
George Lambert d299d245e8
Some checks are pending
offline / test (push) Waiting to run
Add internal staff IAM: named users, roles, and management permissions.
verae-staff-iam (:3028) is the people directory — owner, billing-admin,
cs, sales, accounting, operator, viewer — with scrypt passwords, sessions,
and an audit log. Admin console login uses it when STAFF_IAM_URL is set
and hides tabs the account cannot use. CS/sales/accounting/staff/fleet
check permissions such as cs.credit and fleet.operate. Shared staff key
remains only as a fallback when IAM is unset.
2026-09-11 18:36:12 -04:00

72 lines
3.6 KiB
JavaScript

#!/usr/bin/env node
/** Accounting department: QuickBooks IIF + CSV from zappier-edge invoices. */
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';
import { listCustomers, withCustomerName } from './names.js';
import { denyOrRedirect } from './iam-gate.js';
const PORT = Number(process.env.PORT || 3013);
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(/\/$/, '');
const PUBLIC = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'public');
async function edge(pathname) {
const r = await fetch(`${EDGE}${pathname}`, { headers: { 'x-admin-key': KEY } });
return r;
}
const server = http.createServer(async (req, res) => {
const url = new URL(req.url || '/', `http://127.0.0.1:${PORT}`);
const send = (code, type, body) => {
res.writeHead(code, { 'content-type': type });
res.end(body);
};
const json = (code, obj) => send(code, 'application/json', JSON.stringify(obj));
try {
if (req.method === 'GET' && (url.pathname === '/' || url.pathname === '/index.html')) {
if (!(await denyOrRedirect(req, res, json, { permission: 'accounting.review', html: true }))) return;
return send(200, 'text/html; charset=utf-8', fs.readFileSync(path.join(PUBLIC, 'index.html')));
}
if (req.method === 'GET' && url.pathname === '/health') {
return json(200, { ok: true, role: 'zappier-accounting-export' });
}
if (req.method === 'GET' && url.pathname === '/customers') {
return json(200, { customers: await listCustomers(EDGE, KEY) });
}
const review = url.pathname.match(/^\/review\/([^/]+)$/);
if (req.method === 'GET' && review) {
if (!(await denyOrRedirect(req, res, json, { permission: 'accounting.review' }))) return;
const id = decodeURIComponent(review[1]);
const nats = await billingRequest(SUBJECTS.STATEMENT_GET, { customerId: id });
if (nats) return json(200, await withCustomerName({ ...nats, source: 'nats' }, id, EDGE, KEY));
const r = await fetch(`${BOOKS}/statement/${encodeURIComponent(id)}`);
if (r.ok) return json(200, await withCustomerName({ ...(await r.json()), source: 'account-balance' }, id, EDGE, KEY));
const e = await edge(`/admin/api/statement/${encodeURIComponent(id)}`);
const body = await e.json().catch(() => ({}));
return json(e.status, await withCustomerName(body, id, EDGE, KEY));
}
const period = url.searchParams.get('period');
const q = period ? `?period=${encodeURIComponent(period)}` : '';
if (req.method === 'GET' && url.pathname === '/export/quickbooks.iif') {
if (!(await denyOrRedirect(req, res, json, { permission: 'accounting.export' }))) return;
const r = await edge(`/admin/api/exports/quickbooks.iif${q}`);
return send(r.status, 'text/plain', await r.text());
}
if (req.method === 'GET' && url.pathname === '/export/accounting.csv') {
if (!(await denyOrRedirect(req, res, json, { permission: 'accounting.export' }))) return;
const r = await edge(`/admin/api/exports/accounting.csv${q}`);
return send(r.status, 'text/csv', await r.text());
}
json(404, { error: 'not found' });
} catch (err) {
json(502, { error: err.message });
}
});
server.listen(PORT, '0.0.0.0', () => {
process.stdout.write(`zappier-accounting-export http://127.0.0.1:${PORT}/\n`);
});