Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-buckets-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---

chore(core-flows): pass cart as reference to subflows
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ export const refreshCartItemsWorkflow = createWorkflow(
}).config({ name: "refetch–cart" })

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(
Expand All @@ -176,7 +176,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
})

refreshPaymentCollectionForCartWorkflow.runAsStep({
input: { cart: refetchedCart },
input: { cart_id: input.cart_id },
})

return new WorkflowResponse(refetchedCart)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,78 @@ 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"

/**
* 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 =
"refresh-cart-shipping-methods"
/**
* 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({
* input: {
* 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 cart = transform({ cartQuery }, ({ cartQuery }) => cartQuery.data[0])
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 listShippingOptionsInput = transform({ cart }, ({ cart }) =>
(cart.shipping_methods || [])
.map((shippingMethod) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createWorkflow,
parallelize,
transform,
when,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
Expand All @@ -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.
Expand All @@ -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({
Expand All @@ -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", {
Expand Down Expand Up @@ -119,7 +132,7 @@ export const updateCartPromotionsWorkflow = createWorkflow(
shippingMethodAdjustmentsToCreate,
}),
updateCartPromotionsStep({
id: input.cart_id,
id: cart.id,
promo_codes: computedPromotionCodes,
action: PromotionActions.REPLACE,
})
Expand Down
42 changes: 27 additions & 15 deletions packages/core/core-flows/src/cart/workflows/update-tax-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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?
*/
Expand All @@ -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?
*/
Expand All @@ -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
Expand All @@ -100,31 +105,38 @@ 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({
* input: {
* 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(
Expand Down
Loading