Holding tank batches: throttled emails + round announcement (Marty 2026-11-15: "spread the emails out", "Post it to the new members area")

Assignments still happen at once. Only the assigned people's "X is now your sponsor" emails go
into a MySQL outbox (tank_batch_outbox, survives deploys): tankBatchEmailsPerDay (100) per
Central day, evenly spaced from tankBatchEmailStartHour 8 to tankBatchEmailEndHour 20 CT,
oldest first, through the site mailer (unsubscribe rules + link). Right before each send:
still with that sponsor, not unsubscribed, flagged, spam or erased, else skipped. Exactly once
across app copies: MySQL lock + conditional UPDATE queued->sending + marks claim per row. The
switch-on reminders for a batch person start when their email goes out.

Telegram: one announcement per round for the CTB group's General topic (tankBatchAnnounce,
tankBatchAnnounceChatId blank = the payments group, tankBatchAnnounceTopicId blank = General),
with the ladder card image (banners/holding-tank-leads-1200x675.jpg): total, sponsors by
level, how to qualify; then one card per sponsor in the payments topic, at least
tankBatchFeedGapMin (2) minutes apart. Sponsor emails stay one digest per round.

Admin: GET/POST /api/admin/tank/batches/mail (queued, sent today, next send, pause/resume),
POST /api/admin/tank/batches/round (the normal round path). Preview shows the email plan.
qa/tank-batch.test.mjs 78 checks (cap, window, skip-if-changed, two copies, pause).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-15 15:39:18 -06:00
parent ade472cbc8
commit 8e6b5674ae
5 changed files with 191 additions and 24 deletions
+52 -6
View File
@@ -51,7 +51,7 @@ function bench(opts) {
async listAll() { return [...by.values()].map(pub); }, async positions() { return []; },
async sponsorOf(e) { const a = by.get(e); if (!a || !a.sponsorRef) return null; const t = a.sponsorRef; return find(x => x.username === t || x.code === t || String(x.memberId) === t); }, _by: by };
const events = []; const mails = []; const chats = []; const notices = []; const feeds = []; const toasts = []; const sent = [];
const standing = new Map(); const optOut = new Set();
const standing = new Map(); const optOut = new Set(); const announces = []; const claims = new Set();
const messages = { sendChat: async (id, from, to, text) => { chats.push({ from, to, text, at: now }); sent.push({ from, to, at: now }); }, lastFrom: async (f, t) => sent.filter(x => x.from === f && x.to === t).reduce((m, x) => Math.max(m, x.at), 0) };
const mailer = { hasKey: () => true, send: async (to, subject, text) => { mails.push({ to, subject, text }); } };
const chain = { recentEvents: () => events.slice(), getConfig: () => ({}), catalog: async () => [500, 1000, 2000, 5000, 10000, 25000, 50000].map((p, i) => ({ id: i + 1, priceCents: p })) };
@@ -59,12 +59,12 @@ function bench(opts) {
tank.init({ dataDir: dir, accounts, chain, messages, mailer, site: 'https://instantadpay.test', capOpen: () => 10,
hooks: { isBatchPick: id => tb.isBatchPick(id), isDraftPick: async () => false } });
tank.setClock(clock);
return { tank, tb, accounts, events, mails, chats, notices, feeds, toasts, standing, optOut, site, dir, messages,
return { announces, claims, tank, tb, accounts, events, mails, chats, notices, feeds, toasts, standing, optOut, site, dir, messages,
advance: ms => { now += ms; }, now: () => now,
async start() {
await tb.init({ dataDir: dir, db: { enabled: () => false }, accounts, chain, tank, messages, mailer, siteConfig: () => site, clock,
refuel: { standing: async e => standing.get(e) || null },
hooks: { companyId: () => 1, claim: async () => true, excludedPerson: async e => optOut.has(e) ? 'unsubscribed from email' : null,
hooks: { companyId: () => 1, claim: async k => { if (claims.has(k)) return false; claims.add(k); return true; }, announce: async html => { announces.push(html); }, excludedPerson: async e => optOut.has(e) ? 'unsubscribed from email' : null,
notify: async (to, subject, text) => { notices.push({ to, subject, text }); }, feed: async (who, what, level) => { feeds.push(who + ' just received ' + what + ' from the holding tank (' + level + ' level)'); }, toast: ev => toasts.push(ev) } });
} };
}
@@ -128,11 +128,18 @@ function seed(B, tankCount) {
ok(!B.tank._json.state().adoptions.some(a => a.adoptee === 'upline@tank.co' && a.adopter === 'paid@x.co'), 'never a sponsor\'s own upline');
ok(![...dealt].some(e => /@x\.co$/.test(e)), 'never a sponsor themself');
ok(B.accounts._by.get('t000@tank.co').sponsorRef === 'opgnetwerk', 'the sponsor change is written like an adoption');
const adoptMails = B.mails.filter(m => /is now your sponsor on InstantAdPay/.test(m.subject));
ok(adoptMails.length === 51, 'every person gets the normal "X is now your sponsor" email, once', adoptMails.length);
const adoptMails = () => B.mails.filter(m => /is now your sponsor on InstantAdPay/.test(m.subject));
ok(adoptMails().length === 0, 'the "X is now your sponsor" emails are queued, not sent in a burst', adoptMails().length);
const ob = () => JSON.parse(fs.readFileSync(path.join(B.dir, 'tank-batch.json'), 'utf8')).outbox;
ok(ob().filter(x => x.kind === 'mail' && x.status === 'queued').length === 51 && ob().filter(x => x.kind === 'mail').every(x => /is now your sponsor on InstantAdPay/.test(x.subject)), '51 adoption emails queued, oldest first', ob().length);
ok(!Object.keys(B.tank._ready.state().pay || {}).length, 'no switch-on reminders start before the email goes out');
ok(B.chats.length === 51 && B.chats.every(c => /matched with me from the holding tank/.test(c.text)), 'every person gets a chat from their new sponsor');
ok(B.notices.filter(x => /holding tank are in your line/.test(x.subject)).length === 3 && B.mails.filter(m => /from the holding tank are in your line/.test(m.subject)).length === 3, 'each sponsor gets one notice and one email per batch, not one per person');
ok(B.feeds.length === 3 && B.feeds[0] === '@opgnetwerk just received 21 new team members from the holding tank (Twin Engine level)', 'tank feed card: sponsor, count, level', B.feeds);
ok(B.feeds.length === 0 && B.announces.length === 0, 'Telegram posts wait for the outbox');
for (let i = 0; i < 4; i++) { await B.tb.mailTick(); await B.tb.mailTick(); B.advance(2 * 60000); }
ok(B.announces.length === 1 && /3 new|51 new members just got a sponsor/.test(B.announces[0]) && /<b>Twin Engine<\/b>: @opgnetwerk 21/.test(B.announces[0]) && /= 25;/.test(B.announces[0]) && !/t0\d\d|income|\u2014/.test(B.announces[0]), 'one round announcement: total, sponsors by level, how to qualify, no people, no income', B.announces[0]);
ok(B.feeds.length === 3 && B.feeds[0] === '@opgnetwerk just received 21 new team members from the holding tank (Twin Engine level)', 'tank feed cards: one per sponsor, sponsor, count, level', B.feeds);
ok(adoptMails().length === 1, 'one email per 7.2 minutes at most (8 ticks over 6 minutes: one)', adoptMails().length);
ok(!B.feeds.some(f => /t0\d\d/.test(f)) && B.toasts.length === 3 && B.toasts.every(t => t.type === 'Adopted' && !/t0\d\d/.test(t.member)), 'feed and toasts never name the people');
ok(!B.mails.concat(B.notices).some(m => /—/.test(m.subject + m.text)), 'no em dash in any batch copy');
// the member's own tank button and the 5x draft keep their own allowances
@@ -183,4 +190,43 @@ function seed(B, tankCount) {
const ids = B.tank._json.state().adoptions.filter(a => a.status === 'open').map(a => a.id); draftIds = new Set(ids);
ok(!(await B.tank.partnerEligibility('opg@x.co', { positions: 1, maxOpen: 10 })).ok && (await B.tank.eligibility('opg@x.co')).ok, 'ten draft picks fill the draft cap only');
}
// ---- the throttled outbox: cap, window, skip-if-changed, pause, two app copies ----
{
const B = bench({ site: { tankBatches: '1', tankBatchEmailsPerDay: 5 } }); seed(B, 12); await B.start();
await B.tb.round(); // 10 to paider? no: opg / fueler / paider share 12
const ob = () => JSON.parse(fs.readFileSync(path.join(B.dir, 'tank-batch.json'), 'utf8')).outbox.filter(x => x.kind === 'mail');
const sentMail = () => B.mails.filter(m => /is now your sponsor on InstantAdPay/.test(m.subject));
ok(ob().length === 12, '12 queued');
// window: 9 PM CT (03:00 UTC next day) nothing goes
const at = (h, m) => { const d = new Date(B.now()); const want = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), h + 6, m || 0); B.advance(want - B.now()); };
at(21, 0); await B.tb.mailTick(); ok(sentMail().length === 0, 'outside 8 AM to 8 PM CT: nothing sent');
at(7, 59 + 24 * 60); await B.tb.mailTick(); ok(sentMail().length === 0, '7:59 AM CT: still nothing');
B.advance(60000); await B.tb.mailTick(); ok(sentMail().length === 1, '8:00 AM CT: the first one goes');
const v = await B.tb.mailView(); ok(v.sentToday === 1 && v.queued === 11 && v.spacingMin === 144 && v.nextSendAt === B.now() + 144 * 60000, 'admin view: sent today, queued, next send time (12 h / 5 = 144 min apart)', v);
B.advance(60 * 60000); await B.tb.mailTick(); ok(sentMail().length === 1, 'spread through the day, not a burst');
// skip if changed: the next queued person left that sponsor, the one after unsubscribed: both skipped, the third is sent in the same tick
const q = ob().filter(x => x.status === 'queued');
B.accounts._by.get(q[0].to).sponsorRef = ''; B.optOut.add(q[1].to);
B.advance(90 * 60000); const r = await B.tb.mailTick();
ok(r.skipped === 2 && r.sent === 1 && sentMail().length === 2 && !sentMail().some(m => m.to === q[0].to || m.to === q[1].to), 'skip-if-changed: moved and unsubscribed are skipped, the next is sent', r);
ok(Object.keys(B.tank._ready.state().pay || {}).length === 2, 'the switch-on reminders start when each email goes out');
// pause
await B.tb.setPaused(true); B.advance(150 * 60000); await B.tb.mailTick(); ok(sentMail().length === 2 && (await B.tb.mailView()).paused, 'paused: nothing goes');
await B.tb.setPaused(false); await B.tb.mailTick(); ok(sentMail().length === 3, 'resumed: sending again');
// the day cap (5): 3 sent, 2 more today, then nothing until tomorrow
for (let i = 0; i < 6; i++) { B.advance(145 * 60000); if (!B.tb._inWindow(B.now(), B.tb.cfg())) break; await B.tb.mailTick(); }
const sentToday = (await B.tb.mailView()).sentToday; ok(sentToday <= 5, 'never more than tankBatchEmailsPerDay in a Central day', sentToday);
// two app copies on one store: every row goes once
for (const m of ['tankbatch.js']) delete require.cache[require.resolve(path.join(SITE, m))];
const tb2 = require(path.join(SITE, 'tankbatch.js'));
await tb2.init({ dataDir: B.dir, db: { enabled: () => false }, accounts: B.accounts, chain: { recentEvents: () => [], catalog: async () => [] }, tank: B.tank, messages: B.messages,
mailer: { hasKey: () => true, send: async (to, subject, text) => { B.mails.push({ to, subject, text, copy: 2 }); } }, siteConfig: () => B.site, clock: () => B.now(),
hooks: { claim: async k => { if (B.claims.has(k)) return false; B.claims.add(k); return true; }, excludedPerson: async e => B.optOut.has(e) ? 'unsubscribed' : null } });
B.site.tankBatchEmailsPerDay = 5000; at(9, 0 + 24 * 60);
for (let i = 0; i < 40; i++) { await Promise.all([B.tb.mailTick(), tb2.mailTick()]); B.advance(60000); }
const per = {}; for (const m of sentMail()) per[m.to] = (per[m.to] || 0) + 1;
ok(Object.values(per).every(n => n === 1) && ob().every(x => x.status === 'sent' || x.status === 'skipped'), 'two app copies: every email sent exactly once, the queue drains', per);
// preview shows the email plan
const pv = await B.tb.preview(); ok(pv.emailPlan && pv.emailPlan.perDay === 5000 && typeof pv.emailPlan.daysToClear === 'number', 'preview carries the email plan', pv.emailPlan);
}
console.log('\n' + passes + ' passed, ' + fails + ' failed'); process.exit(fails ? 1 : 0);