commit bd9249dcd8a5c24bf3857a5d52f65f367527ebe0 Author: George Lambert Date: Fri Sep 11 16:06:32 2026 -0400 Initial import of zappier-accounting-export from zapier monorepo diff --git a/README.md b/README.md new file mode 100644 index 0000000..7a4efe1 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# zappier-accounting-export + +Accounting-department export of **invoices, billing, and payment history** to **QuickBooks IIF** and generic CSV (also usable in other ledgers). + +**Forgejo:** https://git.georgelambert.org/marchon/zappier-accounting-export +**Catalog:** https://zapier.georgelambert.org/packages/zappier-accounting-export/README.pdf + +Reads issued/paid invoices from zappier-edge (`ZAPPIER_ADMIN_URL`). Customer statements (credits, balances, usage, payments) come from NATS `verae.billing.statement.get`. + +Staff UI: http://127.0.0.1:3013/ + +```bash +PORT=3013 NATS_URL=nats://127.0.0.1:4222 node src/server.js +curl http://127.0.0.1:3013/review/cust_1 +curl 'http://127.0.0.1:3013/export/quickbooks.iif?period=2026-07' -o zappier.iif +curl 'http://127.0.0.1:3013/export/accounting.csv?period=2026-07' -o zappier.csv +``` + +Import the IIF in QuickBooks Desktop: **File → Utilities → Import → IIF**. The CSV is a flat invoice register for other systems. + +History is the invoice list on zappier-edge (draft / issued / paid) plus CS credit ledger and portal prepaid reloads. diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..bae4794 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,46 @@ +{ + "name": "zappier-accounting-export", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "zappier-accounting-export", + "version": "0.1.0", + "dependencies": { + "nats": "^2.28.2" + } + }, + "node_modules/nats": { + "version": "2.29.3", + "resolved": "https://registry.npmjs.org/nats/-/nats-2.29.3.tgz", + "integrity": "sha512-tOQCRCwC74DgBTk4pWZ9V45sk4d7peoE2njVprMRCBXrhJ5q5cYM7i6W+Uvw2qUrcfOSnuisrX7bEx3b3Wx4QA==", + "deprecated": "Package moved. Use @nats-io/transport-node from https://github.com/nats-io/nats.js", + "license": "Apache-2.0", + "dependencies": { + "nkeys.js": "1.1.0" + }, + "engines": { + "node": ">= 14.0.0" + } + }, + "node_modules/nkeys.js": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/nkeys.js/-/nkeys.js-1.1.0.tgz", + "integrity": "sha512-tB/a0shZL5UZWSwsoeyqfTszONTt4k2YS0tuQioMOD180+MbombYVgzDUYHlx+gejYK6rgf08n/2Df99WY0Sxg==", + "license": "Apache-2.0", + "dependencies": { + "tweetnacl": "1.0.3" + }, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/tweetnacl": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", + "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==", + "license": "Unlicense" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..79041d5 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "zappier-accounting-export", + "version": "0.1.0", + "private": true, + "type": "module", + "description": "Export zappier invoices to QuickBooks IIF and accounting CSV", + "scripts": { + "start": "node src/server.js", + "test": "node --test test/*.test.js" + }, + "dependencies": { + "nats": "^2.28.2" + } +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..d370bbd --- /dev/null +++ b/public/index.html @@ -0,0 +1,51 @@ + + + + + + Accounting — customer review + + + +

Accounting

+

Review payments, usage, credits, and balances. Export invoices to QuickBooks IIF / CSV.

+
+ + + +

QuickBooks IIF · CSV

+
+
+ + + diff --git a/src/nats-billing.js b/src/nats-billing.js new file mode 100644 index 0000000..3634755 --- /dev/null +++ b/src/nats-billing.js @@ -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-accounting-export' }); + 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; + } +} diff --git a/src/server.js b/src/server.js new file mode 100644 index 0000000..564f9e0 --- /dev/null +++ b/src/server.js @@ -0,0 +1,61 @@ +#!/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'; + +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')) { + 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' }); + } + 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 send(e.status, 'application/json', await e.text()); + } + const period = url.searchParams.get('period'); + const q = period ? `?period=${encodeURIComponent(period)}` : ''; + if (req.method === 'GET' && url.pathname === '/export/quickbooks.iif') { + 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') { + 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`); +}); diff --git a/test/health.test.js b/test/health.test.js new file mode 100644 index 0000000..5355ae4 --- /dev/null +++ b/test/health.test.js @@ -0,0 +1,25 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { spawn } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import path from 'node:path'; + +const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); + +test('accounting-export health', async () => { + const port = 18013; + const child = spawn(process.execPath, ['src/server.js'], { + cwd: root, + env: { ...process.env, PORT: String(port) }, + stdio: ['ignore', 'pipe', 'pipe'], + }); + await new Promise((r) => setTimeout(r, 400)); + try { + const r = await fetch(`http://127.0.0.1:${port}/health`); + assert.equal((await r.json()).role, 'zappier-accounting-export'); + const home = await fetch(`http://127.0.0.1:${port}/`); + assert.equal(home.status, 200); + } finally { + child.kill('SIGTERM'); + } +});