← back to Dw Signup Fulfillment

verification/tk11120/browser-cart-setup.js

32 lines

'use strict';
// TK-11120 #3 setup: create a verified-sample-tagged test customer + build a 5-swatch
// cart permalink for the browser checkout test. Prints EMAIL / ID / PERMALINK.
// (Cleanup: delete the customer with --delete <id> after the test.)
const https = require('https'); const fs = require('fs'); const os = require('os'); const path = require('path');
const SHOP = 'designer-laboratory-sandbox.myshopify.com'; const VER = '2024-10';
const STORE_HOST = 'designerwallcoverings.com';
function envval(k){for(const f of [path.join(os.homedir(),'Projects/secrets-manager/.env')]){try{const m=fs.readFileSync(f,'utf8').match(new RegExp('^'+k+'=(.*)$','m'));if(m)return m[1].replace(/^["']|["']$/g,'').trim();}catch{}}return '';}
const ADMIN=envval('SHOPIFY_ADMIN_TOKEN'); const FULFILL=envval('SHOPIFY_FULFILLMENT_TOKEN');
function api(token,method,p,body){return new Promise(r=>{const d=body?JSON.stringify(body):null;const req=https.request({hostname:SHOP,path:`/admin/api/${VER}/${p}`,method,headers:{'X-Shopify-Access-Token':token,'Content-Type':'application/json',...(d?{'Content-Length':Buffer.byteLength(d)}:{})}},res=>{let x='';res.on('data',c=>x+=c);res.on('end',()=>{let j=null;try{j=JSON.parse(x);}catch{}r({status:res.statusCode,json:j});});});req.on('error',e=>r({status:0,error:e.message}));if(d)req.write(d);req.end();});}
function gql(token,q){return new Promise(r=>{const b=JSON.stringify({query:q});const req=https.request({hostname:SHOP,path:`/admin/api/${VER}/graphql.json`,method:'POST',headers:{'X-Shopify-Access-Token':token,'Content-Type':'application/json','Content-Length':Buffer.byteLength(b)}},res=>{let x='';res.on('data',c=>x+=c);res.on('end',()=>{try{r(JSON.parse(x));}catch{r({raw:x});}});});req.on('error',e=>r({error:e.message}));req.write(b);req.end();});}

(async()=>{
  const args=process.argv.slice(2);
  if(args[0]==='--delete'){const s=await api(FULFILL,'DELETE',`customers/${args[1]}.json`);console.log('deleted',args[1],'->',s.status);return;}
  // 5 active $4.25 sample variants
  const pv=await gql(ADMIN,`{ productVariants(first:25, query:"title:Sample"){ nodes{ id price product{ status } } } }`);
  const all=(pv.data&&pv.data.productVariants&&pv.data.productVariants.nodes)||[];
  const samp=all.filter(v=>String(v.price)==='4.25'&&v.product&&v.product.status==='ACTIVE').slice(0,5).map(v=>String(v.id).split('/').pop());
  if(samp.length<5){console.log('WARN only',samp.length,'sample variants');}
  // tagged test customer
  const email=`steve+tk11120-cart-${Date.now()}@designerwallcoverings.com`;
  const c=await api(FULFILL,'POST','customers.json',{customer:{email,first_name:'CartCheck',tags:'verified-sample',verified_email:true}});
  const cust=c.json&&c.json.customer;
  if(!cust){console.log('CUSTOMER CREATE FAILED',c.status,JSON.stringify(c.json));return;}
  const permalink=`https://${STORE_HOST}/cart/${samp.map(id=>id+':1').join(',')}`;
  console.log('EMAIL='+email);
  console.log('ID='+cust.id);
  console.log('TAGS='+cust.tags);
  console.log('PERMALINK='+permalink);
})().catch(e=>{console.error('ERR',e.message);process.exit(1);});