Replace StandingOrderSyncJob with ProxyOrderSyncer service object

This commit is contained in:
Rob Harrington
2017-11-15 14:57:03 +11:00
parent 6ea343f26e
commit d6a9d63de5
6 changed files with 31 additions and 77 deletions

View File

@@ -1,5 +1,6 @@
require 'open_food_network/permissions'
require 'open_food_network/order_cycle_form_applicator'
require 'open_food_network/proxy_order_syncer'
module Admin
class OrderCyclesController < ResourceController
@@ -208,9 +209,10 @@ module Admin
removed_ids = @existing_schedule_ids - @order_cycle.schedule_ids
new_ids = @order_cycle.schedule_ids - @existing_schedule_ids
if removed_ids.any? || new_ids.any?
Schedule.where(id: removed_ids + new_ids).each do |schedule|
Delayed::Job.enqueue StandingOrderSyncJob.new(schedule)
end
schedules = Schedule.where(id: removed_ids + new_ids)
standing_orders = StandingOrder.where(schedule_id: schedules)
syncer = OpenFoodNetwork::ProxyOrderSyncer.new(standing_orders)
syncer.sync!
end
end

View File

@@ -1,4 +1,5 @@
require 'open_food_network/permissions'
require 'open_food_network/proxy_order_syncer'
module Admin
class SchedulesController < ResourceController
@@ -58,7 +59,9 @@ module Admin
removed_ids = @existing_order_cycle_ids - @schedule.order_cycle_ids
new_ids = @schedule.order_cycle_ids - @existing_order_cycle_ids
if removed_ids.any? || new_ids.any?
Delayed::Job.enqueue StandingOrderSyncJob.new(@schedule)
standing_orders = StandingOrder.where(schedule_id: @schedule)
syncer = OpenFoodNetwork::ProxyOrderSyncer.new(standing_orders)
syncer.sync!
end
end
end

View File

@@ -1,23 +0,0 @@
require 'open_food_network/proxy_order_syncer'
class StandingOrderSyncJob
attr_accessor :schedule
def initialize(schedule)
@schedule = schedule
end
def perform
proxy_order_syncer.sync!
end
private
def standing_orders
StandingOrder.where(schedule_id: schedule)
end
def proxy_order_syncer
OpenFoodNetwork::ProxyOrderSyncer.new(standing_orders)
end
end

View File

@@ -189,16 +189,14 @@ module Admin
expect(coordinated_order_cycle.reload.schedules).to_not include coordinated_schedule, uncoordinated_schedule
end
it "enqueues a StandingOrderSyncJob when schedule_ids change" do
expect do
spree_put :update, format: :json, id: coordinated_order_cycle.id, order_cycle: { schedule_ids: [coordinated_schedule.id, coordinated_schedule2.id] }
end.to enqueue_job StandingOrderSyncJob
expect do
spree_put :update, format: :json, id: coordinated_order_cycle.id, order_cycle: { schedule_ids: [coordinated_schedule.id] }
end.to enqueue_job StandingOrderSyncJob
expect do
spree_put :update, format: :json, id: coordinated_order_cycle.id, order_cycle: { schedule_ids: [coordinated_schedule.id] }
end.to_not enqueue_job StandingOrderSyncJob
it "syncs proxy orders when schedule_ids change" do
syncer_mock = double(:syncer)
allow(OpenFoodNetwork::ProxyOrderSyncer).to receive(:new) { syncer_mock }
expect(syncer_mock).to receive(:sync!).exactly(2).times
spree_put :update, format: :json, id: coordinated_order_cycle.id, order_cycle: { schedule_ids: [coordinated_schedule.id, coordinated_schedule2.id] }
spree_put :update, format: :json, id: coordinated_order_cycle.id, order_cycle: { schedule_ids: [coordinated_schedule.id] }
spree_put :update, format: :json, id: coordinated_order_cycle.id, order_cycle: { schedule_ids: [coordinated_schedule.id] }
end
end
end

View File

@@ -89,16 +89,14 @@ describe Admin::SchedulesController, type: :controller do
expect(coordinated_schedule.reload.order_cycles).to_not include coordinated_order_cycle, uncoordinated_order_cycle2
end
it "enqueues a StandingOrderSyncJob when order_cycle_ids change" do
expect do
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: [coordinated_order_cycle.id, coordinated_order_cycle2.id] }
end.to enqueue_job StandingOrderSyncJob
expect do
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: [coordinated_order_cycle.id,] }
end.to enqueue_job StandingOrderSyncJob
expect do
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: [coordinated_order_cycle.id] }
end.to_not enqueue_job StandingOrderSyncJob
it "syncs proxy orders when order_cycle_ids change" do
syncer_mock = double(:syncer)
allow(OpenFoodNetwork::ProxyOrderSyncer).to receive(:new) { syncer_mock }
expect(syncer_mock).to receive(:sync!).exactly(2).times
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: [coordinated_order_cycle.id, coordinated_order_cycle2.id] }
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: [coordinated_order_cycle.id] }
spree_put :update, format: :json, id: coordinated_schedule.id, schedule: { order_cycle_ids: [coordinated_order_cycle.id] }
end
end
@@ -151,8 +149,12 @@ describe Admin::SchedulesController, type: :controller do
expect(schedule.order_cycles).to_not include uncoordinated_order_cycle
end
it "enqueues StandingOrderSyncJob" do
expect { create_schedule params }.to enqueue_job StandingOrderSyncJob
it "sync proxy orders" do
syncer_mock = double(:syncer)
allow(OpenFoodNetwork::ProxyOrderSyncer).to receive(:new) { syncer_mock }
expect(syncer_mock).to receive(:sync!).once
create_schedule params
end
end

View File

@@ -1,28 +0,0 @@
require 'spec_helper'
describe StandingOrderSyncJob do
let(:shop) { create(:distributor_enterprise) }
let(:order_cycle1) { create(:simple_order_cycle, coordinator: shop) }
let(:order_cycle2) { create(:simple_order_cycle, coordinator: shop) }
let(:schedule1) { create(:schedule, order_cycles: [order_cycle1, order_cycle2]) }
let(:job) { StandingOrderSyncJob.new(schedule1) }
describe "performing the job" do
let(:schedule) { double(:schedule) }
let(:standing_order) { double(:standing_order) }
let(:syncer) { double(:syncer) }
let!(:job) { StandingOrderSyncJob.new(schedule) }
before do
allow(job).to receive(:standing_orders) { [standing_order] }
allow(OpenFoodNetwork::ProxyOrderSyncer).to receive(:new) { syncer }
allow(syncer).to receive(:sync!)
end
it "calls sync!" do
job.perform
expect(syncer).to have_received(:sync!).once
end
end
end