forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
246 lines (215 loc) · 8.79 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{% comment %}
Option order
{{ options.search_query_for_orders }}
{{ options.product_ids_to_look_for__number_array }}
{{ options.variant_ids_to_look_for__number_array }}
{{ options.email_subject__required }}
{{ options.email_body__required_multiline }}
{{ options.add_this_tag_to_matching_orders }}
{{ options.test_mode__boolean }}
{{ options.i_certify_that_messages_sent_here_are_related_to_customer_activity__boolean }}
{% endcomment %}
{% if event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
orders(
query: {{ options.search_query_for_orders | json }}
) {
edges {
node {
__typename
id
name
email
lineItems {
edges {
node {
__typename
id
product {
legacyResourceId
title
onlineStoreUrl
}
variant {
legacyResourceId
}
}
}
}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% assign line_items_by_order_id = hash %}
{% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
{% assign qualifying_orders = array %}
{% for object in bulkOperation.objects %}
{% case object.__typename %}
{% when "LineItem" %}
{% assign order = object.__parent %}
{% if line_items_by_order_id[order.id] == nil %}
{% assign line_items_by_order_id[order.id] = array %}
{% endif %}
{% assign count = line_items_by_order_id[order.id].size %}
{% assign line_items_by_order_id[order.id][count] = object %}
{% endcase %}
{% endfor %}
{% for order in orders %}
{% assign order_qualifies = true %}
{% if order.email == blank %}
{% assign order_qualifies = false %}
{% endif %}
{% if options.product_ids_to_look_for__number_array != blank %}
{% assign product_ids_qualify = false %}
{% for line_item in line_items_by_order_id[order.id] %}
{% assign legacy_id_number = line_item.product.legacyResourceId | times: 1 %}
{% if options.product_ids_to_look_for__number_array contains legacy_id_number %}
{% assign product_ids_qualify = true %}
{% break %}
{% endif %}
{% endfor %}
{% unless product_ids_qualify %}
{% assign order_qualifies = false %}
{% endunless %}
{% endif %}
{% if options.variant_ids_to_look_for__number_array != blank %}
{% assign variant_ids_qualify = false %}
{% for line_item in line_items_by_order_id[order.id] %}
{% assign legacy_id_number = line_item.variant.legacyResourceId | times: 1 %}
{% if options.variant_ids_to_look_for__number_array contains legacy_id_number %}
{% assign variant_ids_qualify = true %}
{% break %}
{% endif %}
{% endfor %}
{% unless variant_ids_qualify %}
{% assign order_qualifies = false %}
{% endunless %}
{% endif %}
{% if order_qualifies %}
{% assign qualifying_orders[qualifying_orders.size] = order %}
{% endif %}
{% endfor %}
{% assign test_mode_email_summaries = array %}
{% if event.preview %}
{% assign qualifying_orders[0] = hash %}
{% assign qualifying_orders[0]["id"] = "gid://shopify/Order/1234567890" %}
{% assign qualifying_orders[0]["name"] = "#1234" %}
{% assign qualifying_orders[0]["email"] = "[email protected]" %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"] = array %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][0] = hash %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][0]["product"] = hash %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][0]["product"]["title"] = "Short Sleeve T-Shirt" %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][0]["product"]["onlineStoreUrl"] = "https://" | append: shop.domain %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][1] = hash %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][1]["product"] = hash %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][1]["product"]["title"] = "Medium Sleeve T-Shirt" %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][2] = hash %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][2]["product"] = hash %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][2]["product"]["title"] = "Long Sleeve T-Shirt" %}
{% assign line_items_by_order_id["gid://shopify/Order/1234567890"][2]["product"]["onlineStoreUrl"] = "https://" | append: shop.domain %}
{% endif %}
{% for order in qualifying_orders %}
{% assign email_to = order.email %}
{% assign email_subject = options.email_subject__required | replace: "ORDER_NAME", order.name %}
{% assign email_body = options.email_body__required_multiline | strip | newline_to_br | replace: "ORDER_NAME", order.name %}
{% assign line_items_with_products = line_items_by_order_id[order.id] | where: "product" %}
{% assign line_item_summaries = array %}
{% for line_item in line_items_with_products %}
{% assign summary = line_item.product.title %}
{% if line_item.product.onlineStoreUrl != blank %}
{% assign summary = '<a href="' | append: line_item.product.onlineStoreUrl | append: '">' | append: summary | append: '</a>' %}
{% endif %}
{% if forloop.last and forloop.index0 >= 2 %}
{% assign summary = 'and ' | append: summary %}
{% endif %}
{% assign line_item_summaries[line_item_summaries.size] = summary %}
{% endfor %}
{% if line_item_summaries.size == 2 %}
{% assign line_items_summary = line_item_summaries | join: " and " %}
{% else %}
{% assign line_items_summary = line_item_summaries | join: ", " %}
{% endif %}
{% assign line_items_summary_without_links = line_items_summary | strip_html %}
{% assign email_body = email_body | replace: "PRODUCT_TITLES_WITH_LINKS", line_items_summary %}
{% assign email_body = email_body | replace: "PRODUCT_TITLES", line_items_summary_without_links %}
{% if options.test_mode__boolean %}
{% assign test_mode_email_summary = hash %}
{% assign test_mode_email_summary["to"] = email_to %}
{% assign test_mode_email_summary["subject"] = email_subject %}
{% assign test_mode_email_summary["body"] = email_body %}
{% assign test_mode_email_summaries[test_mode_email_summaries.size] = test_mode_email_summary %}
{% else %}
{% action "email" %}
{
"to": {{ email_to | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% if options.add_this_tag_to_matching_orders != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ options.add_this_tag_to_matching_orders | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
{% endfor %}
{% if options.test_mode__boolean %}
{% action "echo" %}
{
"message": "Found {{ qualifying_orders.size }} qualifying order(s)",
"email_summaries": {{ test_mode_email_summaries | json }}
}
{% endaction %}
{% if event.preview and options.add_this_tag_to_matching_orders != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
id: "gid://shopify/Order/1234567890"
tags: {{ options.add_this_tag_to_matching_orders | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
{% endif %}
{% unless event.preview %}
{% if options.test_mode__boolean == false and options.i_certify_that_messages_sent_here_are_related_to_customer_activity__boolean == false %}
{"error": "Our mail provider, Postmark, does not allow bulk messages that are not related to customer activity. For more information, see https://postmarkapp.com/blog/bulk-vs-transactional"}
{% endif %}
{% endunless %}