mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-23 05:28:53 +00:00
Fix existing code to support webhook_type
This commit is contained in:
@@ -30,7 +30,7 @@ class WebhookEndpointsController < BaseController
|
||||
end
|
||||
|
||||
def webhook_endpoint_params
|
||||
params.require(:webhook_endpoint).permit(:url)
|
||||
params.require(:webhook_endpoint).permit(:url, :webhook_type)
|
||||
end
|
||||
|
||||
def redirect_path
|
||||
|
||||
@@ -6,4 +6,7 @@ class WebhookEndpoint < ApplicationRecord
|
||||
|
||||
validates :url, presence: true
|
||||
validates :webhook_type, presence: true, inclusion: { in: WEBHOOK_TYPES }
|
||||
|
||||
scope :order_cycle_opened, -> { where(webhook_type: "order_cycle_opened") }
|
||||
scope :payment_status, -> { where(webhook_type: "payment_status_changed") }
|
||||
end
|
||||
|
||||
@@ -11,10 +11,12 @@ module OrderCycles
|
||||
.merge(coordinator_name: order_cycle.coordinator.name)
|
||||
|
||||
# Endpoints for coordinator owner
|
||||
webhook_endpoints = order_cycle.coordinator.owner.webhook_endpoints
|
||||
webhook_endpoints = order_cycle.coordinator.owner.webhook_endpoints.order_cycle_opened
|
||||
|
||||
# Plus unique endpoints for distributor owners (ignore duplicates)
|
||||
webhook_endpoints |= order_cycle.distributors.map(&:owner).flat_map(&:webhook_endpoints)
|
||||
webhook_endpoints |= order_cycle.distributors.map(&:owner).flat_map { |owner|
|
||||
owner.webhook_endpoints.order_cycle_opened
|
||||
}
|
||||
|
||||
webhook_endpoints.each do |endpoint|
|
||||
WebhookDeliveryJob.perform_later(endpoint.url, event, webhook_payload, at:)
|
||||
|
||||
@@ -10,8 +10,9 @@
|
||||
%th= t('.url.header')
|
||||
%th.actions
|
||||
%tbody
|
||||
-# TODO handle multiple endpoint and type and tooltip
|
||||
-# Existing endpoints
|
||||
- @user.webhook_endpoints.each do |webhook_endpoint|
|
||||
- @user.webhook_endpoints.order_cycle_opened.each do |webhook_endpoint|
|
||||
%tr
|
||||
%td= t('.event_types.order_cycle_opened') # For now, we only support one type.
|
||||
%td= webhook_endpoint.url
|
||||
@@ -23,11 +24,12 @@
|
||||
= I18n.t(:delete)
|
||||
|
||||
-# Create new
|
||||
- if @user.webhook_endpoints.empty? # Only one allowed for now.
|
||||
- if @user.webhook_endpoints.order_cycle_opened.empty? # Only one allowed for now.
|
||||
%tr
|
||||
%td= t('.event_types.order_cycle_opened') # For now, we only support one type.
|
||||
%td
|
||||
= form_for(@user.webhook_endpoints.build, url: account_webhook_endpoints_path, id: 'new_webhook_endpoint') do |f|
|
||||
= f.url_field :url, placeholder: t('.url.create_placeholder'), required: true, size: 64
|
||||
= f.hidden_field :webhook_type, value: "order_cycle_opened"
|
||||
%td.actions
|
||||
= button_tag t(:create), class: 'button primary tiny no-margin', form: 'new_webhook_endpoint'
|
||||
|
||||
@@ -22,7 +22,8 @@ RSpec.describe OrderCycles::WebhookService do
|
||||
# The co-ordinating enterprise has a non-owner user with an endpoint.
|
||||
# They shouldn't receive a notification.
|
||||
coordinator_user = create(:user, enterprises: [coordinator])
|
||||
coordinator_user.webhook_endpoints.create!(url: "http://coordinator_user_url")
|
||||
coordinator_user.webhook_endpoints.create!(url: "http://coordinator_user_url",
|
||||
webhook_type: "order_cycle_opened")
|
||||
|
||||
expect{ subject }
|
||||
.not_to enqueue_job(WebhookDeliveryJob).with("http://coordinator_user_url", any_args)
|
||||
@@ -30,7 +31,8 @@ RSpec.describe OrderCycles::WebhookService do
|
||||
|
||||
context "coordinator owner has endpoint configured" do
|
||||
before do
|
||||
coordinator.owner.webhook_endpoints.create! url: "http://coordinator_owner_url"
|
||||
coordinator.owner.webhook_endpoints.create! url: "http://coordinator_owner_url",
|
||||
webhook_type: "order_cycle_opened"
|
||||
end
|
||||
|
||||
it "creates webhook payload for order cycle coordinator" do
|
||||
@@ -77,7 +79,8 @@ RSpec.describe OrderCycles::WebhookService do
|
||||
let(:two_distributors) {
|
||||
(1..2).map do |i|
|
||||
user = create(:user)
|
||||
user.webhook_endpoints.create!(url: "http://distributor#{i}_owner_url")
|
||||
user.webhook_endpoints.create!(url: "http://distributor#{i}_owner_url",
|
||||
webhook_type: "order_cycle_opened")
|
||||
create(:distributor_enterprise, owner: user)
|
||||
end
|
||||
}
|
||||
@@ -109,7 +112,8 @@ RSpec.describe OrderCycles::WebhookService do
|
||||
}
|
||||
|
||||
it "creates only one webhook payload for the user's endpoint" do
|
||||
user.webhook_endpoints.create! url: "http://coordinator_owner_url"
|
||||
user.webhook_endpoints.create! url: "http://coordinator_owner_url",
|
||||
webhook_type: "order_cycle_opened"
|
||||
|
||||
expect{ subject }
|
||||
.to enqueue_job(WebhookDeliveryJob).with("http://coordinator_owner_url", any_args)
|
||||
@@ -128,7 +132,8 @@ RSpec.describe OrderCycles::WebhookService do
|
||||
}
|
||||
let(:supplier) {
|
||||
user = create(:user)
|
||||
user.webhook_endpoints.create!(url: "http://supplier_owner_url")
|
||||
user.webhook_endpoints.create!(url: "http://supplier_owner_url",
|
||||
webhook_type: "order_cycle_opened")
|
||||
create(:supplier_enterprise, owner: user)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user