Skip to content

Commit

Permalink
Fix for decrement cart function (#1273)
Browse files Browse the repository at this point in the history
* Add failing test

* Fix for update Mongo command

* Add failing test for "decrease below zero"

* If removeQuantity is more than quantity remove the entire line

* updated linting error

* linting error fix

* listing issues

* more cleanup and linting tweaks

* fix eslint config for object key quotes

* fix tests after error message update

* revert quote-props linter change

* revert lint changes

* Simplify logic per CR comment
  • Loading branch information
brent-hoover authored and jshimko committed Aug 10, 2016
1 parent 29ed27a commit 5590012
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 36 deletions.
42 changes: 40 additions & 2 deletions server/methods/core/cart-remove.app-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@ describe("cart methods", function () {
return done();
});

it("when called with a quantity, should decrease the quantity", function () {
sandbox.stub(Meteor.server.method_handlers, "cart/resetShipmentMethod", function () {
check(arguments, [Match.Any]);
});
sandbox.stub(Meteor.server.method_handlers, "shipping/updateShipmentQuotes", function () {
check(arguments, [Match.Any]);
});
let cart = Factory.create("cart");
const cartUserId = cart.userId;
sandbox.stub(Reaction, "getShopId", () => shop._id);
sandbox.stub(Meteor, "userId", () => cartUserId);
let cartFromCollection = Collections.Cart.findOne(cart._id);
const cartItemId = cartFromCollection.items[0]._id;
const originalQty = cartFromCollection.items[0].quantity;
Meteor.call("cart/removeFromCart", cartItemId, 1);
let updatedCart = Collections.Cart.findOne(cart._id);
expect(updatedCart.items[0].quantity).to.equal(originalQty - 1);
});

it("when quantity is decresed to zero, remove cart item", function () {
sandbox.stub(Meteor.server.method_handlers, "cart/resetShipmentMethod", function () {
check(arguments, [Match.Any]);
});
sandbox.stub(Meteor.server.method_handlers, "shipping/updateShipmentQuotes", function () {
check(arguments, [Match.Any]);
});
let cart = Factory.create("cart");
const cartUserId = cart.userId;
sandbox.stub(Reaction, "getShopId", () => shop._id);
sandbox.stub(Meteor, "userId", () => cartUserId);
let cartFromCollection = Collections.Cart.findOne(cart._id);
const cartItemId = cartFromCollection.items[0]._id;
const originalQty = cartFromCollection.items[0].quantity;
Meteor.call("cart/removeFromCart", cartItemId, originalQty);
let updatedCart = Collections.Cart.findOne(cart._id);
expect(updatedCart.items.length).to.equal(1);
});

it("should throw an exception when attempting to remove item from cart of another user", function (done) {
const cart = Factory.create("cart");
const cartItemId = "testId123";
Expand All @@ -65,7 +103,7 @@ describe("cart methods", function () {
function removeFromCartFunc() {
return Meteor.call("cart/removeFromCart", cartItemId);
}
expect(removeFromCartFunc).to.throw(Meteor.Error, /item not found/);
expect(removeFromCartFunc).to.throw(Meteor.Error, /cart-item-not-found/);
return done();
});

Expand All @@ -79,7 +117,7 @@ describe("cart methods", function () {
function removeFromCartFunc() {
return Meteor.call("cart/removeFromCart", cartItemId);
}
expect(removeFromCartFunc).to.throw(Meteor.Error, /item not found/);
expect(removeFromCartFunc).to.throw(Meteor.Error, /cart-item-not-found/);
return done();
});
});
Expand Down
55 changes: 21 additions & 34 deletions server/methods/core/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ function quantityProcessing(product, variant, itemQty = 1) {

// TODO: think about #152 implementation here
switch (product.type) {
case "not-in-stock":
break;
default: // type: `simple` // todo: maybe it should be "variant"
if (quantity < MIN) {
quantity = MIN;
} else if (quantity > MAX) {
quantity = MAX;
}
case "not-in-stock":
break;
default: // type: `simple` // todo: maybe it should be "variant"
if (quantity < MIN) {
quantity = MIN;
} else if (quantity > MAX) {
quantity = MAX;
}
}

return quantity;
Expand Down Expand Up @@ -417,27 +417,20 @@ Meteor.methods({
userId: userId
});
if (!cart) {
Logger.error(`Cart not found for user: ${ this.userId }`);
throw new Meteor.Error(404, "Cart not found",
"Cart not found for user with such id");
Logger.error(`Cart not found for user: ${this.userId}`);
throw new Meteor.Error("cart-not-found", "Cart not found for user with such id");
}

let cartItem;

if (cart.items) {
cart.items.forEach(item => {
if (item._id === itemId) {
cartItem = item;
}
});
cartItem = _.find(cart.items, (item) => item._id === itemId);
}

// extra check of item exists
if (typeof cartItem !== "object") {
Logger.error(`Unable to find an item: ${itemId
} within the cart: ${cart._id}`);
throw new Meteor.Error(404, "Cart item not found.",
"Unable to find an item with such id within you cart.");
Logger.error(`Unable to find an item: ${itemId} within the cart: ${cart._id}`);
throw new Meteor.Error("cart-item-not-found", "Unable to find an item with such id in cart.");
}

// refresh shipping quotes
Expand All @@ -447,7 +440,7 @@ Meteor.methods({
// reset selected shipment method
Meteor.call("cart/resetShipmentMethod", cart._id);

if (!quantity) {
if (!quantity || quantity >= cartItem.quantity) {
return Collections.Cart.update({
_id: cart._id
}, {
Expand All @@ -463,22 +456,19 @@ Meteor.methods({
"error removing from cart");
return error;
}
if (result) {
Logger.info(`cart: deleted cart item variant id ${
cartItem.variants._id}`);
return result;
}
Logger.info(`cart: deleted cart item variant id ${cartItem.variants._id}`);
return result;
});
}

// if quantity lets convert to negative and increment
const removeQuantity = Math.abs(quantity) * -1;
return Collections.Cart.update({
_id: cart._id,
items: cartItem
"_id": cart._id,
"items._id": cartItem._id
}, {
$inc: {
"items.quantity": removeQuantity
"items.$.quantity": removeQuantity
}
}, (error, result) => {
if (error) {
Expand All @@ -487,11 +477,8 @@ Meteor.methods({
"error removing from cart");
return error;
}
if (result) {
Logger.info(`cart: removed variant ${
cartItem._id} quantity of ${quantity}`);
return result;
}
Logger.info(`cart: removed variant ${cartItem._id} quantity of ${quantity}`);
return result;
});
},

Expand Down

0 comments on commit 5590012

Please sign in to comment.