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" }) }).config({ name: "refetchcart" })
refreshCartShippingMethodsWorkflow.runAsStep({ refreshCartShippingMethodsWorkflow.runAsStep({
input: { cart_id: input.cart_id }, input: { cart: refetchedCart },
}) })
updateTaxLinesWorkflow.runAsStep({ updateTaxLinesWorkflow.runAsStep({
input: { cart_id: input.cart_id }, input: { cart: refetchedCart },
}) })
const cartPromoCodes = transform( const cartPromoCodes = transform(
@@ -176,7 +176,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
}) })
refreshPaymentCollectionForCartWorkflow.runAsStep({ refreshPaymentCollectionForCartWorkflow.runAsStep({
input: { cart: refetchedCart }, input: { cart_id: input.cart_id },
}) })
return new WorkflowResponse(refetchedCart) return new WorkflowResponse(refetchedCart)
@@ -8,7 +8,7 @@ import {
WorkflowData, WorkflowData,
WorkflowResponse, WorkflowResponse,
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { useQueryGraphStep } from "../../common" import { useRemoteQueryStep } from "../../common"
import { removeShippingMethodFromCartStep } from "../steps" import { removeShippingMethodFromCartStep } from "../steps"
import { updateShippingMethodsStep } from "../steps/update-shipping-methods" import { updateShippingMethodsStep } from "../steps/update-shipping-methods"
import { listShippingOptionsForCartWithPricingWorkflow } from "./list-shipping-options-for-cart-with-pricing" import { listShippingOptionsForCartWithPricingWorkflow } from "./list-shipping-options-for-cart-with-pricing"
@@ -20,7 +20,11 @@ export type RefreshCartShippingMethodsWorkflowInput = {
/** /**
* The cart's ID. * The cart's ID.
*/ */
cart_id: string cart_id?: string
/**
* The Cart reference.
*/
cart?: any
} }
export const refreshCartShippingMethodsWorkflowId = export const refreshCartShippingMethodsWorkflowId =
@@ -48,26 +52,34 @@ export const refreshCartShippingMethodsWorkflowId =
export const refreshCartShippingMethodsWorkflow = createWorkflow( export const refreshCartShippingMethodsWorkflow = createWorkflow(
refreshCartShippingMethodsWorkflowId, refreshCartShippingMethodsWorkflowId,
(input: WorkflowData<RefreshCartShippingMethodsWorkflowInput>) => { (input: WorkflowData<RefreshCartShippingMethodsWorkflowInput>) => {
const cartQuery = useQueryGraphStep({ const fetchCart = when({ input }, ({ input }) => {
entity: "cart", return !input.cart
filters: { id: input.cart_id }, }).then(() => {
fields: [ return useRemoteQueryStep({
"id", entry_point: "cart",
"sales_channel_id", fields: [
"currency_code", "id",
"region_id", "sales_channel_id",
"shipping_methods.*", "currency_code",
"shipping_address.city", "region_id",
"shipping_address.country_code", "shipping_methods.*",
"shipping_address.province", "shipping_address.city",
"shipping_methods.shipping_option_id", "shipping_address.country_code",
"shipping_methods.data", "shipping_address.province",
"total", "shipping_methods.shipping_option_id",
], "shipping_methods.data",
options: { throwIfKeyNotFound: true }, "total",
}).config({ name: "get-cart" }) ],
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 }) => const listShippingOptionsInput = transform({ cart }, ({ cart }) =>
(cart.shipping_methods || []) (cart.shipping_methods || [])
.map((shippingMethod) => ({ .map((shippingMethod) => ({
@@ -4,6 +4,7 @@ import {
createWorkflow, createWorkflow,
parallelize, parallelize,
transform, transform,
when,
WorkflowData, WorkflowData,
WorkflowResponse, WorkflowResponse,
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
@@ -27,7 +28,11 @@ export type UpdateCartPromotionsWorkflowInput = {
/** /**
* The cart's ID. * 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, * The promotion codes to add to the cart, remove from the cart,
* or replace all existing promotions in the cart. * or replace all existing promotions in the cart.
@@ -70,11 +75,19 @@ export const updateCartPromotionsWorkflowId = "update-cart-promotions"
export const updateCartPromotionsWorkflow = createWorkflow( export const updateCartPromotionsWorkflow = createWorkflow(
updateCartPromotionsWorkflowId, updateCartPromotionsWorkflowId,
(input: WorkflowData<UpdateCartPromotionsWorkflowInput>) => { (input: WorkflowData<UpdateCartPromotionsWorkflowInput>) => {
const cart = useRemoteQueryStep({ const fetchCart = when({ input }, ({ input }) => {
entry_point: "cart", return !input.cart
fields: cartFieldsForRefreshSteps, }).then(() => {
variables: { id: input.cart_id }, return useRemoteQueryStep({
list: false, 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", { const validate = createHook("validate", {
@@ -119,7 +132,7 @@ export const updateCartPromotionsWorkflow = createWorkflow(
shippingMethodAdjustmentsToCreate, shippingMethodAdjustmentsToCreate,
}), }),
updateCartPromotionsStep({ updateCartPromotionsStep({
id: input.cart_id, id: cart.id,
promo_codes: computedPromotionCodes, promo_codes: computedPromotionCodes,
action: PromotionActions.REPLACE, action: PromotionActions.REPLACE,
}) })
@@ -6,6 +6,7 @@ import {
WorkflowData, WorkflowData,
createWorkflow, createWorkflow,
transform, transform,
when,
} from "@medusajs/framework/workflows-sdk" } from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common" import { useRemoteQueryStep } from "../../common"
import { getItemTaxLinesStep } from "../../tax/steps/get-item-tax-lines" import { getItemTaxLinesStep } from "../../tax/steps/get-item-tax-lines"
@@ -67,7 +68,11 @@ export type UpdateTaxLinesWorkflowInput = {
/** /**
* The cart's ID. * The cart's ID.
*/ */
cart_id: string cart_id?: string
/**
* The Cart reference.
*/
cart?: any
/** /**
* The items to update their tax lines. * The items to update their tax lines.
* If not specified, taxes are updated for all of the cart's * If not specified, taxes are updated for all of the cart's
@@ -118,13 +123,20 @@ export const updateTaxLinesWorkflowId = "update-tax-lines"
export const updateTaxLinesWorkflow = createWorkflow( export const updateTaxLinesWorkflow = createWorkflow(
updateTaxLinesWorkflowId, updateTaxLinesWorkflowId,
(input: WorkflowData<UpdateTaxLinesWorkflowInput>): WorkflowData<void> => { (input: WorkflowData<UpdateTaxLinesWorkflowInput>): WorkflowData<void> => {
const cart = useRemoteQueryStep({ const fetchCart = when({ input }, ({ input }) => {
entry_point: "cart", return !input.cart
fields: cartFields, }).then(() => {
variables: { return useRemoteQueryStep({
id: input.cart_id, entry_point: "cart",
}, fields: cartFields,
list: false, 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( const taxLineItems = getItemTaxLinesStep(