Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for decrement cart function #1273

Merged
merged 13 commits into from
Aug 10, 2016
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