forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-draft-orders-by-originating-staff-member.json
23 lines (23 loc) · 5.58 KB
/
auto-tag-draft-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
{
"docs": "Use this task to keep track of who created what draft order. This task watches for new draft orders, and tags each one with the name of the store staff member who created it. This task can also be run manually, to scan and tag all draft orders already in your store.\n\nThis task watches for new draft orders, that are created by store staff members, auto-tagging each new draft order with the name of the user who created it. Run this task manually to scan and tag all existing draft 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 draft orders by originating staff member",
"online_store_javascript": null,
"options": {
"tag_prefix": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.topic == \"shopify/draft_orders/create\" %}\n {% capture query %}\n query {\n draftOrder(id: {{ draft_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 \"draftOrder\": {\n \"id\": \"gid://shopify/DraftOrder/1234567890\",\n \"tags\": [],\n \"events\": {\n \"edges\": [\n {\n \"node\": {\n \"attributeToUser\": true,\n \"message\": \"Sam Smith created this draft order.\"\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign draftOrderNode = result.data.draftOrder %}\n {% assign eventNode = draftOrderNode.events.edges.first.node %}\n {% if eventNode.attributeToUser and eventNode.message contains \" created \" %}\n {% assign originator = eventNode.message | split: \" created \" | first %}\n {% assign originator_tag = options.tag_prefix | append: originator %}\n\n {% unless draftOrderNode.tags contains originator_tag %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ draftOrderNode.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 draftOrders {\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\": \"DraftOrder\",\n \"id\": \"gid://shopify/DraftOrder/1234567890\",\n \"tags\": []\n },\n {\n \"__typename\": \"BasicEvent\",\n \"id\": \"gid://shopify/BasicEvent/1234567890\",\n \"attributeToUser\": true,\n \"message\": \"Sam Smith created this draft order.\",\n \"__parentId\": \"gid://shopify/DraftOrder/1234567890\",\n \"__parent\": {\n \"__typename\": \"DraftOrder\",\n \"id\": \"gid://shopify/DraftOrder/1234567890\",\n \"tags\": []\n }\n }\n ]\n {% endcapture %}\n\n {% assign bulkOperation[\"objects\"] = objects_json | parse_json %}\n {% endif %}\n\n {% assign eventNodes = bulkOperation.objects | where: \"__typename\", \"BasicEvent\" | where: \"attributeToUser\", true %}\n\n {% for eventNode in eventNodes %}\n {% unless eventNode.message contains \" created \" %}\n {% continue %}\n {% endunless %}\n\n {% assign originator = eventNode.message | split: \" created \" | first %}\n {% assign originator_tag = options.tag_prefix | append: originator %}\n\n {% assign draftOrderNode = eventNode.__parent %}\n\n {% unless draftOrderNode.tags contains originator_tag %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ draftOrderNode.id | json }}\n tags: {{ originator_tag | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"shopify/draft_orders/create",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/draft_orders/create\nmechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Auto-Tag",
"Draft Orders",
"Staff"
]
}