Skip to content

Commit d565a92

Browse files
NicolasGorgaadrien2polivermrbl
authored
fix(core-flows): refresh payment collection inside updateCartPromotionsWorkflow (patch) (medusajs#14274)
* 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 * Force refresh payment collection on dedicated store cart promotion endpoints --------- Co-authored-by: Adrien de Peretti <[email protected]> Co-authored-by: Oli Juhl <[email protected]>
1 parent a78f68f commit d565a92

File tree

2 files changed

+89
-5
lines changed
  • integration-tests/http/__tests__/cart/store
  • packages/medusa/src/api/store/carts/[id]/promotions

2 files changed

+89
-5
lines changed

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

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

5302+
describe("POST /store/carts/:id/promotions", () => {
5303+
it("should add promotions and refresh payment collection", 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+
},
5314+
storeHeaders
5315+
)
5316+
).data.cart
5317+
5318+
const paymentCollection = (
5319+
await api.post(
5320+
`/store/payment-collections`,
5321+
{ cart_id: cart.id },
5322+
storeHeaders
5323+
)
5324+
).data.payment_collection
5325+
5326+
await api.post(
5327+
`/store/payment-collections/${paymentCollection.id}/payment-sessions`,
5328+
{ provider_id: "pp_system_default" },
5329+
storeHeaders
5330+
)
5331+
5332+
cart = (await api.get(`/store/carts/${cart.id}`, storeHeaders)).data
5333+
.cart
5334+
expect(cart.total).toEqual(1500)
5335+
expect(cart.payment_collection.amount).toEqual(1500)
5336+
5337+
const cartAfterPromotion = (
5338+
await api.post(
5339+
`/store/carts/${cart.id}/promotions`,
5340+
{ promo_codes: [promotion.code] },
5341+
storeHeaders
5342+
)
5343+
).data.cart
5344+
5345+
expect(cartAfterPromotion).toEqual(
5346+
expect.objectContaining({
5347+
id: cart.id,
5348+
total: 1395,
5349+
discount_total: 105,
5350+
payment_collection: expect.objectContaining({
5351+
amount: 1395,
5352+
}),
5353+
items: expect.arrayContaining([
5354+
expect.objectContaining({
5355+
adjustments: expect.arrayContaining([
5356+
expect.objectContaining({
5357+
code: "PROMOTION_APPLIED",
5358+
promotion_id: promotion.id,
5359+
amount: 100,
5360+
}),
5361+
]),
5362+
}),
5363+
]),
5364+
})
5365+
)
5366+
})
5367+
})
5368+
53025369
describe("DELETE /store/carts/:id/promotions", () => {
53035370
it("should remove promotions and recalculate payment_collection amount", async () => {
53045371
cart = (
@@ -5316,9 +5383,17 @@ medusaIntegrationTestRunner({
53165383
)
53175384
).data.cart
53185385

5386+
const paymentCollection = await api
5387+
.post(
5388+
`/store/payment-collections`,
5389+
{ cart_id: cart.id },
5390+
storeHeaders
5391+
)
5392+
.then((response) => response.data.payment_collection)
5393+
53195394
await api.post(
5320-
`/store/payment-collections`,
5321-
{ cart_id: cart.id },
5395+
`/store/payment-collections/${paymentCollection.id}/payment-sessions`,
5396+
{ provider_id: "pp_system_default" },
53225397
storeHeaders
53235398
)
53245399

@@ -5328,6 +5403,11 @@ medusaIntegrationTestRunner({
53285403
expect(cart).toEqual(
53295404
expect.objectContaining({
53305405
id: cart.id,
5406+
total: 1395,
5407+
discount_total: 105,
5408+
payment_collection: expect.objectContaining({
5409+
amount: 1395,
5410+
}),
53315411
items: expect.arrayContaining([
53325412
expect.objectContaining({
53335413
adjustments: expect.arrayContaining([
@@ -5352,16 +5432,18 @@ medusaIntegrationTestRunner({
53525432
expect(cartAfterDeletion).toEqual(
53535433
expect.objectContaining({
53545434
id: cart.id,
5435+
total: 1500,
5436+
discount_total: 0,
5437+
payment_collection: expect.objectContaining({
5438+
amount: 1500,
5439+
}),
53555440
items: expect.arrayContaining([
53565441
expect.objectContaining({
53575442
adjustments: [],
53585443
}),
53595444
]),
53605445
})
53615446
)
5362-
5363-
expect(cartAfterDeletion.total).toEqual(1500)
5364-
expect(cartAfterDeletion.discount_total).toEqual(0)
53655447
})
53665448
})
53675449

packages/medusa/src/api/store/carts/[id]/promotions/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const POST = async (
1919
payload.promo_codes.length > 0
2020
? PromotionActions.ADD
2121
: PromotionActions.REPLACE,
22+
force_refresh_payment_collection: true,
2223
},
2324
})
2425

@@ -48,6 +49,7 @@ export const DELETE = async (
4849
promo_codes: payload.promo_codes,
4950
cart_id: req.params.id,
5051
action: PromotionActions.REMOVE,
52+
force_refresh_payment_collection: true,
5153
},
5254
})
5355

0 commit comments

Comments
 (0)