forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days.json
24 lines (24 loc) · 4.54 KB
/
delete-non-purchased-neon-sign-customiser-generated-products-older-than-x-days.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
{
"docs": "Use this task to automatically delete non-purchased products created by [Neon Sign Customiser](https://apps.shopify.com/neon-product-customiser). Set the number of days old products should be before Mechanic deletes a product.\n\nTick the \"Test mode\" checkbox to verify which products will be deleted: after saving, use the \"Run task\" button to run the product scan, and you'll receive a list of products that Mechanic would have deleted in live mode. \n\nOnce you're ready, uncheck the \"Test mode\" box and check the \"Live mode\" box, save the task, then use \"Run task\" to permanently delete all matching products.\n\nCheck the run daily option to have the task run at 12AM daily.\n",
"halt_action_run_sequence_on_error": false,
"name": "Delete non-purchased Neon Sign Customiser app generated products older than X days",
"online_store_javascript": null,
"options": {
"products_must_be_x_number_of_days_old__number__required": "7",
"test_mode__boolean": true,
"live_mode__boolean": false,
"run_daily__boolean": false
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% comment %}\n Options ordering:\n {{ options.products_must_be_x_number_of_days_old__number__required }} \n {{ options.test_mode__boolean }}\n {{ options.live_mode__boolean }} \n{% endcomment %}\n\n{% assign products_must_be_x_number_of_seconds_old = options.products_must_be_x_number_of_days_old__number__required | times: 24 | times: 60 | times: 60 %}\n\n{% assign products_age_threshold_s = \"now\" | date: \"%s\" | minus: products_must_be_x_number_of_seconds_old %}\n\n{% assign products_age_threshold_human = products_age_threshold_s | date: \"%Y-%m-%d %H:%M %:z\" %}\n\n{% assign products_age_threshold_date = products_age_threshold_s | date: \"%Y-%m-%d\" %}\n\n{% log %}{{ \"Threshold for deleteing products: \" | append: products_age_threshold_human | json }}{% endlog %}\n\n{% assign product_ids_and_titles = hash %}\n\n{% capture product_query -%}\n -tag:npc_ordered product_type:'Custom Neon' created_at:<{{ products_age_threshold_date }}\n{% endcapture %}\n\n{% assign cursor = nil %}\n{% for n in (0..100) %}\n {% capture query %}\n query {\n products(\n first: 250\n after: {{ cursor | json }}\n sortKey: TITLE\n query: {{ product_query | json }}\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n id\n title\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"products\": {\n \"pageInfo\": {\n \"hasNextPage\": false\n },\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/Product/1234567890\",\n \"title\": \"[sample product]\"\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for product_edge in result.data.products.edges %}\n {% assign product_ids_and_titles[product_edge.node.id] = product_edge.node.title %}\n {% endfor %}\n\n {% if result.data.products.pageInfo.hasNextPage %}\n {% assign cursor = result.data.products.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n{% endfor %}\n\n{% if options.test_mode__boolean and options.live_mode__boolean %}\n {% error \"Please choose either test mode or live mode.\" %}\n{% elsif options.test_mode__boolean == false and options.live_mode__boolean == false %}\n {% error \"Please choose either test mode or live mode.\" %}\n{% elsif options.test_mode__boolean %}\n {% log products_found_count: product_ids_and_titles.size, products_found: product_ids_and_titles %}\n{% elsif options.live_mode__boolean %}\n {% for keyval in product_ids_and_titles %}\n {% action \"shopify\" %}\n mutation {\n productDelete(\n input: {\n id: {{ keyval[0] | json }}\n }\n ) {\n deletedProductId\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"mechanic/user/trigger"
],
"subscriptions_template": "mechanic/user/trigger\n{% if options.run_daily__boolean %}\n mechanic/scheduler/daily\n{% endif %}",
"tags": [
"Delete",
"Products"
]
}