forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
139 lines (122 loc) · 4.02 KB
/
script.liquid
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% capture query %}
query {
publications(first: 250) {
edges {
node {
id
name
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign publications = array %}
{% for publication_edge in result.data.publications.edges %}
{% if options.sales_channel_names__required_array contains publication_edge.node.name %}
{% assign publications[publications.size] = publication_edge.node %}
{% endif %}
{% endfor%}
{% if event.preview != true and publications.size != options.sales_channel_names__required_array.size %}
{% log publications_named: options.sales_channel_names__required_array, publications_available: result.data.publications.edges, publications_matched: publications %}
{% error "Unable to find all named publications. Double-check your task configuration." %}
{% endif %}
{% capture bulk_operation_query %}
query {
products (query: {{ options.only_publish_products_matching_this_query | json }}) {
edges {
node {
__typename
id
{% for publication in publications %}
publishedOnPublication{{ forloop.index }}: publishedOnPublication(
publicationId: {{ publication.id | json }}
)
{% endfor %}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% capture query %}
query {
publications(first: 250) {
edges {
node {
id
name
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign publications = array %}
{% for publication_edge in result.data.publications.edges %}
{% if options.sales_channel_names__required_array contains publication_edge.node.name %}
{% assign publications[publications.size] = publication_edge.node %}
{% endif %}
{% endfor%}
{% assign products = bulkOperation.objects | where: "__typename", "Product" %}
{% assign product_ids_and_publication_ids = array %}
{% for product in products %}
{% for publication in publications %}
{% assign published_key = "publishedOnPublication" | append: forloop.index %}
{% unless product[published_key] %}
{% assign pair = array %}
{% assign pair[0] = product.id %}
{% assign pair[1] = publication.id %}
{% assign product_ids_and_publication_ids[product_ids_and_publication_ids.size] = pair %}
{% endunless %}
{% endfor %}
{% endfor %}
{% if event.preview %}
{% assign product_ids_and_publication_ids[0] = array %}
{% assign product_ids_and_publication_ids[0] = "gid://shopify/Product/1234567890" %}
{% assign product_ids_and_publication_ids[0] = "gid://shopify/Publication/1234567890" %}
{% endif %}
{% if options.test_mode__boolean and event.preview == false %}
{% action "echo" %}
{
"message": "Found {{ product_ids_and_publication_ids.size }} publishing actions",
"product_ids_and_publication_ids": {{ product_ids_and_publication_ids | json }}
}
{% endaction %}
{% else %}
{% for product_id_and_publication_id in product_ids_and_publication_ids %}
{% action "shopify" %}
mutation {
publishablePublish(
id: {{ product_id_and_publication_id[0] | json }}
input: {
publicationId: {{ product_id_and_publication_id[1] | json }}
}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}
{% endif %}