From 322d108c0395757b4655a1e0055d722acf127b36 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Wed, 26 Feb 2025 08:26:44 -0300 Subject: [PATCH] chore(core-flows): pass cart as reference to subflows (#11617) --- .changeset/eight-buckets-think.md | 5 ++ .../src/cart/workflows/refresh-cart-items.ts | 6 +- .../refresh-cart-shipping-methods.ts | 66 +++++++++++-------- .../cart/workflows/update-cart-promotions.ts | 37 +++++++---- .../src/cart/workflows/update-tax-lines.ts | 42 +++++++----- 5 files changed, 99 insertions(+), 57 deletions(-) create mode 100644 .changeset/eight-buckets-think.md diff --git a/.changeset/eight-buckets-think.md b/.changeset/eight-buckets-think.md new file mode 100644 index 0000000000..456a03a078 --- /dev/null +++ b/.changeset/eight-buckets-think.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +chore(core-flows): pass cart as reference to subflows diff --git a/packages/core/core-flows/src/cart/workflows/refresh-cart-items.ts b/packages/core/core-flows/src/cart/workflows/refresh-cart-items.ts index 807dea450d..8c4cb2b27f 100644 --- a/packages/core/core-flows/src/cart/workflows/refresh-cart-items.ts +++ b/packages/core/core-flows/src/cart/workflows/refresh-cart-items.ts @@ -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( @@ -176,7 +176,7 @@ export const refreshCartItemsWorkflow = createWorkflow( }) refreshPaymentCollectionForCartWorkflow.runAsStep({ - input: { cart: refetchedCart }, + input: { cart_id: input.cart_id }, }) return new WorkflowResponse(refetchedCart) diff --git a/packages/core/core-flows/src/cart/workflows/refresh-cart-shipping-methods.ts b/packages/core/core-flows/src/cart/workflows/refresh-cart-shipping-methods.ts index cd43f3945f..74c977b6b7 100644 --- a/packages/core/core-flows/src/cart/workflows/refresh-cart-shipping-methods.ts +++ b/packages/core/core-flows/src/cart/workflows/refresh-cart-shipping-methods.ts @@ -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) => { - 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) => ({ diff --git a/packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts b/packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts index 8eb5ca3f97..b5d4cf13f9 100644 --- a/packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts +++ b/packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts @@ -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) => { - 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, }) diff --git a/packages/core/core-flows/src/cart/workflows/update-tax-lines.ts b/packages/core/core-flows/src/cart/workflows/update-tax-lines.ts index da289507b0..5b4c3f6ebe 100644 --- a/packages/core/core-flows/src/cart/workflows/update-tax-lines.ts +++ b/packages/core/core-flows/src/cart/workflows/update-tax-lines.ts @@ -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): WorkflowData => { - 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(