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:
1 parent
7605963ded
commit
e31fe4af16
2 files changed
+37
-2
No files matched your search
@@ -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", () => {
|
describe("removeDiscount", () => {
|
||||||
|
|||||||
@@ -779,7 +779,7 @@ class CartService extends TransactionBaseService<CartService> {
|
|||||||
await this.updateShippingAddress_(cart, shippingAddress, addrRepo)
|
await this.updateShippingAddress_(cart, shippingAddress, addrRepo)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.discounts?.length) {
|
if (typeof data.discounts !== "undefined") {
|
||||||
const previousDiscounts = [...cart.discounts]
|
const previousDiscounts = [...cart.discounts]
|
||||||
cart.discounts.length = 0
|
cart.discounts.length = 0
|
||||||
|
|
||||||
@@ -797,7 +797,7 @@ class CartService extends TransactionBaseService<CartService> {
|
|||||||
// we need to update shipping methods to original price
|
// we need to update shipping methods to original price
|
||||||
if (
|
if (
|
||||||
previousDiscounts.some(
|
previousDiscounts.some(
|
||||||
({ rule }) => rule?.type === "free_shipping"
|
({ rule }) => rule.type === "free_shipping"
|
||||||
) &&
|
) &&
|
||||||
!hasFreeShipping
|
!hasFreeShipping
|
||||||
) {
|
) {
|
||||||
|
|||||||
Reference in new issue
Block a user