Skip to content

Commit

Permalink
the dumb solution
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-liuu committed Oct 30, 2024
1 parent bd368ce commit fc2103c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/shopify_api/webhooks/registrations/event_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def build_check_query
edges {
node {
id
includeFields
metafieldNamespaces
endpoint {
__typename
... on WebhookEventBridgeEndpoint {
Expand All @@ -48,12 +50,17 @@ def parse_check_result(body)
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
webhook_id = nil
current_address = nil
fields = nil
metafield_namespaces = nil
unless edges.empty?
node = edges[0]["node"]
webhook_id = node["id"].to_s
current_address = node["endpoint"]["arn"].to_s
fields = node["includeFields"]
metafield_namespaces = node["metafieldNamespaces"]
end
{ webhook_id: webhook_id, current_address: current_address }
{ webhook_id: webhook_id, current_address: current_address, fields: fields,
metafield_namespaces: metafield_namespaces, }
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/shopify_api/webhooks/registrations/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def build_check_query
edges {
node {
id
includeFields
metafieldNamespaces
endpoint {
__typename
... on WebhookHttpEndpoint {
Expand All @@ -54,6 +56,8 @@ def parse_check_result(body)
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
webhook_id = nil
current_address = nil
fields = nil
metafield_namespaces = nil
unless edges.empty?
node = edges[0]["node"]
webhook_id = node["id"].to_s
Expand All @@ -63,8 +67,11 @@ def parse_check_result(body)
else
node["callbackUrl"].to_s
end
fields = node["includeFields"]
metafield_namespaces = node["metafieldNamespaces"]
end
{ webhook_id: webhook_id, current_address: current_address }
{ webhook_id: webhook_id, current_address: current_address, fields: fields,
metafield_namespaces: metafield_namespaces, }
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/shopify_api/webhooks/registrations/pub_sub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def build_check_query
edges {
node {
id
includeFields
metafieldNamespaces
endpoint {
__typename
... on WebhookPubSubEndpoint {
Expand All @@ -53,12 +55,17 @@ def parse_check_result(body)
edges = body.dig("data", "webhookSubscriptions", "edges") || {}
webhook_id = nil
current_address = nil
fields = nil
metafield_namespaces = nil
unless edges.empty?
node = edges[0]["node"]
webhook_id = node["id"].to_s
current_address = "pubsub://#{node["endpoint"]["pubSubProject"]}:#{node["endpoint"]["pubSubTopic"]}"
fields = node["includeFields"]
metafield_namespaces = node["metafieldNamespaces"]
end
{ webhook_id: webhook_id, current_address: current_address }
{ webhook_id: webhook_id, current_address: current_address, fields: fields,
metafield_namespaces: metafield_namespaces, }
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/shopify_api/webhooks/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ def webhook_registration_needed?(client, registration)
"Failed to check if webhook was already registered" unless check_response.ok?

parsed_check_result = registration.parse_check_result(T.cast(check_response.body, T::Hash[String, T.untyped]))
must_register = parsed_check_result[:current_address] != registration.callback_address
must_register = parsed_check_result[:current_address] != registration.callback_address ||
parsed_check_result[:fields] != registration.fields ||
parsed_check_result[:metafield_namespaces] != registration.metafield_namespaces

{ webhook_id: parsed_check_result[:webhook_id], must_register: must_register }
end
Expand Down
6 changes: 6 additions & 0 deletions test/webhooks/webhook_registration_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def queries
edges {
node {
id
includeFields
metafieldNamespaces
endpoint {
__typename
... on WebhookHttpEndpoint {
Expand Down Expand Up @@ -207,6 +209,8 @@ def queries
edges {
node {
id
includeFields
metafieldNamespaces
endpoint {
__typename
... on WebhookEventBridgeEndpoint {
Expand Down Expand Up @@ -397,6 +401,8 @@ def queries
edges {
node {
id
includeFields
metafieldNamespaces
endpoint {
__typename
... on WebhookPubSubEndpoint {
Expand Down

0 comments on commit fc2103c

Please sign in to comment.