forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-remove-a-customer-tag-x-days-after-its-added.json
23 lines (23 loc) · 6.06 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 new customer tag, and to schedule the customer to be untagged some number of days later. Useful for granting temporary access to discounts, or other resources.\n\nThis task monitors new and updated customers, watching for the configured customer tag.\r\n\r\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.\r\n\r\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). If the additional tag is found missing, the task will leave the tag in place instead of auto-removing it.",
"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": "{% 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{% assign tag = options.tag_to_monitor__required %}\n{% assign untag_flag_tag = tag | append: \" - will be auto-removed by Mechanic\" %}\n{% assign tag_removal_interval_s = options.days_to_wait_before_untagging__number_required | times: 24 | times: 60 | times: 60 | round %}\n{% assign now_s = \"now\" | date: \"%s\" | times: 1 %}\n{% assign metafield_key = task.id | sha256 | slice: 0, 7 %}\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 | json }}],\n \"metafield\": null\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 %}\n {% assign time_to_remove_s = customer.metafield.value | times: 1 %}\n\n {% if customer.metafield == nil 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 customerUpdate(\n input: {\n id: {{ customer.id | json }}\n metafields: [\n {\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 ) {\n customer {\n metafield(\n namespace: \"mechanic\"\n key: {{ metafield_key | json }}\n ) {\n id\n }\n }\n userErrors {\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 {% elsif customer.tags contains untag_flag_tag %}\n {% if now_s < time_to_remove_s %}\n {% log message: \"This customer is scheduled to be untagged, but it's not time yet. Skipping.\", tag: tag, untag_flag_tag: untag_flag_tag, now_s: now_s, time_to_remove_s: time_to_remove_s %}\n {% else %}\n {% log message: \"This customer is scheduled to be untagged, and that time is now.\", tag: tag, untag_flag_tag: untag_flag_tag, now_s: now_s, time_to_remove_s: time_to_remove_s %}\n {% action \"shopify\" %}\n mutation {\n tagsRemove(\n id: {{ customer.id | json }}\n tags: [{{ tag | json }}, {{ untag_flag_tag | json }}]\n ) {\n node {\n ... on Customer {\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n\n metafieldDelete(\n input: {\n id: {{ customer.metafield.id | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% else %}\n {% log message: \"This customer has a tag removal time recorded, but the 'untag flag tag' has been removed. Skipping.\", tag: tag, untag_flag_tag: untag_flag_tag, now_s: now_s, time_to_remove_s: time_to_remove_s %}\n {% endif %}\n{% elsif event.topic == \"user/task/untag_customer\" %}\n {% log message: \"The tag auto-removal event has arrived, but the customer has already been untagged by someone/something else. Skipping.\", tag: tag, customer_tags: customer.tags %}\n{% endif %}",
"subscriptions": [
"shopify/customers/update",
"user/task/untag_customer"
],
"subscriptions_template": "shopify/customers/update\nuser/task/untag_customer",
"tags": [
"Schedule",
"Untag"
]
}