WIP, SO placement job: processing orders to completion

This commit is contained in:
Rob Harrington
2016-11-10 15:54:20 +11:00
parent 178aadb311
commit 9ad6dce2d6
3 changed files with 37 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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