forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
362 lines (322 loc) · 11.4 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
{% comment %}
1. Watch for updates to inventory levels.
2. Look up the associated product, when an update comes in.
3. If the product's total inventory is 0, record the current time in a metafield. If the
total inventory is not 0, delete that metafield, if it exists.
4. Scan all products, on a schedule, retrieving each product's out-of-stock time metafield.
For products found with a total inventory of 0, with a recorded time that's at least a
configurable distance in days from the current time, unpublish the product.
{% endcomment %}
{% assign number_of_days_to_wait_before_unpublishing = options.number_of_days_to_wait_before_unpublishing__number_required %}
{% assign sales_channel_names = options.sales_channel_names__required_array %}
{% assign only_include_products_matching_this_search_query = options.only_include_products_matching_this_search_query %}
{% assign test_mode = options.test_mode__boolean %}
{% assign time_format = "%FT%T%z" %}
{% if event.topic == "shopify/inventory_levels/update" %}
{% capture query %}
query {
inventoryLevel(
id: {{ inventory_level.admin_graphql_api_id | json }}
) {
id
item {
variant {
product {
id
title
totalInventory
metafield(
namespace: "mechanic"
key: "out_of_stock_at"
) {
id
value
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890",
"item": {
"variant": {
"product": {
"id": "gid://shopify/Product/1234567890",
"title": "Short sleeve t-shirt",
"totalInventory": 0,
"metafield": null
}
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign product = result.data.inventoryLevel.item.variant.product %}
{% if product.totalInventory <= 0 and product.metafield == nil %}
{% if test_mode %}
{% log %}
{% capture message %}Product {{ product.title | json }} ({{ product.id }}) is out of stock. Its out-of-stock time should be recorded.{% endcapture %}
{{ message | json }}
{% endlog %}
{% else %}
{% action "shopify" %}
mutation {
productUpdate(
input: {
id: {{ product.id | json }}
metafields: [
{
namespace: "mechanic"
key: "out_of_stock_at"
type: "date_time"
value: {{ "now" | date: time_format | json }}
}
]
}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% elsif product.totalInventory > 0 and product.metafield != nil %}
{% if test_mode %}
{% log %}
{% capture message %}Product {{ product.title | json }} ({{ product.id }}) is back in stock. Its out-of-stock time should be cleared.{% endcapture %}
{{ message | json }}
{% endlog %}
{% else %}
{% action "shopify" %}
mutation {
metafieldDelete(
input: {
id: {{ product.metafield.id | json }}
}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
{% elsif event.topic contains "mechanic/scheduler/" or event.topic == "mechanic/user/trigger" %}
{% assign now_s = "now" | date: "%s" | times: 1 %}
{% assign minimum_out_of_stock_at_distance_s = number_of_days_to_wait_before_unpublishing | times: 24 | times: 60 | times: 60 %}
{% capture query %}
query {
publications(first: 250) {
nodes {
id
name
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign publications = array %}
{% assign metafields_to_set = array %}
{% for publication in result.data.publications.nodes %}
{% if sales_channel_names contains publication.name %}
{% assign publications[publications.size] = publication %}
{% endif %}
{% endfor%}
{% if event.preview %}
{% assign publications[0] = hash %}
{% assign publications[0]["id"] = "gid://shopify/Publication/1234567890" %}
{% elsif publications.size != sales_channel_names.size %}
{% log
publications_named: sales_channel_names,
publications_available: result.data.publications.nodes,
publications_matched: publications
%}
{% error "Unable to find all named publications. Double-check your task configuration." %}
{% endif %}
{% assign search_query = "inventory_total:<=0" %}
{% if only_include_products_matching_this_search_query != blank %}
{% capture search_query -%}
{{ search_query }} AND ({{ only_include_products_matching_this_search_query }})
{%- endcapture %}
{% log search_query: search_query %}
{% endif %}
{% assign cursor = nil %}
{% for n in (0..100) %}
{% capture query %}
query {
products(
first: 250
query: {{ search_query | json }}
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
title
totalInventory
{% for publication in publications %}
publishedOnPublication{{ forloop.index }}: publishedOnPublication(publicationId: {{ publication.id | json }})
{% endfor %}
metafield(
namespace: "mechanic"
key: "out_of_stock_at"
) {
id
value
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"products": {
"nodes": [
{
"id": "gid://shopify/Product/1234567890",
"title": "Short sleeve t-shirt",
"totalInventory": 0,
{% for publication in publications %}
{{ "publishedOnPublication" | append: forloop.index | json }}: true,
{% endfor %}
"metafield": {
"value": {{ now_s | minus: minimum_out_of_stock_at_distance_s | minus: 1 | append: "" | json }}
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for product in result.data.products.nodes %}
{% if product.metafield == nil %}
{% log %}
{% capture message %}Product {{ product.id }} is out of stock, but its out-of-stock time was not recorded. This product will not be unpublished now, but its out-of-stock time will be set to the current time.{% endcapture %}
{{ message | json }}
{% endlog %}
{% assign metafield_to_set = hash %}
{% assign metafield_to_set["ownerId"] = product.id %}
{% assign metafield_to_set["namespace"] = "mechanic" %}
{% assign metafield_to_set["key"] = "out_of_stock_at" %}
{% assign metafield_to_set["type"] = "date_time" %}
{% assign metafield_to_set["value"] = "now" | date: time_format %}
{% assign metafields_to_set = metafields_to_set | push: metafield_to_set %}
{% continue %}
{% endif %}
{% assign out_of_stock_at_s = product.metafield.value | date: "%s" | times: 1 %}
{% assign out_of_stock_at_distance_s = now_s | minus: out_of_stock_at_s %}
{% assign unpublishings = array %}
{% for publication in publications %}
{% assign key = "publishedOnPublication" | append: forloop.index %}
{% if product[key] %}
{% assign unpublishing = array %}
{% assign unpublishing[0] = product.id %}
{% assign unpublishing[1] = publication.id %}
{% assign unpublishings[unpublishings.size] = unpublishing %}
{% endif %}
{% endfor %}
{% if unpublishings == empty %}
{% log %}
{% capture message %}Product {{ product.title | json }} ({{ product.id }}) has been out of stock for {{ out_of_stock_at_distance_s | divided_by: 60 | divided_by: 60 | divided_by: 24 | round: 2 }} day(s), but is not published - nothing to do.{% endcapture %}
{{ message | json }}
{% endlog %}
{% continue %}
{% endif %}
{% if out_of_stock_at_distance_s < minimum_out_of_stock_at_distance_s %}
{% log %}
{% capture message %}Product {{ product.title | json }} ({{ product.id }}) is out of stock, and is published, but has only been out of stock for {{ out_of_stock_at_distance_s | divided_by: 60 | divided_by: 60 | divided_by: 24 | round: 2 }} day(s).{% endcapture %}
{{ message | json }}
{% endlog %}
{% continue %}
{% endif %}
{% if test_mode %}
{% log %}
{% capture message %}Product {{ product.title | json }} ({{ product.id }}) is out of stock, and has been out of stock for {{ out_of_stock_at_distance_s | divided_by: 60 | divided_by: 60 | divided_by: 24 | round: 2 }} day(s). It should be unpublished.{% endcapture %}
{{ message | json }}
{% endlog %}
{% else %}
{% action "shopify" %}
mutation {
{% for unpublishing in unpublishings %}
publishableUnpublish{{ forloop.index }}: publishableUnpublish(
id: {{ unpublishing[0] | json }}
input: {
publicationId: {{ unpublishing[1] | json }}
}
) {
userErrors {
field
message
}
}
{% endfor %}
}
{% endaction %}
{% endif %}
{% endfor %}
{% if result.data.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.products.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% if metafields_to_set != blank %}
{% if test_mode %}
{% log %}
"Found {{ metafields_to_set.size }} out of stock products which do not yet have their out of stock time recorded. This task has test mode enabled, so these metafields will not be set on this task run."
{% endlog %}
{% break %}
{% endif %}
{% assign groups_of_metafields_to_set = metafields_to_set | in_groups_of: 25, fill_with: false %}
{% for group_of_metafields_to_set in groups_of_metafields_to_set %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: {{ group_of_metafields_to_set | graphql_arguments }}
) {
metafields {
id
namespace
key
type
value
owner {
... on Product {
id
title
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}