diff --git a/.changeset/large-loops-yell.md b/.changeset/large-loops-yell.md new file mode 100644 index 0000000000000..2b81e428bd614 --- /dev/null +++ b/.changeset/large-loops-yell.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +fix(core-flows): refresh payment collection inside updateCartPromotionsWorkflow diff --git a/integration-tests/http/__tests__/cart/store/cart.spec.ts b/integration-tests/http/__tests__/cart/store/cart.spec.ts index 862d40c6c67c3..6e7a072902781 100644 --- a/integration-tests/http/__tests__/cart/store/cart.spec.ts +++ b/integration-tests/http/__tests__/cart/store/cart.spec.ts @@ -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 = ( diff --git a/packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts b/packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts index b16bb996499fb..3f9beb39973eb 100644 --- a/packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts +++ b/packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts @@ -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. @@ -153,6 +154,10 @@ export const updateCartPromotionsWorkflow = createWorkflow( }) ) + refreshPaymentCollectionForCartWorkflow.runAsStep({ + input: { cart }, + }) + releaseLockStep({ key: cart.id, })