forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
112 lines (97 loc) · 4.09 KB
/
script.liquid
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{% 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 %}
{"error": "Choose at least one condition for sending an email."}
{% endunless %}
{% assign state_transition_descriptions = hash %}
{% assign state_transition_descriptions["DECLINED"] = "A customer has declined an invitation" %}
{% assign state_transition_descriptions["DISABLED"] = "A customer account has been disabled" %}
{% assign state_transition_descriptions["ENABLED"] = "A customer has set an initial account password" %}
{% assign state_transition_descriptions["INVITED"] = "A customer has been invited" %}
{% capture query %}
query {
customer(id: {{ customer.admin_graphql_api_id | json }}) {
state
previousStateMetafield: metafield(
namespace: "mechanic"
key: "previous-state"
) {
id
value
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% assign metafield = hash %}
{% if options.email_when_a_customer_declines_an_invitation__boolean %}
{% assign metafield["value"] = "DECLINED" %}
{% elsif options.email_when_a_customer_account_is_disabled__boolean %}
{% assign metafield["value"] = "DISABLED" %}
{% elsif options.email_when_a_customer_initially_sets_an_account_password__boolean %}
{% assign metafield["value"] = "ENABLED" %}
{% elsif options.email_when_a_customer_is_invited__boolean %}
{% assign metafield["value"] = "INVITED" %}
{% endif %}
{% else %}
{% assign metafield = result.data.customer.previousStateMetafield %}
{% endif %}
{% assign oldState = metafield.value %}
{% assign newState = result.data.customer.state %}
{% if oldState != newState %}
{% if event.topic == "shopify/customers/update" and oldState != blank %}
{% assign send_email = false %}
{% if newState == "DECLINED" and options.email_when_a_customer_declines_an_invitation__boolean %}
{% assign send_email = true %}
{% elsif newState == "DISABLED" and options.email_when_a_customer_account_is_disabled__boolean %}
{% assign send_email = true %}
{% elsif newState == "ENABLED" and options.email_when_a_customer_initially_sets_an_account_password__boolean %}
{% assign send_email = true %}
{% elsif newState == "INVITED" and options.email_when_a_customer_is_invited__boolean %}
{% assign send_email = true %}
{% endif %}
{% if send_email %}
{% assign email_subject = "[" | append: shop.name | append: "] " | append: state_transition_descriptions[newState] %}
{% capture email_body %}
Hello,
{{ state_transition_descriptions[newState] }}.
Customer email: {{ customer.email | default: "(unknown)" }}
Customer name: {{ customer.first_name | append: " " | append: customer.last_name | strip | default: "(unknown)" }}
<a href="https://{{ shop.domain }}/admin/customers/{{ customer.id }}">Manage this customer</a>
Thanks,
Mechanic (for {{ shop.name }})
{% endcapture %}
{% action "email" %}
{
"to": {{ options.email_recipient__email_required | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | unindent | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
{% endif %}
{% action "shopify" %}
mutation {
customerUpdate(
input: {
id: {{ customer.admin_graphql_api_id | json }}
metafields: [
{
id: {{ metafield.id | json }}
namespace: "mechanic"
key: "previous-state"
type: "single_line_text_field"
value: {% if event.preview %}"DISABLED"{% else %}{{ newState | json }}{% endif %}
}
]
}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}