← back to Shopify Sample Shipping

expand-general-graphql.mjs

13 lines

import fs from 'node:fs';
import {query} from './query.mjs';
const p=JSON.parse(fs.readFileSync(new URL('./verification/general-detail.json',import.meta.url))).deliveryProfile;
const g=p.profileLocationGroups[0], z=g.locationGroupZones.nodes.find(x=>x.zone.name==='Domestic'), m=z.methodDefinitions.nodes.find(x=>x.name==='Free Shipping (No Tracking)');
const old=m.methodConditions.map(x=>x.id);
const mutation=`mutation($id:ID!,$profile:DeliveryProfileInput!){deliveryProfileUpdate(id:$id,profile:$profile){profile{id name} userErrors{field message}}}`;
const profile={locationGroupsToUpdate:[{id:g.locationGroup.id,zonesToUpdate:[{id:z.zone.id,methodDefinitionsToUpdate:[{id:m.id,weightConditionsToCreate:[{operator:'GREATER_THAN_OR_EQUAL_TO',criteria:{value:0,unit:'POUNDS'}},{operator:'LESS_THAN_OR_EQUAL_TO',criteria:{value:5,unit:'POUNDS'}}]}]}]}],conditionsToDelete:old};
const remove=await query(mutation,{id:p.id,profile:{conditionsToDelete:old}});
if(remove.deliveryProfileUpdate.userErrors.length)throw Error(JSON.stringify(remove));
const add=await query(mutation,{id:p.id,profile:{locationGroupsToUpdate:[{id:g.locationGroup.id,zonesToUpdate:[{id:z.zone.id,methodDefinitionsToUpdate:[{id:m.id,weightConditionsToCreate:[{operator:'GREATER_THAN_OR_EQUAL_TO',criteria:{value:0,unit:'POUNDS'}},{operator:'LESS_THAN_OR_EQUAL_TO',criteria:{value:1.5,unit:'POUNDS'}}]}]}]}]}});
fs.writeFileSync(new URL('./verification/general-expand-result.json',import.meta.url),JSON.stringify({old,remove,add},null,2)+'\n');console.log(JSON.stringify({remove,add}));
if(add.deliveryProfileUpdate.userErrors.length)process.exitCode=1;