chore(core-flows): pass cart as reference to subflows (#11617)

This commit is contained in:
Carlos R. L. Rodrigues
2025-02-26 08:26:44 -03:00
committed by GitHub
parent e8c953de25
commit 322d108c03
5 changed files with 99 additions and 57 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---
chore(core-flows): pass cart as reference to subflows
@@ -149,11 +149,11 @@ export const refreshCartItemsWorkflow = createWorkflow(
}).config({ name: "refetchcart" })
refreshCartShippingMethodsWorkflow.runAsStep({
input: { cart_id: input.cart_id },
input: { cart: refetchedCart },
})
updateTaxLinesWorkflow.runAsStep({
input: { cart_id: input.cart_id },
input: { cart: refetchedCart },
})
const cartPromoCodes = transform(
@@ -176,7 +176,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
})
refreshPaymentCollectionForCartWorkflow.runAsStep({
input: { cart: refetchedCart },
input: { cart_id: input.cart_id },
})
return new WorkflowResponse(refetchedCart)
@@ -8,7 +8,7 @@ import {
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { useQueryGraphStep } from "../../common"
import { useRemoteQueryStep } from "../../common"
import { removeShippingMethodFromCartStep } from "../steps"
import { updateShippingMethodsStep } from "../steps/update-shipping-methods"
import { listShippingOptionsForCartWithPricingWorkflow } from "./list-shipping-options-for-cart-with-pricing"
@@ -20,7 +20,11 @@ export type RefreshCartShippingMethodsWorkflowInput = {
/**
* The cart's ID.
*/
cart_id: string
cart_id?: string
/**
* The Cart reference.
*/
cart?: any
}
export const refreshCartShippingMethodsWorkflowId =
@@ -48,9 +52,11 @@ export const refreshCartShippingMethodsWorkflowId =
export const refreshCartShippingMethodsWorkflow = createWorkflow(
refreshCartShippingMethodsWorkflowId,
(input: WorkflowData<RefreshCartShippingMethodsWorkflowInput>) => {
const cartQuery = useQueryGraphStep({
entity: "cart",
filters: { id: input.cart_id },
const fetchCart = when({ input }, ({ input }) => {
return !input.cart
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
fields: [
"id",
"sales_channel_id",
@@ -64,10 +70,16 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
"shipping_methods.data",
"total",
],
options: { throwIfKeyNotFound: true },
variables: { id: input.cart_id },
throw_if_key_not_found: true,
list: false,
}).config({ name: "get-cart" })
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
return input.cart ?? fetchCart
})
const cart = transform({ cartQuery }, ({ cartQuery }) => cartQuery.data[0])
const listShippingOptionsInput = transform({ cart }, ({ cart }) =>
(cart.shipping_methods || [])
.map((shippingMethod) => ({
@@ -4,6 +4,7 @@ import {
createWorkflow,
parallelize,
transform,
when,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
@@ -27,7 +28,11 @@ export type UpdateCartPromotionsWorkflowInput = {
/**
* The cart's ID.
*/
cart_id: string
cart_id?: string
/**
* The Cart reference.
*/
cart?: any
/**
* The promotion codes to add to the cart, remove from the cart,
* or replace all existing promotions in the cart.
@@ -70,12 +75,20 @@ export const updateCartPromotionsWorkflowId = "update-cart-promotions"
export const updateCartPromotionsWorkflow = createWorkflow(
updateCartPromotionsWorkflowId,
(input: WorkflowData<UpdateCartPromotionsWorkflowInput>) => {
const cart = useRemoteQueryStep({
const fetchCart = when({ input }, ({ input }) => {
return !input.cart
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
fields: cartFieldsForRefreshSteps,
variables: { id: input.cart_id },
list: false,
})
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
return input.cart ?? fetchCart
})
const validate = createHook("validate", {
input,
@@ -119,7 +132,7 @@ export const updateCartPromotionsWorkflow = createWorkflow(
shippingMethodAdjustmentsToCreate,
}),
updateCartPromotionsStep({
id: input.cart_id,
id: cart.id,
promo_codes: computedPromotionCodes,
action: PromotionActions.REPLACE,
})
@@ -6,6 +6,7 @@ import {
WorkflowData,
createWorkflow,
transform,
when,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { getItemTaxLinesStep } from "../../tax/steps/get-item-tax-lines"
@@ -67,7 +68,11 @@ export type UpdateTaxLinesWorkflowInput = {
/**
* The cart's ID.
*/
cart_id: string
cart_id?: string
/**
* The Cart reference.
*/
cart?: any
/**
* The items to update their tax lines.
* If not specified, taxes are updated for all of the cart's
@@ -118,14 +123,21 @@ export const updateTaxLinesWorkflowId = "update-tax-lines"
export const updateTaxLinesWorkflow = createWorkflow(
updateTaxLinesWorkflowId,
(input: WorkflowData<UpdateTaxLinesWorkflowInput>): WorkflowData<void> => {
const cart = useRemoteQueryStep({
const fetchCart = when({ input }, ({ input }) => {
return !input.cart
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
fields: cartFields,
variables: {
id: input.cart_id,
},
variables: { id: input.cart_id },
throw_if_key_not_found: true,
list: false,
})
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
return input.cart ?? fetchCart
})
const taxLineItems = getItemTaxLinesStep(
transform({ input, cart }, (data) => ({