forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
75 lines (70 loc) · 3.44 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
{% comment %}
Preferred option order:
{{ options.tag_to_add_after_verifying__required }}
{{ options.verification_email_subject__required }}
{{ options.verification_email_body__multiline_required }}
{{ options.verification_confirmed_email_subject__required }}
{{ options.verification_confirmed_email_body__multiline_required }}
{{ options.verification_webhook_event_topic__required }}
{% endcomment %}
{% if event.topic == "shopify/customers/create" %}
{% assign customer_tags = customer.tags | split: ", " %}
{% unless customer_tags contains options.tag_to_add_after_verifying__required %}
{% assign verification_code = "now" | date: "%s" | append: customer.email | sha256 | slice: 0, 6 | upcase %}
{% action "email" %}
{
"to": {{ customer.email | json }},
"subject": {{ options.verification_email_subject__required | json }},
"body": {{ options.verification_email_body__multiline_required | replace: "VERIFICATION_CODE", verification_code | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% assign cache_key = customer.email | downcase | sha256 | prepend: "customer_verification_code:" %}
{% action "cache", "set", cache_key, verification_code %}
{% endunless %}
{% elsif event.topic == options.verification_webhook_event_topic__required %}
{% assign cache_key = event.data.customer_email | downcase | sha256 | prepend: "customer_verification_code:" %}
{% assign submitted_verification_code = event.data.verification_code %}
{% assign required_verification_code = cache[cache_key] %}
{% assign customer = shop.customers[event.data.customer_email] %}
{% if event.preview %}
{% assign submitted_verification_code = "123ABC" %}
{% assign required_verification_code = "123ABC" %}
{% assign customer = hash %}
{% assign customer["admin_graphql_api_id"] = "gid://shopify/Customer/1234567890" %}
{% assign customer["email"] = "[email protected]" %}
{% endif %}
{% if customer == nil %}
{"log": {{ event.data.customer_email | json | prepend: "Unable to locate customer for submitted email address " | json }}}
{% elsif required_verification_code == nil %}
{"log": {{ event.data.customer_email | json | prepend: "No verification code on file for customer " | json }}}
{% elsif required_verification_code != submitted_verification_code %}
{"log": {{ event.data.customer_email | json | prepend: "Submitted verification code did not match the code on file for customer " | json }}}
{"log": {{ required_verification_code | json | prepend: "Verification code on file: " | json }}}
{% else %}
{% action "email" %}
{
"to": {{ customer.email | json }},
"subject": {{ options.verification_confirmed_email_subject__required | json }},
"body": {{ options.verification_confirmed_email_body__multiline_required | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% action "cache", "del", cache_key %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ customer.admin_graphql_api_id | json }}
tags: {{ options.tag_to_add_after_verifying__required | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}