forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-orders-by-sales-channel.json
26 lines (26 loc) · 5.23 KB
/
auto-tag-orders-by-sales-channel.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
26
{
"docs": "Use this task to tag orders as they come in, based on which sales channel created them. Run this task manually to backfill tags for customers based on their historical orders.\n\nPlease note: this task works by *sales channel*, and as such does not apply to orders that are created by an *app*. It's sometimes difficult to distinguish whether a third-party is using a sales channel or an app; if you're having trouble with this task, try its companion, [Auto-tag orders by app](https://usemechanic.com/task/auto-tag-orders-by-app).",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag orders by sales channel",
"online_store_javascript": null,
"options": {
"sales_channel_names_and_tags__keyval_required": {
"Online Store": "online-store",
"Buy Button": "buy-button"
}
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% unless event.preview %}\n {% capture query %}\n query {\n publications(first: 250) {\n edges {\n node {\n id\n name\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign available_channels = result.data.publications.edges | map: \"node\" | map: \"name\" %}\n\n {% assign sales_channel_names = array %}\n\n {% for keyval in options.sales_channel_names_and_tags__keyval_required %}\n {% assign sales_channel_names[sales_channel_names.size] = keyval.first %}\n {% endfor %}\n{% endunless %}\n\n{% assign orders = array %}\n\n{% if event.topic contains \"shopify/orders/\" %}\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n name\n tags\n publication {\n name\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\": \"#1234\",\n \"tags\": [],\n \"publication\": {\n \"name\": {{ options.sales_channel_names_and_tags__keyval_required.first.first | json }}\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign orders[orders.size] = result.data.order %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n orders {\n edges {\n node {\n __typename\n id\n name\n tags\n publication {\n name\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if event.preview %}\n {% capture bulkOperation_json %}\n {\n \"objects\": [\n {\n \"__typename\": \"Order\",\n \"id\": \"gid://shopify/Order/1234567890\",\n \"name\": \"#1234\",\n \"tags\": [],\n \"publication\": {\n \"name\": {{ options.sales_channel_names_and_tags__keyval_required.first.first | json }}\n }\n }\n ]\n }\n {% endcapture %}\n\n {% assign bulkOperation = bulkOperation_json | parse_json %}\n {% endif %}\n\n {% assign orders = bulkOperation.objects %}\n{% endif %}\n\n{% for order in orders %}\n {% assign publication_name = order.publication.name %}\n\n {% if publication_name == blank %}\n {% log order_id: order.id, order_name: order.name, message: \"No sales channel present for this order; it was probably created by an app (see https://usemechanic.com/task/auto-tag-orders-by-app).\" %}\n {% continue %}\n {% endif %}\n\n {% assign tag_to_add = options.sales_channel_names_and_tags__keyval_required[publication_name] %}\n\n {% if tag_to_add == blank %}\n {% log order_id: order.id, order_name: order.name, message: \"No tag configured for this channel. Skipping.\", publication_name: publication_name, available_channels: available_channels %}\n {% elsif result.data.order.tags contains tag_to_add %}\n {% log order_id: order.id, order_name: order.name, message: \"The order already has the tag for this channel. Skipping.\", publication_name: publication_name, tag_to_add: tag_to_add %}\n {% else %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ order.id | json }}\n tags: {{ tag_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endfor %}",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/orders/create\nmechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Auto-Tag",
"Orders",
"Sales Channel"
]
}