diff --git a/app/jobs/connect_app_job.rb b/app/jobs/connect_app_job.rb new file mode 100644 index 0000000000..f439f3b46e --- /dev/null +++ b/app/jobs/connect_app_job.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class ConnectAppJob < ApplicationJob + def perform(app, token) + url = "https://n8n.openfoodnetwork.org.uk/webhook/regen/connect-enterprise" + event = "connect-app" + payload = { + enterprise_id: app.enterprise_id, + access_token: token, + } + + response = WebhookDeliveryJob.perform_now(url, event, payload) + app.update!(data: JSON.parse(response)) + end +end diff --git a/app/jobs/webhook_delivery_job.rb b/app/jobs/webhook_delivery_job.rb index 6de75361dd..1196e6a1d6 100644 --- a/app/jobs/webhook_delivery_job.rb +++ b/app/jobs/webhook_delivery_job.rb @@ -46,5 +46,7 @@ class WebhookDeliveryJob < ApplicationJob # Raise a failed request error and let job runner handle retrying. # In theory, only 5xx errors should be retried, but who knows. raise FailedWebhookRequestError, response.status.to_s unless response.success? + + response.body end end diff --git a/app/reflexes/admin/connected_app_reflex.rb b/app/reflexes/admin/connected_app_reflex.rb index eb4c9abe5e..9f125f18eb 100644 --- a/app/reflexes/admin/connected_app_reflex.rb +++ b/app/reflexes/admin/connected_app_reflex.rb @@ -5,7 +5,8 @@ module Admin def create enterprise = Enterprise.find(element.dataset.enterprise_id) authorize! :admin, enterprise - ConnectedApp.create!(enterprise_id: enterprise.id) + app = ConnectedApp.create!(enterprise_id: enterprise.id) + ConnectAppJob.perform_later(app, current_user.spree_api_key) end end end diff --git a/spec/fixtures/vcr_cassettes/ConnectAppJob/stores_connection_data_on_the_app.yml b/spec/fixtures/vcr_cassettes/ConnectAppJob/stores_connection_data_on_the_app.yml new file mode 100644 index 0000000000..044cb90175 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/ConnectAppJob/stores_connection_data_on_the_app.yml @@ -0,0 +1,54 @@ +--- +http_interactions: +- request: + method: post + uri: https://n8n.openfoodnetwork.org.uk/webhook/regen/connect-enterprise + body: + encoding: UTF-8 + string: '{"id":"6efbbeea-4078-4bb5-88b1-c8dbf599c520","at":"2023-12-14 12:13:39 + +1100","event":"connect-app","data":{"enterprise_id":23,"access_token":"b5fa8c7fa1dd5331a2111fcc907040d842c5eb928c512e43"}}' + headers: + User-Agent: + - openfoodnetwork_webhook/1.0 + Content-Type: + - application/json + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Server: + - nginx + Date: + - Thu, 14 Dec 2023 01:13:41 GMT + Content-Type: + - text/html; charset=utf-8 + Content-Length: + - '35' + Connection: + - keep-alive + Etag: + - W/"23-GW39X6dSljjgz4GPY7ICa+eNupE" + Vary: + - Accept-Encoding + Strict-Transport-Security: + - max-age=63072000 + X-Xss-Protection: + - 1; mode=block + X-Download-Options: + - noopen + X-Content-Type-Options: + - nosniff + X-Permitted-Cross-Domain-Policies: + - none + Referrer-Policy: + - same-origin + body: + encoding: UTF-8 + string: '{"link":"https://example.net/edit"}' + recorded_at: Thu, 14 Dec 2023 01:13:40 GMT +recorded_with: VCR 6.2.0 diff --git a/spec/jobs/connect_app_job_spec.rb b/spec/jobs/connect_app_job_spec.rb new file mode 100644 index 0000000000..9433c0d71c --- /dev/null +++ b/spec/jobs/connect_app_job_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ConnectAppJob, type: :job, vcr: true do + subject { ConnectAppJob.new(app, token) } + + let(:app) { ConnectedApp.create!(enterprise: ) } + let(:enterprise) { create(:enterprise) } + let(:token) { enterprise.owner.spree_api_key } + + before { enterprise.owner.generate_api_key } + + it "stores connection data on the app" do + subject.perform_now + + expect(app.data).to eq({ "link" => "https://example.net/edit" }) + end +end