forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
152 lines (135 loc) · 3.83 KB
/
script.liquid
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{% unless event.preview %}
{% capture query %}
query {
publications(first: 250) {
edges {
node {
id
name
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign available_channels = result.data.publications.edges | map: "node" | map: "name" %}
{% assign sales_channel_names = array %}
{% for keyval in options.sales_channel_names_and_tags__keyval_required %}
{% assign sales_channel_names[sales_channel_names.size] = keyval.first %}
{% endfor %}
{% endunless %}
{% assign orders = array %}
{% if event.topic contains "shopify/orders/" %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
tags
publication {
name
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"name": "#1234",
"tags": [],
"publication": {
"name": {{ options.sales_channel_names_and_tags__keyval_required.first.first | json }}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign orders[orders.size] = result.data.order %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
orders {
edges {
node {
__typename
id
name
tags
publication {
name
}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% if event.preview %}
{% capture bulkOperation_json %}
{
"objects": [
{
"__typename": "Order",
"id": "gid://shopify/Order/1234567890",
"name": "#1234",
"tags": [],
"publication": {
"name": {{ options.sales_channel_names_and_tags__keyval_required.first.first | json }}
}
}
]
}
{% endcapture %}
{% assign bulkOperation = bulkOperation_json | parse_json %}
{% endif %}
{% assign orders = bulkOperation.objects %}
{% endif %}
{% for order in orders %}
{% assign publication_name = order.publication.name %}
{% if publication_name == blank %}
{% 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)." %}
{% continue %}
{% endif %}
{% assign tag_to_add = options.sales_channel_names_and_tags__keyval_required[publication_name] %}
{% if tag_to_add == blank %}
{% 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 %}
{% elsif result.data.order.tags contains tag_to_add %}
{% 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 %}
{% else %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ tag_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}