← back to Dw Signup Fulfillment

verification/tk10836/verify-kelly-cart-profiles.js

49 lines

#!/usr/bin/env node
// TK-10836 Option D (4/n): Kelly's ACTUAL abandoned checkout -> per-line delivery profile + weight,
// and the recorded shipping line. Read-only.
const fs=require('fs'),os=require('os');
for (const line of fs.readFileSync(os.homedir()+'/Projects/secrets-manager/.env','utf8').split('\n')) {
  const m=line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*)$/); if(!m) continue;
  let v=m[2].trim().replace(/^['"]|['"]$/g,''); if(!process.env[m[1]]) process.env[m[1]]=v;
}
const STORE=process.env.SHOPIFY_STORE_DOMAIN||'designer-laboratory-sandbox.myshopify.com';
const TOKEN=process.env.SHOPIFY_FULL_ACCESS_TOKEN, API='2024-10';
async function gql(q,v={}){const r=await fetch(`https://${STORE}/admin/api/${API}/graphql.json`,{method:'POST',
  headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});
  const j=await r.json(); if(j.errors)console.error('ERR',JSON.stringify(j.errors).slice(0,800)); return j.data;}

const IDS=['34015098110003','34017552728115'];
(async()=>{
 for(const id of IDS){
  const d=await gql(`query($id:ID!){ node(id:$id){ ... on AbandonedCheckout {
    id abandonedCheckoutUrl createdAt
    customer{ email }
    subtotalPriceSet{ shopMoney{amount} }
    totalPriceSet{ shopMoney{amount} }
    totalDiscountSet{ shopMoney{amount} }
    totalTaxSet{ shopMoney{amount} }
    shippingAddress{ city provinceCode countryCodeV2 zip }
    lineItems(first:20){ nodes{ title quantity
      variant{ id sku title price
        inventoryItem{ requiresShipping measurement{ weight{unit value} } }
        deliveryProfile{ id name }
        product{ title productType vendor } } } } } } }`,{id:`gid://shopify/AbandonedCheckout/${id}`});
  const ac=d?.node;
  if(!ac){ console.log(`\n### AbandonedCheckout ${id}: NOT FOUND`); continue; }
  console.log(`\n### AbandonedCheckout ${id} — ${ac.customer?.email} — ${ac.createdAt}`);
  console.log(`  subtotal=$${ac.subtotalPriceSet?.shopMoney?.amount} discount=$${ac.totalDiscountSet?.shopMoney?.amount} TOTAL=$${ac.totalPriceSet?.shopMoney?.amount}`);
  const items=Number(ac.subtotalPriceSet?.shopMoney?.amount||0), total=Number(ac.totalPriceSet?.shopMoney?.amount||0), tax=Number(ac.totalTaxSet?.shopMoney?.amount||0);
  console.log(`  tax=$${tax} => IMPLIED SHIPPING $${(total-items-tax).toFixed(2)}  dest=${ac.shippingAddress?ac.shippingAddress.city+', '+ac.shippingAddress.provinceCode+' '+ac.shippingAddress.zip:'(none)'}`);
  const profiles=new Set(); let wt=0;
  for(const li of ac.lineItems.nodes){
    const v=li.variant;
    const w=v?.inventoryItem?.measurement?.weight;
    if(w && w.unit==='POUNDS') wt += (w.value||0)*li.quantity;
    profiles.add(v?.deliveryProfile?.name || '(no variant/profile)');
    console.log(`   - ${li.quantity}x "${li.title}" sku=${v?.sku} price=$${v?.price} reqShip=${v?.inventoryItem?.requiresShipping} weight=${w?w.value+w.unit:'?'} PROFILE="${v?.deliveryProfile?.name}"`);
  }
  console.log(`  => distinct delivery profiles in cart: ${[...profiles].join(' | ')}`);
  console.log(`  => summed weight (lb): ${wt}`);
 }
})();