// Sponsor rewards at point of sale (Marty CONFIRMED 2026-11-16): level 1 (direct sponsor) 20%, levels 2 to 5 at 5% each, // company 60%, on every paid item ($10 deep report, $79 10-pack, $9 watch alerts). // Sponsor line = the network hub line (hubline.js), the one source of truth. A visitor who is not signed in but came through // ?ref= gets that member as their direct sponsor, then that member's own hub line above. // Pass-up: the ViralAdBlitz rule (VABShop.sol): walking up from the direct sponsor, a person who has never bought here is passed // over and the share goes to the next person who has; the first 5 qualified people within MAX_DEPTH places get levels 1 to 5. // A level share nobody qualifies for goes to the company (as in VABShop). No self-commission: the buyer is never in their own line. // Voucher redemptions pay nothing. STAGING: nothing is paid; the line is recorded on each test purchase and shown to the admin. 'use strict'; const db = require('./db'); const hubline = require('./hubline'); const LEVEL_PCT = [20, 5, 5, 5, 5]; const COMPANY_PCT = 60; const MAX_DEPTH = 20; const ITEMS = { deep1: { usd: 10, name: 'One deep report' }, deep10: { usd: 79, name: '10 deep reports' }, watch: { usd: 9, name: 'Watch alerts, 3 months' } }; async function qualified(p) { const r = await db.one("SELECT COUNT(*) n FROM purchases WHERE via <> 'voucher' AND ((buyer_username IS NOT NULL AND buyer_username = ?) OR (buyer_wallet IS NOT NULL AND buyer_wallet = ?))", [p.username || '\u0000', p.wallet || '\u0000']); return Number(r.n) > 0; } // buyer = { username, wallet, email } (signed-in member) or null; ref = username from the ?ref cookie async function lineFor(buyer, ref) { let raw = [], src = 'none', ok = true; if (buyer && (buyer.username || buyer.wallet)) { const l = await hubline.lineOf(buyer.username ? { ref: buyer.username } : { wallet: buyer.wallet }, MAX_DEPTH); ok = l.ok; raw = l.line || []; src = 'hub line of the buyer'; if (!raw.length && ref && (!buyer.username || ref.toLowerCase() !== buyer.username.toLowerCase())) { const r = await hubline.lineOf({ ref }, MAX_DEPTH - 1); ok = r.ok; if (r.person) raw = [{ depth: 1, id: r.person.id, username: r.person.username, wallet: r.person.wallet }].concat((r.line || []).map(x => Object.assign({}, x, { depth: x.depth + 1 }))); src = 'referral link (?ref=' + ref + '), buyer has no hub sponsor'; } } else if (ref) { const r = await hubline.lineOf({ ref }, MAX_DEPTH - 1); ok = r.ok; if (r.person) raw = [{ depth: 1, id: r.person.id, username: r.person.username, wallet: r.person.wallet || null }].concat((r.line || []).map(x => Object.assign({}, x, { depth: x.depth + 1 }))); src = 'referral link (?ref=' + ref + ')'; } const me = buyer ? [buyer.username && buyer.username.toLowerCase(), buyer.wallet && buyer.wallet.toLowerCase()].filter(Boolean) : []; const line = []; for (const p of raw) { const self = me.includes(String(p.username || '').toLowerCase()) || me.includes(String(p.wallet || '').toLowerCase()); line.push({ depth: p.depth, username: p.username || null, wallet: p.wallet || null, self, qualified: self ? false : await qualified(p) }); } return { ok, source: src, line }; } function split(usd, line, via) { const out = { usd, via, levels: [], company: 0, note: '' }; if (via === 'voucher') { out.note = 'Voucher redemption: no payment, nobody is paid.'; return out; } const q = line.filter(p => p.qualified && !p.self).slice(0, 5); let given = 0; LEVEL_PCT.forEach((pct, i) => { const p = q[i]; const amt = Math.round(usd * pct) / 100; if (p) { out.levels.push({ level: i + 1, pct, usd: amt, username: p.username, wallet: p.wallet, place: p.depth }); given += amt; } else out.levels.push({ level: i + 1, pct, usd: amt, username: null, to: 'company (nobody qualified within ' + MAX_DEPTH + ' places)' }); }); out.company = Math.round((usd - given) * 100) / 100; out.note = 'Staging: recorded only, nothing paid.'; return out; } async function record({ buyer, ref, item, via, checkId, note }) { const it = ITEMS[item]; if (!it) throw new Error('unknown item'); const l = await lineFor(buyer, ref).catch(e => ({ ok: false, source: 'error ' + e.message, line: [] })); const s = split(it.usd, l.line, via); await db.q('INSERT INTO purchases (at, buyer, buyer_username, buyer_wallet, item, usd, via, check_id, ref, line_json, split_json, note, real_money) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,0)', [Date.now(), buyer ? buyer.email || null : null, buyer ? buyer.username || null : null, buyer ? buyer.wallet || null : null, item, it.usd, via, checkId || null, ref || null, JSON.stringify(l), JSON.stringify(s), note || null]); return { line: l, split: s }; } module.exports = { record, lineFor, split, ITEMS, LEVEL_PCT, COMPANY_PCT, MAX_DEPTH };