-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
164 lines (139 loc) · 4.6 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
{% assign customer_email_domains = options.customer_email_domains__required_array %}
{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}
{% for email_domain in customer_email_domains %}
{% if email_domain contains "@" %}
{% error "Do not include '@' symbols in email domains. Thanks!" %}
{% endif %}
{% endfor %}
{% assign customer_ids_to_tag = array %}
{% if event.topic contains "shopify/customers/" %}
{% if event.preview %}
{% assign customer = hash %}
{% assign customer["admin_graphql_api_id"] = "gid://shopify/Customer/1234567890" %}
{% assign customer["email"] = "test@" | append: customer_email_domains.first %}
{% endif %}
{% assign customer_email_domain = customer.email | split: "@" | last | downcase %}
{% assign customer_tags = customer.tags | split: ", " %}
{% if customer_email_domains contains customer_email_domain %}
{% unless customer_tags contains customer_tag_to_apply %}
{% assign customer_ids_to_tag[customer_ids_to_tag.size] = customer.admin_graphql_api_id %}
{% endunless %}
{% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% assign domain_query_parts = array %}
{% for email_domain in customer_email_domains %}
{% assign domain_query_parts[domain_query_parts.size]
= "customer_email_domain = '"
| append: email_domain
| append: "'"
%}
{% endfor %}
{% capture search_query -%}
customer_tags NOT CONTAINS '{{ customer_tag_to_apply }}' AND ({{ domain_query_parts | join: " OR " }})
{%- endcapture %}
{% log search_query: search_query %}
{% assign cursor = nil %}
{% comment %}
-- customers resource no longer supports tag filters (JUL 2024), so need to use the customerSegmentMembers resource instead
{% endcomment %}
{% for n in (0..100) %}
{% capture query %}
query {
customerSegmentMembers(
first: 1000
after: {{ cursor | json }}
query: {{ search_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customerSegmentMembers": {
"edges": [
{
"node": {
"id": "gid://shopify/CustomerSegmentMember/1234567890"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign customer_segment_member_ids
= result.data.customerSegmentMembers.edges
| map: "node"
| map: "id"
%}
{% for customer_segment_member_id in customer_segment_member_ids %}
{% comment %}
-- query the customer record in order to verify the domain and absence of the tag, in case the search query filter has "seepage"
{% endcomment %}
{% capture query %}
query {
customer(id: {{ customer_segment_member_id | remove: "SegmentMember" | json }}) {
id
email
tags
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customer": {
"id": "gid://shopify/Customer/1234567890",
"email": {{ "test@" | append: customer_email_domains.first | json }}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign customer = result.data.customer %}
{% assign customer_email_domain = customer.email | split: "@" | last | downcase %}
{% if customer_email_domains contains customer_email_domain %}
{% unless customer.tags contains customer_tag_to_apply %}
{% assign customer_ids_to_tag = customer_ids_to_tag | push: customer.id %}
{% endunless %}
{% endif %}
{% endfor %}
{% if result.data.customers.pageInfo.hasNextPage %}
{% assign cursor = result.data.customers.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}
{% for customer_id in customer_ids_to_tag %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ customer_id | json }}
tags: {{ customer_tag_to_apply | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}