From 2f5667a294dc62e419f92b3b357b457fc97b1c2e Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 12 Mar 2025 12:56:08 +1100 Subject: [PATCH 1/2] Gracefully handle product missing from catalog --- app/jobs/backorder_job.rb | 10 +++++++++- spec/jobs/backorder_job_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/jobs/backorder_job.rb b/app/jobs/backorder_job.rb index a0a3c168e5..1db8d1d6ad 100644 --- a/app/jobs/backorder_job.rb +++ b/app/jobs/backorder_job.rb @@ -55,11 +55,16 @@ class BackorderJob < ApplicationJob ordered_quantities[item] = retail_quantity end + return if backorder.lines.empty? + place_order(user, order, orderer, backorder) items.each do |item| variant = item.variant - variant.on_hand += ordered_quantities[item] if variant.on_demand + quantity = ordered_quantities[item] + next if quantity.zero? + + variant.on_hand += quantity if variant.on_demand end end @@ -80,6 +85,9 @@ class BackorderJob < ApplicationJob needed_quantity = needed_quantity(line_item) solution = broker.best_offer(variant.semantic_links[0].semantic_id) + # If this product was removed from the catalog, we can't order it. + return 0 unless solution.offer + # The number of wholesale packs we need to order to fulfill the # needed quantity. # For example, we order 2 packs of 12 cans if we need 15 cans. diff --git a/spec/jobs/backorder_job_spec.rb b/spec/jobs/backorder_job_spec.rb index 81328e6cf1..93425ebec3 100644 --- a/spec/jobs/backorder_job_spec.rb +++ b/spec/jobs/backorder_job_spec.rb @@ -8,6 +8,10 @@ RSpec.describe BackorderJob do let(:chia_seed) { chia_item.variant } let(:chia_item) { order.line_items.second } let(:user) { order.distributor.owner } + let(:catalog_json) { file_fixture("fdc-catalog.json").read } + let(:catalog_url) { + "https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts" + } let(:product_link) { "https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts/44519466467635" } @@ -105,6 +109,27 @@ RSpec.describe BackorderJob do perform_enqueued_jobs(only: CompleteBackorderJob) end + it "skips unavailable items" do + order.order_cycle = create( + :simple_order_cycle, + distributors: [order.distributor], + variants: [beans], + ) + beans.on_demand = true + beans.on_hand = -3 + beans.semantic_links << SemanticLink.new( + semantic_id: "#{product_link}-removed" + ) + + stub_request(:get, catalog_url).to_return(body: catalog_json) + + expect { + subject.place_backorder(order) + }.not_to change { beans.on_hand } + + # And no error was raised, no order was placed. + end + it "succeeds when no work to be done" do # The database can change before the job is run. So maybe there's nothing # to do. From a082a95ea197169c1cce17860b04a454bded8293 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 12 Mar 2025 13:03:35 +1100 Subject: [PATCH 2/2] Split method into simpler pieces --- app/jobs/backorder_job.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/jobs/backorder_job.rb b/app/jobs/backorder_job.rb index 1db8d1d6ad..64c9cd8e62 100644 --- a/app/jobs/backorder_job.rb +++ b/app/jobs/backorder_job.rb @@ -59,13 +59,7 @@ class BackorderJob < ApplicationJob place_order(user, order, orderer, backorder) - items.each do |item| - variant = item.variant - quantity = ordered_quantities[item] - next if quantity.zero? - - variant.on_hand += quantity if variant.on_demand - end + adjust_stock(items, ordered_quantities) end # We look at linked variants which are either stock controlled or @@ -143,4 +137,14 @@ class BackorderJob < ApplicationJob order.exchange.semantic_links.create!(semantic_id: placed_order.semanticId) end + + def adjust_stock(items, ordered_quantities) + items.each do |item| + variant = item.variant + quantity = ordered_quantities[item] + next if quantity.zero? + + variant.on_hand += quantity if variant.on_demand + end + end end