Created
September 7, 2023 04:51
-
-
Save darryn/b507aa4fd322ef8484b49152f487ff18 to your computer and use it in GitHub Desktop.
B2B Fulfillment Constraints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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 }; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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