-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdemonstration-fulfillment-order-move.json
25 lines (25 loc) · 7.64 KB
/
demonstration-fulfillment-order-move.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"docs": "This task demonstrates how to move line items from their assigned fulfillment location to another. Configure it with the SKUs that should be moved and the name of location they should be moved to. Then, when new orders are created, if any of the configured SKUs appear on a line item that is assigned to any other location besides the configured one, the task will move it.\n\nNote: This task does not check inventory at the confiugred location before making a move, instead only verifying that the SKU is stocked there.",
"halt_action_run_sequence_on_error": false,
"name": "Demonstration: Fulfillment order move",
"online_store_javascript": null,
"options": {
"move_to_location_name__required": null,
"skus_to_move__array_required": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign skus_to_move = options.skus_to_move__array_required %}\n{% assign move_to_location_name = options.move_to_location_name__required %}\n\n{% if event.topic == \"shopify/orders/create\" or event.topic == \"mechanic/user/order\" %}\n {% comment %}\n --validate that the configured location exists and is active\n {% endcomment %}\n\n {% capture query %}\n query {\n locations(\n first: 1\n query: {{ move_to_location_name | json | prepend: \"name:\" | json }}\n ) {\n nodes {\n id\n isActive\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"locations\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Location/1234567890\",\n \"isActive\": true\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign move_to_location = result.data.locations.nodes.first %}\n\n {% if move_to_location == blank %}\n {% error \"The configured location name does not match a location in the shop.\" %}\n {% break %}\n\n {% elsif move_to_location.isActive == false %}\n {% error \"The configured location is not active.\" %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- get all \"displayble\" (i.e. not closed) fulfillment orders and filter by the line items that can move to the configured location\n {% endcomment %}\n\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n name\n fulfillmentOrders(\n displayable: true\n first: 10\n ) {\n nodes {\n id\n status\n locationsForMove(\n first: 1\n locationIds: [\n {{ move_to_location.id | json }}\n ]\n ) {\n nodes {\n location {\n name\n }\n availableLineItems(first: 100) {\n nodes {\n id\n remainingQuantity\n sku\n }\n }\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"name\": \"#PREVIEW\",\n \"fulfillmentOrders\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/FulfillmentOrder/1234567890\",\n \"status\": \"OPEN\",\n \"locationsForMove\": {\n \"nodes\": [\n {\n \"availableLineItems\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/FulfillmentOrderLineItem/1234567890\",\n \"remainingQuantity\": 1,\n \"sku\": {{ skus_to_move.first | json }}\n },\n {\n \"id\": \"gid://shopify/FulfillmentOrderLineItem/9876543210\",\n \"remainingQuantity\": 2,\n \"sku\": \"NOT-GONNA-MOVE\"\n }\n ]\n }\n }\n ]\n }\n },\n {\n \"id\": \"gid://shopify/FulfillmentOrder/2345678901\",\n \"status\": \"OPEN\",\n \"locationsForMove\": {\n \"nodes\": [\n {\n \"availableLineItems\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/FulfillmentOrderLineItem/2345678901\",\n \"remainingQuantity\": 3,\n \"sku\": {{ skus_to_move.first | json }}\n }\n ]\n }\n }\n ]\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign order = result.data.order %}\n\n {% assign fulfillment_order_ids_and_line_items_to_move = hash %}\n\n {% for fulfillment_order in order.fulfillmentOrders.nodes %}\n {% comment %}\n -- check the line items that are available to move for any of the configured SKUs\n -- Note: locationsForMove has already been filtered for the location to move to in the orders query above, so can just use \"first\" array item\n {% endcomment %}\n\n {% assign line_items_to_move = array %}\n\n {% for line_item in fulfillment_order.locationsForMove.nodes.first.availableLineItems.nodes %}\n {% if skus_to_move contains line_item.sku and line_item.remainingQuantity > 0 %}\n {% assign line_item_to_move = hash %}\n {% assign line_item_to_move[\"id\"] = line_item.id %}\n {% assign line_item_to_move[\"quantity\"] = line_item.remainingQuantity %}\n {% assign line_items_to_move = line_items_to_move | push: line_item_to_move %}\n {% endif %}\n {% endfor %}\n\n {% if line_items_to_move != blank %}\n {% assign fulfillment_order_ids_and_line_items_to_move[fulfillment_order.id] = line_items_to_move %}\n {% endif %}\n {% endfor %}\n\n {% comment %}\n -- make the moves if any line items qualify!\n {% endcomment %}\n\n {% for keyval in fulfillment_order_ids_and_line_items_to_move %}\n {% action \"shopify\" %}\n mutation {\n fulfillmentOrderMove(\n id: {{ keyval[0] | json }}\n newLocationId: {{ move_to_location.id | json }}\n fulfillmentOrderLineItems: {{ keyval[1] | graphql_arguments }}\n ) {\n movedFulfillmentOrder {\n id\n assignedLocation {\n name\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% else %}\n {% log \"No fulfillment order/item moves necessary on this order.\" %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/order"
],
"subscriptions_template": "shopify/orders/create\nmechanic/user/order",
"tags": [
"Demonstration",
"Fulfillment",
"Location",
"SKU"
]
}