Skip to content

Instantly share code, notes, and snippets.

@darryn
Created September 7, 2023 04:51
Show Gist options
  • Save darryn/b507aa4fd322ef8484b49152f487ff18 to your computer and use it in GitHub Desktop.
Save darryn/b507aa4fd322ef8484b49152f487ff18 to your computer and use it in GitHub Desktop.
B2B Fulfillment Constraints
// @ts-check
// Use JSDoc annotations for type safety.
/**
* @typedef {import("../generated/api").InputQuery} InputQuery
* @typedef {import("../generated/api").FunctionResult} FunctionResult
*/
/**
* @type {FunctionResult}
*/
const NO_CHANGES = {
operations: [],
};
// The @shopify/shopify_function package will use the default export as your function entrypoint.
export default /**
* @param {InputQuery} input
* @returns {FunctionResult}
*/
(input) => {
let deliverableLineIds = [];
if (input.cart.buyerIdentity?.purchasingCompany?.company?.id == null) { return NO_CHANGES; }
for (const deliverableLine of input.cart.deliverableLines) {
deliverableLineIds.push(deliverableLine.id);
}
// Find the location representing our Warehouse.
let warehouseLocation = input.locations.find(location => location.name == "Warehouse");
// Short-circuit and return no operations if the fulfillment location does not exist.
if (warehouseLocation === undefined) { return NO_CHANGES; }
// Construct the operations, including our MustFulfillFrom fulfillment constraint.
let operations = [
{
mustFulfillFrom: {
deliverableLineIds: deliverableLineIds,
locationIds: [warehouseLocation.id]
}
}
];
// Return the operation.
return { operations: operations };
};
query Input {
cart {
deliverableLines {
id
}
buyerIdentity {
purchasingCompany {
company {
id
}
}
}
}
locations(names: ["Warehouse"]) {
id
name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment