Prepaid login-ad slot queue (Marty, 2026-10-23: "they need to pay for the slot now to get a place in line")

When all member login slots are taken the Ads screen offers "Reserve the next slot" instead of a dead button.
Same price and checks as a slot (purchased credits only), paid at reserve time; the campaign is 'queued' with
queuedAt, never serves and has no 30-day clock. First paid, first served: the member sees their place and an
estimated start (Central time, labelled an estimate) built from the live slots' end dates and the line ahead.
A freed slot (ended, paused, or found by the new 1-minute tick) goes to the oldest reservation: starts now,
expires now + 30 days, never more than loginSlotsMax live. Idempotent across the deploy overlap: JSON mode takes
a lock file and records every promotion/cancel in DATA_DIR/login-queue.json, re-read before any change; MySQL
mode uses guarded UPDATEs (status + live count in one statement). Owner gets an on-site notice and an email
(unsubscribes respected). A paused slot resumes only when live + queued < max. Cancel before start = full
refund (credited back as purchased credits; any unburned accrual released). Edits while queued are free and keep
the free correction. Admin: Login-ad queue card with cancel + refund, plus the Member content action.
New credit activity log (credit-log.json / credit_log table): reserved (queue #N), bought, started, refunds,
shown on the Ads screen. Audit skips queued/cancelled login rows. Chatbot CANNED + FACTS updated (6 slots, paid
queue, full refund before start). Tags bumped: ads-screen.js, admin.js, my.js 20261023q.
Tests: qa/loginqueue.test.cjs (two app copies promoting at once) + qa/loginqueue.e2e.mjs; bash qa/run.sh loginqueue.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-23 07:11:37 -05:00
parent 7c96e59db7
commit 1c2ef3780c
15 changed files with 504 additions and 28 deletions
+32 -1
View File
@@ -611,6 +611,17 @@ async function boot() {
setTimeout(() => ads.dailySweep().catch(e => console.error('sweep', e.message)), 60 * 1000);
setInterval(() => ads.dailySweep().catch(e => console.error('sweep', e.message)), 60 * 60 * 1000);
setInterval(() => ads.scheduleSweep().catch(e => console.error('schedule sweep', e.message)), 5 * 60 * 1000); // scheduled starts/ends
// prepaid login-slot queue (Marty, 2026-10-23): a freed slot goes to the oldest paid reservation within the minute
setInterval(() => ads.promoteLoginQueue().catch(e => console.error('login queue', e.message)), 60 * 1000);
ads.setLoginQueueNotifier(async (c, info) => {
const subject = 'Your login ad is live: ' + c.name;
const text = 'Good news. Your reserved login-ad slot "' + c.name + '" (#' + c.id + ') reached the front of the line and started just now.\n\n'
+ 'Your page ' + c.targetUrl + ' now shows full screen to every member right after they sign in, for ' + info.days + ' days, until ' + info.endsCt + ' (Central time). '
+ 'You paid for it when you reserved, so nothing more is charged.\n\nSee it on the Ads screen: ' + SITE_URL + '/ads';
const html = '<p>' + text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/(https?:\/\/[^\s]+)/g, '<a href="$1" target="_blank" rel="noopener">$1</a>').split('\n\n').join('</p><p>') + '</p>';
try { await messages.deliver(1, ADMIN_EMAIL || 'house@' + SITE_HOST, [c.owner], subject, html, 'notice'); } catch (e) { console.error('login queue notice', e.message); }
try { if (mailer.hasKey() && !(await drip.isUnsubscribed(c.owner))) await mailer.send(c.owner, subject, text + '\n\nStop these emails: ' + drip.unsubUrl(c.owner) + '\n\nLinkSpin'); } catch (e) { console.error('login queue email', e.message); }
});
// follow-up email sequence: send whatever came due (every 10 min, first pass shortly after boot)
coach.init({ dataDir: DATA_DIR, chain, accounts, mailer, ads, tank, lb: () => leaderboard, siteConfig });
pipeline.init({ dataDir: DATA_DIR, accounts, coach, chain });
@@ -3388,7 +3399,10 @@ const server = http.createServer(async (req, res) => {
let gauntletPending = false;
if (!(await ads.welcomeGranted(s.email))) { const tour = (await uplineSlides(s.email)).filter(a => a.lineTargetUrl); if (!tour.length) await ads.grantWelcome(s.email); else gauntletPending = true; }
const out = { campaigns: await ads.listCampaigns(s.email), rates: ads.rates(), bannerSizes: ads.bannerSizes(), gauntletPending, audiences: audiences.list(s.email) };
out.loginSlots = await ads.loginSlots(); // { max, used, free, credits, days }: the Ads screen shows how many are open
out.loginSlots = await ads.loginSlots(); // { max, used, queued, free, credits, days }: the Ads screen shows how many are open
out.loginQueue = await ads.myLoginQueue(s.email); // my paid reservations: place in line + estimated start (Central)
out.loginNext = await ads.loginNext(); // the place and estimated start a new reservation would get
out.creditLog = await ads.creditLog(s.email, 20); // credit activity (login slots, reservations, refunds)
out.editFee = editFee(); // credits per edit after each campaign's free one (the editor says which this is)
out.clickSources = await coach.clickSources(out.campaigns.map(c => c.id));
out.hours = await ads.hoursFor(out.campaigns.map(c => c.id)); // on-site views per UTC hour, last 7 days
@@ -3443,6 +3457,13 @@ const server = http.createServer(async (req, res) => {
const r = await ads.topUpCampaign(s.email, memberId, m[1], b.credits);
return json(res, r.error ? 400 : 200, r);
}
m = /^\/api\/my\/campaigns\/(\d+)\/cancel-reservation$/.exec(p); // a queued login slot: cancel before start = full refund
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+)\/(pause|resume)$/.exec(p);
if (m && req.method === 'POST') {
const s = await auth.fromRequest(req);
@@ -3547,6 +3568,16 @@ const server = http.createServer(async (req, res) => {
const r = await editAd(m[1], await readBody(req), { admin: true });
return json(res, r.error ? 400 : 200, r);
}
if (p === '/api/admin/login-queue' && req.method === 'GET') { // the paid login-slot line, oldest first
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
return json(res, 200, { queue: await ads.loginQueue(), slots: await ads.loginSlots() });
}
m = /^\/api\/admin\/campaigns\/(\d+)\/cancel-reservation$/.exec(p); // admin cancel + full refund of a queued login slot
if (m && req.method === 'POST') {
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
const r = await ads.cancelLoginReservation(null, m[1], { admin: true });
return json(res, r.error ? 400 : 200, r);
}
m = /^\/api\/admin\/campaigns\/(\d+)\/(pause|resume)$/.exec(p);
if (m && req.method === 'POST') {
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });