mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
WIP, SO placement job: processing orders to completion
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user