fix(medusa): CartService fix discounts empty array check on update (#1516)
* fix(medusa): CartService fix constraint check * test(medusa): CartService test case, remove all discount if empty array is passed
This commit is contained in:
committed by
GitHub
parent
7605963ded
commit
e31fe4af16
@@ -2137,6 +2137,41 @@ describe("CartService", () => {
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it("successfully remove all discounts that have been applied", async () => {
|
||||
await cartService.update(IdMap.getId("with-d"), {
|
||||
discounts: [],
|
||||
})
|
||||
expect(eventBusService.emit).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusService.emit).toHaveBeenCalledWith(
|
||||
"cart.updated",
|
||||
expect.any(Object)
|
||||
)
|
||||
|
||||
expect(cartRepository.save).toHaveBeenCalledTimes(1)
|
||||
expect(cartRepository.save).toHaveBeenCalledWith({
|
||||
id: IdMap.getId("cart"),
|
||||
region_id: IdMap.getId("good"),
|
||||
discount_total: 0,
|
||||
shipping_total: 0,
|
||||
subtotal: 0,
|
||||
tax_total: 0,
|
||||
total: 0,
|
||||
items: [
|
||||
{
|
||||
id: "li1",
|
||||
quantity: 2,
|
||||
unit_price: 1000,
|
||||
},
|
||||
{
|
||||
id: "li2",
|
||||
quantity: 1,
|
||||
unit_price: 500,
|
||||
},
|
||||
],
|
||||
discounts: [],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("removeDiscount", () => {
|
||||
|
||||
@@ -779,7 +779,7 @@ class CartService extends TransactionBaseService<CartService> {
|
||||
await this.updateShippingAddress_(cart, shippingAddress, addrRepo)
|
||||
}
|
||||
|
||||
if (data.discounts?.length) {
|
||||
if (typeof data.discounts !== "undefined") {
|
||||
const previousDiscounts = [...cart.discounts]
|
||||
cart.discounts.length = 0
|
||||
|
||||
@@ -797,7 +797,7 @@ class CartService extends TransactionBaseService<CartService> {
|
||||
// we need to update shipping methods to original price
|
||||
if (
|
||||
previousDiscounts.some(
|
||||
({ rule }) => rule?.type === "free_shipping"
|
||||
({ rule }) => rule.type === "free_shipping"
|
||||
) &&
|
||||
!hasFreeShipping
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user