Skip to content

Commit aa4c722

Browse files
committed
fix(link-modules,core-flows): Carry over cart promotions to order promotions
1 parent 541e791 commit aa4c722

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

.changeset/odd-apricots-marry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@medusajs/link-modules": patch
3+
"@medusajs/core-flows": patch
4+
---
5+
6+
fix(link-modules,core-flows): Carry over cart promotions to order promotions

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,60 @@ medusaIntegrationTestRunner({
12521252
)
12531253
})
12541254

1255+
it("should successfully complete cart with promotions", async () => {
1256+
const oldCart = (
1257+
await api.get(`/store/carts/${cart.id}`, storeHeaders)
1258+
).data.cart
1259+
1260+
createCartCreditLinesWorkflow.run({
1261+
input: [
1262+
{
1263+
cart_id: oldCart.id,
1264+
amount: oldCart.total,
1265+
currency_code: "usd",
1266+
reference: "test",
1267+
reference_id: "test",
1268+
},
1269+
],
1270+
container: appContainer,
1271+
})
1272+
1273+
const cartResponse = await api.post(
1274+
`/store/carts/${cart.id}/complete`,
1275+
{},
1276+
storeHeaders
1277+
)
1278+
1279+
const orderResponse = await api.get(
1280+
`/store/orders/${cartResponse.data.order.id}?fields=+promotions.*`,
1281+
storeHeaders
1282+
)
1283+
1284+
expect(cartResponse.status).toEqual(200)
1285+
expect(orderResponse.data.order).toEqual(
1286+
expect.objectContaining({
1287+
promotions: [
1288+
expect.objectContaining({
1289+
code: promotion.code,
1290+
}),
1291+
],
1292+
items: expect.arrayContaining([
1293+
expect.objectContaining({
1294+
unit_price: 1500,
1295+
compare_at_unit_price: null,
1296+
quantity: 1,
1297+
adjustments: expect.arrayContaining([
1298+
expect.objectContaining({
1299+
amount: 100,
1300+
code: promotion.code,
1301+
}),
1302+
]),
1303+
}),
1304+
]),
1305+
})
1306+
)
1307+
})
1308+
12551309
it("should successfully complete cart without shipping for digital products", async () => {
12561310
/**
12571311
* Product has a shipping profile so cart item should not require shipping

packages/core/core-flows/src/cart/utils/fields.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const cartFieldsForRefreshSteps = [
3636
"shipping_methods.tax_lines.*",
3737
"customer.*",
3838
"customer.groups.*",
39+
"promotions.id",
3940
"promotions.code",
4041
"payment_collection.id",
4142
"payment_collection.raw_amount",
@@ -105,6 +106,7 @@ export const completeCartFields = [
105106
"credit_lines.*",
106107
"payment_collection.*",
107108
"payment_collection.payment_sessions.*",
109+
"promotions.id",
108110
"items.variant.id",
109111
"items.variant.product.id",
110112
"items.variant.product.is_giftcard",

packages/core/core-flows/src/cart/workflows/complete-cart.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
CartCreditLineDTO,
33
CartWorkflowDTO,
4+
LinkDefinition,
5+
PromotionDTO,
46
UsageComputedActions,
57
} from "@medusajs/framework/types"
68
import {
@@ -326,13 +328,22 @@ export const completeCartWorkflow = createWorkflow(
326328
const linksToCreate = transform(
327329
{ cart, createdOrder },
328330
({ cart, createdOrder }) => {
329-
const links: Record<string, any>[] = [
331+
const links: LinkDefinition[] = [
330332
{
331333
[Modules.ORDER]: { order_id: createdOrder.id },
332334
[Modules.CART]: { cart_id: cart.id },
333335
},
334336
]
335337

338+
if (cart.promotions?.length) {
339+
cart.promotions.forEach((promotion: PromotionDTO) => {
340+
links.push({
341+
[Modules.ORDER]: { order_id: createdOrder.id },
342+
[Modules.PROMOTION]: { promotion_id: promotion.id },
343+
})
344+
})
345+
}
346+
336347
if (isDefined(cart.payment_collection?.id)) {
337348
links.push({
338349
[Modules.ORDER]: { order_id: createdOrder.id },

packages/modules/link-modules/src/definitions/order-promotion.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const OrderPromotion: ModuleJoinerConfig = {
3232
entity: "Promotion",
3333
primaryKey: "id",
3434
foreignKey: "promotion_id",
35-
alias: "promotion",
35+
alias: "promotions",
3636
args: {
3737
methodSuffix: "Promotions",
3838
},
@@ -44,8 +44,8 @@ export const OrderPromotion: ModuleJoinerConfig = {
4444
serviceName: Modules.ORDER,
4545
entity: "Order",
4646
fieldAlias: {
47-
promotion: {
48-
path: "promotion_link.promotion",
47+
promotions: {
48+
path: "promotion_link.promotions",
4949
isList: true,
5050
},
5151
},
@@ -54,16 +54,7 @@ export const OrderPromotion: ModuleJoinerConfig = {
5454
primaryKey: "order_id",
5555
foreignKey: "id",
5656
alias: "promotion_link",
57-
},
58-
},
59-
{
60-
serviceName: Modules.PROMOTION,
61-
entity: "Promotion",
62-
relationship: {
63-
serviceName: LINKS.OrderPromotion,
64-
primaryKey: "promotion_id",
65-
foreignKey: "id",
66-
alias: "order_link",
57+
isList: true,
6758
},
6859
},
6960
],

0 commit comments

Comments
 (0)