Module: DiscoApp::Concerns::SynchroniseWebhooksJob

Extended by:
ActiveSupport::Concern
Included in:
SynchroniseWebhooksJob
Defined in:
app/jobs/disco_app/concerns/synchronise_webhooks_job.rb

Constant Summary collapse

COMMON_WEBHOOKS =
%i[app/uninstalled shop/update]

Instance Method Summary collapse

Instance Method Details

#perform(_shop) ⇒ Object

Ensure the webhooks registered with our shop are the same as those listed in our application configuration.



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
# File 'app/jobs/disco_app/concerns/synchronise_webhooks_job.rb', line 9

def perform(_shop)
  # Register any webhooks that haven't been registered yet.
  (expected_topics - current_topics).each do |topic|
    with_verbose_output(topic) do
      ShopifyAPI::Webhook.create(
        topic: topic,
        fields: topic_fields(topic),
        address: webhooks_url,
        format: 'json'
      )
    end
  end

  # Remove any extraneous topics.
  current_webhooks.each do |webhook|
    ShopifyAPI::Webhook.delete(webhook.id) unless expected_topics.include?(webhook.topic.to_sym)
  end

  # Ensure webhook addresses are current.
  current_webhooks.each do |webhook|
    expected_fields = topic_fields(webhook.topic.to_sym).map(&:to_s)

    next if webhook.address == webhooks_url && webhook.fields == expected_fields

    webhook.address = webhooks_url
    webhook.fields = expected_fields
    webhook.save
  end
end