← back to Dw Signup Fulfillment

verification/tk10836/verify-ship-geo.js

45 lines

'use strict';
const config = require('../../lib/config');
const shop = config.SHOP_DOMAIN, token = config.SHOPIFY_FULFILLMENT_TOKEN;
const Q = `
query($q: String!) {
  orders(first: 100, query: $q, sortKey: CREATED_AT, reverse: true) {
    nodes {
      name createdAt
      subtotalPriceSet { shopMoney { amount } }
      totalShippingPriceSet { shopMoney { amount } }
      shippingLine { title }
      shippingAddress { city provinceCode countryCodeV2 }
      lineItems(first: 25) { nodes { quantity originalUnitPriceSet { shopMoney { amount } } } }
    }
  }
}`;
(async () => {
  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: 'created_at:>2026-05-01' } }) });
  const j = await r.json();
  if (j.errors) { console.error(JSON.stringify(j.errors)); return; }
  const rows = j.data.orders.nodes.filter(o => {
    const li = o.lineItems.nodes;
    return li.length > 0 && li.every(x => Number(x.originalUnitPriceSet.shopMoney.amount) <= 5);
  });
  const us = rows.filter(o => o.shippingAddress && o.shippingAddress.countryCodeV2 === 'US');
  const free = us.filter(o => Number(o.totalShippingPriceSet.shopMoney.amount) === 0);
  console.log(`US-domestic sample-only orders since 2026-05-01: ${us.length}`);
  console.log(`  of those, shipping $0.00: ${free.length}  (${Math.round(100*free.length/Math.max(us.length,1))}%)`);
  console.log('\n-- US sample-only orders with a 3-item cart (Kelly-shaped) --');
  for (const o of us) {
    const qty = o.lineItems.nodes.reduce((s,x)=>s+x.quantity,0);
    if (qty >= 2 && qty <= 4) {
      console.log([o.name, `qty=${qty}`, `sub=${o.subtotalPriceSet.shopMoney.amount}`,
        (o.shippingLine&&o.shippingLine.title)||'-', `ship=${o.totalShippingPriceSet.shopMoney.amount}`,
        `${o.shippingAddress.city}, ${o.shippingAddress.provinceCode}`].join(' | '));
    }
  }
  console.log('\n-- any MICHIGAN sample-only orders --');
  for (const o of us.filter(o=>o.shippingAddress.provinceCode==='MI')) {
    console.log([o.name, (o.shippingLine&&o.shippingLine.title)||'-', `ship=${o.totalShippingPriceSet.shopMoney.amount}`, o.shippingAddress.city].join(' | '));
  }
})().catch(e => { console.error('ERR', e.message); process.exit(1); });