daf24a8f81
POST /api/admin/house-spins creates or updates Banner Spins on a house account (ADMIN_EMAIL or NETWORK_LINKS_OWNER) for house ads such as the Network Ad Space rows, through the same bannerspin.js calls the member Links screen uses (create, update, addBanner, updateBanner). Admin auth only (portal session or the admin key); no member session is read or minted. Every call, denied ones too, goes to the new admin action log (adminaudit.js: MySQL table admin_audit, JSONL without a database) with who, what and when; GET /api/admin/house-spins/log reads it. qa/housespins.test.mjs (in run.sh netsrc) covers auth, house-account only, member checks, idempotent repeat, prune, serving + click attribution, the log and no session minted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
5.1 KiB
JavaScript
67 lines
5.1 KiB
JavaScript
// House Banner Spins (Marty 2026-11-16: "no proper Banner Spins for your house ads"). The admin creates or updates Banner Spins
|
|
// on a house account (ADMIN_EMAIL or NETWORK_LINKS_OWNER, i.e. Marty's own account) for house ads such as the Network Ad Space
|
|
// rows, WITHOUT signing in as that member: no member session is read or minted. Every step goes through the same bannerspin.js
|
|
// calls the member Links screen uses (create, update, addBanner, updateBanner), with the account's email as the owner, so the
|
|
// checks (sizes, https images and targets, the source belonging to the account, the tier's banner count) are the member's.
|
|
// server.js wires it as POST /api/admin/house-spins (admin only) and writes every call to the admin action log (adminaudit.js).
|
|
//
|
|
// Body: { email, host?, spins: [ { id?, name, size, mode?, fallback?, sourceId?, paused?, prune?,
|
|
// banners: [ { label, image, target, weight?, active? } ] } ] }
|
|
// id given -> that Banner Spin (it must belong to the account) is updated; without id, a Banner Spin of the same name and size on
|
|
// the account is reused, else one is created. Banners match by image address: a match is updated, a new image is
|
|
// added, and with prune: true a banner whose image is no longer listed is removed.
|
|
// Reply: { ok, email, host, spins: [ { id, code, name, size, created, added, updated, removed, img, click, errors } ] }
|
|
'use strict';
|
|
let R = null; // { bannerspin, houseAccounts(): [email], host(wanted): the link domain (wanted when it is one of ours, else the first) }
|
|
function init(refs) { R = refs; }
|
|
const norm = e => String(e || '').trim().toLowerCase();
|
|
|
|
async function apply(body) {
|
|
const email = norm(body && body.email);
|
|
if (!email) return { error: 'Which account? Pass email.' };
|
|
if (!R.houseAccounts().includes(email)) return { error: 'House Banner Spins go on a house account only (the admin account).' };
|
|
const list = Array.isArray(body.spins) ? body.spins : [];
|
|
if (!list.length || list.length > 40) return { error: 'Pass 1 to 40 spins.' };
|
|
const BS = R.bannerspin; const host = R.host(body.host); const out = [];
|
|
for (const want of list) {
|
|
const row = { name: String(want.name || ''), size: String(want.size || ''), created: false, added: 0, updated: 0, removed: 0, errors: [] };
|
|
out.push(row);
|
|
let spin = null;
|
|
const mine = await BS.list(email);
|
|
if (want.id != null) { spin = mine.find(s => s.id === Number(want.id)) || null; if (!spin) { row.errors.push('No Banner Spin ' + want.id + ' on this account.'); continue; } }
|
|
else spin = mine.find(s => s.name === String(want.name || '').trim() && s.size === row.size) || null;
|
|
if (!spin) {
|
|
const r = await BS.create(email, { name: want.name, size: want.size, mode: want.mode, fallback: want.fallback });
|
|
if (r.error) { row.errors.push(r.error); continue; }
|
|
spin = r.spin; row.created = true;
|
|
}
|
|
const patch = {};
|
|
if (want.name != null && String(want.name).trim() !== spin.name) patch.name = want.name;
|
|
if (want.mode != null) patch.mode = want.mode;
|
|
if (want.fallback != null) patch.fallback = want.fallback;
|
|
if (want.sourceId != null) patch.sourceId = want.sourceId;
|
|
if (want.paused != null) patch.paused = !!want.paused;
|
|
if (Object.keys(patch).length) { const r = await BS.update(email, spin.id, patch); if (r.error) row.errors.push(r.error); else spin = r.spin; }
|
|
const banners = Array.isArray(want.banners) ? want.banners : [];
|
|
for (const b of banners) {
|
|
const have = spin.banners.find(x => x.image === String(b.image || '').trim());
|
|
if (have) {
|
|
const p = {}; if (b.label != null && b.label !== have.label) p.label = b.label; if (b.weight != null && Number(b.weight) !== have.weight) p.weight = b.weight;
|
|
if (b.target != null && b.target !== have.target) p.target = b.target; const act = b.active == null ? true : !!b.active; if (act !== have.active) p.active = act;
|
|
if (Object.keys(p).length) { const r = await BS.updateBanner(email, spin.id, have.id, p); if (r.error) row.errors.push(have.id + ': ' + r.error); else { spin = r.spin; row.updated++; } }
|
|
} else {
|
|
const r = await BS.addBanner(email, spin.id, { label: b.label, image: b.image, target: b.target, weight: b.weight });
|
|
if (r.error) row.errors.push(String(b.image).slice(0, 80) + ': ' + r.error); else { spin = r.spin; row.added++; }
|
|
}
|
|
}
|
|
if (want.prune) {
|
|
const keep = new Set(banners.map(b => String(b.image || '').trim()));
|
|
for (const x of spin.banners.filter(x => !keep.has(x.image))) { const r = await BS.updateBanner(email, spin.id, x.id, { remove: true }); if (r.error) row.errors.push(x.id + ': ' + r.error); else { spin = r.spin; row.removed++; } }
|
|
}
|
|
Object.assign(row, { id: spin.id, code: spin.code, name: spin.name, size: spin.size, sourceId: spin.sourceId || '', fallback: spin.fallback || '', banners: spin.banners.length,
|
|
img: 'https://' + host + '/i/' + spin.code + '.gif', click: 'https://' + host + '/c/' + spin.code });
|
|
}
|
|
return { ok: out.every(r => !r.errors.length), email, host, spins: out };
|
|
}
|
|
module.exports = { init, apply };
|