forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag-customers-in-bulk-by-email-address.json
19 lines (19 loc) · 5.78 KB
/
tag-customers-in-bulk-by-email-address.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"docs": "This task prompts you for a list of email addresses and tags, one pair per line, in the format \"[email protected]:tag\". The task adds each tag to its related customer. Developers: this task is an example of working in batches with GraphQL – useful when working with bulk data.\n\nEnter a list of email addresses and tags, one pair per line, in the format \"[email protected]:tag\". The task will add each tag to its related customer, unless (a) the customer already has the tag, (b) the customer could not be found, or (c) a problem was found either with the entered line or with the customer record.",
"halt_action_run_sequence_on_error": false,
"name": "Tag customers in bulk by email address",
"online_store_javascript": null,
"options": {},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.preview %}\n {% assign event = hash %}\n {% assign event[\"data\"] = \"[email protected]:foobar,customer@foo:bar\" | replace: \",\", newline %}\n {% assign event[\"preview\"] = true %}\n{% endif %}\n\n{% assign query_cost = 3 %}\n{% assign mutation_cost = 10 %}\n{% assign bucket_size = 1000 %}\n{% assign buffer_factor = 2 %}\n\n{% assign lines = event.data | strip | split: newline | uniq | sort %}\n{% assign lines_and_customers = hash %}\n\n{% assign scan_batch_size = bucket_size | times: 1.0 | divided_by: query_cost | divided_by: buffer_factor | round %}\n{% assign scan_batches = lines | in_groups_of: scan_batch_size, fill_with: false %}\n\n{% for scan_batch in scan_batches %}\n {% assign batch_lines = array %}\n\n {% for line in scan_batch %}\n {% if line == blank %}\n {% continue %}\n {% endif %}\n\n {% assign parts = line | split: \":\" %}\n {% if parts.size != 2 %}\n {% log message: \"Found an invalid line.\", line: line %}\n {% continue %}\n {% endif %}\n\n {% unless parts.first contains \"@\" %}\n {% log message: \"Found an invalid email address.\", line: line %}\n {% continue %}\n {% endunless %}\n\n {% assign email = parts[0] | strip %}\n {% assign tag = parts[1] | strip %}\n\n {% assign batch_lines[batch_lines.size] = email | append: \":\" | append: tag %}\n {% endfor %}\n\n {% if batch_lines == empty %}\n {% continue %}\n {% endif %}\n\n {% capture query %}\n query {\n {% for line in batch_lines %}\n {% assign email = line | split: \":\" | first %}\n customer{{ forloop.index0 }}: customers(first: 1, query: {{ email | json | prepend: \"email:\" | json }}) {\n edges {\n node {\n id\n email\n tags\n }\n }\n }\n {% endfor %}\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n {% for line in batch_lines %}\n \"customer{{ forloop.index0 }}\": {\n \"edges\": [\n {\n \"node\": {\n \"email\": {{ line | split: \":\" | first | json }},\n \"id\": \"gid://shopify/Customer/{{ forloop.index0 }}\",\n \"tags\": []\n }\n }\n ]\n }\n {% unless forloop.last %},{% endunless %}\n {% endfor %}\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for line in batch_lines %}\n {% assign key = \"customer\" | append: forloop.index0 %}\n {% assign email = line | split: \":\" | first %}\n {% assign tag = line | split: \":\" | last %}\n {% assign customer = result.data[key].edges.first.node %}\n\n {% assign customer_email_downcase = customer.email | downcase %}\n {% assign email_downcase = email | downcase %}\n {% if customer_email_downcase != email_downcase %}\n {% log message: \"Customer returned by Shopify somehow does not match the email address we searched for. Skipping.\", line: line, customer: customer %}\n {% continue %}\n {% endif %}\n\n {% if customer == nil %}\n {% log message: \"This customer could not be found. Skipping.\", line: line %}\n {% continue %}\n {% endif %}\n\n {% if customer.tags contains tag %}\n {% log message: \"Customer already has tag. Skipping.\", line: line, tag: tag, customer: customer %}\n {% continue %}\n {% endif %}\n\n {% assign lines_and_customers[line] = customer %}\n {% endfor %}\n{% endfor %}\n\n{% assign taggings = array %}\n\n{% for keyval in lines_and_customers %}\n {% assign line = keyval[0] %}\n {% assign tag = line | split: \":\" | last %}\n {% assign customer = keyval[1] %}\n\n {% assign tagging = hash %}\n {% assign tagging[\"id\"] = customer.id %}\n {% assign tagging[\"tags\"] = tag %}\n\n {% assign taggings[taggings.size] = tagging %}\n{% endfor %}\n\n{% if taggings != empty %}\n {% assign tagging_batch_size = bucket_size | times: 1.0 | divided_by: mutation_cost | divided_by: buffer_factor | round %}\n {% assign tagging_batches = taggings | in_groups_of: tagging_batch_size, fill_with: false %}\n\n {% for tagging_batch in tagging_batches %}\n {% action \"shopify\" %}\n mutation {\n {% for tagging in tagging_batch %}\n customer{{ forloop.index0 }}: tagsAdd({{ tagging | graphql_arguments }}) {\n userErrors {\n field\n message\n }\n node {\n ... on Customer {\n id\n email\n tags\n }\n }\n }\n {% endfor %}\n }\n {% endaction %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"mechanic/user/text"
],
"subscriptions_template": "mechanic/user/text",
"tags": [
"Customers",
"Email",
"Tag"
]
}