forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
318 lines (273 loc) · 10.7 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
{% assign product_tags = options.product_tags_to_monitor__array %}
{% assign product_types = options.product_types_to_monitor__array %}
{% assign only_sync_active_products = options.only_sync_active_products__boolean %}
{% assign primary_location = shop.locations[shop.primary_location_id] %}
{% if product_tags == blank and product_types == blank %}
{% error "You must choose to monitor products either by tags or types." %}
{% elsif product_tags != blank and product_types != blank %}
{% error "Choose to monitor products either by tags or types, but not both." %}
{% endif %}
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% comment %}
-- run query for all variants matched by product tags or types
{% endcomment %}
{% assign variants_by_product = hash %}
{% assign cursor = nil %}
{% assign search_query_parts = array %}
{% for product_type in product_types %}
{% assign search_query_parts[search_query_parts.size] = product_type | json | prepend: "product_type:" %}
{% endfor %}
{% for product_tag in product_tags %}
{% assign search_query_parts[search_query_parts.size] = product_tag | json | prepend: "tag:" %}
{% endfor %}
{% capture search_query -%}
location_id:{{ shop.primary_location_id }} ({{ search_query_parts | join: " OR " }}) managed:true
{%- endcapture %}
{% if only_sync_active_products %}
{% assign search_query = search_query | append: " product_status:active" %}
{% endif %}
{% for n in (0..200) %}
{% capture query %}
query {
productVariants(
first: 150
query: {{ search_query | json }}
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
inventoryItem {
id
inventoryLevel(locationId: {{ primary_location.admin_graphql_api_id | json }}) {
quantities(names: "available") {
name
quantity
}
}
}
product {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"productVariants": {
"nodes": [
{
"id": "gid://shopify/ProductVariant/1234567890",
"inventoryItem": {
"id": "gid://shopify/InventoryItem/1234567890",
"inventoryLevel": {
"quantities": [
{
"name": "available",
"quantity": 30
}
]
}
},
"product": {
"id": "gid://shopify/Product/1234567890"
}
},
{
"id": "gid://shopify/ProductVariant/2345678901",
"inventoryItem": {
"id": "gid://shopify/InventoryItem/2345678901",
"inventoryLevel": {
"quantities": [
{
"name": "available",
"quantity": {% if event.topic == "mechanic/user/trigger" %}30{% else %}31{% endif %}
}
]
}
},
"product": {
"id": "gid://shopify/Product/1234567890"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for variant in result.data.productVariants.nodes %}
{% assign variants_by_product[variant.product.id]
= variants_by_product[variant.product.id]
| default: array
| push: variant
%}
{% endfor %}
{% if result.data.productVariants.pageInfo.hasNextPage %}
{% assign cursor = result.data.productVariants.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if variants_by_product == blank %}
{% error
message: "No matching product variants found! Check your product tag or type settings for this task.",
search_query: search_query
%}
{% break %}
{% endif %}
{% elsif event.topic == "user/sync_variant_inventory/set_cache" %}
{% comment %}
-- this user event is called from the scheduler event, to process any products without a cache entry
{% endcomment %}
{% if event.data.products_missing_cache == blank %}
{% error "No products with missing cache were passed to this event." %}
{% endif %}
{% assign variants_by_product = event.data.products_missing_cache %}
{% endif %}
{% if event.topic == "mechanic/user/trigger" or event.topic == "user/sync_variant_inventory/set_cache" %}
{% comment %}
-- this event is run manually for initial setup, but will also be triggered via a custom event to handle any products that don't yet have a cache setting (typically new products, but can be existing ones if their cache expires or if the task configuration is changed)
{% endcomment %}
{% assign products_out_of_sync = array %}
{% for pair in variants_by_product %}
{% assign product_id = pair[0] %}
{% assign variants = pair[1] %}
{% assign available_quantites = array %}
{% for variant in variants %}
{% assign available_quantites = available_quantites | push: variant.inventoryItem.inventoryLevel.quantities.first.quantity %}
{% endfor %}
{% assign inventory_total = available_quantites | sum %}
{% assign expected_inventory_total = available_quantites.first | times: available_quantites.size %}
{% if inventory_total != expected_inventory_total %}
{% assign product_out_of_sync = hash %}
{% assign product_out_of_sync["product_id"] = product_id %}
{% assign product_out_of_sync["expected_inventory_total"] = expected_inventory_total %}
{% assign product_out_of_sync["current_inventory_total"] = inventory_total %}
{% assign product_out_of_sync["available_quantites"] = available_quantites %}
{% assign products_out_of_sync = products_out_of_sync | push: product_out_of_sync %}
{% else %}
{% assign cache_key = "inventory_by_product:" | append: product_id %}
{% action "cache", "set", cache_key, available_quantites.first %}
{% endif %}
{% endfor %}
{% if products_out_of_sync != blank %}
{% action "echo"
__error: "Variant inventory levels are not in sync for these products. Manually ensure everything is at the same level so they may be managed by this task.",
products_out_of_sync: products_out_of_sync
%}
{% endif %}
{% elsif event.topic contains "mechanic/scheduler/" %}
{% assign inventory_adjustments = array %}
{% assign products_missing_cache = hash %}
{% for pair in variants_by_product %}
{% assign product_id = pair[0] %}
{% assign variants = pair[1] %}
{% assign cache_key = "inventory_by_product:" | append: product_id %}
{% assign inventory_old = cache[cache_key] %}
{% if event.preview %}
{% assign inventory_old = 30 %}
{% endif %}
{% if inventory_old == blank %}
{% comment %}
-- if no cache has been set for this product yet, then save it for later processing and move to next product
{% endcomment %}
{% assign products_missing_cache[product_id] = variants %}
{% continue %}
{% endif %}
{% assign available_quantites = array %}
{% for variant in variants %}
{% assign available_quantites = available_quantites | push: variant.inventoryItem.inventoryLevel.quantities.first.quantity %}
{% endfor %}
{% assign inventory_total_old = inventory_old | times: available_quantites.size %}
{% assign inventory_total_new = available_quantites | sum %}
{% assign inventory_total_diff = inventory_total_new | minus: inventory_total_old %}
{% assign inventory_new = inventory_old | plus: inventory_total_diff %}
{% if inventory_new == inventory_old %}
{% log
message: "No inventory adjustments needed for this product.",
product_id: product_id
%}
{% continue %}
{% endif %}
{% log
message: "Adjusting inventory for this product.",
inventory_old: inventory_old,
inventory_total_old: inventory_total_old,
inventory_total_new: inventory_total_new,
inventory_total_diff: inventory_total_diff,
inventory_new: inventory_new,
product_id: product_id,
variants_count: variants.size
%}
{% action "cache", "set", cache_key, inventory_new %}
{% for variant in variants %}
{% assign inventory_adjustment = hash %}
{% assign inventory_adjustment["inventoryItemId"] = variant.inventoryItem.id %}
{% assign inventory_adjustment["locationId"] = primary_location.admin_graphql_api_id %}
{% assign inventory_adjustment["delta"] = inventory_new | minus: variant.inventoryItem.inventoryLevel.quantities.first.quantity %}
{% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}
{% endfor %}
{% endfor %}
{% if inventory_adjustments != blank %}
{% 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: {{ group_of_inventory_adjustments | graphql_arguments }}
}
) {
inventoryAdjustmentGroup {
reason
changes {
name
delta
quantityAfterChange
item {
id
sku
}
location {
name
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}
{% if products_missing_cache != blank %}
{% assign product_ids = products_missing_cache | keys %}
{% log
message: "Found these products without cache entries. Sending them to custom event to set caches if possible.",
product_ids: product_ids
%}
{% action "event" %}
{
"topic": "user/sync_variant_inventory/set_cache",
"task_id": {{ task.id | json }},
"data": {
"products_missing_cache": {{ products_missing_cache | json }}
}
}
{% endaction %}
{% endif %}
{% endif %}