forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-recurring-draft-orders.json
29 lines (29 loc) · 17.7 KB
/
auto-recurring-draft-orders.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
25
26
27
28
29
{
"docs": "This task searches for draft orders having the configured tag, and duplicates each one (minus the tag used for searching). Optionally, this task can automatically send an invoice to the customer on file, after the new draft order is created. Or, this task can auto-complete the order, marking the order as paid if you so chose. (Auto-completed orders will result in an email receipt being sent to the customer on file for the original draft order.)\r\n\r\nRun this task manually to scan and duplicate matching draft orders. Use the \"run\" task options to control automatic running of this task.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-recurring draft orders",
"online_store_javascript": null,
"options": {
"draft_order_tag__required": "repeat-me",
"cycle_start_date__required": "2021-01-01",
"number_of_days_in_cycle__number_required": "7",
"complete_the_order_after_creating__boolean": null,
"complete_the_order_and_mark_as_paid_after_creating__boolean": null,
"send_email_invoice_after_creating__boolean": false,
"email_invoice_subject": null,
"email_invoice_bcc__email_array": null,
"email_invoice_custom_message__multiline": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n Option order:\n\n {{ options.draft_order_tag__required }}\n {{ options.cycle_start_date__required }}\n {{ options.number_of_days_in_cycle__number_required }}\n {{ options.complete_the_order_after_creating__boolean }}\n {{ options.complete_the_order_and_mark_as_paid_after_creating__boolean }}\n {{ options.send_email_invoice_after_creating__boolean }}\n {{ options.email_invoice_subject }}\n {{ options.email_invoice_bcc__email_array }}\n {{ options.email_invoice_custom_message__multiline }}\n{% endcomment %}\n\n{% if options.send_email_invoice_after_creating__boolean and options.complete_the_order_after_creating__boolean %}\n {\"error\": \"Choose either an email invoice or completing the order - not both. :)\"}\n{% elsif options.send_email_invoice_after_creating__boolean and options.complete_the_order_and_mark_as_paid_after_creating__boolean %}\n {\"error\": \"Choose either an email invoice or completing the order - not both. :)\"}\n{% endif %}\n\n{% if options.complete_the_order_after_creating__boolean or options.complete_the_order_and_mark_as_paid_after_creating__boolean %}\n {% assign autocomplete = true %}\n{% endif %}\n\n{% assign cycle_start = options.cycle_start_date__required | date: \"%s\" %}\n{% if cycle_start == blank or cycle_start == options.cycle_start_date__required %}\n {\"error\": {{ options.cycle_start_date__required | json | prepend: \"Mechanic wasn't able to parse \" | append: \" - try another value.\" | json }}}\n{% endif %}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% assign run_qualifies = false %}\n\n {% if event.preview %}\n {% action \"shopify\" %}\n mutation {\n draftOrderCreate(\n input: {\n billingAddress: {\n address1: \"123 State St\"\n city: \"Everyville\"\n company: \"Widgets Inc\"\n countryCode: EE\n firstName: \"Victor\"\n lastName: \"Juarez\"\n provinceCode: \"PK\"\n zip: \"54321\"\n }\n purchasingEntity: {\n customerId: \"gid://shopify/Customer/1234567890\"\n }\n email: \"[email protected]\"\n lineItems: [\n {\n weight: {\n unit: GRAMS\n value: 5\n }\n originalUnitPrice: \"5.0\"\n quantity: 2\n requiresShipping: false\n sku: \"123ABC\"\n taxable: true\n title: \"Widget, Our Best\"\n }\n ]\n tags: []\n taxExempt: false\n useCustomerDefaultAddress: false\n }\n ) {\n draftOrder {\n id\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% else %}\n {% assign cycle_start_date = options.cycle_start_date__required %}\n {% assign cycle_start_d = cycle_start_date | date: \"%s\" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %}\n {% assign now_d = \"now\" | date: \"%s\" | times: 1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %}\n {% assign day_gap = now_d | minus: cycle_start_d | modulo: options.number_of_days_in_cycle__number_required %}\n\n {% if day_gap == 0 %}\n {% assign run_qualifies = true %}\n {% else %}\n {\"log\": {{ options.number_of_days_in_cycle__number_required | minus: day_gap | prepend: \"Waiting another \" | append: \" day(s) for cycle to complete\" | json }}}\n {% endif %}\n {% endif %}\n\n {% if run_qualifies %}\n {% for n in (0..1000) %}\n {% capture query %}\n query {\n draftOrders(\n first: 1\n after: {{ cursor | json }}\n query: {{ \"tag:\" | append: options.draft_order_tag__required | json }}\n sortKey: NUMBER\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n id\n appliedDiscount {\n amountV2 {\n amount\n currencyCode\n }\n description\n title\n value\n valueType\n }\n billingAddress {\n address1\n address2\n city\n company\n countryCodeV2\n firstName\n lastName\n phone\n provinceCode\n zip\n }\n customer {\n id\n }\n customAttributes {\n key\n value\n }\n email\n lineItems(first: 150) {\n edges {\n node {\n appliedDiscount {\n description\n title\n value\n valueType\n amountV2 {\n amount\n currencyCode\n }\n }\n customAttributes {\n key\n value\n }\n originalUnitPrice\n quantity\n sku\n taxable\n title\n variant {\n id\n }\n weight {\n unit\n value\n }\n }\n }\n }\n note2\n shippingAddress {\n address1\n address2\n city\n company\n countryCodeV2\n firstName\n lastName\n phone\n provinceCode\n zip\n }\n shippingLine {\n title\n shippingRateHandle\n originalPriceSet {\n shopMoney {\n amount\n currencyCode\n }\n }\n }\n tags\n taxExempt\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% for edge in result.data.draftOrders.edges %}\n {% assign draftOrder = edge.node %}\n\n {% action \"shopify\" %}\n mutation {\n draftOrderCreate(\n input: {\n {% if draftOrder.appliedDiscount %}\n appliedDiscount: {\n amount: {{ draftOrder.appliedDiscount.amountV2.amount | json }}\n description: {{ draftOrder.appliedDiscount.description | json }}\n title: {{ draftOrder.appliedDiscount.title | json }}\n value: {{ draftOrder.appliedDiscount.value | json }}\n valueType: {{ draftOrder.appliedDiscount.valueType }}\n }\n {% endif %}\n {% if draftOrder.billingAddress %}\n billingAddress: {\n address1: {{ draftOrder.billingAddress.address1 | json }}\n address2: {{ draftOrder.billingAddress.address2 | json }}\n city: {{ draftOrder.billingAddress.city | json }}\n company: {{ draftOrder.billingAddress.company | json }}\n countryCode: {{ draftOrder.billingAddress.countryCodeV2 }}\n firstName: {{ draftOrder.billingAddress.firstName | json }}\n lastName: {{ draftOrder.billingAddress.lastName | json }}\n phone: {{ draftOrder.billingAddress.phone | json }}\n provinceCode: {{ draftOrder.billingAddress.provinceCode | json }}\n zip: {{ draftOrder.billingAddress.zip | json }}\n }\n {% endif %}\n {% if draftOrder.customer %}\n purchasingEntity: {\n customerId: {{ draftOrder.customer.id | json }}\n }\n {% endif %}\n {% if draftOrder.customAttributes != empty %}\n customAttributes: [\n {% for customAttribute in draftOrder.customAttributes %}\n {\n key: {{ customAttribute.key | json }}\n value: {{ customAttribute.value | json }}\n }\n {% endfor %}\n ]\n {% endif %}\n {% if draftOrder.email %}\n email: {{ draftOrder.email | json }}\n {% endif %}\n lineItems: [\n {% for lineItem_edge in draftOrder.lineItems.edges %}\n {% assign lineItem = lineItem_edge.node %}\n {\n {% if lineItem.appliedDiscount %}\n appliedDiscount: {\n amount: {{ lineItem.appliedDiscount.amountV2.amount | json }}\n description: {{ lineItem.appliedDiscount.description | json }}\n title: {{ lineItem.appliedDiscount.title | json }}\n value: {{ lineItem.appliedDiscount.value | json }}\n valueType: {{ lineItem.appliedDiscount.valueType }}\n }\n {% endif %}\n {% if lineItem.customAttributes != empty %}\n customAttributes: [\n {% for customAttribute in lineItem.customAttributes %}\n {\n key: {{ customAttribute.key | json }}\n value: {{ customAttribute.value | json }}\n }\n {% endfor %}\n ]\n {% endif %}\n originalUnitPrice: {{ lineItem.originalUnitPrice | json }}\n quantity: {{ lineItem.quantity | json }}\n {% if lineItem.requiresShipping != nil %}\n requiresShipping: {{ lineItem.requiresShipping | json }}\n {% endif %}\n {% if lineItem.sku %}\n sku: {{ lineItem.sku | json }}\n {% endif %}\n taxable: {{ lineItem.taxable | json }}\n title: {{ lineItem.title | json }}\n {% if lineItem.variant %}\n variantId: {{ lineItem.variant.id | json }}\n {% endif %}\n {% if lineItem.weight and lineItem.weight.value != 0 %}\n weight: {\n value: {{ lineItem.weight.value | json }}\n unit: {{ lineItem.weight.unit }}\n }\n {% endif %}\n }\n {% endfor %}\n ]\n {% if options.send_email_invoice_after_creating__boolean or autocomplete %}\n metafields: [\n {\n namespace: \"mechanic\"\n key: {% if options.send_email_invoice_after_creating__boolean %}\"autoinvoice\"{% else %}\"autocomplete\"{% endif %}\n value: \"true\"\n type: \"boolean\"\n }\n ]\n {% endif %}\n {% if draftOrder.note2 != blank %}\n note: {{ draftOrder.note2 | json }}\n {% endif %}\n {% if draftOrder.shippingAddress %}\n shippingAddress: {\n address1: {{ draftOrder.shippingAddress.address1 | json }}\n address2: {{ draftOrder.shippingAddress.address2 | json }}\n city: {{ draftOrder.shippingAddress.city | json }}\n company: {{ draftOrder.shippingAddress.company | json }}\n countryCode: {{ draftOrder.shippingAddress.countryCodeV2 }}\n firstName: {{ draftOrder.shippingAddress.firstName | json }}\n lastName: {{ draftOrder.shippingAddress.lastName | json }}\n phone: {{ draftOrder.shippingAddress.phone | json }}\n provinceCode: {{ draftOrder.shippingAddress.provinceCode | json }}\n zip: {{ draftOrder.shippingAddress.zip | json }}\n }\n {% endif %}\n {% if draftOrder.shippingLine %}\n shippingLine: {\n price: {{ draftOrder.shippingLine.originalPriceSet.shopMoney.amount | json }}\n shippingRateHandle: {{ draftOrder.shippingLine.shippingRateHandle | json }}\n title: {{ draftOrder.shippingLine.title | json }}\n }\n {% endif %}\n tags: {{ draftOrder.tags | remove_tag: options.draft_order_tag__required | json }}\n taxExempt: {{ draftOrder.taxExempt | json }}\n useCustomerDefaultAddress: false\n }\n ) {\n draftOrder {\n id\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n\n {% if result.data.draftOrders.pageInfo.hasNextPage %}\n {% assign cursor = result.data.draftOrders.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n {% endif %}\n{% elsif event.topic == \"shopify/draft_orders/create\" and options.send_email_invoice_after_creating__boolean %}\n {% capture query %}\n query {\n draftOrder(\n id: {{ draft_order.admin_graphql_api_id | json }}\n ) {\n metafield(\n namespace: \"mechanic\"\n key: \"autoinvoice\"\n ) {\n value\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview or result.data.draftOrder.metafield %}\n {% action \"shopify\" %}\n mutation {\n draftOrderInvoiceSend(\n id: {{ draft_order.admin_graphql_api_id | json }}\n email: {\n {% if options.email_invoice_subject != blank %}\n subject: {{ options.email_invoice_subject | json }}\n {% endif %}\n bcc: {{ options.email_invoice_bcc__email_array | json }}\n customMessage: {{ options.email_invoice_custom_message__multiline | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% elsif event.topic == \"shopify/draft_orders/create\" and autocomplete %}\n {% capture query %}\n query {\n draftOrder(\n id: {{ draft_order.admin_graphql_api_id | json }}\n ) {\n metafield(\n namespace: \"mechanic\"\n key: \"autocomplete\"\n ) {\n value\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview or result.data.draftOrder.metafield %}\n {% action \"shopify\" %}\n mutation {\n draftOrderComplete(\n id: {{ draft_order.admin_graphql_api_id | json }}\n paymentPending: {% if options.complete_the_order_and_mark_as_paid_after_creating__boolean %}false{% else %}true{% endif %}\n ) {\n draftOrder {\n order {\n id\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endif %}",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/scheduler/daily"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/scheduler/daily\n{% if options.send_email_invoice_after_creating__boolean or options.complete_the_order_after_creating__boolean or options.complete_the_order_and_mark_as_paid_after_creating__boolean %}shopify/draft_orders/create{% endif %}",
"tags": [
"Draft Orders",
"Recurring"
]
}