From 9ad6dce2d6413c85b4e0b69ddde91fe59678bb4a Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 10 Nov 2016 15:54:20 +1100 Subject: [PATCH] WIP, SO placement job: processing orders to completion --- app/forms/standing_order_form.rb | 2 +- app/jobs/standing_order_placement_job.rb | 25 +++++++++++++++++-- .../jobs/standing_order_placement_job_spec.rb | 13 ++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/forms/standing_order_form.rb b/app/forms/standing_order_form.rb index 0736e2952a..126cb3d7d9 100644 --- a/app/forms/standing_order_form.rb +++ b/app/forms/standing_order_form.rb @@ -10,7 +10,7 @@ class StandingOrderForm delegate :shipping_method, :shipping_method_id, :payment_method, :payment_method_id, to: :standing_order delegate :shipping_method_id_changed?, :shipping_method_id_was, :payment_method_id_changed?, :payment_method_id_was, to: :standing_order - def initialize(standing_order, params) + def initialize(standing_order, params={}) @standing_order = standing_order @params = params end diff --git a/app/jobs/standing_order_placement_job.rb b/app/jobs/standing_order_placement_job.rb index 73e4a20e75..e7d609fb1c 100644 --- a/app/jobs/standing_order_placement_job.rb +++ b/app/jobs/standing_order_placement_job.rb @@ -6,10 +6,31 @@ class StandingOrderPlacementJob end def perform - orders + orders.each do |order| + process(order) + end end + private + def orders - Spree::Order.incomplete.where(order_cycle_id: order_cycle).joins(:standing_order) + Spree::Order.incomplete.where(order_cycle_id: order_cycle).joins(:standing_order).readonly(false) + end + + def process(order) + while order.state != "complete" + if order.errors.any? + Bugsnag.notify(RuntimeError.new("StandingOrderPlacementError"), { + job: "StandingOrderPlacement", + error: "Cannot process order due to errors", + data: { + errors: order.errors.full_messages + } + }) + break + else + order.next + end + end end end diff --git a/spec/jobs/standing_order_placement_job_spec.rb b/spec/jobs/standing_order_placement_job_spec.rb index 8801793b33..d4a3460b98 100644 --- a/spec/jobs/standing_order_placement_job_spec.rb +++ b/spec/jobs/standing_order_placement_job_spec.rb @@ -28,4 +28,17 @@ describe StandingOrderPlacementJob do expect(orders).to_not include order1, order3, order4 end end + + describe "performing the job" do + before do + form = StandingOrderForm.new(standing_order1) + form.send(:initialise_orders!) + end + + it "processes orders to completion" do + order = standing_order1.orders.first + expect{job.perform}.to change{order.reload.completed_at}.from(nil) + expect(order.completed_at).to be_within(5.seconds).of Time.now + end + end end