Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

feat: refactor-discounts #46

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
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
20,068 changes: 76 additions & 19,992 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
"@reactioncommerce/random": "^1.0.2",
"@reactioncommerce/reaction-error": "^1.0.1",
"accounting-js": "^1.1.1",
"deep-object-diff": "^1.1.7",
"lodash": "^4.17.15",
"simpl-schema": "^1.12.0"
"simpl-schema": "^1.12.3"
},
"devDependencies": {
"@babel/core": "^7.7.7",
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import queries from "./queries/index.js";
import resolvers from "./resolvers/index.js";
import schemas from "./schemas/index.js";
import { registerPluginHandlerForCart } from "./registration.js";
import { Cart, CartItem } from "./simpleSchemas.js";
import { Cart, CartItem, Shipment, ShippingMethod, ShipmentQuote } from "./simpleSchemas.js";
import startup from "./startup.js";

/**
Expand Down Expand Up @@ -59,7 +59,10 @@ export default async function register(app) {
policies,
simpleSchemas: {
Cart,
CartItem
CartItem,
Shipment,
ShippingMethod,
ShipmentQuote
}
});
}
12 changes: 4 additions & 8 deletions src/mutations/saveCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import ReactionError from "@reactioncommerce/reaction-error";
* validates, and upserts to database.
* @param {Object} context - App context
* @param {Object} cart - The cart to transform and insert or replace
* @param {Boolean} emittedBy - Who emitted the event
* @returns {Object} Transformed and saved cart
*/
export default async function saveCart(context, cart) {
export default async function saveCart(context, cart, emittedBy) {
const { appEvents, collections: { Cart }, userId = null } = context;

// These will mutate `cart`
await context.mutations.removeMissingItemsFromCart(context, cart);
await context.mutations.transformAndValidateCart(context, cart);
Expand All @@ -20,12 +20,8 @@ export default async function saveCart(context, cart) {
if (upsertedCount === 1) {
appEvents.emit("afterCartCreate", {
cart,
createdBy: userId
});
} else {
appEvents.emit("afterCartUpdate", {
cart,
updatedBy: userId
createdBy: userId,
emittedBy
});
}

Expand Down
1 change: 0 additions & 1 deletion src/mutations/transformAndValidateCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ export default async function transformAndValidateCart(context, cart) {
/* eslint-enable no-await-in-loop */
Logger.debug({ ...logCtx, cartId: cart._id, ms: Date.now() - startTime }, `Finished ${transformInfo.name} cart transform`);
});

cartSchema.validate(cart);
}
2 changes: 1 addition & 1 deletion src/mutations/updateCartItemsQuantity.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default async function updateCartItemsQuantity(context, input) {
if (!update) {
list.push({ ...item });
} else if (update.quantity > 0) {
// Update quantity as instructed, while omitting the item if quantity is 0
// Update quantity as instructed, while omitting the item if quantity is 0`
list.push({
...item,
quantity: update.quantity,
Expand Down
33 changes: 27 additions & 6 deletions src/simpleSchemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const ShippoShippingMethod = new SimpleSchema({
* @property {String} carrier optional
* @property {ShippoShippingMethod} settings optional
*/
const ShippingMethod = new SimpleSchema({
export const ShippingMethod = new SimpleSchema({
"_id": {
type: String,
label: "Shipment Method Id"
Expand Down Expand Up @@ -338,6 +338,10 @@ export const ShipmentQuote = new SimpleSchema({
type: Number,
defaultValue: 0.00
},
undiscountedRate: {
type: Number,
optional: true
},
shippingPrice: {
type: Number,
optional: true
Expand Down Expand Up @@ -508,6 +512,26 @@ export const CartInvoice = new SimpleSchema({
}
});

const Event = new SimpleSchema({
type: String,
params: {
type: Object,
optional: true
}
});


export const Rules = new SimpleSchema({
conditions: {
type: Object,
blackbox: true
},
event: {
type: Event
}
});


/**
* @name Shipment
* @summary Used for cart/order shipment tracking
Expand All @@ -532,7 +556,7 @@ export const CartInvoice = new SimpleSchema({
* @property {String} customsLabelUrl For customs printable label
* @property {ShippoShipment} shippo For Shippo specific properties
*/
const Shipment = new SimpleSchema({
export const Shipment = new SimpleSchema({
"_id": {
type: String,
label: "Shipment Id"
Expand Down Expand Up @@ -624,6 +648,7 @@ const Money = new SimpleSchema({
}
});


/**
* @name CartItemAttribute
* @memberof Schemas
Expand Down Expand Up @@ -832,10 +857,6 @@ export const Cart = new SimpleSchema({
optional: true,
defaultValue: false
},
"discount": {
type: Number,
optional: true
},
"surcharges": {
type: Array,
optional: true
Expand Down
2 changes: 1 addition & 1 deletion src/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function updateAllCartsForVariant({ Cart, context, variant }) {
* @returns {undefined}
*/
export default async function cartStartup(context) {
const { appEvents, collections } = context;
const { appEvents, collections, simpleSchemas: { Cart: CartSchema, CartItem: CartItemSchema } } = context;
const { Cart } = collections;

// When an order is created, delete the source cart
Expand Down
19 changes: 14 additions & 5 deletions src/util/xformCartGroupToCommonOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export default async function xformCartGroupToCommonOrder(cart, group, context)
taxCode: item.taxCode,
title: item.title,
variantId: item.variantId,
variantTitle: item.variantTitle
variantTitle: item.variantTitle,
discounts: item.discounts
};
}));

Expand Down Expand Up @@ -97,7 +98,12 @@ export default async function xformCartGroupToCommonOrder(cart, group, context)

// TODO: In the future, we should update this with a discounts update
// Discounts are stored as the sum of all discounts, per cart. This will need to be updated when we refactor discounts to go by group.
const discountTotal = cart.discount || 0;
const discountTotalFunctions = context.getFunctionsOfType("calculateDiscountTotal");
let discountTotal = 0;
for (const discountTotalFunction of discountTotalFunctions) {
const thisDiscountTotal = discountTotalFunction(context, cart);
discountTotal += thisDiscountTotal;
}
const groupItemTotal = +accounting.toFixed(items.reduce((sum, item) => (sum + item.subtotal.amount), 0), 3);
// orderItemTotal will need to be updated to be the actual total when we eventually have more than one group available
const orderItemTotal = groupItemTotal;
Expand Down Expand Up @@ -129,8 +135,9 @@ export default async function xformCartGroupToCommonOrder(cart, group, context)
}
};


return {
const matchingShippingGroup = cart.shipping.find((thisGroup) => thisGroup._id === group._id);
const discounts = matchingShippingGroup.discounts || [];
const commonOrder = {
accountId,
billingAddress: null,
cartId: cart._id,
Expand All @@ -144,6 +151,8 @@ export default async function xformCartGroupToCommonOrder(cart, group, context)
shippingAddress: address || null,
shopId,
sourceType: "cart",
totals
totals,
discounts
};
return commonOrder;
}