After adding products to cart, return status of cart and available stock levels

This commit is contained in:
Rohan Mitchell
2016-03-24 15:48:35 +11:00
parent 292d027498
commit fee0f90a1b
2 changed files with 48 additions and 4 deletions

View File

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

View File

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