forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
158 lines (139 loc) · 4.97 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
{% assign customer_metafields_to_monitor = options.customer_metafields_to_monitor__array_required %}
{% assign minimum_age_in_days_before_deletion = options.minimum_age_in_days_before_deletion__number_required | at_least: 0 %}
{% assign test_mode = options.test_mode__boolean %}
{% comment %}
-- use midnight local shop time as the base, so cutoff date will provide X full calendar days (excepting clock changes)
{% endcomment %}
{% assign metafield_delete_interval_s = minimum_age_in_days_before_deletion | times: 86400 %}
{% assign last_midnight_s = "now" | date: "%F" | date: "%s" | times: 1 %}
{% assign cutoff_date_s = last_midnight_s | minus: metafield_delete_interval_s %}
{% assign cutoff_date = cutoff_date_s | date: "%FT%T" %}
{% log
cutoff_date: cutoff_date,
task_options: task.options
%}
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% comment %}
-- query up to 25K customers [higher amounts will likely require a bulk operation query]
-- no filter query available to only include customers who have values for any of the configured metafields
{% endcomment %}
{% assign cursor = nil %}
{% assign metafield_inputs = array %}
{% for n in (1..100) %}
{% capture query %}
query {
customers(
first: 250
after: {{ cursor | json }}
query: {{ search_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
metafields(
first: {{ customer_metafields_to_monitor.size }}
keys: {{ customer_metafields_to_monitor | graphql_arguments }}
) {
nodes {
id
createdAt
key
namespace
type
value
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customers": {
"nodes": [
{
"id": "gid://shopify/Customer/1234567890",
"metafields": {
"nodes": [
{
"id": "gid://shopify/Metafield/1234567890",
"createdAt": {{ cutoff_date_s | minus: 1 | json }},
"key": {{ customer_metafields_to_monitor.first | split: "." | first | json }},
"namespace": {{ customer_metafields_to_monitor.first | split: "." | last | json }}
}
]
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for customer in result.data.customers.nodes %}
{% comment %}
-- the keys filter in the metafields query should mean only the configured metafields are returned if present on a customer
{% endcomment %}
{% for metafield in customer.metafields.nodes %}
{% assign metafield_created_at_s = metafield.createdAt | date: "%s" | times: 1 %}
{% if metafield_created_at_s < cutoff_date_s %}
{% log
message: "Found metafield that qualifies for deletion.",
customer_id: customer.id,
metafield: metafield
%}
{% assign metafield_input = hash %}
{% assign metafield_input["ownerId"] = customer.id %}
{% assign metafield_input["namespace"] = metafield.namespace %}
{% assign metafield_input["key"] = metafield.key %}
{% assign metafield_inputs = metafield_inputs | push: metafield_input %}
{% endif %}
{% endfor %}
{% endfor %}
{% if result.data.customers.pageInfo.hasNextPage %}
{% assign cursor = result.data.customers.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if metafield_inputs == blank %}
{% log "No metafields qualified to be deleted on this task run." %}
{% break %}
{% endif %}
{% if test_mode %}
{% log %}
"Found {{ metafield_inputs.size }} metafields which qualified to be deleted. This task has test mode enabled, so the deletion will be skipped."
{% endlog %}
{% break %}
{% endif %}
{% comment %}
-- bulk metafield deletion supports 250 metafields per mutation
{% endcomment %}
{% assign groups_of_metafield_inputs = metafield_inputs | in_groups_of: 250, fill_with: false %}
{% for group_of_metafield_inputs in groups_of_metafield_inputs %}
{% action "shopify" %}
mutation {
metafieldsDelete(
metafields: {{ group_of_metafield_inputs | graphql_arguments }}
) {
deletedMetafields {
ownerId
namespace
key
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}