Query webhook to connect app

This commit is contained in:
Maikel Linke
2023-12-14 12:21:11 +11:00
parent 4eb273b06e
commit 12e5a0d1e1
5 changed files with 92 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

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