diff --git a/app/jobs/amend_backorder_job.rb b/app/jobs/amend_backorder_job.rb index b4d2bf0506e..883bed53284 100644 --- a/app/jobs/amend_backorder_job.rb +++ b/app/jobs/amend_backorder_job.rb @@ -23,8 +23,16 @@ def perform(order) def amend_backorder(order) backorder = BackorderUpdater.new.amend_backorder(order) - user = order.distributor.owner - urls = nil # Not needed to send order. The backorder id is the URL. - FdcBackorderer.new(user, urls).send_order(backorder) if backorder + if backorder + user = order.distributor.owner + urls = nil # Not needed to send order. The backorder id is the URL. + FdcBackorderer.new(user, urls).send_order(backorder) + elsif !order.order_cycle.closed? + + # We don't have an order to amend but the order cycle is or will open. + # We can assume that this job was triggered by an admin creating a new + # order or adding backorderable items to an order. + BackorderJob.new.place_backorder(order) + end end end diff --git a/app/services/backorder_updater.rb b/app/services/backorder_updater.rb index bb163c8be0a..e0a4ec8febf 100644 --- a/app/services/backorder_updater.rb +++ b/app/services/backorder_updater.rb @@ -28,7 +28,7 @@ def amend_backorder(order) backorder = orderer.find_open_order(order) - update(backorder, user, distributor, order_cycle) + update(backorder, user, distributor, order_cycle) if backorder end # Update a given backorder according to a distributor's order cycle. diff --git a/spec/jobs/amend_backorder_job_spec.rb b/spec/jobs/amend_backorder_job_spec.rb index bde82b0741b..3216b109da5 100644 --- a/spec/jobs/amend_backorder_job_spec.rb +++ b/spec/jobs/amend_backorder_job_spec.rb @@ -130,5 +130,32 @@ .to change { backorder.lines.count }.from(2).to(1) .and change { beans.reload.on_hand }.by(-12) end + + it "creates a new order" do + stub_request(:get, catalog_url).to_return(body: catalog_json) + + # Record the placed backorder: + backorder = nil + allow_any_instance_of(FdcBackorderer).to receive(:find_order) do |*_args| + backorder + end + allow_any_instance_of(FdcBackorderer).to receive(:send_order) do |*args| + backorder = args[1] + end + + # Call amending before a backorder has been placed. + expect { subject.amend_backorder(order) } + .to change { backorder.present? } + .to(true) + + # We ordered a case of 12 cans: -3 + 12 = 9 + expect(beans.on_hand).to eq 9 + + # Stock controlled items don't change stock in backorder: + expect(chia_seed.on_hand).to eq 7 + + expect(backorder.lines[0].quantity).to eq 1 # beans + expect(backorder.lines[1].quantity).to eq 5 # chia + end end end diff --git a/spec/services/backorder_updater_spec.rb b/spec/services/backorder_updater_spec.rb index 48aa125fdc2..3dace910760 100644 --- a/spec/services/backorder_updater_spec.rb +++ b/spec/services/backorder_updater_spec.rb @@ -107,6 +107,13 @@ .to change { backorder.lines.count }.from(2).to(1) .and change { beans.reload.on_hand }.by(-12) end + + it "skips updating if there's is no backorder" do + allow_any_instance_of(FdcBackorderer).to receive(:find_open_order) + .and_return(nil) + + expect { subject.amend_backorder(order) }.not_to raise_error + end end describe "#distributed_linked_variants" do