forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemporarily-enable-tax-exempt-status-when-a-customer-is-tagged.json
23 lines (23 loc) · 5.47 KB
/
temporarily-enable-tax-exempt-status-when-a-customer-is-tagged.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": "This task monitors customer tags, applying tax-exempt status when a certain tag is detected, and removing it (and the tag) after a certain amount of time. Useful when you need to regularly re-approve customers for tax exemption.",
"halt_action_run_sequence_on_error": false,
"name": "Temporarily enable tax-exempt status when a customer is tagged",
"online_store_javascript": null,
"options": {
"tax_exempt_tag__required": "tax-exempt-for-30-days",
"days_before_removing_tax_exempt_status__number_required": 30
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign metafield_namespace = \"mechanic\" %}\n{% assign metafield_key = \"tax-exempt-expiry-scheduled\" %}\n{% assign expiry_event_topic = \"user/customers/expire_tax_exempt\" %}\n\n{% if event.topic contains \"shopify/customers/\" %}\n {% if event.preview %}\n {% capture customer_json %}\n {\n \"admin_graphql_api_id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": {{ options.tax_exempt_tag__required | json }},\n \"metafields\": {\n {{ metafield_namespace | json }}: {\n {{ metafield_key | json }}: null\n }\n }\n }\n {% endcapture %}\n\n {% assign customer = customer_json | parse_json %}\n {% endif %}\n\n {% assign customer_qualifies = false %}\n {% assign customer_tags = customer.tags | split: \", \" %}\n {% if customer_tags contains options.tax_exempt_tag__required %}\n {% if customer.metafields[metafield_namespace][metafield_key] != nil %}\n {% log \"Customer already has their metafield set - skipping\" %}\n {% else %}\n {% assign customer_qualifies = true %}\n {% endif %}\n {% endif %}\n\n {% if customer_qualifies %}\n {% assign expiry_interval_s = 60 | times: 60 | times: 24 | times: options.days_before_removing_tax_exempt_status__number_required %}\n {% assign expiry_time = \"now\" | date: \"%s\" | plus: expiry_interval_s | round %}\n\n {% action \"event\" %}\n {\n \"topic\": {{ expiry_event_topic | json }},\n \"data\": {\n \"customer_id\": {{ customer.admin_graphql_api_id | json }},\n \"customer_tag\": {{ options.tax_exempt_tag__required | json }}\n },\n \"run_at\": {{ expiry_time | json }}\n }\n {% endaction %}\n\n {% action \"shopify\" %}\n mutation {\n customerUpdate(\n input: {\n id: {{ customer.admin_graphql_api_id | json }}\n taxExempt: true\n metafields: [\n {\n namespace: {{ metafield_namespace | json }}\n key: {{ metafield_key | json }}\n type: \"number_integer\"\n value: \"1\"\n }\n ]\n }\n ) {\n customer {\n id\n taxExempt\n metafield(namespace: {{ metafield_namespace | json }}, key: {{ metafield_key | json }}) {\n id\n value\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% elsif event.topic == expiry_event_topic %}\n {% capture customer_query %}\n query {\n customer(id: {{ event.data.customer_id | json }}) {\n id\n tags\n metafield(namespace: {{ metafield_namespace | json }}, key: {{ metafield_key | json }}) {\n id\n value\n }\n }\n }\n {% endcapture %}\n\n {% assign customer_result = customer_query | shopify %}\n\n {% if event.preview %}\n {% capture customer_result_json %}\n {\n \"data\": {\n \"customer\": {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": [{{ options.tax_exempt_tag__required | json }}],\n \"metafield\": {\n \"id\": \"gid://shopify/Metafield/1234567890\",\n \"value\": \"1\"\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign customer_result = customer_result_json | parse_json %}\n {% endif %}\n\n {% assign customer = customer_result.data.customer %}\n\n {% assign customer_qualifies = false %}\n {% if customer.tags contains options.tax_exempt_tag__required and customer.metafield.value != nil %}\n {% assign customer_qualifies = true %}\n {% endif %}\n\n {% if event.preview or customer_qualifies %}\n {% action \"shopify\" %}\n mutation {\n customerUpdate(\n input: {\n id: {{ customer.id | json }}\n taxExempt: false\n }\n ) {\n userErrors {\n field\n message\n }\n }\n\n tagsRemove(\n id: {{ customer.id | json }}\n tags: {{ options.tax_exempt_tag__required | 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_result.data.customer.metafield.id | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endif %}",
"subscriptions": [
"shopify/customers/update",
"user/customers/expire_tax_exempt"
],
"subscriptions_template": "shopify/customers/update\nuser/customers/expire_tax_exempt",
"tags": [
"Customers",
"Tag",
"Tax"
]
}