mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-17 04:34:24 +00:00
Merge pull request #13065 from mkllnk/dfc-amend-nothing
Admin updates can trigger new backorders until the order cycle is closed
This commit is contained in:
@@ -130,5 +130,32 @@ RSpec.describe AmendBackorderJob do
|
||||
.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
|
||||
|
||||
@@ -4,6 +4,7 @@ require 'spec_helper'
|
||||
|
||||
RSpec.describe BackorderUpdater do
|
||||
let(:order) { create(:completed_order_with_totals) }
|
||||
let(:order_cycle) { order.order_cycle }
|
||||
let(:distributor) { order.distributor }
|
||||
let(:beans) { beans_item.variant }
|
||||
let(:beans_item) { order.line_items[0] }
|
||||
@@ -107,11 +108,51 @@ RSpec.describe BackorderUpdater do
|
||||
.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 "#update_order_lines" do
|
||||
it "skips unavailable items" 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
|
||||
|
||||
BackorderJob.new.place_backorder(order)
|
||||
|
||||
# Now one of the products becomes unavailable in the catalog.
|
||||
# I simulate that by changing the link so something unknown.
|
||||
beans.semantic_links[0].update!(semantic_id: "https://example.net/unknown")
|
||||
|
||||
variants = [beans, chia_seed]
|
||||
reference_link = chia_seed.semantic_links[0].semantic_id
|
||||
urls = FdcUrlBuilder.new(reference_link)
|
||||
catalog = DfcCatalog.load(user, urls.catalog_url)
|
||||
orderer = FdcBackorderer.new(user, urls)
|
||||
broker = FdcOfferBroker.new(catalog)
|
||||
updated_lines = subject.update_order_lines(
|
||||
backorder, order_cycle, variants, broker, orderer
|
||||
)
|
||||
|
||||
expect(updated_lines.count).to eq 1
|
||||
expect(updated_lines[0].offer.offeredItem.semanticId)
|
||||
.to eq chia_seed.semantic_links[0].semantic_id
|
||||
end
|
||||
end
|
||||
|
||||
describe "#distributed_linked_variants" do
|
||||
let(:order_cycle) { order.order_cycle }
|
||||
|
||||
it "selects available variants with semantic links" do
|
||||
variants = subject.distributed_linked_variants(order_cycle, distributor)
|
||||
expect(variants).to match_array [beans, chia_seed]
|
||||
|
||||
23
spec/system/consumer/checkout/backorder_spec.rb
Normal file
23
spec/system/consumer/checkout/backorder_spec.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "system_helper"
|
||||
|
||||
RSpec.describe "Checkout" do
|
||||
include ShopWorkflow
|
||||
include CheckoutHelper
|
||||
|
||||
let(:variant) { order.variants.first }
|
||||
let(:order) { create(:order_ready_for_confirmation) }
|
||||
|
||||
before do
|
||||
variant.semantic_links << SemanticLink.new(semantic_id: "https://product")
|
||||
set_order order
|
||||
login_as create(:user)
|
||||
end
|
||||
|
||||
it "triggers a backorder" do
|
||||
visit checkout_step_path(:summary)
|
||||
|
||||
expect { place_order }.to enqueue_job BackorderJob
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user