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:
Adrien de Peretti
2022-05-16 10:06:50 +02:00
committed by GitHub
parent 7605963ded
commit e31fe4af16
2 changed files with 37 additions and 2 deletions

View File

@@ -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", () => {

View File

@@ -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
) {