Holding tank batches (Marty 2026-11-15), master switch tankBatches OFF

Active members with their own paid package ($20+ InstantAdPay, $25+ LinkSpin via the
refuel record) receive tank people in batches: Paid 10, Fueled 20 (counted refuel streak
or a $50+ package), Twin Engine 25 (both engines this cycle or the top package, the live
catalog's highest, $500 today), +5 per further consecutive refuel month up to 50. Every
2 days per sponsor, topped up to their size, dealt by rotation highest tier first from
tank.adoptable() (grace, rule 3, LinkSpin adoptions respected), never self, own upline,
adopted twice, flagged, spam, mistyped, erased or unsubscribed. Same tank.adopt path
(sponsor change, chat, "X is now your sponsor" email, switch-on reminders); one notice
per batch to the sponsor. Uncontacted after 7 days -> back to the tank as 'unreached'
(not counted). Tank feed: payments-topic card + live toast, sponsor/count/level only.
Member panel "How to get fed leads" (tankfeed.js). Every number is a site setting.

5xYourTeam draft gets its own cap (fiveXDraftCap, default 10): open draft picks only;
plain adoptions, the 24-hour safety net and batches each count their own people.
Admin: GET /api/admin/tank/batches/preview (read-only) + /api/admin/tank/batches.
Chatbot: coming soon only. qa/tank-batch.test.mjs (60 checks) in run.sh member.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-15 14:58:51 -06:00
parent 1a13f53b9c
commit ccfaacc935
11 changed files with 633 additions and 17 deletions
+33 -11
View File
@@ -17,13 +17,19 @@ const db = require('./db');
const CAP_DEFAULT = 10; let capFn = () => CAP_DEFAULT;
const capOpen = () => { const n = Math.round(Number(capFn())); return n >= 1 && n <= 50 ? n : CAP_DEFAULT; };
async function holding(open) { const out = []; for (const ad of open || []) { const a = await accounts.byEmail(ad.adoptee); if (!(a && Number(a.memberId) > 0)) out.push(ad); } return out; }
// each allowance counts only its own people (Marty 2026-11-15: "5x your team's draft is a different thing altogether"): the member's own
// tank button and the 24-hour safety net count plain adoptions; a 5xYourTeam draft pick counts only against the draft's cap; a holding
// tank batch (tankbatch.js) only against the batch size. A hook that throws counts the adoption as plain (the conservative side).
const pickKind = async id => { try { if (hooks.isDraftPick && await hooks.isDraftPick(id)) return 'draft'; if (hooks.isBatchPick && await hooks.isBatchPick(id)) return 'batch'; } catch (e) {} return 'plain'; };
async function holdingOf(open, kind) { const out = []; for (const ad of await holding(open)) if (await pickKind(ad.id) === kind) out.push(ad); return out; }
const TTL_MS = 7 * 86400000; // an adoption's window to convert
const MAX_ADOPTIONS = 2; // per adoptee, lifetime (dropped adoptions count too, since 2026-09-13)
const HOLD_MS = 3 * 24 * 3600 * 1000; // an adoption is a commitment: no releasing someone you picked up less than 3 days ago (Marty, 2026-09-13)
// statuses that do not count toward a person's two adoptions: a release record, and the later side of a network race
// (both sites adopted the same person within minutes; the earlier adoption won on both, sponsorfollow.js), and a record of a
// member naming who invited them (nobody adopted them; nameSponsor below)
const NOT_COUNTED = ['released', 'conflict', 'invited'];
// 'unreached' (2026-11-15): a holding tank batch person the sponsor never contacted, back in the tank; the sponsor's miss, not theirs
const NOT_COUNTED = ['released', 'conflict', 'invited', 'unreached'];
const MIN_OWN_BUY_CENTS = 2000; // adopter must have bought a $20+ package themselves
const WARN_DAYS = 10; // dormant lead: sponsor warned after this many days without contact
const RESCUE_DAYS = 14; // ...and the lead moves to the tank after this many (warning at least 4 days old)
@@ -57,10 +63,10 @@ const D = {
const rows = adopter ? await db.q("SELECT * FROM adoptions WHERE status='open' AND adopter=?", [adopter]) : await db.q("SELECT * FROM adoptions WHERE status='open'");
return rows.map(rowA);
},
async countFor(adoptee) { const r = await db.q("SELECT COUNT(*) n FROM adoptions WHERE adoptee=? AND status NOT IN ('released','conflict','invited')", [adoptee]); return Number(r[0].n); },
async countFor(adoptee) { const r = await db.q("SELECT COUNT(*) n FROM adoptions WHERE adoptee=? AND status NOT IN ('released','conflict','invited','unreached')", [adoptee]); return Number(r[0].n); },
async setStatus(id, status) { await db.q('UPDATE adoptions SET status=?, closed=? WHERE id=?', [status, Date.now(), Number(id)]); },
async recent(n) { return (await db.q('SELECT * FROM adoptions ORDER BY id DESC LIMIT ?', [Number(n) || 100])).map(rowA); },
async hasRecord(adoptee) { const r = await db.q("SELECT COUNT(*) n FROM adoptions WHERE adoptee=? AND status NOT IN ('released','conflict','invited')", [adoptee]); return Number(r[0].n) > 0; },
async hasRecord(adoptee) { const r = await db.q("SELECT COUNT(*) n FROM adoptions WHERE adoptee=? AND status NOT IN ('released','conflict','invited','unreached')", [adoptee]); return Number(r[0].n) > 0; },
async all() { return (await db.q('SELECT * FROM adoptions ORDER BY id')).map(rowA); },
async mark(lead) { const r = await db.q('SELECT * FROM lead_marks WHERE `lead`=?', [lead]); return r[0] ? { sponsor: r[0].sponsor, contacted: Number(r[0].contacted_ts || 0), warned: Number(r[0].warned_ts || 0) } : null; },
async setMark(lead, f) {
@@ -135,7 +141,7 @@ async function eligibility(email) {
if (!a) return { ok: false, reason: 'Sign in first.' };
if (!a.memberId) return { ok: false, reason: 'Switch on payouts and buy your first $20 package to adopt from the tank.' };
if (!hasOwnBuy(a.memberId)) return { ok: false, reason: 'Buy your own $20 or more package first. Adopting is for members who have made that move themselves.' };
const open = await impl().open(a.email); const held = await holding(open); const cap = capOpen();
const open = await impl().open(a.email); const held = await holdingOf(open, 'plain'); const cap = capOpen();
if (held.length >= cap) return { ok: false, reason: 'You are already helping ' + held.length + ' people from the holding tank, and ' + cap + ' at one time is the most anyone can hold. A slot opens when one of them switches on payouts, or when their 7-day window ends.', full: true, cap, held: held.length };
return { ok: true, account: a, open };
}
@@ -154,11 +160,25 @@ async function partnerEligibility(email, p) {
const payee = await firstOnContract(a);
if (!payee) return { ok: false, code: 'no_contract_line', reason: 'Neither you nor anyone above you has switched on payouts on InstantAdPay, so a pick who starts there would land under the company. Switch on payouts on InstantAdPay first (one free transaction).' };
if (hooks.payoutBlocked && await Promise.resolve(hooks.payoutBlocked(Number(payee.memberId))).catch(() => true)) return { ok: false, code: 'payout_blocked', reason: 'Your line cannot take new members right now.' };
const open = await impl().open(a.email); const held = await holding(open); const cap = Math.min(capOpen(), Number(p.maxOpen) > 0 ? Number(p.maxOpen) : capOpen());
if (held.length >= cap) return { ok: false, code: 'full', reason: 'You are already helping ' + held.length + ' people from the holding tank, and ' + cap + ' at one time is the most anyone can hold.', full: true, cap, held: held.length };
// the draft's own cap (2026-11-15): only open draft picks count, never the member's plain adoptions or tank batches
const open = await impl().open(a.email); const held = await holdingOf(open, 'draft'); const cap = Number(p.maxOpen) > 0 ? Number(p.maxOpen) : 10;
if (held.length >= cap) return { ok: false, code: 'full', reason: 'You already have ' + held.length + ' open draft picks, and ' + cap + ' at one time is the most a coach can hold.', full: true, cap, held: held.length };
return { ok: true, account: a, open };
}
// a holding tank batch (tankbatch.js, 2026-11-15): tankbatch has already decided the sponsor qualifies (active, own paid package on
// InstantAdPay or LinkSpin) and has room in their batch; this is the InstantAdPay side of the same gate as a 5x coach: a real account,
// not suspended or flagged, someone on THIS contract in their line, and that line takes payouts (else the person walks up to the company)
async function batchEligibility(email) {
const a = await accounts.byEmail(email);
if (!a) return { ok: false, code: 'no_account', reason: 'No such member.' };
const bar = hooks.excluded || hooks.isSuspended; if (bar && bar(a.email)) return { ok: false, code: 'excluded', reason: 'Adoptions are not available on this account.' };
const payee = await firstOnContract(a);
if (!payee) return { ok: false, code: 'no_contract_line', reason: 'Nobody in this line has switched on payouts on InstantAdPay.' };
if (hooks.payoutBlocked && await Promise.resolve(hooks.payoutBlocked(Number(payee.memberId))).catch(() => true)) return { ok: false, code: 'payout_blocked', reason: 'This line cannot take new members right now.' };
return { ok: true, account: a };
}
async function view(email) {
const e = String(email || '').toLowerCase();
const el = await eligibility(e);
@@ -175,8 +195,9 @@ async function adopt(adopterEmail, who, note, opts) {
// partner = a 5xYourTeam draft pick (draftpartner.js, 2026-11-15): the coach qualifies by a 5x position instead of the $20 buy,
// the window is the pick's (3 days, partner.ttlMs), the hub is asked fresh that nobody locked is taken, no reminder mails
const partner = (opts && opts.partner) || null;
const batch = !!(opts && opts.batch); // a holding tank batch (tankbatch.js): its own allowance, the grace and the two-adoptions rule still apply
const e = String(adopterEmail || '').toLowerCase();
const el = partner ? await partnerEligibility(e, partner) : await eligibility(e);
const el = partner ? await partnerEligibility(e, partner) : batch ? await batchEligibility(e) : await eligibility(e);
if (!el.ok) return { error: el.reason, code: el.code || 'not_eligible' };
const me = el.account;
const key = String(who || '').trim().toLowerCase().replace(/^@/, '');
@@ -187,10 +208,10 @@ async function adopt(adopterEmail, who, note, opts) {
if (!auto && target.opensAt > clock()) return { error: 'That member joined less than 24 hours ago, so they still have time to tell us who invited them. You can adopt them in ' + leftText(target.opensAt - clock()) + '.', code: 'grace' }; // the safety net is not held by this: it keeps its 24-hour clock from the wallet link
if (remoteOpen(target.email)) return { error: 'That member was just picked up on ' + OTHER + ', so they are no longer in the tank.', code: 'linkspin' };
if (!auto && await countAll(target.email) >= MAX_ADOPTIONS) return { error: 'That member has been adopted twice already and stays where they are.', code: 'adopted_twice' }; // the safety net never leaves anyone to the company
if ((partner || batch) && await inDownlineOf(target.email, me)) return { error: 'That member is above you in your own line.', code: 'loop' };
if (partner) { // never anyone locked on a network contract: asked fresh, a hub that does not answer refuses (the conservative side)
const lk = await lockedWhy(target.email);
if (lk) return { error: lk === 'locked' ? 'That member is locked to a sponsor on a network contract.' : 'The network record cannot be checked right now. Try again in a few minutes.', code: lk === 'locked' ? 'locked' : 'hub_unknown' };
if (await inDownlineOf(target.email, me)) return { error: 'That member is above you in your own line.', code: 'loop' };
}
const token = me.username || me.code || String(me.memberId);
const r = await setSponsor(target.email, token, 'adopt');
@@ -255,7 +276,8 @@ async function sweep() {
let done = 0, back = 0, kept = 0;
for (const ad of await impl().open(null)) {
if (ad.expires > now) continue;
if (hooks.isDraftPick && await Promise.resolve(hooks.isDraftPick(ad.id)).catch(() => true)) continue; // a 5xYourTeam pick: draftpartner.js owns its 3-day window (started = payouts on, not a wallet)
if (hooks.isDraftPick && await Promise.resolve(hooks.isDraftPick(ad.id)).catch(() => true)) continue;
if (hooks.isBatchPick && await Promise.resolve(hooks.isBatchPick(ad.id)).catch(() => true)) continue; // a holding tank batch: tankbatch.js owns its follow-up window // a 5xYourTeam pick: draftpartner.js owns its 3-day window (started = payouts on, not a wallet)
const a = await accounts.byEmail(ad.adoptee);
if (!a) { await impl().setStatus(ad.id, 'expired'); continue; }
if (a.address || a.memberId) { await impl().setStatus(ad.id, 'done'); done++; continue; }
@@ -517,7 +539,7 @@ async function autoAssignRun() {
let done = null;
for (const c of pool) {
if (c.a.email === e) continue;
if ((await holding(await impl().open(c.a.email))).length >= capOpen()) continue;
if ((await holdingOf(await impl().open(c.a.email), 'plain')).length >= capOpen()) continue;
if (hooks.payoutBlocked && await Promise.resolve(hooks.payoutBlocked(Number(c.a.memberId))).catch(() => true)) continue; // no-payout chain: they would fall to the company after all
if (await inDownlineOf(e, c.a)) continue;
const msg = 'Hi, I am ' + nameOf(c.a) + '. ' + ((a.joinedVia === 'btc4hits' || a.joinedRef === 'btc4hits') ? 'Your BTC4Hits account moved to InstantAdPay' : 'You joined InstantAdPay') + ' without a sponsor and nobody picked you up from the holding tank within 24 hours, so you were matched with me. I will walk you through the first steps whenever you are ready. Reply here.';
@@ -718,5 +740,5 @@ async function adminView() {
return { waiting: await waiting(), adoptions: recent.map(ad => Object.assign({}, ad, { adopteeName: names[ad.adoptee], adopterName: names[ad.adopter] })), cap: capOpen(), ttlDays: TTL_MS / 86400000, holdHours: READY_MS / 3600000, aligned: alignedRecent(30), payReminders: payView() };
}
module.exports = { _ready: { state: () => RS.load(), save: () => RS.save() }, _json: { state: () => J.db, save: () => J.save() } /* member erase (erase.js) */, partnerEligibility, openOf, closeAdoption, countAll, remoteCount: email => remoteCount(String(email || '').toLowerCase()), lockedWhy, inTank, setSponsorDeliberate: (email, token, by, note) => setSponsor(email, token, by, note), HOLD_MS, NOT_COUNTED,
module.exports = { batchEligibility, holdingOf, firstOnContract, inDownlineOf, markOf: email => impl().mark(String(email || '').toLowerCase()), _ready: { state: () => RS.load(), save: () => RS.save() }, _json: { state: () => J.db, save: () => J.save() } /* member erase (erase.js) */, partnerEligibility, openOf, closeAdoption, countAll, remoteCount: email => remoteCount(String(email || '').toLowerCase()), lockedWhy, inTank, setSponsorDeliberate: (email, token, by, note) => setSponsor(email, token, by, note), HOLD_MS, NOT_COUNTED,
init, view, adopt, holdFor, markReady, readyRec, readyTick, autoAssign, payTick, payView, activateLink, PAY_R1_MS, PAY_R2_MS, alignToNetwork, alignedRecent, setClock, eligibility, READY_MS, release, sweep, rescueSweep, rescueInfo, markContacted, adminView, waiting, adoptable, adoptableOf, openedSince, leftText, inviteAsk, dismissInvite, nameSponsor, GRACE_MS, hasOwnBuy, recordGift, onFollow, summaryMap, summaryFor, historyMap, capOpen, TTL_MS, WARN_DAYS, RESCUE_DAYS, MAX_ADOPTIONS };