From 993f02a989111e2d60c37347c1c40353cf511fa8 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 21 Feb 2018 11:52:26 +1100 Subject: [PATCH] Add knowledge of variant overrides to OrderFactory --- app/services/order_factory.rb | 11 +++++- spec/services/order_factory_spec.rb | 55 +++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/app/services/order_factory.rb b/app/services/order_factory.rb index df5ce3e50d..9c656c97e4 100644 --- a/app/services/order_factory.rb +++ b/app/services/order_factory.rb @@ -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 diff --git a/spec/services/order_factory_spec.rb b/spec/services/order_factory_spec.rb index c8758c9f0c..3231545ce5 100644 --- a/spec/services/order_factory_spec.rb +++ b/spec/services/order_factory_spec.rb @@ -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