From fee0f90a1b90251c7a8b6f969c35ed2eef3bb66f Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 24 Mar 2016 15:48:35 +1100 Subject: [PATCH] After adding products to cart, return status of cart and available stock levels --- .../spree/orders_controller_decorator.rb | 18 ++++++++-- .../spree/orders_controller_spec.rb | 34 +++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index c6d325d0f8..4c2ab8cb63 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -30,19 +30,33 @@ Spree::OrdersController.class_eval do Spree::Adjustment.without_callbacks do populator = Spree::OrderPopulator.new(current_order(true), current_currency) + if populator.populate(params.slice(:products, :variants, :quantity), true) fire_event('spree.cart.add') fire_event('spree.order.contents_changed') current_order.update! - render json: true, status: 200 + render json: {error: false, stock_levels: stock_levels}, status: 200 + else - render json: false, status: 402 + render json: {error: true}, status: 412 end end end + def stock_levels + Hash[ + current_order.line_items.map do |li| + [li.variant.id, + {quantity: li.quantity, + max_quantity: li.max_quantity, + on_hand: li.variant.on_hand}] + end + ] + end + + def update_distribution @order = current_order(true) diff --git a/spec/controllers/spree/orders_controller_spec.rb b/spec/controllers/spree/orders_controller_spec.rb index 73c72f3386..756be1d3ec 100644 --- a/spec/controllers/spree/orders_controller_spec.rb +++ b/spec/controllers/spree/orders_controller_spec.rb @@ -42,6 +42,36 @@ describe Spree::OrdersController do flash[:info].should == "The hub you have selected is temporarily closed for orders. Please try again later." end + describe "returning stock levels in JSON on success" do + let(:product) { create(:simple_product) } + + it "returns stock levels as JSON" do + controller.stub(:stock_levels) { 'my_stock_levels' } + Spree::OrderPopulator.stub(:new).and_return(populator = double()) + populator.stub(:populate) { true } + + xhr :post, :populate, use_route: :spree, format: :json + + data = JSON.parse(response.body) + data['stock_levels'].should == 'my_stock_levels' + end + + describe "generating stock levels" do + let!(:order) { create(:order) } + let!(:li) { create(:line_item, order: order, variant: v, quantity: 2, max_quantity: 3) } + let!(:v) { create(:variant, count_on_hand: 4) } + + before do + order.reload + controller.stub(:current_order) { order } + end + + it "returns a hash with variant id, quantity, max_quantity and stock on hand" do + controller.stock_levels.should == {v.id => {quantity: 2, max_quantity: 3, on_hand: 4}} + end + end + end + context "adding a group buy product to the cart" do it "sets a variant attribute for the max quantity" do distributor_product = create(:distributor_enterprise) @@ -68,7 +98,7 @@ describe Spree::OrdersController do Spree::OrderPopulator.stub(:new).and_return(populator = double()) populator.stub(:populate).and_return false xhr :post, :populate, use_route: :spree, format: :json - response.status.should == 402 + response.status.should == 412 end it "tells populator to overwrite" do @@ -78,7 +108,7 @@ describe Spree::OrdersController do end end - context "removing line items from cart" do + describe "removing line items from cart" do describe "when I pass params that includes a line item no longer in our cart" do it "should silently ignore the missing line item" do order = subject.current_order(true)