forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduled-theme-publishing.json
21 lines (21 loc) · 4.07 KB
/
scheduled-theme-publishing.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"docs": "This task will allow you to schedule any number of themes to be published at future dates and times. When the task runs at its normally scheduled 10 minute interval, it will find the entry with the most recent configured date in the past, and if that theme is not currently published, then the task will publish it.\n\nWithin the *Theme IDs and publish dates* field, simply configure the theme IDs on the left along with the related publish dates on the right. Publish dates may be entered with a date and time, using a 24-hour clock, in the format **YYYY-MM-DD HH:MM**, or only with a date in the format **YYYY-MM-DD**, in which case the time would default to midnight.\n\n*Important notes:*\n- All times are in local shop time.\n- The task will attempt to parse the configured dates and times, and will display an error at run time if any of them are unparsable in the expected format.\n- While this task is active it may override any manual publishing actions.",
"halt_action_run_sequence_on_error": false,
"name": "Scheduled theme publishing",
"online_store_javascript": null,
"options": {
"theme_ids_and_publish_dates__keyval": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign theme_ids_and_publish_dates = options.theme_ids_and_publish_dates__keyval %}\n\n{% if event.preview %}\n {% assign theme_to_publish = hash %}\n {% assign theme_to_publish[\"id\"] = 1234567890 %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% assign shop_theme_ids = shop.themes | map: \"id\" %}\n\n {% assign publication_candidates = array %}\n\n {% for keyval in theme_ids_and_publish_dates %}\n {% assign theme_id = keyval.first %}\n {% assign publish_date = keyval.last %}\n\n {% assign theme = shop.themes[theme_id] %}\n\n {% if theme == blank %}\n {% error\n message: \"A theme with this id was not found in this shop.\",\n theme_id: theme_id,\n shop_theme_ids: shop_theme_ids\n %}\n {% endif %}\n\n {% assign valid_publish_date = publish_date | parse_date: \"%Y-%m-%d %H:%M\" %}\n\n {% unless valid_publish_date %}\n {% assign valid_publish_date = publish_date | parse_date: \"%Y-%m-%d\" %}\n {% endunless %}\n\n {% if valid_publish_date == blank %}\n {% error\n message: \"A theme publish date could not be parsed. Refer to the task documentation for valid date/time formats.\",\n theme_id: theme_id,\n publish_date: publish_date\n %}\n {% endif %}\n\n {% assign publication_candidate = hash %}\n {% assign publication_candidate[\"theme\"] = theme %}\n {% assign publication_candidate[\"publish_date\"] = valid_publish_date | date: \"%s\" %}\n {% assign publication_candidates = publication_candidates | push: publication_candidate %}\n {% endfor %}\n\n {% assign publication_candidates = publication_candidates | sort: \"publish_date\" %}\n\n {% assign now = \"now\" | date: \"%s\" %}\n {% assign theme_to_publish = nil %}\n\n {% for publication_candidate in publication_candidates %}\n {% if publication_candidate.publish_date <= now %}\n {% assign theme_to_publish = publication_candidate.theme %}\n\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n{% endif %}\n\n{% if theme_to_publish != blank %}\n {% if theme_to_publish.role == \"main\" %}\n {% log\n message: \"The theme to be published is already set as the main theme on the shop; skipping.\",\n theme_to_publish: theme_to_publish\n %}\n\n {% else %}\n {% action \"shopify\" %}\n [\n \"update\",\n \"theme\",\n {\n \"id\": {{ theme_to_publish.id }},\n \"role\": \"main\"\n }\n ]\n {% endaction %}\n {% endif %}\n\n{% else %}\n {% log \"No themes qualify to be published based on their configured publish dates.\" %}\n{% endif %}",
"subscriptions": [
"mechanic/scheduler/10min",
"mechanic/user/trigger"
],
"subscriptions_template": "mechanic/scheduler/10min\nmechanic/user/trigger",
"tags": [
"Publish",
"Schedule"
]
}