chore(): Improve some cart operation flows to remove extraneous operations when not required (#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
This commit is contained in:
Adrien de Peretti
2025-09-07 14:52:36 +02:00
committed by GitHub
parent 9b3831d258
commit 2fe68a975b
6 changed files with 57 additions and 42 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---
chore(): Tweak update payment collection and refresh shipping method workflow execution

View File

@@ -85,16 +85,7 @@ export const getPromotionCodesToApply = createStep(
})
})
const promotionCodesToApply: Set<string> = new Set(
adjustmentCodes.length
? (
await promotionService.listPromotions(
{ code: adjustmentCodes },
{ select: ["code"] }
)
).map((p) => p.code!)
: []
)
const promotionCodesToApply: Set<string> = new Set(adjustmentCodes)
if (action === PromotionActions.REMOVE) {
promo_codes.forEach((code) => promotionCodesToApply.delete(code))

View File

@@ -321,7 +321,7 @@ export const createCartWorkflow = createWorkflow(
parallelize(
refreshPaymentCollectionForCartWorkflow.runAsStep({
input: {
cart_id: cart.id,
cart: cart,
},
}),
emitEventStep({

View File

@@ -263,25 +263,15 @@ export const refreshCartItemsWorkflow = createWorkflow(
list: false,
}).config({ name: "refetchcart" })
const refreshCartInput = transform(
{ refetchedCart, input },
({ refetchedCart, input }) => {
return {
cart: !input.force_refresh ? refetchedCart : undefined,
cart_id: !!input.force_refresh ? input.cart_id : undefined,
}
}
)
refreshCartShippingMethodsWorkflow.runAsStep({
input: refreshCartInput,
input: { cart: refetchedCart },
})
when("force-refresh-update-tax-lines", { input }, ({ input }) => {
return !!input.force_refresh
}).then(() => {
updateTaxLinesWorkflow.runAsStep({
input: refreshCartInput,
input: { cart_id: input.cart_id },
})
})
@@ -331,7 +321,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
)
refreshPaymentCollectionForCartWorkflow.runAsStep({
input: { cart_id: input.cart_id },
input: { cart: refetchedCart },
})
return new WorkflowResponse(refetchedCart, {

View File

@@ -55,9 +55,24 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
idempotent: false,
},
(input: WorkflowData<RefreshCartShippingMethodsWorkflowInput>) => {
const fetchCart = when("fetch-cart", { input }, ({ input }) => {
return !input.cart
}).then(() => {
const shouldExecute = transform({ input }, ({ input }) => {
return (
!!input.cart_id ||
(!!input.cart && !!input.cart.shipping_methods?.length)
)
})
const cartId = transform({ input }, ({ input }) => {
return input.cart_id ?? input.cart?.id
})
const fetchCart = when(
"fetch-cart",
{ shouldExecute },
({ shouldExecute }) => {
return shouldExecute
}
).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
fields: [
@@ -73,14 +88,14 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
"shipping_methods.data",
"total",
],
variables: { id: input.cart_id },
variables: { id: cartId },
throw_if_key_not_found: true,
list: false,
}).config({ name: "get-cart" })
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
return input.cart ?? fetchCart
return fetchCart ?? input.cart
})
const listShippingOptionsInput = transform({ cart }, ({ cart }) =>

View File

@@ -59,8 +59,18 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
idempotent: false,
},
(input: WorkflowData<RefreshPaymentCollectionForCartWorklowInput>) => {
const shouldExecute = transform({ input }, ({ input }) => {
return (
!!input.cart_id || (!!input.cart && !!input.cart.payment_collection)
)
})
const cartId = transform({ input }, ({ input }) => {
return input.cart_id ?? input.cart?.id
})
const fetchCart = when("should-fetch-cart", { input }, ({ input }) => {
return !input.cart
return shouldExecute
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
@@ -76,14 +86,14 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
"payment_collection.currency_code",
"payment_collection.payment_sessions.id",
],
variables: { id: input.cart_id },
variables: { id: cartId },
throw_if_key_not_found: true,
list: false,
})
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
return input.cart ?? fetchCart
return fetchCart ?? input.cart
})
const validate = createHook("validate", {
@@ -91,18 +101,22 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
cart,
})
when("should-update-payment-collection", { cart }, ({ cart }) => {
const valueIsEqual = MathBN.eq(
cart.payment_collection?.raw_amount ?? -1,
cart.raw_total
)
when(
"should-update-payment-collection",
{ cart, shouldExecute },
({ cart, shouldExecute }) => {
const valueIsEqual = MathBN.eq(
cart.payment_collection?.raw_amount ?? -1,
cart.raw_total
)
if (valueIsEqual) {
return cart.payment_collection.currency_code !== cart.currency_code
if (valueIsEqual) {
return cart.payment_collection.currency_code !== cart.currency_code
}
return shouldExecute
}
return true
}).then(() => {
).then(() => {
const deletePaymentSessionInput = transform(
{ paymentCollection: cart.payment_collection },
(data) => {