Skip to content

Commit 2fe68a9

Browse files
authored
chore(): Improve some cart operation flows to remove extraneous operations when not required (medusajs#13418)
RESOLVES CORE-1155 **What** - prevent update payment collection worklow to fetch data and call steps when there is no payment collection - prevent refresh Cart Shipping Methods Workflow to fetch data and call steps when there is no shipping methods - update promotion step to remove extraneous module call
1 parent 9b3831d commit 2fe68a9

File tree

6 files changed

+57
-42
lines changed

6 files changed

+57
-42
lines changed

.changeset/short-rocks-hide.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+
chore(): Tweak update payment collection and refresh shipping method workflow execution

packages/core/core-flows/src/cart/steps/get-promotion-codes-to-apply.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,7 @@ export const getPromotionCodesToApply = createStep(
8585
})
8686
})
8787

88-
const promotionCodesToApply: Set<string> = new Set(
89-
adjustmentCodes.length
90-
? (
91-
await promotionService.listPromotions(
92-
{ code: adjustmentCodes },
93-
{ select: ["code"] }
94-
)
95-
).map((p) => p.code!)
96-
: []
97-
)
88+
const promotionCodesToApply: Set<string> = new Set(adjustmentCodes)
9889

9990
if (action === PromotionActions.REMOVE) {
10091
promo_codes.forEach((code) => promotionCodesToApply.delete(code))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ export const createCartWorkflow = createWorkflow(
321321
parallelize(
322322
refreshPaymentCollectionForCartWorkflow.runAsStep({
323323
input: {
324-
cart_id: cart.id,
324+
cart: cart,
325325
},
326326
}),
327327
emitEventStep({

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,15 @@ export const refreshCartItemsWorkflow = createWorkflow(
263263
list: false,
264264
}).config({ name: "refetch–cart" })
265265

266-
const refreshCartInput = transform(
267-
{ refetchedCart, input },
268-
({ refetchedCart, input }) => {
269-
return {
270-
cart: !input.force_refresh ? refetchedCart : undefined,
271-
cart_id: !!input.force_refresh ? input.cart_id : undefined,
272-
}
273-
}
274-
)
275-
276266
refreshCartShippingMethodsWorkflow.runAsStep({
277-
input: refreshCartInput,
267+
input: { cart: refetchedCart },
278268
})
279269

280270
when("force-refresh-update-tax-lines", { input }, ({ input }) => {
281271
return !!input.force_refresh
282272
}).then(() => {
283273
updateTaxLinesWorkflow.runAsStep({
284-
input: refreshCartInput,
274+
input: { cart_id: input.cart_id },
285275
})
286276
})
287277

@@ -331,7 +321,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
331321
)
332322

333323
refreshPaymentCollectionForCartWorkflow.runAsStep({
334-
input: { cart_id: input.cart_id },
324+
input: { cart: refetchedCart },
335325
})
336326

337327
return new WorkflowResponse(refetchedCart, {

packages/core/core-flows/src/cart/workflows/refresh-cart-shipping-methods.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,24 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
5555
idempotent: false,
5656
},
5757
(input: WorkflowData<RefreshCartShippingMethodsWorkflowInput>) => {
58-
const fetchCart = when("fetch-cart", { input }, ({ input }) => {
59-
return !input.cart
60-
}).then(() => {
58+
const shouldExecute = transform({ input }, ({ input }) => {
59+
return (
60+
!!input.cart_id ||
61+
(!!input.cart && !!input.cart.shipping_methods?.length)
62+
)
63+
})
64+
65+
const cartId = transform({ input }, ({ input }) => {
66+
return input.cart_id ?? input.cart?.id
67+
})
68+
69+
const fetchCart = when(
70+
"fetch-cart",
71+
{ shouldExecute },
72+
({ shouldExecute }) => {
73+
return shouldExecute
74+
}
75+
).then(() => {
6176
return useRemoteQueryStep({
6277
entry_point: "cart",
6378
fields: [
@@ -73,14 +88,14 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
7388
"shipping_methods.data",
7489
"total",
7590
],
76-
variables: { id: input.cart_id },
91+
variables: { id: cartId },
7792
throw_if_key_not_found: true,
7893
list: false,
7994
}).config({ name: "get-cart" })
8095
})
8196

8297
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
83-
return input.cart ?? fetchCart
98+
return fetchCart ?? input.cart
8499
})
85100

86101
const listShippingOptionsInput = transform({ cart }, ({ cart }) =>

packages/core/core-flows/src/cart/workflows/refresh-payment-collection.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,18 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
5959
idempotent: false,
6060
},
6161
(input: WorkflowData<RefreshPaymentCollectionForCartWorklowInput>) => {
62+
const shouldExecute = transform({ input }, ({ input }) => {
63+
return (
64+
!!input.cart_id || (!!input.cart && !!input.cart.payment_collection)
65+
)
66+
})
67+
68+
const cartId = transform({ input }, ({ input }) => {
69+
return input.cart_id ?? input.cart?.id
70+
})
71+
6272
const fetchCart = when("should-fetch-cart", { input }, ({ input }) => {
63-
return !input.cart
73+
return shouldExecute
6474
}).then(() => {
6575
return useRemoteQueryStep({
6676
entry_point: "cart",
@@ -76,33 +86,37 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
7686
"payment_collection.currency_code",
7787
"payment_collection.payment_sessions.id",
7888
],
79-
variables: { id: input.cart_id },
89+
variables: { id: cartId },
8090
throw_if_key_not_found: true,
8191
list: false,
8292
})
8393
})
8494

8595
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
86-
return input.cart ?? fetchCart
96+
return fetchCart ?? input.cart
8797
})
8898

8999
const validate = createHook("validate", {
90100
input,
91101
cart,
92102
})
93103

94-
when("should-update-payment-collection", { cart }, ({ cart }) => {
95-
const valueIsEqual = MathBN.eq(
96-
cart.payment_collection?.raw_amount ?? -1,
97-
cart.raw_total
98-
)
104+
when(
105+
"should-update-payment-collection",
106+
{ cart, shouldExecute },
107+
({ cart, shouldExecute }) => {
108+
const valueIsEqual = MathBN.eq(
109+
cart.payment_collection?.raw_amount ?? -1,
110+
cart.raw_total
111+
)
99112

100-
if (valueIsEqual) {
101-
return cart.payment_collection.currency_code !== cart.currency_code
102-
}
113+
if (valueIsEqual) {
114+
return cart.payment_collection.currency_code !== cart.currency_code
115+
}
103116

104-
return true
105-
}).then(() => {
117+
return shouldExecute
118+
}
119+
).then(() => {
106120
const deletePaymentSessionInput = transform(
107121
{ paymentCollection: cart.payment_collection },
108122
(data) => {

0 commit comments

Comments
 (0)