mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
Add knowledge of variant overrides to OrderFactory
This commit is contained in:
@@ -25,6 +25,10 @@ class OrderFactory
|
||||
@customer ||= Customer.find(attrs[:customer_id])
|
||||
end
|
||||
|
||||
def shop
|
||||
@shop ||= Enterprise.find(attrs[:distributor_id])
|
||||
end
|
||||
|
||||
def create_order
|
||||
@order = Spree::Order.create!(create_attrs)
|
||||
end
|
||||
@@ -38,7 +42,8 @@ class OrderFactory
|
||||
def build_line_items
|
||||
attrs[:line_items].each do |li|
|
||||
next unless variant = Spree::Variant.find_by_id(li[:variant_id])
|
||||
li[:quantity] = stock_limited_quantity(variant.on_hand, li[:quantity])
|
||||
scoper.scope(variant)
|
||||
li[:quantity] = stock_limited_quantity(variant.count_on_hand, li[:quantity])
|
||||
build_item_from(li)
|
||||
end
|
||||
end
|
||||
@@ -68,4 +73,8 @@ class OrderFactory
|
||||
return requested if opts[:skip_stock_check]
|
||||
[stock, requested].min
|
||||
end
|
||||
|
||||
def scoper
|
||||
@scoper ||= OpenFoodNetwork::ScopeVariantToHub.new(shop)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -54,26 +54,51 @@ describe OrderFactory do
|
||||
end
|
||||
|
||||
context "when requested quantity is greater than available stock" do
|
||||
before do
|
||||
variant1.update_attribute(:count_on_hand, 2)
|
||||
attrs[:line_items].first[:quantity] = 5
|
||||
end
|
||||
context "when no override is present" do
|
||||
before do
|
||||
variant1.update_attribute(:count_on_hand, 2)
|
||||
attrs[:line_items].first[:quantity] = 5
|
||||
end
|
||||
|
||||
context "when skip_stock_check is not requested" do
|
||||
it "initialised the order but limits stock to the available amount" do
|
||||
expect{ order }.to change{ Spree::Order.count }.by(1)
|
||||
expect(order).to be_a Spree::Order
|
||||
expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 2
|
||||
context "when skip_stock_check is not requested" do
|
||||
it "initialised the order but limits stock to the available amount" do
|
||||
expect{ order }.to change{ Spree::Order.count }.by(1)
|
||||
expect(order).to be_a Spree::Order
|
||||
expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 2
|
||||
end
|
||||
end
|
||||
|
||||
context "when skip_stock_check is requested" do
|
||||
let(:opts) { { skip_stock_check: true } }
|
||||
|
||||
it "initialises the order with the requested quantity regardless" do
|
||||
expect{ order }.to change{ Spree::Order.count }.by(1)
|
||||
expect(order).to be_a Spree::Order
|
||||
expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 5
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when skip_stock_check is requested" do
|
||||
let(:opts) { { skip_stock_check: true } }
|
||||
context "when an override is present" do
|
||||
let!(:override) { create(:variant_override, hub_id: shop.id, variant_id: variant1.id, count_on_hand: 3) }
|
||||
before { attrs[:line_items].first[:quantity] = 6 }
|
||||
|
||||
it "initialises the order with the requested quantity regardless" do
|
||||
expect{ order }.to change{ Spree::Order.count }.by(1)
|
||||
expect(order).to be_a Spree::Order
|
||||
expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 5
|
||||
context "when skip_stock_check is not requested" do
|
||||
it "initialised the order but limits stock to the available amount" do
|
||||
expect{ order }.to change{ Spree::Order.count }.by(1)
|
||||
expect(order).to be_a Spree::Order
|
||||
expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 3
|
||||
end
|
||||
end
|
||||
|
||||
context "when skip_stock_check is requested" do
|
||||
let(:opts) { { skip_stock_check: true } }
|
||||
|
||||
it "initialises the order with the requested quantity regardless" do
|
||||
expect{ order }.to change{ Spree::Order.count }.by(1)
|
||||
expect(order).to be_a Spree::Order
|
||||
expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 6
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user