-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdelete-customer-metafields-in-bulk.json
26 lines (26 loc) · 5.97 KB
/
delete-customer-metafields-in-bulk.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
26
{
"docs": "With no configuration, this task will delete all customer metafields in bulk. Configure it with a metafield namespace to only delete metafields with that namespace, or add both a namespace and key to get even more specific. Run this task with the test mode option enabled, the first time, to make sure you're deleting the right material.",
"halt_action_run_sequence_on_error": false,
"name": "Delete customer metafields in bulk",
"online_store_javascript": null,
"options": {
"metafield_namespace": null,
"metafield_key": null,
"test_mode__boolean": true
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign metafield_namespace = options.metafield_namespace %}\n{% assign metafield_key = options.metafield_key %}\n{% assign test_mode = options.test_mode__boolean %}\n\n{% if metafield_namespace == blank and metafield_key != blank %}\n {% error \"If you provide a metafield key, you must also provide a metafield namespace.\" %}\n{% endif %}\n\n{% if event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n customers {\n edges {\n node {\n __typename\n id\n {% if metafield_key != blank %}\n metafield(\n namespace: {{ metafield_namespace | json }}\n key: {{ metafield_key | json }}\n ) {\n __typename\n id\n description\n namespace\n key\n value\n type\n }\n {% else %}\n metafields\n {% if metafield_namespace != blank %}\n (namespace: {{ metafield_namespace | json }})\n {% endif %}\n {\n edges {\n node {\n __typename\n id\n description\n namespace\n key\n value\n type\n }\n }\n }\n {% endif %}\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if event.preview %}\n {% capture objects_jsonl %}\n {\"__typename\":\"Customer\",\"id\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\",\"metafield\":{\"__typename\":\"Metafield\",\"id\":\"gid:\\/\\/shopify\\/Metafield\\/1234567890\"}}\n {\"__typename\":\"Metafield\",\"id\":\"gid:\\/\\/shopify\\/Metafield\\/2345678901\",\"__parentId\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\"}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = objects_jsonl | parse_jsonl %}\n {% endif %}\n\n {% assign metafields_to_delete = array %}\n\n {% if metafield_key != blank %}\n {% comment %}\n -- metafield key was configured, thus the metafields will only be on the customer object in the returned bulk objects\n {% endcomment %}\n\n {% assign customers_with_metafield\n = bulkOperation.objects\n | where: \"__typename\", \"Customer\"\n | where: \"metafield\"\n %}\n\n {% for customer in customers_with_metafield %}\n {% assign metafield_to_delete = hash %}\n {% assign metafield_to_delete[\"ownerId\"] = customer.id %}\n {% assign metafield_to_delete[\"namespace\"] = metafield_namespace %}\n {% assign metafield_to_delete[\"key\"] = metafield_key %}\n {% assign metafields_to_delete = metafields_to_delete | push: metafield_to_delete %}\n {% endfor %}\n\n {% else %}\n {% comment %}\n -- metafield key was not configured, thus the metafields will be separated in the returned bulk objects\n {% endcomment %}\n\n {% assign metafields = bulkOperation.objects | where: \"__typename\", \"Metafield\" %}\n\n {% for metafield in metafields %}\n {% assign metafield_to_delete = hash %}\n {% assign metafield_to_delete[\"ownerId\"] = metafield.__parentId %}\n {% assign metafield_to_delete[\"namespace\"] = metafield.namespace %}\n {% assign metafield_to_delete[\"key\"] = metafield.key %}\n {% assign metafields_to_delete = metafields_to_delete | push: metafield_to_delete %}\n {% endfor %}\n {% endif %}\n\n {% log %}\n \"Found {{ metafields_to_delete.size }} metafields to delete.\"\n {% endlog %}\n\n {% if metafields_to_delete != blank %}\n {% if test_mode %}\n {% log \"Task has test mode enabled. No metafields will be deleted on this task run.\" %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- metafields may be deleted in batches of 250\n {% endcomment %}\n\n {% assign groups_of_metafields_to_delete = metafields_to_delete | in_groups_of: 250, fill_with: false %}\n\n {% for group_of_metafields_to_delete in groups_of_metafields_to_delete %}\n {% action \"shopify\" %}\n mutation {\n metafieldsDelete(\n metafields: {{ group_of_metafields_to_delete | 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{% endif %}\n",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Bulk",
"Customers",
"Delete",
"Metafields"
]
}