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: do not emit afterCartUpdate unless surcharges are updated #5001

Merged
merged 2 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions imports/plugins/core/discounts/server/no-meteor/startup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from "@reactioncommerce/logger";
import appEvents from "/imports/node-app/core/util/appEvents";
import getDiscountsTotalForCart from "/imports/plugins/core/discounts/server/no-meteor/util/getDiscountsTotalForCart";

Expand All @@ -14,6 +15,7 @@ export default function startup(context) {
if (!cart) {
throw new Error("afterCartUpdate hook run with no cart argument");
}
Logger.debug("Handling afterCartUpdate: discounts");

const cartId = cart._id;
const { total: discount } = await getDiscountsTotalForCart(context, cartId);
Expand Down
2 changes: 2 additions & 0 deletions imports/plugins/core/shipping/server/no-meteor/startup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from "@reactioncommerce/logger";
import Random from "@reactioncommerce/random";
import ReactionError from "@reactioncommerce/reaction-error";

Expand Down Expand Up @@ -28,6 +29,7 @@ export default function startup({ appEvents, collections }) {
if (!updatedCart) {
throw new Error("afterCartUpdate hook run with no cart argument");
}
Logger.debug("Handling afterCartUpdate: shipping");

// Every time the cart is updated, create any missing fulfillment groups as necessary.
// We need one group per type per shop, containing only the items from that shop.
Expand Down
2 changes: 2 additions & 0 deletions imports/plugins/core/taxes/server/no-meteor/startup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isEqual } from "lodash";
import Logger from "@reactioncommerce/logger";
import getUpdatedCartItems from "./util/getUpdatedCartItems";

const EMITTED_BY_NAME = "TAXES_CORE_PLUGIN";
Expand All @@ -18,6 +19,7 @@ export default function startup(context) {
// have changed.
appEvents.on("afterCartUpdate", async ({ cart }, { emittedBy } = {}) => {
if (emittedBy === EMITTED_BY_NAME) return; // short circuit infinite loops
Logger.debug("Handling afterCartUpdate: taxes");

const { cartItems, taxSummary } = await getUpdatedCartItems(context, cart);

Expand Down
14 changes: 12 additions & 2 deletions imports/plugins/included/surcharges/server/no-meteor/startup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isEqual } from "lodash";
import Logger from "@reactioncommerce/logger";
import xformCartGroupToCommonOrder from "/imports/plugins/core/cart/server/no-meteor/util/xformCartGroupToCommonOrder";
import collectionIndex from "/imports/utils/collectionIndex";
import getSurcharges from "./getSurcharges";
Expand All @@ -20,8 +22,9 @@ export default function startup(context) {
// Update the cart to include surcharges, if applicable
appEvents.on("afterCartUpdate", async ({ cart }, { emittedBy } = {}) => {
if (emittedBy === EMITTED_BY_NAME) return; // short circuit infinite loops
Logger.debug("Handling afterCartUpdate: surcharges");

const { shipping } = cart;
const { surcharges, shipping } = cart;
const cartSurcharges = [];

// Merge surcharges from each shipping group
Expand All @@ -35,6 +38,13 @@ export default function startup(context) {
});
}

// To avoid infinite looping among various `afterCartUpdate` handlers that also
// update cart and emit a subsequent `afterCartUpdate`, we need to be sure we
// do not do the update or emit the event unless we truly need to update something.
const previousSurcharges = (surcharges || []).map((appliedSurcharge) => ({ ...appliedSurcharge, _id: null }));
const nextSurcharges = cartSurcharges.map((appliedSurcharge) => ({ ...appliedSurcharge, _id: null }));
if (isEqual(previousSurcharges, nextSurcharges)) return;

const { value: updatedCart } = await Cart.findOneAndUpdate({ _id: cart._id }, {
$set: {
surcharges: cartSurcharges
Expand All @@ -44,6 +54,6 @@ export default function startup(context) {
returnOriginal: false
});

appEvents.emit("afterCartUpdate", { cart: updatedCart }, { emittedBy: EMITTED_BY_NAME });
appEvents.emit("afterCartUpdate", { cart: updatedCart, updatedBy: null }, { emittedBy: EMITTED_BY_NAME });
});
}