From fd14e243daf2724ce91aaf85c29806f22f3b6623 Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Fri, 20 Aug 2021 17:12:37 +0200 Subject: [PATCH] fix: Account for non-discountable items in getRefundTotal (#347) --- .../api/__tests__/store/returns.js | 63 +++++++++++++++++++ .../medusa/src/services/__tests__/totals.js | 6 ++ packages/medusa/src/services/totals.js | 3 +- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/integration-tests/api/__tests__/store/returns.js b/integration-tests/api/__tests__/store/returns.js index 9a30dabf41..f916572a52 100644 --- a/integration-tests/api/__tests__/store/returns.js +++ b/integration-tests/api/__tests__/store/returns.js @@ -9,6 +9,8 @@ const { ProductVariant, ShippingOption, LineItem, + Discount, + DiscountRule, } = require("@medusajs/medusa"); const setupServer = require("../../../helpers/setup-server"); @@ -64,6 +66,38 @@ describe("/store/carts", () => { currency_code: "usd", }); + await manager.insert(DiscountRule, { + id: "discount_rule_id", + description: "test description", + value: 10, + allocation: "total", + type: "percentage", + }); + + const d = manager.create(Discount, { + id: "test-discount", + code: "TEST", + is_dynamic: false, + is_disabled: false, + rule_id: "discount_rule_id", + }); + + await manager.save(d); + + const ord = manager.create(Order, { + id: "order_with_discount", + email: "test@email.com", + display_id: 112, + customer_id: "cus_1234", + region_id: "region", + tax_rate: 0, + currency_code: "usd", + }); + + ord.discounts = [d]; + + await manager.save(ord); + const defaultProfile = await manager.findOne(ShippingProfile, { type: "default", }); @@ -147,6 +181,35 @@ describe("/store/carts", () => { expect(response.data.return.refund_amount).toEqual(8000); }); + it("creates a return with discount and non-discountable item", async () => { + const api = useApi(); + + await dbConnection.manager.query( + `UPDATE line_item set allow_discounts=false where id='test-item'` + ); + + await dbConnection.manager.query( + `UPDATE line_item set order_id='order_with_discount' where id='test-item'` + ); + + const response = await api + .post("/store/returns", { + order_id: "order_with_discount", + items: [ + { + item_id: "test-item", + quantity: 1, + }, + ], + }) + .catch((err) => { + return err.response; + }); + + expect(response.status).toEqual(200); + expect(response.data.return.refund_amount).toEqual(8000); + }); + it("creates a return with shipping method", async () => { const api = useApi(); diff --git a/packages/medusa/src/services/__tests__/totals.js b/packages/medusa/src/services/__tests__/totals.js index b07b2c887f..5127a8dec1 100644 --- a/packages/medusa/src/services/__tests__/totals.js +++ b/packages/medusa/src/services/__tests__/totals.js @@ -330,6 +330,7 @@ describe("TotalsService", () => { { id: "line2", unit_price: 100, + allow_discounts: true, variant: { id: "variant", product_id: "product2", @@ -349,6 +350,7 @@ describe("TotalsService", () => { { id: "line2", unit_price: 100, + allow_discounts: true, variant: { id: "variant", product_id: "product2", @@ -368,6 +370,7 @@ describe("TotalsService", () => { { id: "line", unit_price: 100, + allow_discounts: true, variant: { id: "variant", product_id: "product", @@ -386,6 +389,7 @@ describe("TotalsService", () => { { id: "line2", unit_price: 100, + allow_discounts: true, variant: { id: "variant", product_id: "testp2", @@ -404,6 +408,7 @@ describe("TotalsService", () => { { id: "line2", unit_price: 100, + allow_discounts: true, variant: { id: "variant", product_id: "testp2", @@ -422,6 +427,7 @@ describe("TotalsService", () => { { id: "notInOrder", unit_price: 123, + allow_discounts: true, variant: { id: "variant", product_id: "pid", diff --git a/packages/medusa/src/services/totals.js b/packages/medusa/src/services/totals.js index 4ec4360b71..6b98692b96 100644 --- a/packages/medusa/src/services/totals.js +++ b/packages/medusa/src/services/totals.js @@ -122,7 +122,7 @@ class TotalsService extends BaseService { const discount = discounts.find(({ rule }) => rule.type !== "free_shipping") - if (!discount) { + if (!discount || !lineItem.allow_discounts) { return lineItem.unit_price * lineItem.quantity * (1 + taxRate) } @@ -168,6 +168,7 @@ class TotalsService extends BaseService { return this.getLineItemRefund(order, i) }) + return this.rounded(refunds.reduce((acc, next) => acc + next, 0)) }