← back to Dw Signup Fulfillment

verification/tk11114/backfill-scope.js

44 lines

'use strict';
// TK-11114 backfill SCOPE — READ-ONLY. Enumerates customers created since go-live and
// classifies each: AFFECTED (no custom.sample_verify_sent=true → never got the verify letter),
// GOT_IT (flag set), TEST/INTERNAL (excluded), TRADE (excluded — not a retail-samples signup).
// No writes, no sends. Prints a JSON summary + the AFFECTED list for Steve's go.
const path = require('path');
const shopify = require(path.join(__dirname, '..', '..', 'lib', 'shopify'));

const SINCE = process.env.SINCE || '2026-08-28T00:00:00Z';
function isInternalOrTest(e){ e=(e||'').toLowerCase(); return /@designerwallcoverings\.com$/.test(e)||/dwgolive|\btest@|example\.com$/.test(e)||/\+dwgolive/.test(e); }

async function listSince(sinceIso){
  const out=[]; let url=`/customers.json?limit=250&created_at_min=${encodeURIComponent(sinceIso)}&fields=id,email,created_at,tags,state`;
  // single page is enough for <250; paginate defensively via since_id
  let lastId=0;
  for(let i=0;i<10;i++){
    const u = url + (lastId?`&since_id=${lastId}`:'');
    const r = await shopify.request('GET', u, undefined);
    const cs = (r&&r.json&&r.json.customers)||[];
    if(!cs.length) break;
    out.push(...cs); lastId = cs[cs.length-1].id;
    if(cs.length<250) break;
  }
  return out;
}

(async()=>{
  const custs = await listSince(SINCE);
  const res={ since:SINCE, total:custs.length, affected:[], got_it:0, test_internal:0, trade:0 };
  for(const c of custs){
    const tags=(c.tags||'').toLowerCase();
    if(isInternalOrTest(c.email)){ res.test_internal++; continue; }
    if(/\btrade\b/.test(tags)){ res.trade++; continue; }
    const flag = await shopify.getCustomerMetafield(c.id,'custom','sample_verify_sent');
    if(flag && String(flag).toLowerCase()==='true'){ res.got_it++; continue; }
    res.affected.push({ id:c.id, email:c.email, created_at:c.created_at, tags:c.tags||'' });
  }
  res.affected_count=res.affected.length;
  require('fs').writeFileSync(path.join(__dirname,'backfill-affected.json'), JSON.stringify(res,null,2));
  console.log(JSON.stringify({ since:res.since, total:res.total, affected_count:res.affected_count, got_it:res.got_it, test_internal:res.test_internal, trade:res.trade },null,2));
  console.log('--- AFFECTED (first 20, email masked) ---');
  for(const a of res.affected.slice(0,20)) console.log(`${a.created_at}  ${String(a.email).replace(/(.{2}).*@/,'$1***@')}  id=${a.id}`);
})().catch(e=>{ console.error('ERR', e.message); process.exit(1); });