-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
210 lines (191 loc) · 5.53 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
{% assign metafield_namespace = options.metafield_namespace__required | strip %}
{% assign location_ids_and_metafield_keys = options.location_ids_and_metafield_keys__keyval_required %}
{% if event.topic contains "shopify/inventory_levels/" %}
{% if event.preview %}
{% capture inventory_level_json %}
{
"location_id": {{ location_ids_and_metafield_keys.first.first }},
"admin_graphql_api_id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890"
}
{% endcapture %}
{% assign inventory_level = inventory_level_json | parse_json %}
{% endif %}
{% assign inventory_level_string = inventory_level.location_id | append: "" %}
{% assign metafield_key = location_ids_and_metafield_keys[inventory_level_string] %}
{% if metafield_key != blank %}
{% capture query %}
query {
inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
id
quantities(names: "available") {
name
quantity
}
item {
variant {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"quantities": [
{
"name": "available",
"quantity": 20
}
],
"item": {
"variant": {
"id": "gid://shopify/ProductVariant/1234567890"
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: [
{
ownerId: {{ result.data.inventoryLevel.item.variant.id | json }}
namespace: {{ metafield_namespace | json }}
key: {{ metafield_key | json }}
type: "number_integer"
value: {{ result.data.inventoryLevel.quantities.first.quantity | append: "" | json }}
}
]
) {
metafields {
id
namespace
key
type
value
owner {
... on ProductVariant {
id
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
productVariants {
edges {
node {
id
inventoryItem {
id
inventoryLevels {
edges {
node {
__typename
id
quantities(names: "available") {
name
quantity
}
location {
legacyResourceId
}
}
}
}
}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% if event.preview %}
{% capture jsonl_string %}
{"id":"gid:\/\/shopify\/ProductVariant\/1234567890"}
{"__typename":"InventoryLevel","quantities":[{"name":"available","quantity":20}],"location":{"legacyResourceId":{{ location_ids_and_metafield_keys.first.first | json }}},"__parentId":"gid:\/\/shopify\/ProductVariant\/1234567890"}
{% endcapture %}
{% assign bulkOperation = hash %}
{% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
{% endif %}
{% assign inventory_levels = bulkOperation.objects | where: "__typename", "InventoryLevel" %}
{% assign metafields = array %}
{% for inventory_level in inventory_levels %}
{% assign metafield_key = location_ids_and_metafield_keys[inventory_level.location.legacyResourceId] %}
{% if metafield_key != blank and inventory_level.quantities.first.quantity != blank %}
{% capture metafield %}
{
ownerId: {{ inventory_level.__parentId | json }}
namespace: {{ metafield_namespace | json }}
key: {{ metafield_key | json }}
type: "number_integer"
value: {{ inventory_level.quantities.first.quantity | append: "" | json }}
}
{% endcapture %}
{% assign metafields = metafields | push: metafield %}
{% endif %}
{% endfor %}
{% assign groups_of_metafields = metafields | in_groups_of: 25, fill_with: false %}
{% for group_of_metafields in groups_of_metafields %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: [
{{ group_of_metafields | join: newline }}
]
) {
metafields {
id
namespace
key
type
value
owner {
... on ProductVariant {
id
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}