Skip to content

Commit

Permalink
fix: avoid infinite looping when taxes are enabled
Browse files Browse the repository at this point in the history
fixes #4620
  • Loading branch information
aldeed committed Sep 20, 2018
1 parent e90da0d commit 11e95ba
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions imports/plugins/core/taxes/server/methods/methods.js
@@ -1,3 +1,4 @@
import _ from "lodash";
import Logger from "@reactioncommerce/logger";
import ReactionError from "@reactioncommerce/reaction-error";
import { Meteor } from "meteor/meteor";
Expand Down Expand Up @@ -123,17 +124,31 @@ export const methods = {

const { cartTaxData, cartTaxRate, itemsWithTax, taxRatesByShop } = options;

const result = Cart.update({ _id: cartId }, {
$set: {
taxes: cartTaxData,
tax: cartTaxRate,
items: itemsWithTax,
taxRatesByShop
}
});
const cart = Cart.findOne({ _id: cartId });

// Compare only the props we care about for tax purposes
const oldCartItems = (cart.items || []).map(({ taxData, taxRate, tax }) => ({ taxData, taxRate, tax }));
const newCartItems = (itemsWithTax || []).map(({ taxData, taxRate, tax }) => ({ taxData, taxRate, tax }));

let result;
if (
!_.isEqual(cart.taxes, cartTaxData) ||
!_.isEqual(cart.tax, cartTaxRate) ||
!_.isEqual(oldCartItems, newCartItems) ||
!_.isEqual(cart.taxRatesByShop, taxRatesByShop)
) {
result = Cart.update({ _id: cartId }, {
$set: {
taxes: cartTaxData,
tax: cartTaxRate,
items: itemsWithTax,
taxRatesByShop
}
});

const updatedCart = Cart.findOne({ _id: cartId });
Promise.await(appEvents.emit("afterCartUpdate", cartId, updatedCart));
const updatedCart = Cart.findOne({ _id: cartId });
Promise.await(appEvents.emit("afterCartUpdate", cartId, updatedCart));
}

return result;
},
Expand Down

0 comments on commit 11e95ba

Please sign in to comment.