// Member erase helper (Marty 2026-11-15, ticket LS-1021): take one person out of a data document without touching anyone // else's entries. The SAME file ships in InstantAdPay and LinkSpin (qa/erase.test.cjs checks the copies match). // // needles = { emails: [...], exact: [...] }: an email matches anywhere inside a string at word edges ("x|a@b.com|y", // "tankpay1:a@b.com:17"); an exact needle (username, referral code, hub id) only matches a whole string or key, so a short // username never eats unrelated text. Rules, applied from the top of the document down: // - a key that matches is deleted with everything under it (byEmail[a@b.com], hashes[a@b.com], own[..][a@b.com]) // - an array item that is a matching string, or an object with a matching field (other than a pointer, below), is removed // - in a map, a record that is THEIRS (its email / owner / adoptee / username ... field matches) is removed whole // (clicks {owner}, refill records {owner}, rows {email}) // - in another member's record, a field that only POINTS at them (sponsorEmail, prevEmail, adopter, ...) is set to null // - any other matching string value in a map is deleted with its key (byCode[code] = email) // - anything else is walked into. Numbers, booleans and other people's records stay as they were. // Returns how many keys / items / records / fields were removed or blanked (0 = nothing about them in it). 'use strict'; const esc = s => String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); function matcher(needles) { const emails = [...new Set(((needles && needles.emails) || []).map(e => String(e || '').trim().toLowerCase()).filter(e => e.includes('@')))]; const exact = new Set(((needles && needles.exact) || []).map(x => String(x || '').trim().toLowerCase()).filter(x => x.length >= 3)); const re = emails.length ? new RegExp('(^|[^a-z0-9._%+-])(' + emails.map(esc).join('|') + ')(?![a-z0-9-]|\\.[a-z0-9])', 'i') : null; const hit = s => { if (typeof s !== 'string' || !s) return false; const l = s.trim().toLowerCase(); return exact.has(l) || (re ? re.test(s) : false); }; const inText = s => { if (!s) return false; if (re && re.test(s)) return true; for (const x of exact) if (s.toLowerCase().includes('"' + x + '"')) return true; return false; }; return { hit, inText, empty: !emails.length && !exact.size }; } const isObj = v => v && typeof v === 'object' && !Array.isArray(v); const OWN = new Set(['email', 'owner', 'owneremail', 'adoptee', 'member', 'memberemail', 'user', 'username', 'account', 'lead', 'person', 'recipient', 'e', 'u']); const REF = new Set(['sponsoremail', 'prevemail', 'previousemail', 'adopter', 'adopteremail', 'sponsor', 'from', 'to', 'by', 'inviter', 'referrer', 'mentor', 'coach']); const otherHit = (v, m) => isObj(v) && Object.entries(v).some(([k, x]) => typeof x === 'string' && !REF.has(k.toLowerCase()) && m.hit(x)); // not just a pointer const ownHit = (v, m) => isObj(v) && Object.entries(v).some(([k, x]) => typeof x === 'string' && OWN.has(k.toLowerCase()) && m.hit(x)); function scrubDeep(node, needles, m0) { const m = m0 || matcher(needles); if (m.empty) return 0; let n = 0; if (Array.isArray(node)) { for (let i = node.length - 1; i >= 0; i--) { const v = node[i]; if ((typeof v === 'string' && m.hit(v)) || otherHit(v, m)) { node.splice(i, 1); n++; } // their item / a log line about them else if (v && typeof v === 'object') n += scrubDeep(v, null, m); } } else if (isObj(node)) { for (const k of Object.keys(node)) { const v = node[k]; if (m.hit(k) || ownHit(v, m)) { delete node[k]; n++; continue; } if (typeof v === 'string' && m.hit(v)) { if (REF.has(k.toLowerCase())) node[k] = null; else delete node[k]; n++; continue; } if (v && typeof v === 'object') n += scrubDeep(v, null, m); } } return n; } // a whole document as text: JSON is parsed, scrubbed and written back; JSON Lines lose every line that mentions them. // Returns { body, removed } (body unchanged and removed 0 when nothing matched). function scrubText(body, needles, isLines) { const m = matcher(needles); if (m.empty || !m.inText(body)) return { body, removed: 0 }; if (isLines) { const lines = String(body).split('\n'); const keep = lines.filter(l => !m.inText(l)); return { body: keep.join('\n'), removed: lines.length - keep.length }; } const obj = JSON.parse(body); const removed = scrubDeep(obj, null, m); return { body: removed ? JSON.stringify(obj) : body, removed }; } const mentions = (body, needles) => matcher(needles).inText(String(body || '')); module.exports = { scrubDeep, scrubText, mentions, matcher };