mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Also send webhook payloads for distributor owners
But not supplier owners.
This commit is contained in:
@@ -11,6 +11,9 @@ class OrderCycleWebhookService
|
||||
# Endpoints for coordinator owner
|
||||
webhook_endpoints = order_cycle.coordinator.owner.webhook_endpoints
|
||||
|
||||
# Plus unique endpoints for distributor owners (ignore duplicates)
|
||||
webhook_endpoints |= order_cycle.distributors.map(&:owner).flat_map(&:webhook_endpoints)
|
||||
|
||||
webhook_endpoints.each do |endpoint|
|
||||
WebhookDeliveryJob.perform_later(endpoint.url, event, webhook_payload)
|
||||
end
|
||||
|
||||
@@ -61,9 +61,77 @@ describe OrderCycleWebhookService do
|
||||
end
|
||||
end
|
||||
|
||||
pending "creates webhook payload for order cycle distributor"
|
||||
describe "distributors" do
|
||||
context "multiple distributors have owners with endpoint configured" do
|
||||
let(:order_cycle) {
|
||||
create(
|
||||
:simple_order_cycle,
|
||||
coordinator: coordinator,
|
||||
distributors: two_distributors,
|
||||
)
|
||||
}
|
||||
let(:two_distributors) {
|
||||
(1..2).map do |i|
|
||||
user = create(:user)
|
||||
user.webhook_endpoints.create!(url: "http://distributor#{i}_owner_url")
|
||||
create(:distributor_enterprise, owner: user)
|
||||
end
|
||||
}
|
||||
|
||||
pending "doesn't create webhook payload for order cycle supplier"
|
||||
it "creates webhook payload for each order cycle distributor" do
|
||||
data = {
|
||||
coordinator_id: order_cycle.coordinator_id,
|
||||
coordinator_name: "Starship Enterprise",
|
||||
}
|
||||
|
||||
expect{ OrderCycleWebhookService.create_webhook_job(order_cycle, "order_cycle.opened") }
|
||||
.to enqueue_job(WebhookDeliveryJob).with("http://distributor1_owner_url",
|
||||
"order_cycle.opened", hash_including(data))
|
||||
.and enqueue_job(WebhookDeliveryJob).with("http://distributor2_owner_url",
|
||||
"order_cycle.opened", hash_including(data))
|
||||
end
|
||||
end
|
||||
|
||||
context "distributor owner is same user as coordinator owner" do
|
||||
let(:user) { coordinator.owner }
|
||||
let(:order_cycle) {
|
||||
create(
|
||||
:simple_order_cycle,
|
||||
coordinator: coordinator,
|
||||
distributors: [create(:distributor_enterprise, owner: user)],
|
||||
)
|
||||
}
|
||||
|
||||
it "creates only one webhook payload for the user's endpoint" do
|
||||
user.webhook_endpoints.create! url: "http://coordinator_owner_url"
|
||||
|
||||
expect{ OrderCycleWebhookService.create_webhook_job(order_cycle, "order_cycle.opened") }
|
||||
.to enqueue_job(WebhookDeliveryJob).with("http://coordinator_owner_url", any_args)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "suppliers" do
|
||||
context "supplier has owner with endpoint configured" do
|
||||
let(:order_cycle) {
|
||||
create(
|
||||
:simple_order_cycle,
|
||||
coordinator: coordinator,
|
||||
suppliers: [supplier],
|
||||
)
|
||||
}
|
||||
let(:supplier) {
|
||||
user = create(:user)
|
||||
user.webhook_endpoints.create!(url: "http://supplier_owner_url")
|
||||
create(:supplier_enterprise, owner: user)
|
||||
}
|
||||
|
||||
it "doesn't create a webhook payload for supplier owner" do
|
||||
expect{ OrderCycleWebhookService.create_webhook_job(order_cycle, "order_cycle.opened") }
|
||||
.to_not enqueue_job(WebhookDeliveryJob).with("http://supplier_owner_url", any_args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "without webhook subscribed to enterprise" do
|
||||
|
||||
Reference in New Issue
Block a user