diff --git a/CHANGELOG.md b/CHANGELOG.md index 88fd4f74e..eac45c405 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Unreleased * Set `access_scopes` column to string by default [#1636](https://github.com/Shopify/shopify_app/pull/1636) * Fixes a bug with `EnsureBilling` causing infinite redirect in embedded apps [#1578](https://github.com/Shopify/shopify_app/pull/1578) * Modifies SessionStorage#with_shopify_session to call a block with a supplied session instance [#1488](https://github.com/Shopify/shopify_app/pull/1488) +* Refactors `ShopifyApp::WebhhooksManager#recreate_webhooks!` to have a uniform webhook inventory that doesn't clash with the API library. Updates webhook generator to use supplied session. [#1686](https://github.com/Shopify/shopify_app/pull/1686) 21.4.1 (Feb 21, 2023) ---------- diff --git a/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt b/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt index 2c7021355..ec32ea4db 100644 --- a/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt +++ b/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt @@ -12,11 +12,11 @@ class <%= @job_class_name %> < ActiveJob::Base if shop.nil? logger.error("#{self.class} failed: cannot find shop with domain '#{shop_domain}'") - + raise ActiveRecord::RecordNotFound, "Shop Not Found" end - shop.with_shopify_session do + shop.with_shopify_session do |session| end end end diff --git a/lib/shopify_app/managers/webhooks_manager.rb b/lib/shopify_app/managers/webhooks_manager.rb index b1ca01203..09acc85fc 100644 --- a/lib/shopify_app/managers/webhooks_manager.rb +++ b/lib/shopify_app/managers/webhooks_manager.rb @@ -24,10 +24,8 @@ def recreate_webhooks!(session:) destroy_webhooks(session: session) return unless ShopifyApp.configuration.has_webhooks? - add_registrations - ShopifyApp::Logger.debug("Recreating webhooks") - ShopifyAPI::Webhooks::Registry.register_all(session: session) + add_registrations end def destroy_webhooks(session:) diff --git a/test/shopify_app/managers/webhooks_manager_test.rb b/test/shopify_app/managers/webhooks_manager_test.rb index 86e6fc1d7..2e47f76ee 100644 --- a/test/shopify_app/managers/webhooks_manager_test.rb +++ b/test/shopify_app/managers/webhooks_manager_test.rb @@ -78,33 +78,15 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase end test "#recreate_webhooks! destroys all webhooks and recreates" do + ShopifyApp.configuration.expects(:has_webhooks?).returns(true) session = ShopifyAPI::Auth::Session.new(shop: "shop.myshopify.com") - ShopifyAPI::Webhooks::Registry.expects(:register_all) - ShopifyAPI::Webhooks::Registry.expects(:unregister).with(topic: "orders/updated", session: session) - ShopifyApp::WebhooksManager.expects(:add_registrations).twice + ShopifyApp::WebhooksManager.expects(:destroy_webhooks) + ShopifyApp::WebhooksManager.expects(:add_registrations) - ShopifyApp.configure do |config| - config.webhooks = [ - { topic: "orders/updated", path: "webhooks" }, - ] - end - ShopifyApp::WebhooksManager.add_registrations ShopifyApp::WebhooksManager.recreate_webhooks!(session: session) end - test "#recreate_webhooks! does not call unregister if there is no webhook" do - ShopifyAPI::Webhooks::Registry.expects(:register_all).never - ShopifyAPI::Webhooks::Registry.expects(:unregister).never - ShopifyAPI::Webhooks::Registry.expects(:add_registration).never - - ShopifyApp.configure do |config| - config.webhooks = [] - end - ShopifyApp::WebhooksManager.add_registrations - ShopifyApp::WebhooksManager.recreate_webhooks!(session: ShopifyAPI::Auth::Session.new(shop: "shop.myshopify.com")) - end - test "#destroy_webhooks destroy all webhooks" do session = ShopifyAPI::Auth::Session.new(shop: "shop.myshopify.com") ShopifyAPI::Webhooks::Registry.expects(:unregister).with(topic: "orders/updated", session: session)