forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccept-a-maximum-number-of-orders-per-hour.json
25 lines (25 loc) · 14 KB
/
accept-a-maximum-number-of-orders-per-hour.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 works by monitoring the number of orders created per hour, and clearing the inventory for all in-stock items when the hourly order limit is reached. Optionally, this task can restore inventory to its original levels at minute zero of the next hour, or on demand.\n\nThis task works by setting your inventory to zero when the hourly order limit is reached. (Specifically, this means setting inventory levels to 0 for all items that have a greater-than-zero inventory level.) There are no popups, or any specific messaging - your inventory will simply be dropped to zero, and if your shop is configured to stop selling out-of-stock products, your customers will be prevented from making additional purchases.\r\n\r\nOptionally, this task can restore inventory to its original levels at midnight the next hour, or on demand. (Restore levels on demand by enabling this option, then using the \"Run task\" button.)\r\n\r\n**This task _does not_ work well when you have multiple orders per minute, and we do not recommend using it for high-volume stores.** It works by counting the current hour's orders, at the time of each purchase - if the current count exactly equals your maximum, it performs the inventory reset. Because that counting process can take a few seconds, receiving multiple orders per minute can result in missing that very specific window when the current count exactly equals your maximum.",
"halt_action_run_sequence_on_error": false,
"name": "Accept a maximum number of orders per hour",
"online_store_javascript": null,
"options": {
"maximum_hourly_orders__number_required": 10,
"only_count_orders_matching_this_query": "-status:cancelled",
"only_clear_inventory_for_products_with_this_tag": null,
"restore_inventory_levels_the_next_hour__boolean": true,
"restore_inventory_levels_on_demand__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n Options order:\n\n {{ options.maximum_hourly_orders__number_required }}\n {{ options.only_count_orders_matching_this_query }}\n {{ options.only_clear_inventory_for_products_with_this_tag }}\n {{ options.restore_inventory_levels_the_next_hour__boolean }}\n {{ options.restore_inventory_levels_on_demand__boolean }}\n{% endcomment %}\n\n{% if options.maximum_hourly_orders__number_required <= 0 %}\n {% error \"'Maximum hourly orders' must be at least 1. :)\" %}\n{% endif %}\n\n{% assign inventory_is_zeroed_cache_key = \"inventory_is_zeroed:\" | append: task.id %}\n{% assign inventory_is_zeroed = cache[inventory_is_zeroed_cache_key] | default: false %}\n\n{% if event.topic contains \"shopify/orders\" %}\n {% assign cursor = nil %}\n {% assign orders_this_hour = 0 %}\n {% assign previous_hour = \"now\" | date: \"%Y-%m-%dT%H:00:00%z\" %}\n {% assign previous_hour_s = previous_hour | date: \"%s\" %}\n {% assign base_cache_key = \"inventory_to_restore:\" | append: previous_hour_s %}\n\n {% capture orders_query %}\n created_at:>={{ previous_hour | json }} {{ options.only_count_orders_matching_this_query }}\n {% endcapture %}\n\n {% assign orders_query = orders_query | strip %}\n\n {% for n in (0..100) %}\n {% capture query %}\n query {\n orders(\n first: 250\n after: {{ cursor | json }}\n query: {{ orders_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% assign inventory_is_zeroed = false %}\n\n {% capture result_json %}\n {\n \"data\": {\n \"orders\": {\n \"nodes\": [\n {% for n in (1..options.maximum_hourly_orders__number_required) %}\n {\n \"id\": \"gid://shopify/Order/1234567890\"\n }{% unless forloop.last %},{% endunless %}\n {% endfor %}\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign orders_this_hour = orders_this_hour | plus: result.data.orders.nodes.size %}\n\n {% if result.data.orders.pageInfo.hasNextPage %}\n {% assign cursor = result.data.orders.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% log orders_this_hour_thus_far: orders_this_hour, orders_query: orders_query, inventory_is_already_zeroed: inventory_is_zeroed %}\n\n {% if orders_this_hour >= options.maximum_hourly_orders__number_required and inventory_is_zeroed == false %}\n {% action \"cache\", \"set\", inventory_is_zeroed_cache_key, true %}\n\n {% assign cursor = nil %}\n {% assign inventory_adjustments = array %}\n\n {% for n in (0..2000) %}\n {% capture query %}\n query {\n inventoryItems(\n first: 3\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n variant {\n product {\n tags\n }\n }\n inventoryLevels(first: 100) {\n nodes {\n id\n quantities(names: \"available\") {\n name\n quantity\n }\n location {\n id\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 \"inventoryItems\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/InventoryItem/1234567890\",\n \"variant\": {\n \"product\": {\n \"tags\": [\n {% if options.only_clear_inventory_for_products_with_this_tag != blank %}\n {{ options.only_clear_inventory_for_products_with_this_tag | json }}\n {% endif %}\n ]\n }\n },\n \"inventoryLevels\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890\",\n \"quantities\": [\n {\n \"name\": \"available\",\n \"quantity\": 20\n }\n ],\n \"location\": {\n \"id\": \"gid://shopify/Location/1234567890\"\n }\n }\n ]\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for inventory_item in result.data.inventoryItems.nodes %}\n {% if options.only_clear_inventory_for_products_with_this_tag != blank %}\n {% unless inventory_item.variant.product.tags contains options.only_clear_inventory_for_products_with_this_tag %}\n {% continue %}\n {% endunless %}\n {% endif %}\n\n {% for inventory_level in inventory_item.inventoryLevels.nodes %}\n {% if inventory_level.quantities.first.quantity > 0 %}\n {% comment %}\n NOTE: use availble quantity in the hash to be stored in cache... however, a negative delta will be made in the mutation to zero out the inventory\n {% endcomment %}\n\n {% assign inventory_adjustment = hash %}\n {% assign inventory_adjustment[\"inventoryItemId\"] = inventory_item.id %}\n {% assign inventory_adjustment[\"locationId\"] = inventory_level.location.id %}\n {% assign inventory_adjustment[\"delta\"] = inventory_level.quantities.first.quantity %}\n {% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}\n {% endif %}\n {% endfor %}\n {% endfor %}\n\n {% if result.data.inventoryItems.pageInfo.hasNextPage %}\n {% assign cursor = result.data.inventoryItems.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if inventory_adjustments == blank %}\n {% break %}\n {% endif %}\n\n {% assign groups_of_inventory_adjustments = inventory_adjustments | in_groups_of: 250, fill_with: false %}\n\n {% for group_of_inventory_adjustments in groups_of_inventory_adjustments %}\n {% action \"shopify\" %}\n mutation {\n inventoryAdjustQuantities(\n input: {\n reason: \"correction\"\n name: \"available\"\n changes: [\n {% for inventory_adjustment in group_of_inventory_adjustments -%}\n {\n inventoryItemId: {{ inventory_adjustment.inventoryItemId | json }}\n locationId: {{ inventory_adjustment.locationId | json }}\n delta: {{ inventory_adjustment.delta | times: -1 }}\n }\n {%- endfor %}\n ]\n }\n ) {\n inventoryAdjustmentGroup {\n reason\n changes {\n name\n delta\n quantityAfterChange\n item {\n id\n sku\n }\n location {\n name\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% comment %}\n -- store the inventory to restore in cache even if neither restore option is selected... just in case :)\n {% endcomment %}\n\n {% assign cache_key = base_cache_key | append: \"_group\" | append: forloop.index0 %}\n {% assign cache_value = hash %}\n {% assign cache_value[\"value\"] = group_of_inventory_adjustments %}\n\n {% unless forloop.last %}\n {% assign next_cache_key = base_cache_key | append: \"_group\" | append: forloop.index %}\n {% assign cache_value[\"next_key\"] = next_cache_key %}\n {% endunless %}\n\n {% action \"cache\", \"set\", cache_key, cache_value %}\n {% endfor %}\n {% endif %}\n\n{% elsif event.topic == \"mechanic/scheduler/hourly\" or event.topic == \"mechanic/user/trigger\" %}\n {% action \"cache\", \"del\", inventory_is_zeroed_cache_key %}\n {% assign cache_keys = array %}\n\n {% assign hour_in_s = 60 | times: 60 %}\n {% assign previous_hour = \"now\" | date: \"%s\" | minus: hour_in_s | date: \"%Y-%m-%dT%H:00:00%z\" %}\n {% assign previous_hour_s = previous_hour | date: \"%s\" %}\n\n {% assign cache_key = \"inventory_to_restore:\" | append: previous_hour_s | append: \"_group0\" %}\n\n {% assign inventory_adjustments_cache_group = cache[cache_key] | default: hash %}\n\n {% if inventory_adjustments_cache_group == blank %}\n {% assign previous_hour = \"now\" | date: \"%Y-%m-%dT%H:00:00%z\" %}\n {% assign previous_hour_s = previous_hour | date: \"%s\" %}\n {% assign cache_key = \"inventory_to_restore:\" | append: previous_hour_s | append: \"_group0\" %}\n {% assign inventory_adjustments_cache_group = cache[cache_key] | default: hash %}\n {% endif %}\n\n {% if event.preview %}\n {% assign inventory_adjustment = hash %}\n {% assign inventory_adjustment[\"inventoryItemId\"] = \"gid://shopify/InventoryItem/1234567890\" %}\n {% assign inventory_adjustment[\"locationId\"] = \"gid://shopify/Location/1234567890\" %}\n {% assign inventory_adjustment[\"delta\"] = 20 %}\n {% assign inventory_adjustments_cache_group[\"value\"] = array | push: inventory_adjustment %}\n {% endif %}\n\n {% if inventory_adjustments_cache_group == blank %}\n {% error\n message: \"No cached inventory data was found!\",\n cache_key: cache_key\n %}\n {% break %}\n\n {% else %}\n {% for n in (0..1000) %}\n {% action \"shopify\" %}\n mutation {\n inventoryAdjustQuantities(\n input: {\n reason: \"correction\"\n name: \"available\"\n changes: {{ inventory_adjustments_cache_group.value | graphql_arguments }}\n }\n ) {\n inventoryAdjustmentGroup {\n reason\n changes {\n name\n delta\n quantityAfterChange\n item {\n id\n sku\n }\n location {\n name\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% assign cache_keys = cache_keys | push: cache_key %}\n\n {% if inventory_adjustments_cache_group.next_key %}\n {% assign cache_key = inventory_adjustments_cache_group.next_key %}\n {% assign inventory_adjustments_cache_group = cache[cache_key] %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% for cache_key in cache_keys %}\n {% action \"cache\", \"del\", cache_key %}\n {% endfor %}\n {% endif %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/create",
"mechanic/scheduler/hourly"
],
"subscriptions_template": "shopify/orders/create\n{% if options.restore_inventory_levels_the_next_hour__boolean %}\n mechanic/scheduler/hourly\n{% endif %}\n{% if options.restore_inventory_levels_on_demand__boolean %}\n mechanic/user/trigger\n{% endif %}",
"tags": [
"Max Orders",
"Orders"
]
}