Files

34 lines
1.9 KiB
JavaScript

// Legacy bridge: former Faucet Wave / Tier One Ads members (EvolutionScript sites Marty closed)
// arrive via /from/<brand>; if their email is on the private legacy list (DATA_DIR/legacy.json,
// email -> { b: 'faucetwave'|'tier1ads'|'both', s: 'a' (advertiser) | 'e' (earner) }) they get a
// one-time welcome-back credit grant at account creation. Grants are recorded in
// DATA_DIR/legacy-grants.json so a person is only ever credited once.
const fs = require('fs');
const path = require('path');
let DATA_DIR = null, list = null, grants = null;
function init(opts) { DATA_DIR = opts.dataDir; list = null; grants = null; }
function load() {
if (list === null) { try { list = JSON.parse(fs.readFileSync(path.join(DATA_DIR, 'legacy.json'), 'utf8')); } catch (e) { list = {}; } }
if (grants === null) { try { grants = JSON.parse(fs.readFileSync(path.join(DATA_DIR, 'legacy-grants.json'), 'utf8')); } catch (e) { grants = {}; } }
}
function reload() { list = null; grants = null; load(); }
function lookup(email) { load(); return list[String(email || '').trim().toLowerCase()] || null; }
// credits for this email, or null when not listed / already granted. Records the grant.
function grant(email, cfg) {
load();
const e = String(email || '').trim().toLowerCase();
const rec = list[e];
if (!rec || grants[e]) return null;
const credits = rec.s === 'a' ? (Number(cfg.legacyCreditsAdvertiser) || 500) : (Number(cfg.legacyCreditsEarner) || 150);
grants[e] = { credits, seg: rec.s, brand: rec.b, at: Date.now() };
fs.writeFileSync(path.join(DATA_DIR, 'legacy-grants.json'), JSON.stringify(grants));
return { credits, seg: rec.s, brand: rec.b };
}
function stats() {
load();
const g = Object.values(grants);
return { listed: Object.keys(list).length, granted: g.length, credits: g.reduce((a, x) => a + (x.credits || 0), 0) };
}
module.exports = { init, reload, lookup, grant, stats };