-
Notifications
You must be signed in to change notification settings - Fork 35
/
script.liquid
239 lines (202 loc) · 6.82 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
{% assign metafields_to_copy = options.metafields_to_copy__array %}
{% assign copy_all_metafields = options.copy_all_metafields__boolean %}
{% comment %}
-- check that task has been configured with at least one option, and that any metafields entered have the proper format (using match fitler, i.e. regex)
{% endcomment %}
{% unless copy_all_metafields %}
{% if metafields_to_copy == blank %}
{% error "Choose either to 'Copy all metafields' or enter at least one metafield to copy." %}
{% else %}
{% for metafield_to_copy in metafields_to_copy %}
{% assign metafield_check = metafield_to_copy | match: "^([\w-]{3,})\.([\w-]{3,})$" %}
{% unless metafield_check %}
{% log invalid_metafield_format: metafield_to_copy %}
{% error "All configured metafields should be entered as namespace.key using only alphanumeric characters, underscores, and dashes." %}
{% break %}
{% endunless %}
{% endfor %}
{% endif %}
{% endunless %}
{% if event.topic == "shopify/orders/create" or event.topic == "mechanic/user/order" %}
{% if event.preview %}
{% capture order_json %}
{
"admin_graphql_api_id": "gid://shopify/Order/1234567890",
"source_name": "shopify_draft_order"
}
{% endcapture %}
{% assign order = order_json | parse_json %}
{% endif %}
{% if order.source_name != "shopify_draft_order" %}
{% log "Order was not generated from a draft; skipping" %}
{% break %}
{% endif %}
{% comment %}
-- Shopify does not have any data in the REST or GraphQL order resources linking back to the completed draft order
-- Query recent completed draft orders to find the link to this order
-- As this task allows orders to be sent via the admin link, it will need to paginate the draft orders
{% endcomment %}
{% assign cursor = nil %}
{% assign draft_order_id = nil %}
{% for n in (1..10) %}
{% capture query %}
query {
draftOrders(
first: 250
after: {{ cursor | json }}
reverse: true
query: "status:completed"
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
order {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"draftOrders": {
"nodes": [
{
"id": "gid://shopify/DraftOrder/1234567890",
"order": {
"id": "gid://shopify/Order/1234567890"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% comment %}
-- search for order ID in this batch before querying another page of results
{% endcomment %}
{% for draft_order in result.data.draftOrders.nodes %}
{% if draft_order.order.id == order.admin_graphql_api_id %}
{% assign draft_order_id = draft_order.id %}
{% break %}
{% endif %}
{% endfor %}
{% if draft_order_id == blank and result.data.draftOrders.pageInfo.hasNextPage %}
{% assign cursor = result.data.draftOrders.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if draft_order_id == blank %}
{% log "Source draft order for this order was not found in most recent 2500 completed draft orders" %}
{% break %}
{% endif %}
{% comment %}
-- get metafields from draft order
-- Shopify supports up to 200 metafields per resource (as of June 2023)
{% endcomment %}
{% capture query %}
query {
draftOrder(id: {{ draft_order_id | json }}) {
metafields(first: 200) {
nodes {
namespace
key
type
value
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% comment %}
-- preview block uses the first configured metafield (if there is one) so that the logic below will find a "match"
{% endcomment %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"draftOrder": {
"metafields": {
"nodes": [
{
"namespace": {{ metafields_to_copy.first | split: "." | first | default: "custom" | json }},
"key": {{ metafields_to_copy.first | split: "." | last | default: "preview" | json }},
"type": "single_line_text_field",
"value": "alpha"
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign draft_order_metafields = result.data.draftOrder.metafields.nodes %}
{% assign matched_metafields = array %}
{% if copy_all_metafields %}
{% assign matched_metafields = draft_order_metafields %}
{% else %}
{% for metafield_to_copy in metafields_to_copy %}
{% assign metafield_namespace = metafield_to_copy | split: "." | first %}
{% assign metafield_key = metafield_to_copy | split: "." | last %}
{% assign matched_metafield
= draft_order_metafields
| where: "namespace", metafield_namespace
| where: "key", metafield_key
| first
%}
{% if matched_metafield %}
{% assign matched_metafields = matched_metafields | push: matched_metafield %}
{% endif %}
{% endfor %}
{% endif %}
{% comment %}
-- add order ID as the owner on each matched metafield
{% endcomment %}
{% assign metafields_to_set = array %}
{% for matched_metafield in matched_metafields %}
{% assign metafield_to_set = matched_metafield %}
{% assign metafield_to_set["ownerId"] = order.admin_graphql_api_id %}
{% assign metafields_to_set = metafields_to_set | push: metafield_to_set %}
{% else %}
{% log "The source draft order either has no metafields or none of the ones configured in this task." %}
{% break %}
{% endfor %}
{% comment %}
-- set the matched metafields on the order
-- metafieldsSet mutation only allows 25 metafields to be set at a time (as of June 2023)
{% endcomment %}
{% assign groups_of_metafields_to_set = metafields_to_set | in_groups_of: 25, fill_with: false %}
{% for group_of_metafields_to_set in groups_of_metafields_to_set %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: {{ group_of_metafields_to_set | graphql_arguments }}
) {
metafields {
namespace
key
type
value
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}