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"
@@ -16,11 +16,15 @@ import { listShippingOptionsForCartWithPricingWorkflow } from "./list-shipping-o
/**
* The details of the cart to refresh.
*/
export type RefreshCartShippingMethodsWorkflowInput = {
export type RefreshCartShippingMethodsWorkflowInput = {
/**
* The cart's ID.
*/
cart_id: string
cart_id?: string
/**
* The Cart reference.
*/
cart?: any
}
export const refreshCartShippingMethodsWorkflowId =
@@ -28,9 +32,9 @@ export const refreshCartShippingMethodsWorkflowId =
/**
* This workflow refreshes a cart's shipping methods, ensuring that their associated shipping options can still be used on the cart,
* and retrieve their correct pricing after a cart update. This workflow is used by the {@link refreshCartItemsWorkflow}.
*
*
* You can use this workflow within your own customizations or custom workflows, allowing you to refresh the cart's shipping method after making updates to the cart.
*
*
* @example
* const { result } = await refreshCartShippingMethodsWorkflow(container)
* .run({
@@ -38,36 +42,44 @@ export const refreshCartShippingMethodsWorkflowId =
* cart_id: "cart_123",
* }
* })
*
*
* @summary
*
*
* Refresh a cart's shipping methods after an update.
*
*
* @property hooks.validate - This hook is executed before all operations. You can consume this hook to perform any custom validation. If validation fails, you can throw an error to stop the workflow execution.
*/
export const refreshCartShippingMethodsWorkflow = createWorkflow(
refreshCartShippingMethodsWorkflowId,
(input: WorkflowData<RefreshCartShippingMethodsWorkflowInput>) => {
const cartQuery = useQueryGraphStep({
entity: "cart",
filters: { id: input.cart_id },
fields: [
"id",
"sales_channel_id",
"currency_code",
"region_id",
"shipping_methods.*",
"shipping_address.city",
"shipping_address.country_code",
"shipping_address.province",
"shipping_methods.shipping_option_id",
"shipping_methods.data",
"total",
],
options: { throwIfKeyNotFound: true },
}).config({ name: "get-cart" })
const fetchCart = when({ input }, ({ input }) => {
return !input.cart
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
fields: [
"id",
"sales_channel_id",
"currency_code",
"region_id",
"shipping_methods.*",
"shipping_address.city",
"shipping_address.country_code",
"shipping_address.province",
"shipping_methods.shipping_option_id",
"shipping_methods.data",
"total",
],
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.
@@ -47,9 +52,9 @@ export const updateCartPromotionsWorkflowId = "update-cart-promotions"
* This workflow updates a cart's promotions, applying or removing promotion codes from the cart. It also computes the adjustments
* that need to be applied to the cart's line items and shipping methods based on the promotions applied. This workflow is used by
* [Add Promotions Store API Route](https://docs.medusajs.com/api/store#carts_postcartsidpromotions).
*
*
* You can use this workflow within your own customizations or custom workflows, allowing you to update a cart's promotions within your custom flows.
*
*
* @example
* const { result } = await updateCartPromotionsWorkflow(container)
* .run({
@@ -60,21 +65,29 @@ export const updateCartPromotionsWorkflowId = "update-cart-promotions"
* action: PromotionActions.ADD,
* }
* })
*
*
* @summary
*
*
* Update a cart's applied promotions to add, replace, or remove them.
*
*
* @property hooks.validate - This hook is executed before all operations. You can consume this hook to perform any custom validation. If validation fails, you can throw an error to stop the workflow execution.
*/
export const updateCartPromotionsWorkflow = createWorkflow(
updateCartPromotionsWorkflowId,
(input: WorkflowData<UpdateCartPromotionsWorkflowInput>) => {
const cart = useRemoteQueryStep({
entry_point: "cart",
fields: cartFieldsForRefreshSteps,
variables: { id: input.cart_id },
list: false,
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", {
@@ -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,12 +68,16 @@ 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
* line items.
*
*
* @privateRemarks
* This doesn't seem to be used?
*/
@@ -81,7 +86,7 @@ export type UpdateTaxLinesWorkflowInput = {
* The shipping methods to update their tax lines.
* If not specified, taxes are updated for all of the cart's
* shipping methods.
*
*
* @privateRemarks
* This doesn't seem to be used?
*/
@@ -90,7 +95,7 @@ export type UpdateTaxLinesWorkflowInput = {
* Whether to force re-calculating tax amounts, which
* may include sending requests to a third-part tax provider, depending
* on the configurations of the cart's tax region.
*
*
* @defaultValue false
*/
force_tax_calculation?: boolean
@@ -100,9 +105,9 @@ export const updateTaxLinesWorkflowId = "update-tax-lines"
/**
* This workflow updates a cart's tax lines that are applied on line items and shipping methods. You can update the line item's quantity, unit price, and more. This workflow is executed
* by the [Calculate Taxes Store API Route](https://docs.medusajs.com/api/store#carts_postcartsidtaxes).
*
*
* You can use this workflow within your own customizations or custom workflows, allowing you to update a cart's tax lines in your custom flows.
*
*
* @example
* const { result } = await updateTaxLinesWorkflow(container)
* .run({
@@ -110,21 +115,28 @@ export const updateTaxLinesWorkflowId = "update-tax-lines"
* cart_id: "cart_123",
* }
* })
*
*
* @summary
*
*
* Update a cart's tax lines.
*/
export const updateTaxLinesWorkflow = createWorkflow(
updateTaxLinesWorkflowId,
(input: WorkflowData<UpdateTaxLinesWorkflowInput>): WorkflowData<void> => {
const cart = useRemoteQueryStep({
entry_point: "cart",
fields: cartFields,
variables: {
id: input.cart_id,
},
list: false,
const fetchCart = when({ input }, ({ input }) => {
return !input.cart
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
fields: cartFields,
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(