Skip to content

Commit cf8069a

Browse files
vanpho93sujithvn
authored andcommitted
fix: expected total mismatch with shipping discount
Signed-off-by: Brian Nguyen <[email protected]>
1 parent 773ac27 commit cf8069a

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

apps/reaction/tests/integration/api/mutations/checkout/promotionCheckout.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ describe("Promotions", () => {
262262
test("placed order get the correct values", async () => {
263263
const orderId = decodeOpaqueIdForNamespace("reaction/order")(placedOrderId);
264264
const newOrder = await testApp.collections.Orders.findOne({ _id: orderId });
265-
expect(newOrder.shipping[0].invoice.total).toEqual(112.44);
265+
expect(newOrder.shipping[0].invoice.total).toEqual(139.94);
266266
expect(newOrder.shipping[0].invoice.discounts).toEqual(10);
267267
expect(newOrder.shipping[0].invoice.subtotal).toEqual(119.94);
268268

@@ -299,7 +299,7 @@ describe("Promotions", () => {
299299
const orderId = decodeOpaqueIdForNamespace("reaction/order")(placedOrderId);
300300
const newOrder = await testApp.collections.Orders.findOne({ _id: orderId });
301301

302-
expect(newOrder.shipping[0].invoice.total).toEqual(110.45);
302+
expect(newOrder.shipping[0].invoice.total).toEqual(137.95);
303303
expect(newOrder.shipping[0].invoice.discounts).toEqual(11.99);
304304
expect(newOrder.shipping[0].invoice.subtotal).toEqual(119.94);
305305

@@ -656,13 +656,13 @@ describe("Promotions", () => {
656656
test("placed order get the correct values", async () => {
657657
const orderId = decodeOpaqueIdForNamespace("reaction/order")(placedOrderId);
658658
const newOrder = await testApp.collections.Orders.findOne({ _id: orderId });
659-
expect(newOrder.shipping[0].invoice.total).toEqual(121.94);
659+
expect(newOrder.shipping[0].invoice.total).toEqual(144.94);
660660
expect(newOrder.shipping[0].invoice.discounts).toEqual(0);
661661
expect(newOrder.shipping[0].invoice.subtotal).toEqual(119.94);
662-
expect(newOrder.shipping[0].invoice.shipping).toEqual(2);
663-
expect(newOrder.shipping[0].shipmentMethod.discount).toEqual(0.5);
664-
expect(newOrder.shipping[0].shipmentMethod.rate).toEqual(0.5);
665-
expect(newOrder.shipping[0].shipmentMethod.handling).toEqual(1.5);
662+
expect(newOrder.shipping[0].invoice.shipping).toEqual(25);
663+
expect(newOrder.shipping[0].shipmentMethod.discount).toEqual(5);
664+
expect(newOrder.shipping[0].shipmentMethod.rate).toEqual(5);
665+
expect(newOrder.shipping[0].shipmentMethod.handling).toEqual(20);
666666

667667
expect(newOrder.shipping[0].items[0].quantity).toEqual(6);
668668

@@ -709,13 +709,13 @@ describe("Promotions", () => {
709709
test("placed order get the correct values", async () => {
710710
const orderId = decodeOpaqueIdForNamespace("reaction/order")(placedOrderId);
711711
const newOrder = await testApp.collections.Orders.findOne({ _id: orderId });
712-
expect(newOrder.shipping[0].invoice.total).toEqual(121.89);
712+
expect(newOrder.shipping[0].invoice.total).toEqual(144.44);
713713
expect(newOrder.shipping[0].invoice.discounts).toEqual(0);
714714
expect(newOrder.shipping[0].invoice.subtotal).toEqual(119.94);
715-
expect(newOrder.shipping[0].invoice.shipping).toEqual(1.95);
716-
expect(newOrder.shipping[0].shipmentMethod.discount).toEqual(0.55);
717-
expect(newOrder.shipping[0].shipmentMethod.rate).toEqual(0.45);
718-
expect(newOrder.shipping[0].shipmentMethod.handling).toEqual(1.5);
715+
expect(newOrder.shipping[0].invoice.shipping).toEqual(24.5);
716+
expect(newOrder.shipping[0].shipmentMethod.discount).toEqual(5.5);
717+
expect(newOrder.shipping[0].shipmentMethod.rate).toEqual(4.5);
718+
expect(newOrder.shipping[0].shipmentMethod.handling).toEqual(20);
719719

720720
expect(newOrder.appliedPromotions).toHaveLength(2);
721721
expect(newOrder.discounts).toHaveLength(2);

packages/api-plugin-orders/src/util/orderValidators/getFinalFulfillmentGroups.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export default async function getFinalFulfillmentGroups(context, inputData) {
6161
group.itemIds = group.items.map((item) => item._id);
6262
group.totalItemQuantity = group.items.reduce((sum, item) => sum + item.quantity, 0);
6363

64+
if (cart && Array.isArray(cart.shipping)) {
65+
const cartShipping = cart.shipping.find((shipping) => shipping.shipmentMethod?._id === selectedFulfillmentMethodId);
66+
group.shipmentMethod = cartShipping?.shipmentMethod;
67+
}
68+
6469
// Apply shipment method
6570
group.shipmentMethod = await addShipmentMethodToGroup(context, {
6671
accountId,
@@ -110,17 +115,10 @@ export default async function getFinalFulfillmentGroups(context, inputData) {
110115
});
111116

112117
if (expectedGroupTotal) {
113-
// For now we expect that the client has NOT included discounts in the expected total it sent.
114-
// Note that we don't currently know which parts of `discountTotal` go with which fulfillment groups.
115-
// This needs to be rewritten soon for discounts to work when there are multiple fulfillment groups.
116-
// Probably the client should be sending all applied discount IDs and amounts in the order input (by group),
117-
// and include total discount in `groupInput.totalPrice`, and then we simply verify that they are valid here.
118-
const expectedTotal = Math.max(expectedGroupTotal - discountTotal, 0);
119-
120118
// Compare expected and actual totals to make sure client sees correct calculated price
121119
// Error if we calculate total price differently from what the client has shown as the preview.
122120
// It's important to keep this after adding and verifying the shipmentMethod and order item prices.
123-
compareExpectedAndActualTotals(group.invoice.total, expectedTotal);
121+
compareExpectedAndActualTotals(group.invoice.total, expectedGroupTotal);
124122
}
125123

126124
// We save off the first shipping address found, for passing to payment services. They use this

0 commit comments

Comments
 (0)