-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
180 lines (164 loc) · 4.13 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
{% assign update_customer_first_and_last_name = options.update_customer_first_and_last_name__boolean %}
{% assign update_customer_phone = options.update_customer_phone__boolean %}
{% unless update_customer_first_and_last_name or update_customer_phone %}
{% error "Choose at least one of the update customer options." %}
{% endunless %}
{% if event.topic == "shopify/customers/update" %}
{% assign customers = array %}
{% capture query %}
query {
customer(id: {{ customer.admin_graphql_api_id | json }}) {
id
firstName
lastName
phone
defaultAddress {
firstName
lastName
phone
}
addresses {
firstName
lastName
phone
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign customers[0] = result.data.customer %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
customers {
edges {
node {
id
firstName
lastName
phone
defaultAddress {
firstName
lastName
phone
}
addresses {
firstName
lastName
phone
}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% assign customers = bulkOperation.objects %}
{% endif %}
{% if event.preview %}
{% capture customers_json %}
[
{
"id": "gid://shopify/Customer/1234567890",
"phone": null,
"defaultAddress": {
"firstName": "Jean",
"lastName": "Deaux"
},
"addresses": [
{
"phone": null
},
{
"phone": "+18885559876"
}
]
}
]
{% endcapture %}
{% assign customers = customers_json | parse_json %}
{% endif %}
{% for customer in customers %}
{% assign first_name = nil %}
{% assign last_name = nil %}
{% assign phone = nil %}
{% assign customer_addresses
= customer.addresses
| unshift: customer.defaultAddress
%}
{% if update_customer_first_and_last_name %}
{% if customer.firstName == blank %}
{% for customer_address in customer_addresses %}
{% unless customer_address.firstName == blank %}
{% assign first_name = customer_address.firstName %}
{% break %}
{% endunless %}
{% endfor %}
{% endif %}
{% if customer.lastName == blank %}
{% for customer_address in customer_addresses %}
{% unless customer_address.lastName == blank %}
{% assign last_name = customer_address.lastName %}
{% break %}
{% endunless %}
{% endfor %}
{% endif %}
{% endif %}
{% if update_customer_phone %}
{% if customer.phone == blank %}
{% for customer_address in customer_addresses %}
{% unless customer_address.phone == blank %}
{% assign phone = customer_address.phone %}
{% break %}
{% endunless %}
{% endfor %}
{% endif %}
{% endif %}
{% if first_name or last_name or phone %}
{% action "shopify" %}
mutation {
customerUpdate(
input: {
id: {{ customer.id | json }}
{% if first_name %}
firstName: {{ first_name | json }}
{% endif %}
{% if last_name %}
lastName: {{ last_name | json }}
{% endif %}
{% if phone %}
phone: {{ phone | json }}
{% endif %}
}
) {
customer {
id
firstName
lastName
phone
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}