forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.liquid
157 lines (132 loc) · 4.57 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
{% if event.preview %}
{% assign event = hash %}
{% assign event["data"] = "[email protected]:foobar,customer@foo:bar" | replace: ",", newline %}
{% assign event["preview"] = true %}
{% endif %}
{% assign query_cost = 3 %}
{% assign mutation_cost = 10 %}
{% assign bucket_size = 1000 %}
{% assign buffer_factor = 2 %}
{% assign lines = event.data | strip | split: newline | uniq | sort %}
{% assign lines_and_customers = hash %}
{% assign scan_batch_size = bucket_size | times: 1.0 | divided_by: query_cost | divided_by: buffer_factor | round %}
{% assign scan_batches = lines | in_groups_of: scan_batch_size, fill_with: false %}
{% for scan_batch in scan_batches %}
{% assign batch_lines = array %}
{% for line in scan_batch %}
{% if line == blank %}
{% continue %}
{% endif %}
{% assign parts = line | split: ":" %}
{% if parts.size != 2 %}
{% log message: "Found an invalid line.", line: line %}
{% continue %}
{% endif %}
{% unless parts.first contains "@" %}
{% log message: "Found an invalid email address.", line: line %}
{% continue %}
{% endunless %}
{% assign email = parts[0] | strip %}
{% assign tag = parts[1] | strip %}
{% assign batch_lines[batch_lines.size] = email | append: ":" | append: tag %}
{% endfor %}
{% if batch_lines == empty %}
{% continue %}
{% endif %}
{% capture query %}
query {
{% for line in batch_lines %}
{% assign email = line | split: ":" | first %}
customer{{ forloop.index0 }}: customers(first: 1, query: {{ email | json | prepend: "email:" | json }}) {
edges {
node {
id
email
tags
}
}
}
{% endfor %}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
{% for line in batch_lines %}
"customer{{ forloop.index0 }}": {
"edges": [
{
"node": {
"email": {{ line | split: ":" | first | json }},
"id": "gid://shopify/Customer/{{ forloop.index0 }}",
"tags": []
}
}
]
}
{% unless forloop.last %},{% endunless %}
{% endfor %}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for line in batch_lines %}
{% assign key = "customer" | append: forloop.index0 %}
{% assign email = line | split: ":" | first %}
{% assign tag = line | split: ":" | last %}
{% assign customer = result.data[key].edges.first.node %}
{% assign customer_email_downcase = customer.email | downcase %}
{% assign email_downcase = email | downcase %}
{% if customer_email_downcase != email_downcase %}
{% log message: "Customer returned by Shopify somehow does not match the email address we searched for. Skipping.", line: line, customer: customer %}
{% continue %}
{% endif %}
{% if customer == nil %}
{% log message: "This customer could not be found. Skipping.", line: line %}
{% continue %}
{% endif %}
{% if customer.tags contains tag %}
{% log message: "Customer already has tag. Skipping.", line: line, tag: tag, customer: customer %}
{% continue %}
{% endif %}
{% assign lines_and_customers[line] = customer %}
{% endfor %}
{% endfor %}
{% assign taggings = array %}
{% for keyval in lines_and_customers %}
{% assign line = keyval[0] %}
{% assign tag = line | split: ":" | last %}
{% assign customer = keyval[1] %}
{% assign tagging = hash %}
{% assign tagging["id"] = customer.id %}
{% assign tagging["tags"] = tag %}
{% assign taggings[taggings.size] = tagging %}
{% endfor %}
{% if taggings != empty %}
{% assign tagging_batch_size = bucket_size | times: 1.0 | divided_by: mutation_cost | divided_by: buffer_factor | round %}
{% assign tagging_batches = taggings | in_groups_of: tagging_batch_size, fill_with: false %}
{% for tagging_batch in tagging_batches %}
{% action "shopify" %}
mutation {
{% for tagging in tagging_batch %}
customer{{ forloop.index0 }}: tagsAdd({{ tagging | graphql_arguments }}) {
userErrors {
field
message
}
node {
... on Customer {
id
email
tags
}
}
}
{% endfor %}
}
{% endaction %}
{% endfor %}
{% endif %}