ab4f1693df
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
30 lines
2.8 KiB
JavaScript
30 lines
2.8 KiB
JavaScript
// Admin > Overview > Refill notices (2026-10-24): who was told their ads ran low, ran out or sat idle, and what they did next.
|
|
(function () {
|
|
const $ = id => document.getElementById(id);
|
|
if (!$('rfBox')) return;
|
|
const esc = s => String(s == null ? '' : s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
const ct = ts => new Date(Number(ts)).toLocaleString('en-US', { timeZone: 'America/Chicago', month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' }) + ' CT';
|
|
const STATE = { low: 'running low', out: 'out of credits', idle: 'idle a week' };
|
|
async function load() {
|
|
try {
|
|
const r = await (await fetch('/api/admin/refill', { cache: 'no-store' })).json();
|
|
if (r.error) throw new Error(r.error);
|
|
const t = r.totals || {};
|
|
$('rfSub').textContent = (r.enabled ? 'on' : 'OFF (site setting refillNotices)') + ', low at ' + r.lowPct + '% delivered';
|
|
const st = Object.entries(r.byState || {}).map(([k, v]) => esc(STATE[k] || k) + ': ' + v.sent + ' sent, ' + v.converted + ' acted').join(' · ');
|
|
const kinds = Object.entries(r.byKind || {}).map(([k, v]) => esc(k) + ' ' + v).join(', ');
|
|
$('rfBox').innerHTML = '<p class="small" style="margin:0 0 8px"><b>' + (t.sent || 0) + '</b> sent (' + (t.emailed || 0) + ' emailed), <b>' + (t.converted || 0) + '</b> acted on it (' + (t.ratePct || 0) + '%)'
|
|
+ (t.purchasesUsd ? ', $' + t.purchasesUsd + ' in packages after a notice' : '') + (t.medianHours != null ? ', median ' + t.medianHours + ' h' : '') + '</p>'
|
|
+ '<p class="small muted" style="margin:0 0 8px">' + st + (kinds ? '<br>' + kinds : '') + '</p>'
|
|
+ ((r.recent || []).length ? '<div class="tbl-wrap"><table class="tbl small"><tr><th>When</th><th>Owner</th><th>State</th><th>Ads</th><th>Credits then</th><th>Next</th></tr>'
|
|
+ r.recent.slice(0, 20).map(x => '<tr><td>' + esc(ct(x.at)) + '</td><td>' + esc(x.owner) + '</td><td>' + esc(STATE[x.state] || x.state) + (x.emailed ? '' : ' <span class="muted">(on-site only)</span>') + '</td><td>' + esc((x.names || []).join(', ')) + '</td><td>' + Number(x.credits || 0).toLocaleString() + '</td><td>' + (x.converted ? esc(x.converted.kind) + ' after ' + x.converted.hours + ' h' : '<span class="muted">nothing yet</span>') + '</td></tr>').join('') + '</table></div>' : '<p class="small muted">Nothing sent yet.</p>');
|
|
} catch (e) { $('rfBox').textContent = 'Could not load: ' + e.message; }
|
|
}
|
|
$('rfRun') && $('rfRun').addEventListener('click', async () => {
|
|
$('rfRun').disabled = true;
|
|
try { const r = await (await fetch('/api/admin/refill/run', { method: 'POST', cache: 'no-store' })).json(); $('rfSub').textContent = 'pass done: ' + (r.sent || 0) + ' sent'; } catch (e) {}
|
|
$('rfRun').disabled = false; load();
|
|
});
|
|
load();
|
|
})();
|