forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.liquid
206 lines (181 loc) · 5.84 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
{% assign hours_to_wait_in_seconds = options.hours_to_wait__number_required | times: 60 | times: 60 %}
{% if event.topic == "mechanic/user/text" %}
{% if event.preview %}
{% assign event = hash %}
{% assign event["preview"] = true %}
{% assign event["data"] = "#1234" %}
{% endif %}
{% assign order_numbers = event.data | strip | split: newline | sort | uniq %}
{% assign order_number_params = array %}
{% for number in order_numbers %}
{% assign order_number_params[order_number_params.size] = number | json %}
{% endfor %}
{% assign orders = array %}
{% assign cursor = nil %}
{% for n in (0..100) %}
{% capture query %}
query {
orders(
first: 250
query: {{ order_number_params | join: " OR " | json }}
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
name
note
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"orders": {
"edges": [
{
"node": {
"id": "gid://shopify/Order/1234567890",
"name": "#1234",
"note": "Existing order note"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for order_edge in result.data.orders.edges %}
{% assign order = order_edge.node %}
{% if order_numbers contains order.name %}
{% assign orders[orders.size] = order %}
{% endif %}
{% endfor %}
{% if result.data.orders.pageInfo.hasNextPage %}
{% assign cursor = result.data.orders.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% assign found_order_numbers = orders | map: "name" | sort %}
{% if found_order_numbers != order_numbers %}
{% error %}
{
"message": "Tried to find {{ order_numbers.size }} order(s), found {{ found_order_numbers.size }} instead. Please check your input, and try again. Make sure to use the complete and exact order number, including any symbols and prefixes/suffixes (e.g. W#1234, not 1234).",
"order_numbers": {{ order_numbers | json }},
"found_order_numbers": {{ found_order_numbers | json }}
}
{% enderror %}
{% elsif orders != blank %}
{% for order in orders %}
{% capture order_note_to_save %}{% if order.note != blank %}{{ order.note }}{{ newline }}{{ newline }}{% endif %}{{ options.order_note_to_add__required }}{% endcapture %}
{% log order: order %}
{% action "shopify" %}
mutation {
orderUpdate(
input: {
id: {{ order.id | json }}
note: {{ order_note_to_save | json }}
}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% action "event" %}
{
"topic": "user/orders/remove_note",
"run_at": {{ "now" | date: "%s" | plus: hours_to_wait_in_seconds | round | json }},
"data": {
"order_ids": {{ orders | map: "id" | json }},
"order_note_content_to_remove": {{ options.order_note_to_add__required | json }}
}
}
{% endaction %}
{% endif %}
{% elsif event.topic == "user/orders/remove_note" %}
{% if event.preview %}
{% capture event_json %}
{
"preview": true,
"data": {
"order_ids": ["gid://shopify/Order/1234567890"],
"order_note_content_to_remove": {{ options.order_note_to_add__required | json }}
}
}
{% endcapture %}
{% assign event = event_json | parse_json %}
{% endif %}
{% for order_id in event.data.order_ids %}
{% capture query %}
query {
order(id: {{ order_id | json }}) {
id
name
note
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"name": "#1234",
"note": {{ "Existing note!" | append: newline | append: newline | append: options.order_note_to_add__required | json }}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% if order == nil %}
{% log message: "Order no longer exists", id: order_id %}
{% continue %}
{% endif %}
{% assign order_note_parts = order.note | split: newline %}
{% assign order_note_to_save = "" %}
{% for order_note_part in order_note_parts %}
{% unless order_note_part == options.order_note_to_add__required %}
{% capture order_note_to_save %}{% if order_note_to_save != blank %}{{ order_note_to_save }}{{ newline }}{% endif %}{{ order_note_part }}{% endcapture %}
{% endunless %}
{% endfor %}
{% assign order_note_to_save = order_note_to_save | strip %}
{% if order_note_to_save == order.note %}
{% log message: "Order note to save appears to match the existing order note", order: order, order_note_to_save: order_note_to_save %}
{% else %}
{% action "shopify" %}
mutation {
orderUpdate(
input: {
id: {{ order.id | json }}
note: {{ order_note_to_save | json }}
}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% endif %}