-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-reserve-draft-order-items-for-x-days.json
25 lines (25 loc) · 6 KB
/
auto-reserve-draft-order-items-for-x-days.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": "Use this task to auto-reserve all of the line items for a set amount of time on any new or updated draft orders that contain a certain tag. A common use case is when you have an app or service that is creating draft orders on your behalf and you want to make sure that the inventory for those orders is set aside if possible.\n\nConfigure the task with the \"Tag to watch for\" and the \"Amount of days to reserve\". Optionally, enabling the \"Preserve the initial reservation date if line items change\" will override the default behavior of resetting the reseve until date.\n\n**Important notes:**\n\n- This task does not check the inventory of any line items before reserving them\n- Line items can only be reserved at the default location configured in Shopify, since draft orders do not support location assignment\n- The one minute delay on the draft order update subscription and the enabled \"Perform action runs in sequence\" run time option should both be left as configured, to prevent interference with draft order completions and updates",
"halt_action_run_sequence_on_error": false,
"name": "Auto-reserve draft order items for X days",
"online_store_javascript": null,
"options": {
"tag_to_watch_for__required": null,
"amount_of_days_to_reserve__number_required": null,
"preserve_the_initial_reservation_date_if_line_items_change__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": true,
"preview_event_definitions": [],
"script": "{% assign tag_to_watch_for = options.tag_to_watch_for__required %}\n{% assign amount_of_days_to_reserve = options.amount_of_days_to_reserve__number_required %}\n{% assign preserve_reservation_date = options.preserve_the_initial_reservation_date_if_line_items_change__boolean %}\n\n{% if amount_of_days_to_reserve < 1 %}\n {% error \"The amount of days to reserve must be a positive number\" %}\n{% endif %}\n\n{% if event.topic == \"shopify/draft_orders/create\" or event.topic == \"shopify/draft_orders/update\" %}\n {% comment %}\n -- get draft order data\n {% endcomment %}\n\n {% capture query %}\n query {\n draftOrder(id: {{ draft_order.admin_graphql_api_id | json }}) {\n id\n reserveInventoryUntil\n status\n tags\n lineItems(first: 250) {\n nodes {\n id\n quantity\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 \"draftOrder\": {\n \"id\": \"gid://shopify/DraftOrder/1234567890\",\n \"tags\": {{ tag_to_watch_for | json }},\n \"lineItems\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/LineItem/1234567890\",\n \"quantity\": 1\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign draft_order = result.data.draftOrder %}\n {% assign cache_key = draft_order.id %}\n {% assign draft_order_snapshot = cache[cache_key] %}\n\n {% comment %}\n -- if draft order does not have the tag to watch for, has no line items, or has been completed, then skip it\n {% endcomment %}\n\n {% unless draft_order.tags contains tag_to_watch_for\n and draft_order.lineItems != blank\n and draft_order.status != \"COMPLETED\"\n %}\n {% break %}\n {% endunless %}\n\n {% comment %}\n -- create hash of draft order line items to be saved to cache\n {% endcomment %}\n\n {% assign draft_order_hash = draft_order.lineItems.nodes | json | sha256 %}\n\n {% comment %}\n -- if the draft order already has a reserve date AND there is no change in the line items from the cache snapshot, then exit\n {% endcomment %}\n\n {% if draft_order.reserveInventoryUntil != blank and draft_order_hash == draft_order_snapshot %}\n {% log \"Draft order already has a reservation date set, however no line items have changed since the last update; skipping\" %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- calculate the reserve date to be midnight (local shop time) of the day following the future date\n {% endcomment %}\n\n {% assign reserve_duration_s\n = amount_of_days_to_reserve\n | plus: 1\n | times: 86400\n %}\n {% assign reserve_date\n = \"now\"\n | date: \"%s\"\n | plus: reserve_duration_s\n | date: \"%F\"\n | date: \"%FT%TZ\", tz: \"UTC\"\n %}\n\n {% comment %}\n -- re-apply an existing reservation date if the preserve option has been checked\n {% endcomment %}\n\n {% if preserve_reservation_date and draft_order.reserveInventoryUntil != blank %}\n {% log \"Draft order line items have changed since they were saved in the cache. The 'Preserve reservation date' option is checked, so the existing reservation date wil be re-applied to the draft order to make sure that all line items are properly reserved\" %}\n\n {% assign reserve_date = draft_order.reserveInventoryUntil %}\n {% endif %}\n\n {% comment %}\n -- set the cache value so the draft order update event that fires will have the new hash value to check\n {% endcomment %}\n\n {% action \"cache\", \"set\", cache_key, draft_order_hash %}\n\n {% comment %}\n -- update the draft order with the reserve date\n {% endcomment %}\n\n {% action \"shopify\" %}\n mutation {\n draftOrderUpdate(\n id: {{ draft_order.id | json }}\n input: {\n reserveInventoryUntil: {{ reserve_date | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n draftOrder {\n name\n reserveInventoryUntil\n }\n }\n }\n {% endaction %}\n{% endif %}",
"subscriptions": [
"shopify/draft_orders/create",
"shopify/draft_orders/update+1.minute"
],
"subscriptions_template": "shopify/draft_orders/create\nshopify/draft_orders/update+1.minute",
"tags": [
"Draft Orders",
"Products",
"Reserve"
]
}