Skip to content

Commit

Permalink
wrap temp mutation in preview block (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
german-zarate committed May 8, 2024
1 parent fddfeed commit 66130b8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,22 @@
-- temporary preview action to force customer write permissions (placed here because there are break tags below)
{% endcomment %}

{% action "shopify" %}
mutation {
customerCreate(
input: {
email: "[email protected]"
}
) {
userErrors {
field
message
{% if event.preview %}
{% action "shopify" %}
mutation {
customerCreate(
input: {
email: "[email protected]"
}
) {
userErrors {
field
message
}
}
}
}
{% endaction %}
{% endaction %}
{% endif %}

{% if metafield_inputs == blank %}
{% log "No metafields qualified to be deleted on this task run." %}
Expand Down
26 changes: 14 additions & 12 deletions docs/auto-delete-product-metafields-older-than-x-days/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,22 @@
-- temporary preview action to force product write permissions (placed here because there are break tags below)
{% endcomment %}

{% action "shopify" %}
mutation {
productCreate(
input: {
title: "Preview only"
}
) {
userErrors {
field
message
{% if event.preview %}
{% action "shopify" %}
mutation {
productCreate(
input: {
title: "Preview only"
}
) {
userErrors {
field
message
}
}
}
}
{% endaction %}
{% endaction %}
{% endif %}

{% if metafield_inputs == blank %}
{% log "No metafields qualified to be deleted on this task run." %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign customer_metafields_to_monitor = options.customer_metafields_to_monitor__array_required %}\n{% assign minimum_age_in_days_before_deletion = options.minimum_age_in_days_before_deletion__number_required | at_least: 0 %}\n{% assign test_mode = options.test_mode__boolean %}\n\n{% comment %}\n -- use midnight local shop time as the base, so cutoff date will provide X full calendar days (excepting clock changes)\n{% endcomment %}\n\n{% assign metafield_delete_interval_s = minimum_age_in_days_before_deletion | times: 86400 %}\n{% assign last_midnight_s = \"now\" | date: \"%F\" | date: \"%s\" | times: 1 %}\n{% assign cutoff_date_s = last_midnight_s | minus: metafield_delete_interval_s %}\n{% assign cutoff_date = cutoff_date_s | date: \"%FT%T\" %}\n\n{% log\n cutoff_date: cutoff_date,\n task_options: task.options\n%}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% comment %}\n -- query up to 25K customers [higher amounts will likely require a bulk operation query]\n -- no filter query available to only include customers who have values for any of the configured metafields\n {% endcomment %}\n\n {% assign cursor = nil %}\n {% assign metafield_inputs = array %}\n\n {% for n in (1..100) %}\n {% capture query %}\n query {\n customers(\n first: 250\n after: {{ cursor | json }}\n query: {{ search_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n metafields(\n first: {{ customer_metafields_to_monitor.size }}\n keys: {{ customer_metafields_to_monitor | graphql_arguments }}\n ) {\n nodes {\n id\n createdAt\n key\n namespace\n type\n value\n }\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 \"customers\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"metafields\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Metafield/1234567890\",\n \"createdAt\": {{ cutoff_date_s | minus: 1 | json }},\n \"key\": {{ customer_metafields_to_monitor.first | split: \".\" | first | json }},\n \"namespace\": {{ customer_metafields_to_monitor.first | split: \".\" | last | json }}\n }\n ]\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for customer in result.data.customers.nodes %}\n {% comment %}\n -- the keys filter in the metafields query should mean only the configured metafields are returned if present on a customer\n {% endcomment %}\n\n {% for metafield in customer.metafields.nodes %}\n {% assign metafield_created_at_s = metafield.createdAt | date: \"%s\" | times: 1 %}\n\n {% if metafield_created_at_s < cutoff_date_s %}\n {% log\n message: \"Found metafield that qualifies for deletion.\",\n customer_id: customer.id,\n metafield: metafield\n %}\n\n {% assign metafield_input = hash %}\n {% assign metafield_input[\"ownerId\"] = customer.id %}\n {% assign metafield_input[\"namespace\"] = metafield.namespace %}\n {% assign metafield_input[\"key\"] = metafield.key %}\n {% assign metafield_inputs = metafield_inputs | push: metafield_input %}\n {% endif %}\n {% endfor %}\n {% endfor %}\n\n {% if result.data.customers.pageInfo.hasNextPage %}\n {% assign cursor = result.data.customers.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% comment %}\n -- temporary preview action to force customer write permissions (placed here because there are break tags below)\n {% endcomment %}\n\n {% action \"shopify\" %}\n mutation {\n customerCreate(\n input: {\n email: \"[email protected]\"\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n {% if metafield_inputs == blank %}\n {% log \"No metafields qualified to be deleted on this task run.\" %}\n {% break %}\n {% endif %}\n\n {% if test_mode %}\n {% log %}\n \"Found {{ metafield_inputs.size }} metafields which qualified to be deleted. This task has the test mode enabled, so the deletion will be skipped.\"\n {% endlog %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- bulk metafield deletion supports 250 metafields per mutation\n {% endcomment %}\n\n {% assign groups_of_metafield_inputs = metafield_inputs | in_groups_of: 250, fill_with: false %}\n\n {% for group_of_metafield_inputs in groups_of_metafield_inputs %}\n {% action \"shopify\" %}\n mutation {\n metafieldsDelete(\n metafields: {{ group_of_metafield_inputs | graphql_arguments }}\n ) {\n deletedMetafields {\n ownerId\n namespace\n key\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n{% endif %}\n",
"script": "{% assign customer_metafields_to_monitor = options.customer_metafields_to_monitor__array_required %}\n{% assign minimum_age_in_days_before_deletion = options.minimum_age_in_days_before_deletion__number_required | at_least: 0 %}\n{% assign test_mode = options.test_mode__boolean %}\n\n{% comment %}\n -- use midnight local shop time as the base, so cutoff date will provide X full calendar days (excepting clock changes)\n{% endcomment %}\n\n{% assign metafield_delete_interval_s = minimum_age_in_days_before_deletion | times: 86400 %}\n{% assign last_midnight_s = \"now\" | date: \"%F\" | date: \"%s\" | times: 1 %}\n{% assign cutoff_date_s = last_midnight_s | minus: metafield_delete_interval_s %}\n{% assign cutoff_date = cutoff_date_s | date: \"%FT%T\" %}\n\n{% log\n cutoff_date: cutoff_date,\n task_options: task.options\n%}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% comment %}\n -- query up to 25K customers [higher amounts will likely require a bulk operation query]\n -- no filter query available to only include customers who have values for any of the configured metafields\n {% endcomment %}\n\n {% assign cursor = nil %}\n {% assign metafield_inputs = array %}\n\n {% for n in (1..100) %}\n {% capture query %}\n query {\n customers(\n first: 250\n after: {{ cursor | json }}\n query: {{ search_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n metafields(\n first: {{ customer_metafields_to_monitor.size }}\n keys: {{ customer_metafields_to_monitor | graphql_arguments }}\n ) {\n nodes {\n id\n createdAt\n key\n namespace\n type\n value\n }\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 \"customers\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"metafields\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Metafield/1234567890\",\n \"createdAt\": {{ cutoff_date_s | minus: 1 | json }},\n \"key\": {{ customer_metafields_to_monitor.first | split: \".\" | first | json }},\n \"namespace\": {{ customer_metafields_to_monitor.first | split: \".\" | last | json }}\n }\n ]\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for customer in result.data.customers.nodes %}\n {% comment %}\n -- the keys filter in the metafields query should mean only the configured metafields are returned if present on a customer\n {% endcomment %}\n\n {% for metafield in customer.metafields.nodes %}\n {% assign metafield_created_at_s = metafield.createdAt | date: \"%s\" | times: 1 %}\n\n {% if metafield_created_at_s < cutoff_date_s %}\n {% log\n message: \"Found metafield that qualifies for deletion.\",\n customer_id: customer.id,\n metafield: metafield\n %}\n\n {% assign metafield_input = hash %}\n {% assign metafield_input[\"ownerId\"] = customer.id %}\n {% assign metafield_input[\"namespace\"] = metafield.namespace %}\n {% assign metafield_input[\"key\"] = metafield.key %}\n {% assign metafield_inputs = metafield_inputs | push: metafield_input %}\n {% endif %}\n {% endfor %}\n {% endfor %}\n\n {% if result.data.customers.pageInfo.hasNextPage %}\n {% assign cursor = result.data.customers.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% comment %}\n -- temporary preview action to force customer write permissions (placed here because there are break tags below)\n {% endcomment %}\n\n {% if event.preview %}\n {% action \"shopify\" %}\n mutation {\n customerCreate(\n input: {\n email: \"[email protected]\"\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n\n {% if metafield_inputs == blank %}\n {% log \"No metafields qualified to be deleted on this task run.\" %}\n {% break %}\n {% endif %}\n\n {% if test_mode %}\n {% log %}\n \"Found {{ metafield_inputs.size }} metafields which qualified to be deleted. This task has the test mode enabled, so the deletion will be skipped.\"\n {% endlog %}\n {% break %}\n {% endif %}\n\n {% comment %}\n -- bulk metafield deletion supports 250 metafields per mutation\n {% endcomment %}\n\n {% assign groups_of_metafield_inputs = metafield_inputs | in_groups_of: 250, fill_with: false %}\n\n {% for group_of_metafield_inputs in groups_of_metafield_inputs %}\n {% action \"shopify\" %}\n mutation {\n metafieldsDelete(\n metafields: {{ group_of_metafield_inputs | graphql_arguments }}\n ) {\n deletedMetafields {\n ownerId\n namespace\n key\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"mechanic/scheduler/daily",
"mechanic/user/trigger"
Expand Down
Loading

0 comments on commit 66130b8

Please sign in to comment.