forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-new-orders-by-staff-member.json
27 lines (27 loc) · 2.99 KB
/
auto-tag-new-orders-by-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
25
26
27
{
"docs": "Use this task to easily keep track of who's responsible for which orders. Works for draft orders, too!\n\nTo use this task, fill in the \"Staff names and tags\" option with staff names on the left, and the tags you'd like to apply on the right. Make sure to use each staff member's exact full name – it's case-sensitive! Tags will be applied as soon as the order or draft order is created.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag new orders by staff member",
"online_store_javascript": null,
"options": {
"tag_orders__boolean": true,
"tag_draft_orders__boolean": true,
"staff_names_and_tags__keyval_required": {
"Jane Doe": "created-by-jane",
"Zhang Wei": "created-by-zhang"
}
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% unless options.tag_orders__boolean or options.tag_draft_orders__boolean %}\n {% error \"Choose at least one resource to auto-tag.\" %}\n{% endunless %}\n\n{% if event.topic contains \"shopify/orders/\" %}\n {% assign resource_id = order.admin_graphql_api_id | default: \"gid://shopify/Order/1234567890\" %}\n {% assign connection = \"order\" %}\n{% elsif event.topic contains \"shopify/draft_orders/\" %}\n {% assign resource_id = draft_order.admin_graphql_api_id | default: \"gid://shopify/DraftOrder/1234567890\" %}\n {% assign connection = \"draftOrder\" %}\n{% endif %}\n\n{% capture query %}\n query {\n {{ connection }}(id: {{ resource_id | json }}) {\n id\n events(first: 250) {\n edges {\n node {\n attributeToUser\n message\n }\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% assign placed_event = result.data.first.last.events.edges | map: \"node\" | where: \"attributeToUser\" | first %}\n{% assign name = placed_event.message | split: \" created this \" | first %}\n{% assign tag_to_add = options.staff_names_and_tags__keyval_required[name] %}\n\n{% if event.preview %}\n {% assign name = options.staff_names_and_tags__keyval_required.first.first %}\n {% assign tag_to_add = options.staff_names_and_tags__keyval_required.first.last %}\n{% endif %}\n\n{% if name == blank %}\n {% log message: \"No staff member detected.\", resource: result.data.first.last %}\n{% elsif tag_to_add == blank %}\n {% log message: \"No tag applicable for staff member.\", staff_member: name %}\n{% else %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ resource_id | json }}\n tags: {{ tag_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create",
"shopify/draft_orders/create"
],
"subscriptions_template": "{% if options.tag_orders__boolean %}shopify/orders/create{% endif %}\n{% if options.tag_draft_orders__boolean %}shopify/draft_orders/create{% endif %}",
"tags": [
"Auto-Tag",
"Orders",
"Staff"
]
}