Merge pull request #13207 from mkllnk/dfc-missing-product

Gracefully handle product missing from catalog
This commit is contained in:
Gaetan Craig-Riou
2025-03-24 10:12:48 +11:00
committed by GitHub
2 changed files with 41 additions and 4 deletions

View File

@@ -55,12 +55,11 @@ 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
end
adjust_stock(items, ordered_quantities)
end
# We look at linked variants which are either stock controlled or
@@ -80,6 +79,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.
@@ -135,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

View File

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