From 5444c1aac3a1b576361703e405e501cf410a7562 Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 7 Oct 2022 12:22:03 +1100 Subject: [PATCH] 3. Add job to create webhook payloads for an order cycle event --- app/jobs/order_cycle_webhook_job.rb | 16 ++++++ spec/jobs/order_cycle_webhook_job_spec.rb | 59 +++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 app/jobs/order_cycle_webhook_job.rb create mode 100644 spec/jobs/order_cycle_webhook_job_spec.rb diff --git a/app/jobs/order_cycle_webhook_job.rb b/app/jobs/order_cycle_webhook_job.rb new file mode 100644 index 00000000000..67df2a1654c --- /dev/null +++ b/app/jobs/order_cycle_webhook_job.rb @@ -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 diff --git a/spec/jobs/order_cycle_webhook_job_spec.rb b/spec/jobs/order_cycle_webhook_job_spec.rb new file mode 100644 index 00000000000..559ad242732 --- /dev/null +++ b/spec/jobs/order_cycle_webhook_job_spec.rb @@ -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