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:
1 parent
9b3831d258
commit
2fe68a975b
6 files changed
+50
-35
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/core-flows": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore(): Tweak update payment collection and refresh shipping method workflow execution
|
||||||
@@ -85,16 +85,7 @@ export const getPromotionCodesToApply = createStep(
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const promotionCodesToApply: Set<string> = new Set(
|
const promotionCodesToApply: Set<string> = new Set(adjustmentCodes)
|
||||||
adjustmentCodes.length
|
|
||||||
? (
|
|
||||||
await promotionService.listPromotions(
|
|
||||||
{ code: adjustmentCodes },
|
|
||||||
{ select: ["code"] }
|
|
||||||
)
|
|
||||||
).map((p) => p.code!)
|
|
||||||
: []
|
|
||||||
)
|
|
||||||
|
|
||||||
if (action === PromotionActions.REMOVE) {
|
if (action === PromotionActions.REMOVE) {
|
||||||
promo_codes.forEach((code) => promotionCodesToApply.delete(code))
|
promo_codes.forEach((code) => promotionCodesToApply.delete(code))
|
||||||
|
|||||||
@@ -321,7 +321,7 @@ export const createCartWorkflow = createWorkflow(
|
|||||||
parallelize(
|
parallelize(
|
||||||
refreshPaymentCollectionForCartWorkflow.runAsStep({
|
refreshPaymentCollectionForCartWorkflow.runAsStep({
|
||||||
input: {
|
input: {
|
||||||
cart_id: cart.id,
|
cart: cart,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
emitEventStep({
|
emitEventStep({
|
||||||
|
|||||||
@@ -263,25 +263,15 @@ export const refreshCartItemsWorkflow = createWorkflow(
|
|||||||
list: false,
|
list: false,
|
||||||
}).config({ name: "refetch–cart" })
|
}).config({ name: "refetch–cart" })
|
||||||
|
|
||||||
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({
|
refreshCartShippingMethodsWorkflow.runAsStep({
|
||||||
input: refreshCartInput,
|
input: { cart: refetchedCart },
|
||||||
})
|
})
|
||||||
|
|
||||||
when("force-refresh-update-tax-lines", { input }, ({ input }) => {
|
when("force-refresh-update-tax-lines", { input }, ({ input }) => {
|
||||||
return !!input.force_refresh
|
return !!input.force_refresh
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
updateTaxLinesWorkflow.runAsStep({
|
updateTaxLinesWorkflow.runAsStep({
|
||||||
input: refreshCartInput,
|
input: { cart_id: input.cart_id },
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -331,7 +321,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
|
|||||||
)
|
)
|
||||||
|
|
||||||
refreshPaymentCollectionForCartWorkflow.runAsStep({
|
refreshPaymentCollectionForCartWorkflow.runAsStep({
|
||||||
input: { cart_id: input.cart_id },
|
input: { cart: refetchedCart },
|
||||||
})
|
})
|
||||||
|
|
||||||
return new WorkflowResponse(refetchedCart, {
|
return new WorkflowResponse(refetchedCart, {
|
||||||
|
|||||||
@@ -55,9 +55,24 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
|
|||||||
idempotent: false,
|
idempotent: false,
|
||||||
},
|
},
|
||||||
(input: WorkflowData<RefreshCartShippingMethodsWorkflowInput>) => {
|
(input: WorkflowData<RefreshCartShippingMethodsWorkflowInput>) => {
|
||||||
const fetchCart = when("fetch-cart", { input }, ({ input }) => {
|
const shouldExecute = transform({ input }, ({ input }) => {
|
||||||
return !input.cart
|
return (
|
||||||
}).then(() => {
|
!!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({
|
return useRemoteQueryStep({
|
||||||
entry_point: "cart",
|
entry_point: "cart",
|
||||||
fields: [
|
fields: [
|
||||||
@@ -73,14 +88,14 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
|
|||||||
"shipping_methods.data",
|
"shipping_methods.data",
|
||||||
"total",
|
"total",
|
||||||
],
|
],
|
||||||
variables: { id: input.cart_id },
|
variables: { id: cartId },
|
||||||
throw_if_key_not_found: true,
|
throw_if_key_not_found: true,
|
||||||
list: false,
|
list: false,
|
||||||
}).config({ name: "get-cart" })
|
}).config({ name: "get-cart" })
|
||||||
})
|
})
|
||||||
|
|
||||||
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
|
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
|
||||||
return input.cart ?? fetchCart
|
return fetchCart ?? input.cart
|
||||||
})
|
})
|
||||||
|
|
||||||
const listShippingOptionsInput = transform({ cart }, ({ cart }) =>
|
const listShippingOptionsInput = transform({ cart }, ({ cart }) =>
|
||||||
|
|||||||
@@ -59,8 +59,18 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
|||||||
idempotent: false,
|
idempotent: false,
|
||||||
},
|
},
|
||||||
(input: WorkflowData<RefreshPaymentCollectionForCartWorklowInput>) => {
|
(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 }) => {
|
const fetchCart = when("should-fetch-cart", { input }, ({ input }) => {
|
||||||
return !input.cart
|
return shouldExecute
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
return useRemoteQueryStep({
|
return useRemoteQueryStep({
|
||||||
entry_point: "cart",
|
entry_point: "cart",
|
||||||
@@ -76,14 +86,14 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
|||||||
"payment_collection.currency_code",
|
"payment_collection.currency_code",
|
||||||
"payment_collection.payment_sessions.id",
|
"payment_collection.payment_sessions.id",
|
||||||
],
|
],
|
||||||
variables: { id: input.cart_id },
|
variables: { id: cartId },
|
||||||
throw_if_key_not_found: true,
|
throw_if_key_not_found: true,
|
||||||
list: false,
|
list: false,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
|
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
|
||||||
return input.cart ?? fetchCart
|
return fetchCart ?? input.cart
|
||||||
})
|
})
|
||||||
|
|
||||||
const validate = createHook("validate", {
|
const validate = createHook("validate", {
|
||||||
@@ -91,7 +101,10 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
|||||||
cart,
|
cart,
|
||||||
})
|
})
|
||||||
|
|
||||||
when("should-update-payment-collection", { cart }, ({ cart }) => {
|
when(
|
||||||
|
"should-update-payment-collection",
|
||||||
|
{ cart, shouldExecute },
|
||||||
|
({ cart, shouldExecute }) => {
|
||||||
const valueIsEqual = MathBN.eq(
|
const valueIsEqual = MathBN.eq(
|
||||||
cart.payment_collection?.raw_amount ?? -1,
|
cart.payment_collection?.raw_amount ?? -1,
|
||||||
cart.raw_total
|
cart.raw_total
|
||||||
@@ -101,8 +114,9 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
|
|||||||
return cart.payment_collection.currency_code !== cart.currency_code
|
return cart.payment_collection.currency_code !== cart.currency_code
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return shouldExecute
|
||||||
}).then(() => {
|
}
|
||||||
|
).then(() => {
|
||||||
const deletePaymentSessionInput = transform(
|
const deletePaymentSessionInput = transform(
|
||||||
{ paymentCollection: cart.payment_collection },
|
{ paymentCollection: cart.payment_collection },
|
||||||
(data) => {
|
(data) => {
|
||||||
|
|||||||
Reference in new issue
Block a user