forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaintain-a-tag-for-orders-processed-today.json
22 lines (22 loc) · 4.52 KB
/
maintain-a-tag-for-orders-processed-today.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"docs": "This task auto-tags today's new orders, as they're processed, and auto-untags them when the date changes. Optionally, choose to untag outside of a rolling 24-hour window, instead of untagging yesterday's orders at midnight.\r\n\r\n[YouTube: See the development video for this task!](https://youtu.be/1BzhNUZ8Efo)",
"halt_action_run_sequence_on_error": false,
"name": "Maintain a tag for orders processed today",
"online_store_javascript": null,
"options": {
"use_rolling_24_hour_window__boolean": null,
"order_tag__required": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if options.use_rolling_24_hour_window__boolean %}\n {% assign now_s = \"now\" | date: \"%s\" | times: 1 %}\n {% assign twenty_four_hours_s = 24 | times: 60 | times: 60 %}\n {% assign threshold_s = now_s | minus: twenty_four_hours_s %}\n{% else %}\n {% assign threshold_s = \"now\" | date: \"%Y-%m-%d\" | date: \"%s\" | times: 1 %}\n{% endif %}\n\n{% assign threshold_iso8601 = threshold_s | date: \"%FT%T%:z\" %}\n\n{% assign order_nodes = array %}\n\n{% if event.topic == \"shopify/orders/create\" %}\n {% if event.preview %}\n {% assign order = hash %}\n {% assign order[\"processed_at\"] = threshold_s | plus: 1 | date: \"%FT%T%:z\" %}\n {% assign order[\"admin_graphql_api_id\"] = \"gid://shopify/Order/1234567890\" %}\n {% endif %}\n\n {% assign order_node = hash %}\n {% assign order_node[\"id\"] = order.admin_graphql_api_id %}\n {% assign order_node[\"processedAt\"] = order.processed_at %}\n {% assign order_node[\"tags\"] = order.tags | split: \", \" %}\n\n {% assign order_nodes[order_nodes.size] = order_node %}\n{% elsif event.topic contains \"mechanic/scheduler/\" %}\n {% assign cursor = nil %}\n\n {% for n in (0..100) %}\n {% capture query %}\n query {\n orders(\n first: 250\n after: {{ cursor | json }}\n query: {{ options.order_tag__required | json | prepend: \"tag:\" | json }}\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n id\n processedAt\n tags\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 \"orders\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"processedAt\": \"2000-01-01T00:00:00Z\",\n \"tags\": [\n {{ options.order_tag__required | json }}\n ]\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for order_edge in result.data.orders.edges %}\n {% assign order_nodes[order_nodes.size] = order_edge.node %}\n {% endfor %}\n\n {% if result.data.orders.pageInfo.hasNextPage %}\n {% assign cursor = result.data.orders.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n{% endif %}\n\n{% for order_node in order_nodes %}\n {% assign processed_at_s = order_node.processedAt | date: \"%s\" | times: 1 %}\n {% if processed_at_s >= threshold_s %}\n {% unless order_node.tags contains options.order_tag__required %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ order_node.id | json }}\n tags: {{ options.order_tag__required | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n {% elsif order_node.tags contains options.order_tag__required %}\n {% action \"shopify\" %}\n mutation {\n tagsRemove(\n id: {{ order_node.id | json }}\n tags: {{ options.order_tag__required | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endfor %}",
"subscriptions": [
"shopify/orders/create",
"mechanic/scheduler/daily"
],
"subscriptions_template": "shopify/orders/create\n\n{% if options.use_rolling_24_hour_window__boolean %}\n mechanic/scheduler/10min\n{% else %}\n mechanic/scheduler/daily\n{% endif %}",
"tags": [
"Auto-Tag",
"Orders"
]
}