-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathscript.liquid
236 lines (203 loc) · 7.47 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
{% assign skus_to_monitor = options.product_skus_to_monitor__array_required %}
{% assign primary_location = shop.locations[shop.primary_location_id] %}
{% assign query_components = array %}
{% for sku in skus_to_monitor %}
{% assign query_components[query_components.size] = sku | json | prepend: "sku:" %}
{% endfor %}
{% capture search_query -%}
location_id:{{ shop.primary_location_id }} AND ({{ query_components | join: " OR " }})
{%- endcapture %}
{% log search_query: search_query %}
{% if event.preview %}
{% assign skus_to_monitor = array | push: skus_to_monitor[0] %}
{% endif %}
{% assign variants_by_sku = hash %}
{% assign cursor = nil %}
{% for n in (0..200) %}
{% capture query %}
query {
productVariants(
first: 200
query: {{ search_query | json }}
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
sku
inventoryItem {
id
inventoryLevel(locationId: {{ primary_location.admin_graphql_api_id | json }}) {
quantities(names: "available") {
name
quantity
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"productVariants": {
"nodes": [
{
"id": "gid://shopify/ProductVariant/1234567890",
"sku": {{ skus_to_monitor[0] | json }},
"inventoryItem": {
"id": "gid://shopify/InventoryItem/1234567890",
"inventoryLevel": {
"quantities": [
{
"name": "available",
"quantity": {% if event.topic == "mechanic/user/trigger" %}5{% else %}4{% endif %}
}
]
}
}
},
{
"id": "gid://shopify/ProductVariant/2345678901",
"sku": {{ skus_to_monitor[0] | json }},
"inventoryItem": {
"id": "gid://shopify/InventoryItem/2345678901",
"inventoryLevel": {
"quantities": [
{
"name": "available",
"quantity": {% if event.topic == "mechanic/user/trigger" %}5{% else %}4{% endif %}
}
]
}
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for variant in result.data.productVariants.nodes %}
{% assign variants_by_sku[variant.sku]
= variants_by_sku[variant.sku]
| default: array
| push: variant
%}
{% endfor %}
{% if result.data.productVariants.pageInfo.hasNextPage %}
{% assign cursor = result.data.productVariants.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% assign unmatched_skus = array %}
{% for sku in skus_to_monitor %}
{% if variants_by_sku[sku] == blank %}
{% assign unmatched_skus = unmatched_skus | push: sku %}
{% endif %}
{% endfor %}
{% if unmatched_skus != blank %}
{% error
message: "One or more configured SKUs were not matched in this shop. Please check the task configuration and try again.",
unmatched_skus: unmatched_skus
%}
{% endif %}
{% log variants_by_sku: variants_by_sku %}
{% if event.topic == "mechanic/user/trigger" %}
{% for pair in variants_by_sku %}
{% assign sku = pair[0] %}
{% assign variants = pair[1] %}
{% assign expected_inventory_quantity = variants[0].inventoryItem.inventoryLevel.quantities.first.quantity %}
{% for variant in variants %}
{% if variant.inventoryItem.inventoryLevel.quantities.first.quantity != expected_inventory_quantity %}
{% error %}
{{ "Expected all '" | append: sku | append: "' variants to have an available inventory quantity of " | append: expected_inventory_quantity | append: ", but not every variant is at this level. Ensure every variant is in sync, and try again." | json }}
{% enderror %}
{% endif %}
{% endfor %}
{% endfor %}
{% for pair in variants_by_sku %}
{% assign sku = pair[0] %}
{% assign variants = pair[1] %}
{% assign cache_key = task.id | append: "-" | append: sku | sha256 %}
{% action "cache", "set", cache_key, variants[0].inventoryItem.inventoryLevel.quantities.first.quantity %}
{% endfor %}
{% elsif event.topic contains "mechanic/scheduler/" %}
{% assign inventory_adjustments = array %}
{% for sku in skus_to_monitor %}
{% assign cache_key = task.id | append: "-" | append: sku | sha256 %}
{% assign cached_inventory_quantity = cache[cache_key] %}
{% if event.preview %}
{% assign cached_inventory_quantity = 5 %}
{% endif %}
{% if cached_inventory_quantity == nil %}
{% error %}{{ "Missing a cached inventory quantity for SKU '" | append: sku | append: "'. Make sure all inventory is in sync across all of this task's monitored SKUs, then use the 'Run task' button to cache current inventory quantities." | json }}{% enderror %}
{% endif %}
{% assign total_delta = 0 %}
{% assign deltas = hash %}
{% for variant in variants_by_sku[sku] %}
{% assign deltas[variant.id] = variant.inventoryItem.inventoryLevel.quantities.first.quantity | minus: cached_inventory_quantity %}
{% assign total_delta = total_delta | plus: deltas[variant.id] %}
{% endfor %}
{% log sku: sku, total_delta: total_delta %}
{% for variant in variants_by_sku[sku] %}
{% assign delta_for_sync = total_delta | minus: deltas[variant.id] %}
{% if delta_for_sync != 0 %}
{% 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"] = delta_for_sync %}
{% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}
{% endif %}
{% endfor %}
{% if total_delta != 0 %}
{% assign current_inventory_quantity = cached_inventory_quantity | plus: total_delta %}
{% action "cache", "set", cache_key, current_inventory_quantity %}
{% endif %}
{% 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 %}
{% endif %}