forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
191 lines (170 loc) · 4.79 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
{% assign orders = array %}
{% if event.topic contains "shopify/orders/" %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
tags
lineItems(first: 20) {
nodes {
variant {
id
displayName
inventoryQuantity
inventoryItem {
id
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% unless result.data.order.tags contains options.order_tag_to_add__required %}
{% assign orders[0] = result.data.order %}
{% endunless %}
{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% assign cursor = nil %}
{% for n in (0..200) %}
{% capture query %}
query {
orders(
first: 10
after: {{ cursor | json }}
query: {{ options.order_tag_to_add__required | json | prepend: "fulfillment_status:unshipped tag_not:" | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
name
lineItems(first: 20) {
nodes {
variant {
id
displayName
inventoryQuantity
inventoryItem {
id
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign orders = result.data.orders.nodes | default: array | concat: orders %}
{% if result.data.orders.pageInfo.hasNextPage %}
{% assign cursor = result.data.orders.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% if event.preview %}
{% capture orders_json %}
[
{
"id": "gid://shopify/Order/1234567890",
"name": "#PREVIEW",
"lineItems": {
"nodes": [
{
"variant": {
"id": "gid://shopify/ProductVariant/1234567890",
"displayName": "ACME Widget - Alpha",
"inventoryQuantity": 2,
"inventoryItem": {
"id": "gid://shopify/inventoryItem/1234567890"
}
}
}
]
}
}
]
{% endcapture %}
{% assign orders = orders_json | parse_json %}
{% endif %}
{% for order in orders %}
{% assign order_qualifies = nil %}
{% for line_item in order.lineItems.nodes %}
{% if line_item.variant == blank %}
{% continue %}
{% endif %}
{% if options.ignore_line_items_not_fulfilled_manually__boolean %}
{% capture query %}
query {
inventoryItem(id: {{ line_item.variant.inventoryItem.id | json }}) {
inventoryLevels(first: 100) {
nodes {
location {
fulfillmentService {
handle
serviceName
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% comment %}
Note: no preview stub data needed here, as null values for the fulfillment services are literally the happy path :)
{% endcomment %}
{% assign fulfillment_services
= result.data.inventoryItem.inventoryLevels.nodes
| map: "location"
| map: "fulfillmentService"
| compact
%}
{% if fulfillment_services != blank %}
{% log
message: "This inventory item for this line item variant is configured to use at least one 3rd party fulfillment service'; skipping.",
order_name: order.name,
line_item: line_item,
fulfillment_services: fulfillment_services
%}
{% continue %}
{% endif %}
{% endif %}
{% if order_qualifies == nil and line_item.variant.inventoryQuantity >= 0 %}
{% assign order_qualifies = true %}
{% elsif line_item.variant.inventoryQuantity < 0 %}
{% assign order_qualifies = false %}
{% break %}
{% endif %}
{% endfor %}
{% if order_qualifies %}
{% if options.test_mode__boolean %}
{% log order_id_to_tag: order.id, order_name_to_tag: order.name %}
{% else %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ options.order_tag_to_add__required | json }}
) {
node {
... on Order {
id
name
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
{% endfor %}