From 8a62d26af4728bd709d174a443be143170321b8d Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 24 Mar 2016 16:24:36 +1100 Subject: [PATCH] After adding an item to the cart, when out of stock, remove from cart and reset client-side stock level --- .../darkswarm/services/cart.js.coffee | 11 ++++++++++ .../darkswarm/services/cart_spec.js.coffee | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index f1c9421a15..c9fdd1471b 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -31,12 +31,23 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, storage)-> $http.post('/orders/populate', @data()).success (data, status)=> @saved() @update_running = false + + @compareAndNotifyStockLevels data.stock_levels + @popQueue() if @update_enqueued .error (response, status)=> @scheduleRetry(status) @update_running = false + compareAndNotifyStockLevels: (stockLevels) => + for li in @line_items_present() + if !stockLevels[li.variant.id]? + alert "Variant out of stock: #{li.variant.id}" + li.quantity = 0 + li.max_quantity = 0 if li.max_quantity? + li.variant.count_on_hand = 0 + popQueue: => @update_enqueued = false @scheduleUpdate() diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index 518a524010..41cee8de4b 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -126,6 +126,27 @@ describe 'Cart service', -> $httpBackend.flush() expect(Cart.scheduleRetry).toHaveBeenCalled() + describe "verifying stock levels after update", -> + describe "when an item is out of stock", -> + it "reduces the quantity in the cart", -> + li = {variant: {id: 1}, quantity: 5} + spyOn(Cart, 'line_items_present').andReturn [li] + 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({}) + 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({}) + expect(li.variant.count_on_hand).toEqual 0 + it "pops the queue", -> Cart.update_enqueued = true spyOn(Cart, 'scheduleUpdate')