Skip to content

Commit 842c0f5

Browse files
fix(core-flows): refresh payment collection inside updateCartPromotionsWorkflow (medusajs#13963)
* Refresh payment collection after cart promotion update * Add missing test * Add changeset * Update changeset * Add force_refresh_payment_collection to updateCartPromotionsWorkflow to conditionally refresh payment collection * Prevent refreshing payment collection multiple times * Fix test * Formatting and unused vars --------- Co-authored-by: Adrien de Peretti <[email protected]>
1 parent 144f0f4 commit 842c0f5

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

.changeset/large-loops-yell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/core-flows": patch
3+
---
4+
5+
fix(core-flows): refresh payment collection inside updateCartPromotionsWorkflow

integration-tests/http/__tests__/cart/store/cart.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5299,6 +5299,72 @@ medusaIntegrationTestRunner({
52995299
})
53005300
})
53015301

5302+
describe("DELETE /store/carts/:id/promotions", () => {
5303+
it("should remove promotions and recalculate payment_collection amount", async () => {
5304+
cart = (
5305+
await api.post(
5306+
`/store/carts`,
5307+
{
5308+
currency_code: "usd",
5309+
sales_channel_id: salesChannel.id,
5310+
region_id: region.id,
5311+
shipping_address: shippingAddressData,
5312+
items: [{ variant_id: product.variants[0].id, quantity: 1 }],
5313+
promo_codes: [promotion.code],
5314+
},
5315+
storeHeaders
5316+
)
5317+
).data.cart
5318+
5319+
await api.post(
5320+
`/store/payment-collections`,
5321+
{ cart_id: cart.id },
5322+
storeHeaders
5323+
)
5324+
5325+
cart = (await api.get(`/store/carts/${cart.id}`, storeHeaders)).data
5326+
.cart
5327+
5328+
expect(cart).toEqual(
5329+
expect.objectContaining({
5330+
id: cart.id,
5331+
items: expect.arrayContaining([
5332+
expect.objectContaining({
5333+
adjustments: expect.arrayContaining([
5334+
expect.objectContaining({
5335+
code: "PROMOTION_APPLIED",
5336+
promotion_id: promotion.id,
5337+
amount: 100,
5338+
}),
5339+
]),
5340+
}),
5341+
]),
5342+
})
5343+
)
5344+
5345+
const cartAfterDeletion = await api
5346+
.delete(`/store/carts/${cart.id}/promotions`, {
5347+
data: { promo_codes: [promotion.code] },
5348+
...storeHeaders,
5349+
})
5350+
.then((response) => response.data.cart)
5351+
5352+
expect(cartAfterDeletion).toEqual(
5353+
expect.objectContaining({
5354+
id: cart.id,
5355+
items: expect.arrayContaining([
5356+
expect.objectContaining({
5357+
adjustments: [],
5358+
}),
5359+
]),
5360+
})
5361+
)
5362+
5363+
expect(cartAfterDeletion.total).toEqual(1500)
5364+
expect(cartAfterDeletion.discount_total).toEqual(0)
5365+
})
5366+
})
5367+
53025368
describe("POST /store/carts/:id/customer", () => {
53035369
beforeEach(async () => {
53045370
cart = (

packages/core/core-flows/src/cart/workflows/create-carts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export const createCartWorkflow = createWorkflow(
234234
input: {
235235
cart_id: cart.id,
236236
promo_codes: input.promo_codes,
237+
force_refresh_payment_collection: false,
237238
},
238239
})
239240

packages/core/core-flows/src/cart/workflows/refresh-cart-items.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
230230
cart: refetchedCart, // Pass cart to avoid refetch in updateCartPromotionsWorkflow
231231
promo_codes: cartPromoCodes,
232232
action: PromotionActions.REPLACE,
233+
force_refresh_payment_collection: false,
233234
},
234235
})
235236

packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from "../steps"
2323
import { updateCartPromotionsStep } from "../steps/update-cart-promotions"
2424
import { cartFieldsForRefreshSteps } from "../utils/fields"
25+
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
2526

2627
/**
2728
* The details of the promotion updates on a cart.
@@ -47,6 +48,11 @@ export type UpdateCartPromotionsWorkflowInput = {
4748
| PromotionActions.ADD
4849
| PromotionActions.REMOVE
4950
| PromotionActions.REPLACE
51+
/**
52+
* Wether to force the refresh of the cart payment collection. If the caller doesn't refresh it explicitly,
53+
* you should probably set this property to true.
54+
*/
55+
force_refresh_payment_collection?: boolean
5056
}
5157

5258
export const updateCartPromotionsWorkflowId = "update-cart-promotions"
@@ -153,6 +159,15 @@ export const updateCartPromotionsWorkflow = createWorkflow(
153159
})
154160
)
155161

162+
when(
163+
{ input },
164+
({ input }) => input.force_refresh_payment_collection === true
165+
).then(() => {
166+
refreshPaymentCollectionForCartWorkflow.runAsStep({
167+
input: { cart },
168+
})
169+
})
170+
156171
releaseLockStep({
157172
key: cart.id,
158173
})

0 commit comments

Comments
 (0)