diff --git a/app/controllers/admin/connected_apps_controller.rb b/app/controllers/admin/connected_apps_controller.rb index 2cbaca7c9b..9ddf7d9405 100644 --- a/app/controllers/admin/connected_apps_controller.rb +++ b/app/controllers/admin/connected_apps_controller.rb @@ -6,11 +6,7 @@ module Admin authorize! :admin, enterprise app = ConnectedApp.create!(enterprise_id: enterprise.id) - - ConnectAppJob.perform_later( - app, spree_current_user.spree_api_key, - channel: SessionChannel.for_request(request), - ) + app.connect(api_key: spree_current_user.spree_api_key, channel: SessionChannel.for_request(request)) render_panel end @@ -21,12 +17,6 @@ module Admin app = enterprise.connected_apps.first app.destroy - WebhookDeliveryJob.perform_later( - app.data["destroy"], - "disconnect-app", - nil - ) - render_panel end diff --git a/app/models/connected_app.rb b/app/models/connected_app.rb index d2e6ffa8b7..85cdc6f9a5 100644 --- a/app/models/connected_app.rb +++ b/app/models/connected_app.rb @@ -5,6 +5,7 @@ # Here we store keys and links to access the app. class ConnectedApp < ApplicationRecord belongs_to :enterprise + after_destroy :disconnect scope :discover_regen, -> { where(type: "ConnectedApp") } @@ -15,4 +16,16 @@ class ConnectedApp < ApplicationRecord def ready? !connecting? end + + def connect(api_key:, channel:) + ConnectAppJob.perform_later(self, api_key, channel:) + end + + def disconnect + WebhookDeliveryJob.perform_later( + data["destroy"], + "disconnect-app", + nil + ) + end end