-
Notifications
You must be signed in to change notification settings - Fork 35
/
auto-remove-a-customer-tag-x-days-after-its-added.json
23 lines (23 loc) · 6.43 KB
/
auto-remove-a-customer-tag-x-days-after-its-added.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"docs": "Use this this task to monitor for the addition of a specific customer tag, and to schedule the customer to be untagged a configurable number of days later. Useful for granting temporary access to discounts, or other resources.\n\nAs soon as that tag is detected, the task will _add_ a second tag, indicating that the customer is scheduled to be untagged. (For example, if the task is configured to watch for the tag \"Approved\", the task will _add_ the tag \"Approved - will be auto-removed by Mechanic\".) The task will then schedule a followup event for the future, according to the configured number of days to wait. At that time, the task will remove both tags.\n\nImportant note: To _prevent_ the task from untagging the customer later, manually remove the task's additional tag (i.e. the \"will be auto-removed by Mechanic\" tag).",
"halt_action_run_sequence_on_error": false,
"name": "Auto-remove a customer tag x days after it's added",
"online_store_javascript": null,
"options": {
"tag_to_monitor__required": null,
"days_to_wait_before_untagging__number_required": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign tag_to_monitor = options.tag_to_monitor__required %}\n{% assign tag_removal_interval_s = options.days_to_wait_before_untagging__number_required | times: 24 | times: 60 | times: 60 | round %}\n\n{% assign untag_flag_tag = tag_to_monitor | append: \" - will be auto-removed by Mechanic\" %}\n{% assign now_s = \"now\" | date: \"%s\" | times: 1 %}\n{% assign metafield_key = task.id | sha256 | slice: 0, 7 %}\n\n{% if event.topic == \"user/task/untag_customer\" %}\n {% assign customer_id = event.data.customer_id %}\n{% else %}\n {% assign customer_id = customer.admin_graphql_api_id %}\n{% endif %}\n\n{% capture query %}\n query {\n customer(id: {{ customer_id | json }}) {\n id\n tags\n metafield(\n namespace: \"mechanic\"\n key: {{ metafield_key | json }}\n ) {\n id\n value\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"customer\": {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": {{ tag_to_monitor | json }}\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign customer = result.data.customer %}\n\n{% if customer.tags contains tag_to_monitor %}\n {% assign time_to_remove_s = customer.metafield.value | times: 1 %}\n\n {% if customer.metafield == blank or time_to_remove_s == 0 %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ untag_flag_tag | json }}\n ) {\n node {\n ... on Customer {\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n\n metafieldsSet(\n metafields: [\n {\n ownerId: {{ customer.id | json }}\n namespace: \"mechanic\"\n key: {{ metafield_key | json }}\n value: {{ now_s | plus: tag_removal_interval_s | append: \"\" | json }}\n type: \"number_integer\"\n }\n ]\n ) {\n metafields {\n id\n namespace\n key\n type\n value\n owner {\n ... on Customer {\n id\n displayName\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% action \"event\" %}\n {\n \"topic\": \"user/task/untag_customer\",\n \"data\": {\n \"customer_id\": {{ customer.id | json }}\n },\n \"run_at\": {{ now_s | plus: tag_removal_interval_s | json }},\n \"task_id\": {{ task.id | json }}\n }\n {% endaction %}\n\n {% elsif customer.tags contains untag_flag_tag %}\n {% if now_s < time_to_remove_s %}\n {% log\n message: \"This customer is scheduled to be untagged, but it's not time yet. Skipping.\",\n tag_to_monitor: tag_to_monitor,\n untag_flag_tag: untag_flag_tag,\n now_s: now_s,\n time_to_remove_s: time_to_remove_s\n %}\n\n {% else %}\n {% log\n message: \"This customer is scheduled to be untagged, and that time is now.\",\n tag_to_monitor: tag_to_monitor,\n untag_flag_tag: untag_flag_tag,\n now_s: now_s,\n time_to_remove_s: time_to_remove_s\n %}\n\n {% action \"shopify\" %}\n mutation {\n tagsRemove(\n id: {{ customer.id | json }}\n tags: {{ array | push: tag_to_monitor, untag_flag_tag | json }}\n ) {\n node {\n ... on Customer {\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n\n metafieldsDelete(\n metafields: [\n {\n ownerId: {{ customer.id | json }}\n namespace: \"mechanic\"\n key: {{ metafield_key | json }}\n }\n ]\n ) {\n deletedMetafields {\n ownerId\n namespace\n key\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n\n {% else %}\n {% log\n message: \"This customer has a tag removal time recorded, but the 'untag flag tag' has been removed. Skipping.\",\n tag_to_monitor: tag_to_monitor,\n untag_flag_tag: untag_flag_tag,\n now_s: now_s,\n time_to_remove_s: time_to_remove_s\n %}\n {% endif %}\n\n{% elsif event.topic == \"user/task/untag_customer\" %}\n {% log\n message: \"The tag auto-removal event has arrived, but the customer has already been untagged by someone/something else. Skipping.\",\n tag_to_monitor: tag_to_monitor,\n customer_tags: customer.tags\n %}\n{% endif %}\n",
"subscriptions": [
"shopify/customers/update",
"user/task/untag_customer"
],
"subscriptions_template": "shopify/customers/update\nuser/task/untag_customer",
"tags": [
"Schedule",
"Untag"
]
}