-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
348 lines (306 loc) · 11.2 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
{% comment %}
Options order:
{{ options.maximum_hourly_orders__number_required }}
{{ options.only_count_orders_matching_this_query }}
{{ options.only_clear_inventory_for_products_with_this_tag }}
{{ options.restore_inventory_levels_the_next_hour__boolean }}
{{ options.restore_inventory_levels_on_demand__boolean }}
{% endcomment %}
{% if options.maximum_hourly_orders__number_required <= 0 %}
{% error "'Maximum hourly orders' must be at least 1. :)" %}
{% endif %}
{% assign inventory_is_zeroed_cache_key = "inventory_is_zeroed:" | append: task.id %}
{% assign inventory_is_zeroed = cache[inventory_is_zeroed_cache_key] | default: false %}
{% if event.topic contains "shopify/orders" %}
{% assign cursor = nil %}
{% assign orders_this_hour = 0 %}
{% assign previous_hour = "now" | date: "%Y-%m-%dT%H:00:00%z" %}
{% assign previous_hour_s = previous_hour | date: "%s" %}
{% assign base_cache_key = "inventory_to_restore:" | append: previous_hour_s %}
{% capture orders_query %}
created_at:>={{ previous_hour | json }} {{ options.only_count_orders_matching_this_query }}
{% endcapture %}
{% assign orders_query = orders_query | strip %}
{% for n in (0..100) %}
{% capture query %}
query {
orders(
first: 250
after: {{ cursor | json }}
query: {{ orders_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% assign inventory_is_zeroed = false %}
{% capture result_json %}
{
"data": {
"orders": {
"nodes": [
{% for n in (1..options.maximum_hourly_orders__number_required) %}
{
"id": "gid://shopify/Order/1234567890"
}{% unless forloop.last %},{% endunless %}
{% endfor %}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign orders_this_hour = orders_this_hour | plus: result.data.orders.nodes.size %}
{% if result.data.orders.pageInfo.hasNextPage %}
{% assign cursor = result.data.orders.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% log orders_this_hour_thus_far: orders_this_hour, orders_query: orders_query, inventory_is_already_zeroed: inventory_is_zeroed %}
{% if orders_this_hour >= options.maximum_hourly_orders__number_required and inventory_is_zeroed == false %}
{% action "cache", "set", inventory_is_zeroed_cache_key, true %}
{% assign cursor = nil %}
{% assign inventory_adjustments = array %}
{% for n in (0..2000) %}
{% capture query %}
query {
inventoryItems(
first: 3
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
variant {
product {
tags
}
}
inventoryLevels(first: 100) {
nodes {
id
quantities(names: "available") {
name
quantity
}
location {
id
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryItems": {
"nodes": [
{
"id": "gid://shopify/InventoryItem/1234567890",
"variant": {
"product": {
"tags": [
{% if options.only_clear_inventory_for_products_with_this_tag != blank %}
{{ options.only_clear_inventory_for_products_with_this_tag | json }}
{% endif %}
]
}
},
"inventoryLevels": {
"nodes": [
{
"id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890",
"quantities": [
{
"name": "available",
"quantity": 20
}
],
"location": {
"id": "gid://shopify/Location/1234567890"
}
}
]
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for inventory_item in result.data.inventoryItems.nodes %}
{% if options.only_clear_inventory_for_products_with_this_tag != blank %}
{% unless inventory_item.variant.product.tags contains options.only_clear_inventory_for_products_with_this_tag %}
{% continue %}
{% endunless %}
{% endif %}
{% for inventory_level in inventory_item.inventoryLevels.nodes %}
{% if inventory_level.quantities.first.quantity > 0 %}
{% comment %}
NOTE: use availble quantity in the hash to be stored in cache... however, a negative delta will be made in the mutation to zero out the inventory
{% endcomment %}
{% assign inventory_adjustment = hash %}
{% assign inventory_adjustment["inventoryItemId"] = inventory_item.id %}
{% assign inventory_adjustment["locationId"] = inventory_level.location.id %}
{% assign inventory_adjustment["delta"] = inventory_level.quantities.first.quantity %}
{% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}
{% endif %}
{% endfor %}
{% endfor %}
{% if result.data.inventoryItems.pageInfo.hasNextPage %}
{% assign cursor = result.data.inventoryItems.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if inventory_adjustments == blank %}
{% break %}
{% endif %}
{% assign groups_of_inventory_adjustments = inventory_adjustments | in_groups_of: 250, fill_with: false %}
{% for group_of_inventory_adjustments in groups_of_inventory_adjustments %}
{% action "shopify" %}
mutation {
inventoryAdjustQuantities(
input: {
reason: "correction"
name: "available"
changes: [
{% for inventory_adjustment in group_of_inventory_adjustments -%}
{
inventoryItemId: {{ inventory_adjustment.inventoryItemId | json }}
locationId: {{ inventory_adjustment.locationId | json }}
delta: {{ inventory_adjustment.delta | times: -1 }}
}
{%- endfor %}
]
}
) {
inventoryAdjustmentGroup {
reason
changes {
name
delta
quantityAfterChange
item {
id
sku
}
location {
name
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% comment %}
-- store the inventory to restore in cache even if neither restore option is selected... just in case :)
{% endcomment %}
{% assign cache_key = base_cache_key | append: "_group" | append: forloop.index0 %}
{% assign cache_value = hash %}
{% assign cache_value["value"] = group_of_inventory_adjustments %}
{% unless forloop.last %}
{% assign next_cache_key = base_cache_key | append: "_group" | append: forloop.index %}
{% assign cache_value["next_key"] = next_cache_key %}
{% endunless %}
{% action "cache", "set", cache_key, cache_value %}
{% endfor %}
{% endif %}
{% elsif event.topic == "mechanic/scheduler/hourly" or event.topic == "mechanic/user/trigger" %}
{% action "cache", "del", inventory_is_zeroed_cache_key %}
{% assign cache_keys = array %}
{% assign hour_in_s = 60 | times: 60 %}
{% assign previous_hour = "now" | date: "%s" | minus: hour_in_s | date: "%Y-%m-%dT%H:00:00%z" %}
{% assign previous_hour_s = previous_hour | date: "%s" %}
{% assign cache_key = "inventory_to_restore:" | append: previous_hour_s | append: "_group0" %}
{% assign inventory_adjustments_cache_group = cache[cache_key] | default: hash %}
{% if inventory_adjustments_cache_group == blank %}
{% assign previous_hour = "now" | date: "%Y-%m-%dT%H:00:00%z" %}
{% assign previous_hour_s = previous_hour | date: "%s" %}
{% assign cache_key = "inventory_to_restore:" | append: previous_hour_s | append: "_group0" %}
{% assign inventory_adjustments_cache_group = cache[cache_key] | default: hash %}
{% endif %}
{% if event.preview %}
{% assign inventory_adjustment = hash %}
{% assign inventory_adjustment["inventoryItemId"] = "gid://shopify/InventoryItem/1234567890" %}
{% assign inventory_adjustment["locationId"] = "gid://shopify/Location/1234567890" %}
{% assign inventory_adjustment["delta"] = 20 %}
{% assign inventory_adjustments_cache_group["value"] = array | push: inventory_adjustment %}
{% endif %}
{% if inventory_adjustments_cache_group == blank %}
{% error
message: "No cached inventory data was found!",
cache_key: cache_key
%}
{% break %}
{% else %}
{% for n in (0..1000) %}
{% action "shopify" %}
mutation {
inventoryAdjustQuantities(
input: {
reason: "correction"
name: "available"
changes: {{ inventory_adjustments_cache_group.value | graphql_arguments }}
}
) {
inventoryAdjustmentGroup {
reason
changes {
name
delta
quantityAfterChange
item {
id
sku
}
location {
name
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% assign cache_keys = cache_keys | push: cache_key %}
{% if inventory_adjustments_cache_group.next_key %}
{% assign cache_key = inventory_adjustments_cache_group.next_key %}
{% assign inventory_adjustments_cache_group = cache[cache_key] %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% for cache_key in cache_keys %}
{% action "cache", "del", cache_key %}
{% endfor %}
{% endif %}
{% endif %}