Prepaid login-ad line: when all member slots are taken, members pay now to reserve the next one (Marty, 2026-10-23: "they need to pay for the slot now to get a place in line")

- ads.js: a login buy with every slot taken returns full:true and a reserve offer; with reserve:true it is charged the full slot price at once (purchased credits only, credit log 'Login slot reserved (queue #N)') and stored with status 'queued' + queuedAt, no starts/expires, never served or counted as a slot. First paid, first served.
- Promotion (promoteLoginQueue): on a slot expiring (scheduleSweep), a member pausing a slot, every new login buy, and a one-minute tick. The oldest reservation becomes active, starts now, ends in 30 days; the owner gets an on-site notice (kind 'notice') and an email unless unsubscribed.
- Deploy-overlap safety: the cap decision runs under a lease row in the new app_locks table (one conditional UPDATE takes it, an expired lease can be taken over); every start/cancel/edit is a conditional UPDATE on status='queued', so a reservation starts at most once and a cancel racing a start has exactly one winner. Live login buys and paused-slot resumes also take the lease, so the cap holds for them too.
- A freed slot goes to the line first: a paused slot resumes only while live + queued < loginSlotsMax, with a plain message saying the line is ahead.
- Cancel before the start: full refund as purchased-grade credits (credit log 'Login slot reservation cancelled: refund'), once. Edit target/image/headline before the start, free. Cancelled reservations can be archived.
- Estimated start per place from the live slots' end dates and the reservations ahead, shown in Central time and labelled an estimate.
- db.js: campaigns.queued_at BIGINT NULL, app_locks table + 'login_queue' row, all added on boot.
- server.js: loginLine in GET /api/my/campaigns, POST /api/my/campaigns/:id/cancel-reservation and /edit-reservation (safe-browsing, reach and image checks), GET /api/admin/login-queue, POST /api/admin/login-queue/:id/cancel, the promotion notice hook and the one-minute tick.
- Member UI (my.js, my.html): open slots or the line with place and estimated start on the Login ad form, the button becomes "Reserve the next slot", IAP.confirmBox before paying, In line #N + estimated start in the list, Edit and Cancel and refund. Admin (admin.js, admin.html): Login-ad line card with owner, queued at, place, estimated start, cancel + refund. Tags bumped to 20261023lq.
- chatbot.js: new canned 'login-ads' answer and the LOGIN ADS fact updated (6 slots, the paid line, cancel = full refund before start). The knowledge base seeds the new entry on boot.
- audit.js: queued/cancelled reservations are not per-day login campaigns.
- QA: qa/login-queue.cjs (JSON store end to end) and qa/login-queue-sql.cjs (SQL path guard logic against a fake SQL layer), bash qa/run.sh login-queue. qa/login-slot.mjs pattern follows the refactor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-23 07:14:57 -05:00
parent c45f1769e7
commit 89b5969b25
13 changed files with 455 additions and 12 deletions
+53
View File
@@ -571,6 +571,25 @@ async function boot() {
creditsFor: async email => { const v = await adminMember.view(email); return (v && v.credits && v.credits.available) || 0; },
message: async (to, subject, html) => { await messages.deliver(1, ADMIN_EMAIL || 'house@instantadpay.com', [to], subject, html, 'notice'); },
});
// prepaid login-ad line (2026-10-23): when a reservation starts, its owner gets an on-site notice (kind 'notice', never the
// sign-in pop) and an email unless they unsubscribed. ads.js calls this once per start, from the one copy that won it.
ads.onLoginPromoted(async c => {
const owner = String(c.owner || '').toLowerCase(); if (!owner) return;
const name = c.name || ('campaign #' + c.id);
const until = new Date(Number(c.expires)).toLocaleString('en-US', { timeZone: 'America/Chicago', month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' }) + ' Central';
const days = Number(c.body) || ads.rates().loginSlotDays || 30;
const subject = 'Your login ad "' + name + '" is live';
const text = 'Your place in line came up. Your reserved login-ad slot has started, so "' + name + '" now shows full screen to members as they sign in, for ' + days + ' days, until ' + until + '.\n\n'
+ 'You paid for it when you reserved, so nothing more is charged. See it in Members > Campaigns: https://instantadpay.com/my#campaigns\n\nInstantAdPay';
const esc = s => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
const html = '<p>Your place in line came up. Your reserved login-ad slot has started, so <b>' + esc(name) + '</b> now shows full screen to members as they sign in, for ' + days + ' days, until ' + esc(until) + '.</p>'
+ '<p>You paid for it when you reserved, so nothing more is charged. See it in <a href="/my#campaigns">Members &gt; Campaigns</a>.</p>';
try { await messages.deliver(1, ADMIN_EMAIL || 'house@instantadpay.com', [owner], subject, html, 'notice'); } catch (e) { console.error('login line inbox', e.message); }
const stopped = drip && drip.isUnsubscribed ? await drip.isUnsubscribed(owner).catch(() => false) : false;
if (!stopped && mailer.hasKey()) { try { await mailer.send(owner, subject, text); } catch (e) { console.error('login line mail', e.message); } }
console.log('login line: #' + c.id + ' started for ' + owner.replace(/^(.).*(@.*)$/, '$1***$2'));
});
setInterval(() => ads.promoteLoginQueue().catch(e => console.error('login line', e.message)), 60 * 1000); // safety net: a slot that expired between changes
setTimeout(() => refill.tick().catch(e => console.error('refill', e.message)), 90000);
setInterval(() => refill.tick().catch(e => console.error('refill', e.message)), 20 * 60 * 1000);
setInterval(() => friday.tick().catch(e => console.error('friday', e.message)), 5 * 60 * 1000);
@@ -4665,6 +4684,7 @@ const server = http.createServer(async (req, res) => {
out.earnedReserved = pool.earnedReserved;
out.inCampaigns = pool.inCampaigns; // budget still to deliver across live campaigns
out.availableCredits = pool.available;
out.loginLine = await ads.loginQueueView(s.email); // login-ad slots: open now, the paid line, this member's places + estimated starts
return json(res, 200, out);
}
if (p === '/api/my/campaigns' && req.method === 'POST') {
@@ -4725,6 +4745,28 @@ const server = http.createServer(async (req, res) => {
const r = await ads.setStatus(s.email, m[1], m[2] === 'pause' ? 'paused' : 'active');
return json(res, r.error ? 400 : 200, r);
}
// prepaid login-ad line (2026-10-23): cancel a reservation before it starts (full refund), or edit it for free
m = /^\/api\/my\/campaigns\/(\d+)\/cancel-reservation$/.exec(p);
if (m && req.method === 'POST') {
const s = await auth.fromRequest(req);
if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
const r = await ads.cancelLoginReservation(s.email, m[1]);
return json(res, r.error ? 400 : 200, r);
}
m = /^\/api\/my\/campaigns\/(\d+)\/edit-reservation$/.exec(p);
if (m && req.method === 'POST') {
const s = await auth.fromRequest(req);
if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
const b = await readBody(req);
if (b.targetUrl !== undefined) {
const t = String(b.targetUrl || '').trim();
{ const sb = await sbcheck.check(t); if (!sb.ok) return json(res, 400, { error: sb.reason }); }
{ const rc = await reach.check(t); if (!rc.ok) return json(res, 400, { error: rc.reason }); }
}
if (b.imageUrl) { const ic = await imageCheck(b.imageUrl); if (!ic.ok) return json(res, 400, { error: ic.reason }); }
const r = await ads.editLoginReservation(s.email, m[1], b);
return json(res, r.error ? 400 : 200, r);
}
// -- admin portal: email magic-code sign-in, allowlisted to ADMIN_EMAIL
if (p === '/api/admin/auth/start' && req.method === 'POST') {
@@ -4923,6 +4965,17 @@ const server = http.createServer(async (req, res) => {
console.log('admin stop campaign', c.id, owner, String(b.reason || '').slice(0, 80), JSON.stringify({ freed: a.freed }));
return json(res, a.error ? 400 : 200, Object.assign({ id: c.id, owner }, a));
}
if (p === '/api/admin/login-queue' && req.method === 'GET') { // the prepaid login-ad line, oldest paid first (2026-10-23)
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
return json(res, 200, await ads.adminLoginQueue());
}
m = /^\/api\/admin\/login-queue\/(\d+)\/cancel$/.exec(p);
if (m && req.method === 'POST') { // admin cancel = the same full refund the owner would get
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
const r = await ads.cancelLoginReservation(null, m[1], { admin: true });
if (!r.error) console.log('admin cancelled login reservation', r.id, String(r.owner || '').replace(/^(.).*(@.*)$/, '$1***$2'), 'refunded', r.refunded);
return json(res, r.error ? 400 : 200, r);
}
if (p === '/api/admin/campaigns' && req.method === 'GET') {
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
return json(res, 200, { campaigns: await ads.adminList(), rates: ads.rates(), bannerSizes: ads.bannerSizes(), houseOwner: ads.HOUSE_OWNER });