From 6fbbe580c5bfc35c2455bb8ca19c834628d8efaf Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 24 Mar 2016 16:41:54 +1100 Subject: [PATCH] After adding an item to the cart, when less quantity available, reduce quantity and reset client-side stock level --- .../darkswarm/services/cart.js.coffee | 8 +++++ .../darkswarm/services/cart_spec.js.coffee | 29 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index c9fdd1471b..973170e414 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -47,6 +47,14 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, storage)-> li.quantity = 0 li.max_quantity = 0 if li.max_quantity? li.variant.count_on_hand = 0 + else + if stockLevels[li.variant.id].quantity < li.quantity + alert "Variant quantity reduced: #{li.variant.id}" + li.quantity = stockLevels[li.variant.id].quantity + if stockLevels[li.variant.id].max_quantity < li.max_quantity + alert "Variant max_quantity reduced: #{li.variant.id}" + li.max_quantity = stockLevels[li.variant.id].max_quantity + li.variant.count_on_hand = stockLevels[li.variant.id].on_hand popQueue: => @update_enqueued = false diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index 41cee8de4b..58b7795124 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -131,22 +131,45 @@ describe 'Cart service', -> it "reduces the quantity in the cart", -> li = {variant: {id: 1}, quantity: 5} spyOn(Cart, 'line_items_present').andReturn [li] - Cart.compareAndNotifyStockLevels({}) + Cart.compareAndNotifyStockLevels {} expect(li.quantity).toEqual 0 expect(li.max_quantity).toBeUndefined() it "reduces the max_quantity in the cart", -> li = {variant: {id: 1}, quantity: 5, max_quantity: 6} spyOn(Cart, 'line_items_present').andReturn [li] - Cart.compareAndNotifyStockLevels({}) + Cart.compareAndNotifyStockLevels {} expect(li.max_quantity).toEqual 0 it "resets the count on hand available", -> li = {variant: {id: 1, count_on_hand: 10}, quantity: 5} spyOn(Cart, 'line_items_present').andReturn [li] - Cart.compareAndNotifyStockLevels({}) + Cart.compareAndNotifyStockLevels {} expect(li.variant.count_on_hand).toEqual 0 + describe "when the quantity available is less than that requested", -> + it "reduces the quantity in the cart", -> + li = {variant: {id: 1}, quantity: 6} + stockLevels = {1: {quantity: 5, on_hand: 5}} + spyOn(Cart, 'line_items_present').andReturn [li] + Cart.compareAndNotifyStockLevels stockLevels + expect(li.quantity).toEqual 5 + expect(li.max_quantity).toBeUndefined() + + it "reduces the max_quantity in the cart", -> + li = {variant: {id: 1}, quantity: 6, max_quantity: 7} + stockLevels = {1: {quantity: 5, max_quantity: 5, on_hand: 5}} + spyOn(Cart, 'line_items_present').andReturn [li] + Cart.compareAndNotifyStockLevels stockLevels + expect(li.max_quantity).toEqual 5 + + it "resets the count on hand available", -> + li = {variant: {id: 1}, quantity: 6} + stockLevels = {1: {quantity: 5, on_hand: 6}} + spyOn(Cart, 'line_items_present').andReturn [li] + Cart.compareAndNotifyStockLevels stockLevels + expect(li.variant.count_on_hand).toEqual 6 + it "pops the queue", -> Cart.update_enqueued = true spyOn(Cart, 'scheduleUpdate')