-
-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3. Add job to create webhook payloads for an order cycle event
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
# Create a webhook payload for an order cycle, triggered by an event. | ||
# The payload will be delivered by another job. | ||
class OrderCycleWebhookJob < ApplicationJob | ||
def perform(order_cycle_id, event) | ||
order_cycle = OrderCycle.find(order_cycle_id) | ||
data = order_cycle | ||
.slice(:id, :name, :orders_open_at, :orders_close_at, :coordinator_id) | ||
.merge(coordinator_name: order_cycle.coordinator.name) | ||
|
||
order_cycle.coordinator.owner.webhook_endpoints.each do |endpoint| | ||
WebhookDeliveryJob.perform_later(endpoint.url, event, data) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe OrderCycleWebhookJob do | ||
let(:order_cycle) { | ||
create( | ||
:order_cycle, | ||
name: "Order cycle 1", | ||
orders_open_at: "2022-09-19 09:00:00".to_time, | ||
orders_close_at: "2022-09-19 17:00:00".to_time, | ||
) | ||
} | ||
|
||
context "enterprise owner has a webhook endpoint" do | ||
before do | ||
order_cycle.coordinator.update! name: "Starship Enterprise" | ||
order_cycle.coordinator.owner.webhook_endpoints.create! url: "http://url" | ||
end | ||
|
||
it "creates webhook payload for specified order cycle only" do | ||
order_cycle.dup.save # Another OC should not be called | ||
|
||
data = { | ||
id: order_cycle.id, | ||
name: "Order cycle 1", | ||
orders_open_at: "2022-09-19 09:00:00".to_time, | ||
orders_close_at: "2022-09-19 17:00:00".to_time, | ||
coordinator_id: order_cycle.coordinator_id, | ||
coordinator_name: "Starship Enterprise", | ||
} | ||
|
||
expect{ OrderCycleWebhookJob.perform_now(order_cycle.id, "order_cycle.opened") } | ||
.to enqueue_job(WebhookDeliveryJob) | ||
.with("http://url", "order_cycle.opened", hash_including(data)) | ||
end | ||
|
||
it "creates webhook payload when no open or close times specified" do | ||
order_cycle.update!(orders_open_at: nil, orders_close_at: nil) | ||
data = { | ||
orders_open_at: nil, | ||
orders_close_at: nil, | ||
} | ||
|
||
expect{ OrderCycleWebhookJob.perform_now(order_cycle.id, "order_cycle.opened") } | ||
.to enqueue_job(WebhookDeliveryJob) | ||
.with("http://url", "order_cycle.opened", hash_including(data)) | ||
end | ||
end | ||
|
||
context "without webhook subscribed to enterprise" do | ||
it "doesn't create webhook payload" do | ||
expect{ OrderCycleWebhookJob.perform_now(order_cycle.id, "order_cycle.opened") } | ||
.not_to enqueue_job(WebhookDeliveryJob) | ||
end | ||
end | ||
|
||
pending "doesn't create duplicate webhook jobs" | ||
end |