-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathauto-tag-orders-by-originating-staff-member.json
24 lines (24 loc) · 6.58 KB
/
auto-tag-orders-by-originating-staff-member.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
{
"docs": "Use this task to keep track of who created what order. This task watches for new orders, and tags each one with the name of the store staff member who created it (if that's how the order was created). This task can also be run manually, to scan and tag all orders already in your store.\n\nThis task watches for new orders, that are created by store staff members, auto-tagging each new order with the name of the user who created it. Run this task manually to scan and tag all existing orders in your store.\r\n\r\n[YouTube: Watch the development video!](https://youtu.be/6E-oEGeBumE)",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag orders by originating staff member",
"online_store_javascript": null,
"options": {
"include_pos_orders__boolean": null,
"tag_prefix": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.topic == \"shopify/orders/create\" %}\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n tags\n events(first: 1, sortKey: CREATED_AT) {\n edges {\n node {\n attributeToUser\n message\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 \"tags\": [],\n \"events\": {\n \"edges\": [\n {\n \"node\": {\n \"attributeToUser\": true,\n \"message\": \"Sam Smith created this order.\"\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign orderNode = result.data.order %}\n {% assign eventNode = orderNode.events.edges.first.node %}\n {% assign originator = nil %}\n\n {% if eventNode.attributeToUser and eventNode.message contains \" created \" %}\n {% assign originator = eventNode.message | split: \" created \" | first %}\n {% elsif options.include_pos_orders__boolean and eventNode.attributeToUser == false and eventNode.message contains \" processed this order on Shopify POS.\" %}\n {% assign originator = eventNode.message | split: \" processed \" | first %}\n {% endif %}\n\n {% if originator %}\n {% assign originator_tag = options.tag_prefix | append: originator %}\n {% unless orderNode.tags contains originator_tag %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ orderNode.id | json }}\n tags: {{ originator_tag | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n {% endif %}\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 tags\n events(sortKey: CREATED_AT) {\n edges {\n node {\n __typename\n id\n attributeToUser\n message\n }\n }\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 {% assign bulkOperation = hash %}\n\n {% capture objects_json %}\n [\n {\n \"__typename\": \"Order\",\n \"id\": \"gid://shopify/Order/1234567890\",\n \"tags\": []\n },\n {\n \"__typename\": \"BasicEvent\",\n \"id\": \"gid://shopify/BasicEvent/1234567890\",\n \"attributeToUser\": true,\n \"message\": \"Sam Smith created this order from draft order <a href=\\\"https://example.myshopify.com/admin/draft_orders/1234567890\\\">#D1</a>.\",\n \"__parentId\": \"gid://shopify/Order/1234567890\",\n \"__parent\": {\n \"__typename\": \"Order\",\n \"id\": \"gid://shopify/Order/1234567890\",\n \"tags\": []\n }\n },\n {\n \"__typename\": \"BasicEvent\",\n \"id\": \"gid://shopify/BasicEvent/2345678901\",\n \"attributeToUser\": false,\n \"message\": \"Received new order <a href=\\\"https://mechanic-demo.myshopify.com/admin/orders/1835132911651\\\">#1001</a>.\",\n \"__parentId\": \"gid://shopify/Order/1234567890\"\n }\n ]\n {% endcapture %}\n\n {% assign bulkOperation[\"objects\"] = objects_json | parse_json %}\n {% endif %}\n\n {% assign eventNodes = bulkOperation.objects | where: \"__typename\", \"BasicEvent\" %}\n\n {% for eventNode in eventNodes %}\n {% assign originator = nil %}\n\n {% if eventNode.attributeToUser and eventNode.message contains \" created \" %}\n {% assign originator = eventNode.message | split: \" created \" | first %}\n {% elsif options.include_pos_orders__boolean and eventNode.attributeToUser == false and eventNode.message contains \" processed this order on Shopify POS.\" %}\n {% assign originator = eventNode.message | split: \" processed \" | first %}\n {% endif %}\n\n {% if originator %}\n {% assign originator_tag = options.tag_prefix | append: originator %}\n\n {% assign orderNode = eventNode.__parent %}\n\n {% unless orderNode.tags contains originator_tag %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ orderNode.id | json }}\n tags: {{ originator_tag | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n {% endif %}\n {% endfor %}\n{% endif %}",
"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",
"Staff"
]
}