Wire zappier-edge into the live stack and add billing department APIs
Some checks are pending
offline / test (push) Waiting to run
Some checks are pending
offline / test (push) Waiting to run
Fleet now spawns the real zappier and middleware processes. Metered timestamp/receipt/hash calls proxy to middleware when ZAPPIER_UPSTREAM is set. CS credits, sales per-customer pricing, and QuickBooks export are separate repos plugged into zappier-edge admin.
This commit is contained in:
parent
65bfa544b2
commit
a1a5b957fd
44 changed files with 822 additions and 18 deletions
73
packages/zappier-customer-service/src/server.js
Normal file
73
packages/zappier-customer-service/src/server.js
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#!/usr/bin/env node
|
||||
/**
|
||||
* Customer-service department API: goodwill credits, CS notes.
|
||||
* Source of truth for balances remains zappier-edge admin.
|
||||
*/
|
||||
import http from 'node:http';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
const PORT = Number(process.env.PORT || 3011);
|
||||
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 STORE = process.env.CS_STORE || path.join(process.cwd(), 'data', 'credits.json');
|
||||
|
||||
function load() {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(STORE, 'utf8'));
|
||||
} catch {
|
||||
return { credits: [] };
|
||||
}
|
||||
}
|
||||
function save(data) {
|
||||
fs.mkdirSync(path.dirname(STORE), { recursive: true });
|
||||
fs.writeFileSync(STORE, JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
async function edge(pathname, { method = 'GET', body } = {}) {
|
||||
const r = await fetch(`${EDGE}${pathname}`, {
|
||||
method,
|
||||
headers: { 'content-type': 'application/json', 'x-admin-key': KEY },
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
const text = await r.text();
|
||||
try {
|
||||
return { status: r.status, body: JSON.parse(text) };
|
||||
} catch {
|
||||
return { status: r.status, body: text };
|
||||
}
|
||||
}
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url || '/', `http://127.0.0.1:${PORT}`);
|
||||
const json = (code, obj) => {
|
||||
res.writeHead(code, { 'content-type': 'application/json' });
|
||||
res.end(JSON.stringify(obj));
|
||||
};
|
||||
try {
|
||||
if (req.method === 'GET' && url.pathname === '/health') {
|
||||
return json(200, { ok: true, role: 'zappier-customer-service' });
|
||||
}
|
||||
if (req.method === 'POST' && url.pathname === '/credits') {
|
||||
const chunks = [];
|
||||
for await (const c of req) chunks.push(c);
|
||||
const payload = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}');
|
||||
const forwarded = await edge('/admin/api/credits', { method: 'POST', body: payload });
|
||||
const data = load();
|
||||
data.credits.unshift({ ...payload, at: new Date().toISOString(), edge: forwarded.status });
|
||||
save(data);
|
||||
return json(forwarded.status, forwarded.body);
|
||||
}
|
||||
if (req.method === 'GET' && url.pathname === '/credits') {
|
||||
const forwarded = await edge(`/admin/api/credits${url.search}`);
|
||||
return json(forwarded.status, forwarded.body);
|
||||
}
|
||||
json(404, { error: 'not found' });
|
||||
} catch (err) {
|
||||
json(502, { error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(PORT, '0.0.0.0', () => {
|
||||
process.stdout.write(`zappier-customer-service http://127.0.0.1:${PORT}/\n`);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue