← back to Dw Signup Fulfillment

verification/tk10836/verify-3free-cart-shapes.js

70 lines

'use strict';
// Cody hole #1 revised: do the "free" 3FREE orders actually contain REAL $4.25
// samples, or $0.00 synthetic items? And does any order match Kelly's exact
// shape: ~3 real $4.25 samples + code 3FREE ?
const config = require('../../lib/config');
const shop = config.SHOP_DOMAIN, token = config.SHOPIFY_FULFILLMENT_TOKEN;
const Q = `
query($q: String!, $cursor: String) {
  orders(first: 100, query: $q, sortKey: CREATED_AT, reverse: true, after: $cursor) {
    pageInfo { hasNextPage endCursor }
    nodes {
      name createdAt discountCodes
      subtotalPriceSet { shopMoney { amount } }
      totalShippingPriceSet { shopMoney { amount } }
      shippingLine { title }
      shippingAddress { provinceCode countryCodeV2 }
      discountApplications(first: 5) { nodes { __typename allocationMethod
        ... on DiscountCodeApplication { code } 
        ... on AutomaticDiscountApplication { title } } }
      lineItems(first: 30) { nodes { title quantity
        originalUnitPriceSet { shopMoney { amount } } } }
    }
  }
}`;
async function page(q, cursor) {
  const r = await fetch(`https://${shop}/admin/api/2024-10/graphql.json`, {
    method: 'POST', headers: { 'X-Shopify-Access-Token': token, 'Content-Type': 'application/json' },
    body: JSON.stringify({ query: Q, variables: { q, cursor } }) });
  const j = await r.json();
  if (j.errors) throw new Error(JSON.stringify(j.errors));
  return j.data.orders;
}
(async () => {
  let all = [], cursor = null;
  for (let i = 0; i < 12; i++) {
    const p = await page('created_at:>2025-01-01', cursor);
    all = all.concat(p.nodes);
    if (!p.pageInfo.hasNextPage) break;
    cursor = p.pageInfo.endCursor;
  }
  const has3free = o => (o.discountCodes||[]).some(c=>String(c).toUpperCase()==='3FREE')
    || (o.discountApplications.nodes||[]).some(d=>String(d.code||'').toUpperCase()==='3FREE');
  const rows = all.filter(has3free);
  console.log(`scanned ${all.length} orders; with code 3FREE: ${rows.length}\n`);
  console.log('order | realSamples($4.25±) | zeroItems | totalUnits | preDiscountValue | ship | shipTitle | state | discountApps');
  for (const o of rows) {
    const li = o.lineItems.nodes;
    const real = li.filter(x => Number(x.originalUnitPriceSet.shopMoney.amount) >= 3.5 && Number(x.originalUnitPriceSet.shopMoney.amount) <= 6);
    const zero = li.filter(x => Number(x.originalUnitPriceSet.shopMoney.amount) === 0);
    const units = li.reduce((s,x)=>s+x.quantity,0);
    const preVal = li.reduce((s,x)=>s+x.quantity*Number(x.originalUnitPriceSet.shopMoney.amount),0);
    const apps = o.discountApplications.nodes.map(d=>d.code||d.title||d.__typename).join('+');
    console.log([o.name, real.reduce((s,x)=>s+x.quantity,0), zero.reduce((s,x)=>s+x.quantity,0), units,
      '$'+preVal.toFixed(2), '$'+o.totalShippingPriceSet.shopMoney.amount,
      (o.shippingLine&&o.shippingLine.title)||'(none)',
      o.shippingAddress?`${o.shippingAddress.provinceCode}/${o.shippingAddress.countryCodeV2}`:'?',
      apps].join(' | '));
  }
  console.log('\n=== KELLY-SHAPE TEST: code 3FREE + >=1 real $4.25 sample + 2-4 real units ===');
  const kellyShape = rows.filter(o => {
    const real = o.lineItems.nodes.filter(x => { const p=Number(x.originalUnitPriceSet.shopMoney.amount); return p>=3.5&&p<=6; })
      .reduce((s,x)=>s+x.quantity,0);
    return real >= 2 && real <= 4;
  });
  for (const o of kellyShape) {
    console.log(`  ${o.name} ${o.createdAt.slice(0,10)} ship=$${o.totalShippingPriceSet.shopMoney.amount} "${(o.shippingLine&&o.shippingLine.title)||''}" ${o.shippingAddress?o.shippingAddress.provinceCode:''}`);
  }
  if (!kellyShape.length) console.log('  NONE — no historical precedent for Kelly\'s exact redemption shape.');
})().catch(e => { console.error('ERR', e.message); process.exit(1); });