forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
155 lines (137 loc) · 3.87 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
{% comment %}
Options ordering:
{{ options.shipping_statuses_and_tags__keyval_required }}
{{ options.limit_to_orders_matching_this_query_for_manual_runs }}
{% endcomment %}
{% if event.topic contains "shopify/fulfillments/" %}
{% capture query %}
query {
order(id: {{ fulfillment.order.admin_graphql_api_id | json }}) {
id
tags
fulfillments {
displayStatus
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"tags": [],
"fulfillments": [
{
"displayStatus": "IN_TRANSIT"
},
{
"displayStatus": "CANCELED"
},
{
"displayStatus": "OUT_FOR_DELIVERY"
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign orders = array %}
{% assign orders[0] = result.data.order %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% assign orders_query = nil %}
{% if options.limit_to_orders_matching_this_query_for_manual_runs != blank %}
{% assign orders_query = options.limit_to_orders_matching_this_query_for_manual_runs %}
{% endif %}
{% capture bulk_operation_query %}
query {
orders(
query: {{ orders_query | json }}
) {
edges {
node {
id
tags
fulfillments {
displayStatus
}
}
}
}
}
{% 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 objects_jsonl %}
{"id":"gid:\/\/shopify\/Order\/1234567890","tags":[],"fulfillments":[{"displayStatus":"IN_TRANSIT"},{"displayStatus":"CANCELED"},{"displayStatus":"OUT_FOR_DELIVERY"}]}
{"id":"gid:\/\/shopify\/Order\/2345678901","tags":[],"fulfillments":[{"displayStatus":"DELIVERED"}]}
{% endcapture %}
{% assign bulkOperation = hash %}
{% assign bulkOperation["objects"] = objects_jsonl | parse_jsonl %}
{% endif %}
{% assign orders = bulkOperation.objects %}
{% endif %}
{% for order in orders %}
{% assign tags_to_add = array %}
{% assign tags_to_remove = array %}
{% assign display_statuses = order.fulfillments | map: "displayStatus" %}
{% for keyval in options.shipping_statuses_and_tags__keyval_required %}
{% assign shipping_status = keyval.first %}
{% assign tag = keyval.last %}
{% if display_statuses contains shipping_status %}
{% unless order.tags contains tag %}
{% assign tags_to_add[tags_to_add.size] = tag %}
{% endunless %}
{% elsif order.tags contains tag %}
{% assign tags_to_remove[tags_to_remove.size] = tag %}
{% endif %}
{% endfor %}
{% if tags_to_add != empty or tags_to_remove != empty %}
{% action "shopify" %}
mutation {
{% if tags_to_add != empty %}
tagsAdd(
id: {{ order.id | json }}
tags: {{ tags_to_add | json }}
) {
userErrors {
field
message
}
}
{% endif %}
{% if tags_to_remove != empty %}
tagsRemove(
id: {{ order.id | json }}
tags: {{ tags_to_remove | json }}
) {
userErrors {
field
message
}
}
{% endif %}
}
{% endaction %}
{% endif %}
{% endfor %}