5xYourTeam The Draft on the network holding tank (Marty approved the rules 2026-11-15): signed partner routes draftpartner.js, LOCAL ONLY

POST /api/partner/draft/{pool,adopt,release,status}, HMAC DRAFT_PARTNER_SECRET over 'draft.v1.' + ts + newline + body (X-Draft-Ts /
X-Draft-Sig, 5 min), 503 until the secret is set; GET /api/admin/draft (picks + log). Every pick is tank.adopt() with opts.partner:
the signed 5x claim (positions >= 1) replaces the own-$20-buy rule for these calls only (the member's own tank button keeps it); the
coach needs an account here with a wallet linked and someone on this contract in their line (else a pick who starts here would walk
up to the company), not flagged, under the people-held cap; max 10 open picks; max 2 adoptions per person; the 24 h grace; rule 3
plus a fresh hub lock check at adopt (hub silent = refused); pool also drops spam/flagged (fraud + link-farm list), throwaway and
mistyped email domains, anyone adopted twice. Cards carry no contact data (hub ids or opaque i_ ids, door, joined, last seen,
wallet yes/no, surfing credits; games null, not tracked here). The adoption email is the only mail: no payTrack reminders, no
payments-topic post. Sponsor moves via setSponsor (sponsorfollow + hub sponsor-change, by 'adopt' / 'release').

Pick window: adoption row expires +3 days; tank.sweep skips picks (hooks.isDraftPick); draftpartner.sweep (hourly) closes them by the
pick rule: started (payouts on / purchase) = done, adopted twice or locked = kept with the coach, hub silent = wait, else back to the
tank with the original join time (no new grace). Coach release after 48 h. rid makes adopt/release idempotent. A pick closed some
other way reconciles as 'moved'. MySQL tables draft_picks + draft_log (JSON fallback locally); every call logged with a code/reason.

Tests: qa/draft-partner.test.mjs 66/66 (real tank.js, mocked clock, fake hub + a local copy's routes on 8825), in run.sh member and
run.sh draft; bash qa/run.sh member exit 0 (bugs 0); tank-cap 13/13, sponsorfollow 30/30.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-15 07:20:14 -06:00
parent 7f57aedcb6
commit e17d8c4fbb
5 changed files with 672 additions and 12 deletions
+47 -12
View File
@@ -114,6 +114,7 @@ async function waiting() {
// what each of them has earned by surfing on their own, no sponsor (Marty, 2026-09-29), so an adopter can spot the workers
const tot = ads && ads.earnedTotals ? await ads.earnedTotals(rows.map(a => a.email)) : {};
return rows.map(a => ({ email: a.email, name: nameOf(a), username: a.username || null, joined: a.created, lastSeen: a.lastSeen || 0,
via: a.joinedVia || null, // the door they came in by (the 5xYourTeam draft card shows it)
wallet: !!a.address, activated: !!a.memberId, // in the tank "activated" is always false (payouts on = out of the tank); wallet can still be linked
earned: (tot[a.email] || {}).earned || 0, balance: (tot[a.email] || {}).balance || 0,
// wallet linked = ready to switch on payouts, held for an adopter (24-hour clock, autoAssign after that)
@@ -139,6 +140,25 @@ async function eligibility(email) {
return { ok: true, account: a, open };
}
// 5xYourTeam coaches (Marty 2026-11-15: "not a fan of people adopting if they have nothing vested"; a $10 5x position qualifies,
// no IAP $20 package needed). draftpartner.js has already checked the signed claim from 5xYourTeam (p.positions); this is the
// InstantAdPay side of the same gate: a real account here with a wallet linked, not suspended or flagged, someone on THIS
// contract in their line (them or an upline with payouts on, else a pick who starts here would be walked up to the company, the
// one thing the tank exists to stop; the nameSponsor rule), and room under the same people-held-at-once cap as every adopter.
async function partnerEligibility(email, p) {
const a = await accounts.byEmail(email);
if (!a) return { ok: false, code: 'coach_unknown', reason: 'This coach has no InstantAdPay account yet. Sign in on InstantAdPay once (network sign-in) and link the same wallet.' };
if (!(Number(p && p.positions) >= 1)) return { ok: false, code: 'not_vested', reason: 'Only 5xYourTeam position holders can draft and coach.' };
if (!a.address) return { ok: false, code: 'no_wallet', reason: 'Link a wallet on InstantAdPay first. A coach needs a linked wallet.' };
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: '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 };
return { ok: true, account: a, open };
}
async function view(email) {
const e = String(email || '').toLowerCase();
const el = await eligibility(e);
@@ -152,27 +172,36 @@ async function view(email) {
async function adopt(adopterEmail, who, note, opts) {
const auto = !!(opts && opts.auto); // the 24-hour safety net (autoAssign): same path, a different record note, both sides emailed
// 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 e = String(adopterEmail || '').toLowerCase();
const el = await eligibility(e);
if (!el.ok) return { error: el.reason };
const el = partner ? await partnerEligibility(e, partner) : 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(/^@/, '');
const list = await waiting();
const target = list.find(w => (w.username && w.username.toLowerCase() === key) || w.email === key);
if (!target) return { error: 'That member is no longer in the tank.' };
if (target.email === e) return { error: 'That is you.' };
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()) + '.' }; // 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.' };
if (!auto && await countAll(target.email) >= MAX_ADOPTIONS) return { error: 'That member has been adopted twice already and stays where they are.' }; // the safety net never leaves anyone to the company
if (!target) return { error: 'That member is no longer in the tank.', code: 'gone' };
if (target.email === e) return { error: 'That is you.', code: 'self' };
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) { // 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');
if (r.error) return r;
const now = Date.now();
const tgt0 = await accounts.byEmail(target.email); const b4h = !!(tgt0 && (tgt0.joinedVia === 'btc4hits' || tgt0.joinedRef === 'btc4hits')); // a BTC4Hits import (2026-11-11)
const text = String(note || '').trim().slice(0, 600) || ('Hi, I am ' + nameOf(me) + '. ' + (b4h ? 'Your BTC4Hits account moved to InstantAdPay' : 'You joined InstantAdPay') + ' without a sponsor, so I picked you up from the holding tank. I will walk you through the first three steps whenever you are ready. Reply here.');
const ad = await impl().add({ adoptee: target.email, adopter: e, ts: now, expires: now + TTL_MS, status: 'open', note: (opts && opts.recordNote) || text });
const text = String(note || '').trim().slice(0, 600) || ('Hi, I am ' + nameOf(me) + (partner ? ', your coach from 5xYourTeam' : '') + '. ' + (b4h ? 'Your BTC4Hits account moved to InstantAdPay' : 'You joined InstantAdPay') + ' without a sponsor, so I picked you up from the holding tank' + (partner ? ' in The Draft' : '') + '. I will walk you through the first three steps whenever you are ready. Reply here.');
const ttl = partner && Number(partner.ttlMs) > 0 ? Number(partner.ttlMs) : TTL_MS;
const ad = await impl().add({ adoptee: target.email, adopter: e, ts: now, expires: now + ttl, status: 'open', note: (opts && opts.recordNote) || text });
{ const rr = RS.load().ready[target.email]; if (rr && !rr.done) { rr.done = now; rr.adoptedBy = e; RS.save(); } } // the hold lifts: their sponsor resolves now
payTrack(target.email, e, ad.id, auto); // switch-on-payouts reminders at 24 h and 72 h (payTick)
if (!partner) payTrack(target.email, e, ad.id, auto); // switch-on-payouts reminders at 24 h and 72 h (payTick); a draft pick gets the adoption email only (Marty 2026-11-15)
const link = activateLink();
try { await messages.sendChat(me.memberId || 0, e, target.email, text + '\n\n' + chatPayLine(link)); } catch (err) {}
if (mailer && mailer.hasKey()) {
@@ -187,7 +216,7 @@ async function adopt(adopterEmail, who, note, opts) {
} catch (err) {}
}
}
return { ok: true, auto, adoption: ad, name: target.name, adopterName: nameOf(me), adopteeName: target.name };
return { ok: true, auto, adoption: ad, name: target.name, adopterName: nameOf(me), adopteeName: target.name, adoptee: target.email };
}
// pay it forward: give one of your own free referrals to the tank
@@ -226,6 +255,7 @@ 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)
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; }
@@ -677,6 +707,10 @@ async function nameSponsor(email, who, opts) {
return { ok: true, name: nameOf(sp), sponsorEmail: sp.email, member: name };
}
// the open adoptions of one person (draftpartner.js checks a pick's adoption is still the open one), and closing one
async function openOf(email) { const e = String(email || '').toLowerCase(); return (await impl().open(null)).filter(ad => ad.adoptee === e); }
async function closeAdoption(id, status) { await impl().setStatus(id, status); }
async function adminView() {
const recent = await impl().recent(200);
const names = {};
@@ -684,4 +718,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 = { 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 };
module.exports = { 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 };