forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
580 lines (493 loc) · 18.1 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
{% comment %}
-- using a keyval with multiline configuration field allows location names to be mapped to multiple markets
{% endcomment %}
{% assign locations_and_market_names = options.locations_and_market_names__keyval_multiline_required %}
{% assign ignore_products_with_any_of_these_tags = options.ignore_products_with_any_of_these_tags__array %}
{% assign ignore_variants_that_do_not_track_inventory = options.ignore_variants_that_do_not_track_inventory__boolean %}
{% assign ignore_variants_that_are_configured_for_overselling = options.ignore_variants_that_are_configured_for_overselling__boolean %}
{% if event.preview %}
{% assign locations_and_market_names = hash %}
{% assign locations_and_market_names["Warehouse"] = "International" %}
{% endif %}
{% assign location_names = locations_and_market_names | keys %}
{% assign market_names
= locations_and_market_names
| values
| join: newline
| split: newline
| uniq
%}
{% assign location_names_and_ids = hash %}
{% assign inventory_level_inputs = array %}
{% comment %}
-- check to make sure all of the location names configured in the task match locations in this shop
-- Shopify supports 1000 locations (as of June 2023), so searching by name filter instead of paginating
{% endcomment %}
{% for location_name in location_names %}
{% capture query %}
query {
locations(
first: 1
query: {{ location_name | json | prepend: "name:" | json }}
) {
nodes {
id
name
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"locations": {
"nodes": [
{
"id": "gid://shopify/Location/1234567890"
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% comment %}
-- using "echo" __error instead of log so that an error indicator shows in the event run log but the task still continues processing
{% endcomment %}
{% if result.data.locations == blank %}
{% action "echo"
__error: "Configured location name does not match a location in this shop.",
location_name: location_name
%}
{% continue %}
{% endif %}
{% assign location_id = result.data.locations.nodes.first.id %}
{% assign location_names_and_ids[location_name] = location_id %}
{% comment %}
-- save inventory level inputs for bulk querying so that only the locations configured in this task are returned
-- have to set alias for the inventoryLevel field because GraphQL requires field name uniqueness within each returned node
{% endcomment %}
{% capture inventory_level_input %}
{{ location_id | replace: "gid://shopify/Location/", "location_" }}: inventoryLevel(locationId: {{ location_id | json }}) {
location {
name
}
quantities(names: "available") {
quantity
}
}
{% endcapture %}
{% assign inventory_level_inputs = inventory_level_inputs | push: inventory_level_input %}
{% endfor %}
{% comment %}
-- Shopify only supports 50 markets (as of June 2023) and has no query filters for them, so have to retrieve them all
-- each market has only a single catalog and publication, so we can map market names to publication IDs in a hash for quicker lookup
{% endcomment %}
{% assign market_names_and_publication_ids = hash %}
{% capture query %}
query {
markets(first: 50) {
nodes {
name
catalogs(first: 1) {
nodes {
publication {
id
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"markets": {
"nodes": [
{
"name": "International",
"catalogs": {
"nodes": [
{
"publication": {
"id": "gid://shopify/Publication/1234567890"
}
}
]
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign published_inputs = array %}
{% for market in result.data.markets.nodes %}
{% assign publication_id = market.catalogs.nodes.first.publication.id %}
{% assign market_names_and_publication_ids[market.name] = publication_id %}
{% comment %}
-- save published inputs for checking to see which publications a product is currently published to
-- have to set alias for the publishedOnPublication field because GraphQL requires field name uniqueness within each returned node
{% endcomment %}
{% capture published_input -%}
published_{{ publication_id | remove: "gid://shopify/Publication/" }}: publishedOnPublication(publicationId: {{ publication_id | json }})
{%- endcapture %}
{% assign published_inputs = published_inputs | push: published_input %}
{% endfor %}
{% comment %}
-- check to make sure all of the market names configured in the task match markets in this shop
-- using "echo" __error instead of log or error tags so that an error indicator shows in the event run log but the task still continues processing
{% endcomment %}
{% for market_name in market_names %}
{% if market_names_and_publication_ids[market_name] == blank %}
{% action "echo"
__error: "Configured market name does not match a market in this shop.",
market_name: market_name
%}
{% endif %}
{% endfor %}
{% comment %}
-- setting a variable that can be overriden on inventory level update to only check the relevant location
{% endcomment %}
{% assign location_names_to_process = location_names %}
{% if event.topic == "shopify/inventory_levels/update" %}
{% comment %}
-- use the inventory level and location IDs from this event webhook to query available inventory for ALL of the product's variants at this location
-- Shopify supports 100 variants per product (as of June 2023)
{% endcomment %}
{% capture query %}
query {
inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
location {
name
}
item {
variant {
product {
id
title
status
tags
{{ published_inputs | join: newline }}
variants(first: 100) {
nodes {
inventoryPolicy
inventoryItem {
tracked
inventoryLevel(locationId: {{ inventory_level.location_id | prepend: "gid://shopify/Location/" | json }}) {
quantities(names: "available") {
quantity
}
}
}
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"location": {
"name": "Warehouse"
},
"item": {
"variant": {
"product": {
"id": "gid://shopify/Product/1234567890",
"title": "Widget",
"status": "ACTIVE",
"published_1234567890": true,
"variants": {
"nodes": [
{
"inventoryPolicy": "DENY",
"inventoryItem": {
"tracked": true,
"inventoryLevel": {
"quantities": [
{
"quantity": 0
}
]
}
}
}
]
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign location_name = result.data.inventoryLevel.location.name %}
{% assign product = result.data.inventoryLevel.item.variant.product %}
{% comment %}
-- stop processing if this product has any of the configured ignore tags
{% endcomment %}
{% assign has_ignore_tag = nil %}
{% for ignore_tag in ignore_products_with_any_of_these_tags %}
{% if product.tags contains ignore_tag %}
{% assign has_ignore_tag = true %}
{% break %}
{% endif %}
{% endfor %}
{% if has_ignore_tag %}
{% log
message: "Product has one of the configured ignore tags and will be skipped.",
product: product
%}
{% break %}
{% endif %}
{% comment %}
-- stop processing if this location isn't configured in the task or if the product is not active
{% endcomment %}
{% unless location_names contains location_name and product.status == "ACTIVE" %}
{% break %}
{% endunless %}
{% comment %}
-- make sure the products processing loop only checks this specific product and location
{% endcomment %}
{% assign products = array | push: product %}
{% assign location_names_to_process = array | push: location_name %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% comment %}
-- use bulk operation to query all active products, and their variants and paired inventory items in the shop
-- only query for inventory levels of locations configured in task, using inputs captured earlier
-- NOTE: bulk operation queries require edges and ids for each node, and __typename should be included for filtering returned data
{% endcomment %}
{% assign search_query = "status:active" %}
{% for ignore_tag in ignore_products_with_any_of_these_tags %}
{% assign search_query
= ignore_tag
| json
| prepend: " tag_not:"
| prepend: search_query
%}
{% endfor %}
{% capture bulk_operation_query %}
query {
products(
query: {{ search_query | json }}
) {
edges {
node {
__typename
id
title
{{ published_inputs | join: newline }}
variants {
edges {
node {
__typename
id
inventoryPolicy
inventoryItem {
tracked
{{ inventory_level_inputs | join: newline }}
}
}
}
}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% break %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% if event.preview %}
{% capture jsonl_string %}
{"__typename":"Product","id":"gid://shopify/Product/1234567890","title":"Widget","published_1234567890":true}
{"__typename":"ProductVariant","inventoryPolicy": "DENY","inventoryItem":{"tracked":true,"location_1234567890":{"location":{"name":"International"},"quantities":[{"quantity":0}]}},"__parentId":"gid://shopify/Product/1234567890"}
{% endcapture %}
{% assign bulkOperation = hash %}
{% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
{% endif %}
{% assign products = bulkOperation.objects | where: "__typename", "Product" %}
{% assign bulk_variants = bulkOperation.objects | where: "__typename", "ProductVariant" %}
{% endif %}
{% comment %}
-- this loop will process all products returned by the bulk operation OR the single product which had an inventory level event
{% endcomment %}
{% for product in products %}
{% comment %}
-- variants array depends on how this task run was initiated
{% endcomment %}
{% if event.topic == "shopify/inventory_levels/update" %}
{% assign variants = product.variants.nodes %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% assign variants = bulk_variants | where: "__parentId", product.id %}
{% endif %}
{% assign publication_ids_that_should_be_published = array %}
{% assign publication_ids_that_should_be_unpublished = array %}
{% for location_name in location_names_to_process %}
{% assign location_id = location_names_and_ids[location_name] %}
{% assign location_alias = location_id | replace: "gid://shopify/Location/", "location_" %}
{% assign location_market_names = locations_and_market_names[location_name] | split: newline %}
{% comment %}
-- a product is considered in stock at a location IF
a) ANY of the variants stocked at that location have inventory tracking disabled ~OR~
b) ANY of the variants stocked at that location have overselling enabled ~OR~
c) ANY of the variants stocked at that location have > 0 "available" inventory
-- checks for a) and b) can each be disabled through task options; meaning the task will skip variants which meet that criteria
{% endcomment %}
{% assign location_in_stock = nil %}
{% for variant in variants %}
{% comment %}
-- on bulk runs, only proceed with inventory check if the variant is stocked at this location
{% endcomment %}
{% if event.topic == "mechanic/shopify/bulk_operation" and variant.inventoryItem[location_alias] == blank %}
{% continue %}
{% endif %}
{% unless variant.inventoryItem.tracked %}
{% comment %}
-- variants that do not track inventory are considered in stock, unless the option to ignore them is checked
{% endcomment %}
{% if ignore_variants_that_do_not_track_inventory %}
{% continue %}
{% else %}
{% assign location_in_stock = true %}
{% break %}
{% endif %}
{% endunless %}
{% if variant.inventoryPolicy == "CONTINUE" %}
{% comment %}
-- variants set for overselling are considered in stock, unless the option to ignore them is checked
{% endcomment %}
{% if ignore_variants_that_are_configured_for_overselling %}
{% continue %}
{% else %}
{% assign location_in_stock = true %}
{% break %}
{% endif %}
{% endif %}
{% comment %}
-- variant "available" quantity depends on how this task run was initiated
{% endcomment %}
{% if event.topic == "shopify/inventory_levels/update" %}
{% assign variant_available = variant.inventoryItem.inventoryLevel.quantities.first.quantity %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% assign variant_available = variant.inventoryItem[location_alias].quantities.first.quantity %}
{% endif %}
{% if variant_available > 0 %}
{% assign location_in_stock = true %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- for each location market, add the paired publication ID to appropriate un/publish array based on location stock status
{% endcomment %}
{% for location_market_name in location_market_names %}
{% assign publication_id = market_names_and_publication_ids[location_market_name] %}
{% if location_in_stock %}
{% assign publication_ids_that_should_be_published = publication_ids_that_should_be_published | push: publication_id %}
{% else %}
{% assign publication_ids_that_should_be_unpublished = publication_ids_that_should_be_unpublished | push: publication_id %}
{% endif %}
{% endfor %}
{% endfor %}
{% comment %}
-- create publish inputs for each qualifying publication ID where the product isn't already published
{% endcomment %}
{% assign publish_inputs = array %}
{% for publication_id in publication_ids_that_should_be_published %}
{% assign published_alias = publication_id | replace: "gid://shopify/Publication/", "published_" %}
{% unless product[published_alias] %}
{% assign publish_input = hash %}
{% assign publish_input["publicationId"] = publication_id %}
{% assign publish_inputs = publish_inputs | push: publish_input %}
{% endunless %}
{% endfor %}
{% comment %}
-- create unpublish inputs for each qualifying publication ID where the product isn't already unpublished AND the publication ID does not appear in the "should be published array" (to account for locations that share a market)
{% endcomment %}
{% assign unpublish_inputs = array %}
{% for publication_id in publication_ids_that_should_be_unpublished %}
{% assign published_alias = publication_id | replace: "gid://shopify/Publication/", "published_" %}
{% unless publication_ids_that_should_be_published contains publication_id %}
{% if product[published_alias] %}
{% assign unpublish_input = hash %}
{% assign unpublish_input["publicationId"] = publication_id %}
{% assign unpublish_inputs = unpublish_inputs | push: unpublish_input %}
{% endif %}
{% endunless %}
{% endfor %}
{% comment %}
-- "ALLOW" or "DENY" a product access to/from markets by un/publishing it from the associated market publications
{% endcomment %}
{% if publish_inputs != blank %}
{% action "shopify" %}
mutation {
publishablePublish(
id: {{ product.id | json }}
input: {{ publish_inputs | uniq | graphql_arguments }}
) {
publishable {
... on Product {
title
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% if unpublish_inputs != blank %}
{% action "shopify" %}
mutation {
publishableUnpublish(
id: {{ product.id | json }}
input: {{ unpublish_inputs | uniq | graphql_arguments }}
) {
publishable {
... on Product {
title
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}