fix: Account for non-discountable items in getRefundTotal (#347)
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user