Skip to content

Commit

Permalink
3. Add job to create webhook payloads for an order cycle event
Browse files Browse the repository at this point in the history
  • Loading branch information
dacook committed Oct 28, 2022
1 parent 570e1be commit 5444c1a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/jobs/order_cycle_webhook_job.rb
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)
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, data)
end
end
end
59 changes: 59 additions & 0 deletions spec/jobs/order_cycle_webhook_job_spec.rb
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) }
.to enqueue_job(WebhookDeliveryJob)
.with("http://url", 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) }
.to enqueue_job(WebhookDeliveryJob)
.with("http://url", 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) }
.not_to enqueue_job(WebhookDeliveryJob)
end
end

pending "doesn't create duplicate webhook jobs"
end

0 comments on commit 5444c1a

Please sign in to comment.