Move connect logic to model

Then subtypes can override as needed.
This commit is contained in:
David Cook
2024-07-16 13:04:23 +10:00
parent 9b37eacb8d
commit 9d89b4726b
2 changed files with 14 additions and 11 deletions

View File

@@ -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

View File

@@ -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