Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/large-loops-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---

fix(core-flows): refresh payment collection inside updateCartPromotionsWorkflow
69 changes: 69 additions & 0 deletions integration-tests/http/__tests__/cart/store/cart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5299,6 +5299,75 @@ medusaIntegrationTestRunner({
})
})

describe("DELETE /store/carts/:id/promotions", () => {
it("should remove promotions and recalculate payment_collection amount", async () => {
cart = (
await api.post(
`/store/carts`,
{
currency_code: "usd",
sales_channel_id: salesChannel.id,
region_id: region.id,
shipping_address: shippingAddressData,
items: [{ variant_id: product.variants[0].id, quantity: 1 }],
promo_codes: [promotion.code],
},
storeHeaders
)
).data.cart

await api.post(
`/store/payment-collections`,
{ cart_id: cart.id },
storeHeaders
)

cart = (
await api.get(`/store/carts/${cart.id}`, storeHeaders)
).data.cart

expect(cart).toEqual(
expect.objectContaining({
id: cart.id,
items: expect.arrayContaining([
expect.objectContaining({
adjustments: expect.arrayContaining([
expect.objectContaining({
code: "PROMOTION_APPLIED",
promotion_id: promotion.id,
amount: 100,
}),
]),
}),
]),
})
)

const paymentCollectionAmount = cart.payment_collection.amount
const discountTotal = cart.discount_total

const cartAfterDeletion = (await api.delete(
`/store/carts/${cart.id}/promotions`,
{ promo_codes: [promotion.code] },
storeHeaders
)).data.cart

expect(cartAfterDeletion).toEqual(
expect.objectContaining({
id: cart.id,
items: expect.arrayContaining([
expect.objectContaining({
adjustments: []
}),
]),
})
)

expect(cartAfterDeletion.payment_collection.amount).toEqual(paymentCollectionAmount - discountTotal)
expect(cartAfterDeletion.discount_total).toEqual(0)
})
})

describe("POST /store/carts/:id/customer", () => {
beforeEach(async () => {
cart = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from "../steps"
import { updateCartPromotionsStep } from "../steps/update-cart-promotions"
import { cartFieldsForRefreshSteps } from "../utils/fields"
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"

/**
* The details of the promotion updates on a cart.
Expand Down Expand Up @@ -153,6 +154,10 @@ export const updateCartPromotionsWorkflow = createWorkflow(
})
)

refreshPaymentCollectionForCartWorkflow.runAsStep({
input: { cart },
})

releaseLockStep({
key: cart.id,
})
Expand Down
Loading