forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
213 lines (195 loc) · 5.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
{% if event.topic == "shopify/orders/create" %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
tags
events(
first: 1
sortKey: CREATED_AT
query: "verb:placed"
) {
edges {
node {
id
... on BasicEvent {
appTitle
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"name": "#1234",
"tags": [],
"events": {
"edges": [
{
"node": {
"appTitle": {{ options.app_titles_and_order_tags__keyval_required.first.first | json }}
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign app_title = result.data.order.events.edges.first.node.appTitle %}
{% assign tag_to_add = options.app_titles_and_order_tags__keyval_required[app_title] %}
{% if app_title == blank %}
{% log message: "This order was not created by an app. Skipping." %}
{% elsif tag_to_add == blank %}
{% log message: "No tag found for this app. Skipping.", app_title: app_title %}
{% elsif result.data.order.tags contains tag_to_add %}
{% log message: "The order already has the tag for this app. Skipping.", app_title: app_title, tag_to_add: tag_to_add %}
{% elsif options.test_mode__boolean %}
{% action "echo" order_id: result.data.order.id, order_name: result.data.order.name, order_app_title: app_title, order_tag_to_add: tag_to_add %}
{% else %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ result.data.order.id | json }}
tags: {{ tag_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
orders {
edges {
node {
__typename
id
name
tags
events(
first: 1
sortKey: CREATED_AT
query: "verb:placed"
) {
edges {
node {
__typename
id
... on BasicEvent {
appTitle
}
}
}
}
}
}
}
}
{% 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",
"name": "#1234",
"tags": [],
"id": "gid://shopify/Order/1234567890"
},
{
"__typename": "BasicEvent",
"id":" gid://shopify/BasicEvent/1234567890",
"appTitle": {{ options.app_titles_and_order_tags__keyval_required.first.first | json }},
"__parentId": "gid://shopify/Order/1234567890"
}
]
}
{% endcapture %}
{% assign bulkOperation = bulkOperation_json | parse_json %}
{% endif %}
{% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
{% assign events = bulkOperation.objects | where: "__typename", "BasicEvent" %}
{% assign app_title_counts = hash %}
{% for event in events %}
{% assign app_title = event.appTitle %}
{% if app_title == blank %}
{% assign app_title = "(not placed via app)" %}
{% endif %}
{% assign app_title_counts[app_title] = app_title_counts[app_title] | plus: 1 %}
{% endfor %}
{% log app_title_counts: app_title_counts %}
{% if options.test_mode__boolean %}
{% assign summaries = array %}
{% endif %}
{% for order in orders %}
{% assign order_app_title = nil %}
{% assign order_tag_to_add = nil %}
{% assign order_event = events | where: "__parentId", order.id | first %}
{% assign order_app_title = order_event.appTitle %}
{% if order_app_title != blank %}
{% unless order.tags contains options.app_titles_and_order_tags__keyval_required[order_app_title] %}
{% assign order_tag_to_add = options.app_titles_and_order_tags__keyval_required[order_app_title] %}
{% endunless %}
{% endif %}
{% if order_app_title != blank %}
{% if options.test_mode__boolean %}
{% assign summary = hash %}
{% assign summary["order_id"] = order.id %}
{% assign summary["order_name"] = order.name %}
{% assign summary["order_tags"] = order.tags %}
{% assign summary["order_app_title"] = order_app_title %}
{% assign summary["order_tag_to_add"] = order_tag_to_add %}
{% assign summaries[summaries.size] = summary %}
{% elsif order_tag_to_add != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ order_tag_to_add | uniq | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
{% endfor %}
{% if options.test_mode__boolean %}
{% action "echo" summaries %}
{% endif %}
{% endif %}