← back to Dw Signup Fulfillment
lib/mint-ledger.js
29 lines
'use strict';
// Daily gift-card mint ledger — the money backstop for the public webhook. Persists a
// per-UTC-day {count,total} to data/mint-ledger.json so a restart doesn't reset the cap,
// and so there is an auditable record of how much store liability was minted each day.
const fs = require('fs');
const path = require('path');
const P = path.join(__dirname, '..', 'data', 'mint-ledger.json');
function today() { return new Date().toISOString().slice(0, 10); } // UTC YYYY-MM-DD
function read() { try { return JSON.parse(fs.readFileSync(P, 'utf8')); } catch { return {}; } }
function write(o) { fs.mkdirSync(path.dirname(P), { recursive: true }); fs.writeFileSync(P, JSON.stringify(o, null, 2)); }
function todayCount() { const d = read()[today()]; return d ? d.count : 0; }
function todayTotal() { const d = read()[today()]; return d ? d.total : 0; }
// Record one mint of `value` dollars; returns the updated day record {count,total}.
function recordMint(value) {
const o = read();
const d = today();
o[d] = o[d] || { count: 0, total: 0 };
o[d].count += 1;
o[d].total = +(o[d].total + (Number(value) || 0)).toFixed(2);
write(o);
return o[d];
}
module.exports = { today, todayCount, todayTotal, recordMint };