forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-an-email-alert-when-a-customer-changes-state.json
26 lines (26 loc) · 5.84 KB
/
send-an-email-alert-when-a-customer-changes-state.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": "This task monitors for updates to a customer's state (account disabled, password set, invited, or invitation declined), and sends alert emails for any state changes you care about.\n\nThis task monitors for updates to [a customer's state](https://help.shopify.com/en/api/graphql-admin-api/reference/enum/customerstate), and sends alert emails for any state changes you care about.\r\n\r\nThis task works by storing the customer's previous state, and comparing it to the new state. Therefore, this task cannot send emails for customers it hasn't seen before. This means that you may not see emails for some customers immediately after installing the task; emails will begin sending as customers are created and updated, allowing Mechanic to fill in its knowledge of customer state.",
"halt_action_run_sequence_on_error": false,
"name": "Send an email alert when a customer changes state",
"online_store_javascript": null,
"options": {
"email_when_a_customer_declines_an_invitation__boolean": true,
"email_when_a_customer_account_is_disabled__boolean": true,
"email_when_a_customer_initially_sets_an_account_password__boolean": true,
"email_when_a_customer_is_invited__boolean": true,
"email_recipient__email_required": ""
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% unless options.email_when_a_customer_declines_an_invitation__boolean or options.email_when_a_customer_account_is_disabled__boolean or options.email_when_a_customer_initially_sets_an_account_password__boolean or options.email_when_a_customer_is_invited__boolean %}\n {\"error\": \"Choose at least one condition for sending an email.\"}\n{% endunless %}\n\n{% assign state_transition_descriptions = hash %}\n{% assign state_transition_descriptions[\"DECLINED\"] = \"A customer has declined an invitation\" %}\n{% assign state_transition_descriptions[\"DISABLED\"] = \"A customer account has been disabled\" %}\n{% assign state_transition_descriptions[\"ENABLED\"] = \"A customer has set an initial account password\" %}\n{% assign state_transition_descriptions[\"INVITED\"] = \"A customer has been invited\" %}\n\n{% capture query %}\n query {\n customer(id: {{ customer.admin_graphql_api_id | json }}) {\n state\n previousStateMetafield: metafield(\n namespace: \"mechanic\"\n key: \"previous-state\"\n ) {\n id\n value\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% assign metafield = hash %}\n {% if options.email_when_a_customer_declines_an_invitation__boolean %}\n {% assign metafield[\"value\"] = \"DECLINED\" %}\n {% elsif options.email_when_a_customer_account_is_disabled__boolean %}\n {% assign metafield[\"value\"] = \"DISABLED\" %}\n {% elsif options.email_when_a_customer_initially_sets_an_account_password__boolean %}\n {% assign metafield[\"value\"] = \"ENABLED\" %}\n {% elsif options.email_when_a_customer_is_invited__boolean %}\n {% assign metafield[\"value\"] = \"INVITED\" %}\n {% endif %}\n{% else %}\n {% assign metafield = result.data.customer.previousStateMetafield %}\n{% endif %}\n\n{% assign oldState = metafield.value %}\n{% assign newState = result.data.customer.state %}\n\n{% if oldState != newState %}\n {% if event.topic == \"shopify/customers/update\" and oldState != blank %}\n {% assign send_email = false %}\n\n {% if newState == \"DECLINED\" and options.email_when_a_customer_declines_an_invitation__boolean %}\n {% assign send_email = true %}\n {% elsif newState == \"DISABLED\" and options.email_when_a_customer_account_is_disabled__boolean %}\n {% assign send_email = true %}\n {% elsif newState == \"ENABLED\" and options.email_when_a_customer_initially_sets_an_account_password__boolean %}\n {% assign send_email = true %}\n {% elsif newState == \"INVITED\" and options.email_when_a_customer_is_invited__boolean %}\n {% assign send_email = true %}\n {% endif %}\n\n {% if send_email %}\n {% assign email_subject = \"[\" | append: shop.name | append: \"] \" | append: state_transition_descriptions[newState] %}\n\n {% capture email_body %}\n Hello,\n\n {{ state_transition_descriptions[newState] }}.\n\n Customer email: {{ customer.email | default: \"(unknown)\" }}\n Customer name: {{ customer.first_name | append: \" \" | append: customer.last_name | strip | default: \"(unknown)\" }}\n\n <a href=\"https://{{ shop.domain }}/admin/customers/{{ customer.id }}\">Manage this customer</a>\n\n Thanks,\n Mechanic (for {{ shop.name }})\n {% endcapture %}\n\n {% action \"email\" %}\n {\n \"to\": {{ options.email_recipient__email_required | json }},\n \"subject\": {{ email_subject | json }},\n \"body\": {{ email_body | unindent | strip | newline_to_br | json }},\n \"reply_to\": {{ shop.customer_email | json }},\n \"from_display_name\": {{ shop.name | json }}\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n\n {% action \"shopify\" %}\n mutation {\n customerUpdate(\n input: {\n id: {{ customer.admin_graphql_api_id | json }}\n metafields: [\n {\n id: {{ metafield.id | json }}\n namespace: \"mechanic\"\n key: \"previous-state\"\n type: \"single_line_text_field\"\n value: {% if event.preview %}\"DISABLED\"{% else %}{{ newState | json }}{% endif %}\n }\n ]\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% endif %}",
"subscriptions": [
"shopify/customers/update",
"shopify/customers/create"
],
"subscriptions_template": "shopify/customers/update\nshopify/customers/create",
"tags": [
"Alert",
"Customers",
"Email"
]
}