forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-delete-customer-metafields-older-than-x-days.json
25 lines (25 loc) · 6.75 KB
/
auto-delete-customer-metafields-older-than-x-days.json
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
{
"docs": "Use this task to auto-delete customer metafields that are older than the configured minimum age in days. Useful for removing transient metafields a certain number of days after they have been created.\n\nEnter one or more metafields using *namespace.key* format (e.g. \"custom.recent_purchase\" ), and on the daily task run it will find and delete any matching customer metafield which was **created at** prior to the cutoff date.\n\nIt is highly recommended to first run this task using the *Test mode* option, so it can log out which metafields have qualified for deletion without actually deleting them. Pair this with running the task manually to avoid waiting for the next scheduled task run to see the test mode logging.\n\n**Note:**\nThe \"cutoff date\" uses the beginning of the task run day (i.e. midnight local shop time) as the start time to count backwards the configured minimum age in days.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-delete customer metafields older than X days",
"online_store_javascript": null,
"options": {
"customer_metafields_to_monitor__array_required": null,
"minimum_age_in_days_before_deletion__number_required": null,
"test_mode__boolean": true
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign customer_metafields_to_monitor = options.customer_metafields_to_monitor__array_required %}\n{% assign minimum_age_in_days_before_deletion = options.minimum_age_in_days_before_deletion__number_required | at_least: 0 %}\n{% assign test_mode = options.test_mode__boolean %}\n\n{% comment %}\n -- use midnight local shop time as the base, so cutoff date will provide X full calendar days (excepting clock changes)\n{% endcomment %}\n\n{% assign metafield_delete_interval_s = minimum_age_in_days_before_deletion | times: 86400 %}\n{% assign last_midnight_s = \"now\" | date: \"%F\" | date: \"%s\" | times: 1 %}\n{% assign cutoff_date_s = last_midnight_s | minus: metafield_delete_interval_s %}\n{% assign cutoff_date = cutoff_date_s | date: \"%FT%T\" %}\n\n{% log\n cutoff_date: cutoff_date,\n task_options: task.options\n%}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% comment %}\n -- query up to 25K customers [higher amounts will likely require a bulk operation query]\n -- no filter query available to only include customers who have values for any of the configured metafields\n {% endcomment %}\n\n {% assign cursor = nil %}\n {% assign metafield_inputs = array %}\n\n {% for n in (1..100) %}\n {% capture query %}\n query {\n customers(\n first: 250\n after: {{ cursor | json }}\n query: {{ search_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n metafields(\n first: {{ customer_metafields_to_monitor.size }}\n keys: {{ customer_metafields_to_monitor | graphql_arguments }}\n ) {\n nodes {\n id\n createdAt\n key\n namespace\n type\n value\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"customers\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"metafields\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Metafield/1234567890\",\n \"createdAt\": {{ cutoff_date_s | minus: 1 | json }},\n \"key\": {{ customer_metafields_to_monitor.first | split: \".\" | first | json }},\n \"namespace\": {{ customer_metafields_to_monitor.first | split: \".\" | last | json }}\n }\n ]\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for customer in result.data.customers.nodes %}\n {% comment %}\n -- the keys filter in the metafields query should mean only the configured metafields are returned if present on a customer\n {% endcomment %}\n\n {% for metafield in customer.metafields.nodes %}\n {% assign metafield_created_at_s = metafield.createdAt | date: \"%s\" | times: 1 %}\n\n {% if metafield_created_at_s < cutoff_date_s %}\n {% log\n message: \"Found metafield that qualifies for deletion.\",\n customer_id: customer.id,\n metafield: metafield\n %}\n\n {% assign metafield_input = hash %}\n {% assign metafield_input[\"ownerId\"] = customer.id %}\n {% assign metafield_input[\"namespace\"] = metafield.namespace %}\n {% assign metafield_input[\"key\"] = metafield.key %}\n {% assign metafield_inputs = metafield_inputs | push: metafield_input %}\n {% endif %}\n {% endfor %}\n {% endfor %}\n\n {% if result.data.customers.pageInfo.hasNextPage %}\n {% assign cursor = result.data.customers.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if metafield_inputs == blank %}\n {% log \"No metafields qualified to be deleted on this task run.\" %}\n {% break %}\n {% endif %}\n\n {% if test_mode %}\n {% log %}\n \"Found {{ metafield_inputs.size }} metafields which qualified to be deleted. This task has test mode enabled, so the deletion will be skipped.\"\n {% endlog %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- bulk metafield deletion supports 250 metafields per mutation\n {% endcomment %}\n\n {% assign groups_of_metafield_inputs = metafield_inputs | in_groups_of: 250, fill_with: false %}\n\n {% for group_of_metafield_inputs in groups_of_metafield_inputs %}\n {% action \"shopify\" %}\n mutation {\n metafieldsDelete(\n metafields: {{ group_of_metafield_inputs | graphql_arguments }}\n ) {\n deletedMetafields {\n ownerId\n namespace\n key\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"mechanic/scheduler/daily",
"mechanic/user/trigger"
],
"subscriptions_template": "mechanic/scheduler/daily\nmechanic/user/trigger",
"tags": [
"Customers",
"Delete",
"Metafields"
]
}