Skip to content

Commit 322d108

Browse files
chore(core-flows): pass cart as reference to subflows (#11617)
1 parent e8c953d commit 322d108

File tree

5 files changed

+100
-58
lines changed

5 files changed

+100
-58
lines changed

.changeset/eight-buckets-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/core-flows": patch
3+
---
4+
5+
chore(core-flows): pass cart as reference to subflows

packages/core/core-flows/src/cart/workflows/refresh-cart-items.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ export const refreshCartItemsWorkflow = createWorkflow(
149149
}).config({ name: "refetch–cart" })
150150

151151
refreshCartShippingMethodsWorkflow.runAsStep({
152-
input: { cart_id: input.cart_id },
152+
input: { cart: refetchedCart },
153153
})
154154

155155
updateTaxLinesWorkflow.runAsStep({
156-
input: { cart_id: input.cart_id },
156+
input: { cart: refetchedCart },
157157
})
158158

159159
const cartPromoCodes = transform(
@@ -176,7 +176,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
176176
})
177177

178178
refreshPaymentCollectionForCartWorkflow.runAsStep({
179-
input: { cart: refetchedCart },
179+
input: { cart_id: input.cart_id },
180180
})
181181

182182
return new WorkflowResponse(refetchedCart)

packages/core/core-flows/src/cart/workflows/refresh-cart-shipping-methods.ts

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,78 @@ import {
88
WorkflowData,
99
WorkflowResponse,
1010
} from "@medusajs/framework/workflows-sdk"
11-
import { useQueryGraphStep } from "../../common"
11+
import { useRemoteQueryStep } from "../../common"
1212
import { removeShippingMethodFromCartStep } from "../steps"
1313
import { updateShippingMethodsStep } from "../steps/update-shipping-methods"
1414
import { listShippingOptionsForCartWithPricingWorkflow } from "./list-shipping-options-for-cart-with-pricing"
1515

1616
/**
1717
* The details of the cart to refresh.
1818
*/
19-
export type RefreshCartShippingMethodsWorkflowInput = {
19+
export type RefreshCartShippingMethodsWorkflowInput = {
2020
/**
2121
* The cart's ID.
2222
*/
23-
cart_id: string
23+
cart_id?: string
24+
/**
25+
* The Cart reference.
26+
*/
27+
cart?: any
2428
}
2529

2630
export const refreshCartShippingMethodsWorkflowId =
2731
"refresh-cart-shipping-methods"
2832
/**
2933
* This workflow refreshes a cart's shipping methods, ensuring that their associated shipping options can still be used on the cart,
3034
* and retrieve their correct pricing after a cart update. This workflow is used by the {@link refreshCartItemsWorkflow}.
31-
*
35+
*
3236
* 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.
33-
*
37+
*
3438
* @example
3539
* const { result } = await refreshCartShippingMethodsWorkflow(container)
3640
* .run({
3741
* input: {
3842
* cart_id: "cart_123",
3943
* }
4044
* })
41-
*
45+
*
4246
* @summary
43-
*
47+
*
4448
* Refresh a cart's shipping methods after an update.
45-
*
49+
*
4650
* @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.
4751
*/
4852
export const refreshCartShippingMethodsWorkflow = createWorkflow(
4953
refreshCartShippingMethodsWorkflowId,
5054
(input: WorkflowData<RefreshCartShippingMethodsWorkflowInput>) => {
51-
const cartQuery = useQueryGraphStep({
52-
entity: "cart",
53-
filters: { id: input.cart_id },
54-
fields: [
55-
"id",
56-
"sales_channel_id",
57-
"currency_code",
58-
"region_id",
59-
"shipping_methods.*",
60-
"shipping_address.city",
61-
"shipping_address.country_code",
62-
"shipping_address.province",
63-
"shipping_methods.shipping_option_id",
64-
"shipping_methods.data",
65-
"total",
66-
],
67-
options: { throwIfKeyNotFound: true },
68-
}).config({ name: "get-cart" })
69-
70-
const cart = transform({ cartQuery }, ({ cartQuery }) => cartQuery.data[0])
55+
const fetchCart = when({ input }, ({ input }) => {
56+
return !input.cart
57+
}).then(() => {
58+
return useRemoteQueryStep({
59+
entry_point: "cart",
60+
fields: [
61+
"id",
62+
"sales_channel_id",
63+
"currency_code",
64+
"region_id",
65+
"shipping_methods.*",
66+
"shipping_address.city",
67+
"shipping_address.country_code",
68+
"shipping_address.province",
69+
"shipping_methods.shipping_option_id",
70+
"shipping_methods.data",
71+
"total",
72+
],
73+
variables: { id: input.cart_id },
74+
throw_if_key_not_found: true,
75+
list: false,
76+
}).config({ name: "get-cart" })
77+
})
78+
79+
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
80+
return input.cart ?? fetchCart
81+
})
82+
7183
const listShippingOptionsInput = transform({ cart }, ({ cart }) =>
7284
(cart.shipping_methods || [])
7385
.map((shippingMethod) => ({

packages/core/core-flows/src/cart/workflows/update-cart-promotions.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createWorkflow,
55
parallelize,
66
transform,
7+
when,
78
WorkflowData,
89
WorkflowResponse,
910
} from "@medusajs/framework/workflows-sdk"
@@ -27,7 +28,11 @@ export type UpdateCartPromotionsWorkflowInput = {
2728
/**
2829
* The cart's ID.
2930
*/
30-
cart_id: string
31+
cart_id?: string
32+
/**
33+
* The Cart reference.
34+
*/
35+
cart?: any
3136
/**
3237
* The promotion codes to add to the cart, remove from the cart,
3338
* or replace all existing promotions in the cart.
@@ -47,9 +52,9 @@ export const updateCartPromotionsWorkflowId = "update-cart-promotions"
4752
* This workflow updates a cart's promotions, applying or removing promotion codes from the cart. It also computes the adjustments
4853
* that need to be applied to the cart's line items and shipping methods based on the promotions applied. This workflow is used by
4954
* [Add Promotions Store API Route](https://docs.medusajs.com/api/store#carts_postcartsidpromotions).
50-
*
55+
*
5156
* You can use this workflow within your own customizations or custom workflows, allowing you to update a cart's promotions within your custom flows.
52-
*
57+
*
5358
* @example
5459
* const { result } = await updateCartPromotionsWorkflow(container)
5560
* .run({
@@ -60,21 +65,29 @@ export const updateCartPromotionsWorkflowId = "update-cart-promotions"
6065
* action: PromotionActions.ADD,
6166
* }
6267
* })
63-
*
68+
*
6469
* @summary
65-
*
70+
*
6671
* Update a cart's applied promotions to add, replace, or remove them.
67-
*
72+
*
6873
* @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.
6974
*/
7075
export const updateCartPromotionsWorkflow = createWorkflow(
7176
updateCartPromotionsWorkflowId,
7277
(input: WorkflowData<UpdateCartPromotionsWorkflowInput>) => {
73-
const cart = useRemoteQueryStep({
74-
entry_point: "cart",
75-
fields: cartFieldsForRefreshSteps,
76-
variables: { id: input.cart_id },
77-
list: false,
78+
const fetchCart = when({ input }, ({ input }) => {
79+
return !input.cart
80+
}).then(() => {
81+
return useRemoteQueryStep({
82+
entry_point: "cart",
83+
fields: cartFieldsForRefreshSteps,
84+
variables: { id: input.cart_id },
85+
list: false,
86+
})
87+
})
88+
89+
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
90+
return input.cart ?? fetchCart
7891
})
7992

8093
const validate = createHook("validate", {
@@ -119,7 +132,7 @@ export const updateCartPromotionsWorkflow = createWorkflow(
119132
shippingMethodAdjustmentsToCreate,
120133
}),
121134
updateCartPromotionsStep({
122-
id: input.cart_id,
135+
id: cart.id,
123136
promo_codes: computedPromotionCodes,
124137
action: PromotionActions.REPLACE,
125138
})

packages/core/core-flows/src/cart/workflows/update-tax-lines.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
WorkflowData,
77
createWorkflow,
88
transform,
9+
when,
910
} from "@medusajs/framework/workflows-sdk"
1011
import { useRemoteQueryStep } from "../../common"
1112
import { getItemTaxLinesStep } from "../../tax/steps/get-item-tax-lines"
@@ -67,12 +68,16 @@ export type UpdateTaxLinesWorkflowInput = {
6768
/**
6869
* The cart's ID.
6970
*/
70-
cart_id: string
71+
cart_id?: string
72+
/**
73+
* The Cart reference.
74+
*/
75+
cart?: any
7176
/**
7277
* The items to update their tax lines.
7378
* If not specified, taxes are updated for all of the cart's
7479
* line items.
75-
*
80+
*
7681
* @privateRemarks
7782
* This doesn't seem to be used?
7883
*/
@@ -81,7 +86,7 @@ export type UpdateTaxLinesWorkflowInput = {
8186
* The shipping methods to update their tax lines.
8287
* If not specified, taxes are updated for all of the cart's
8388
* shipping methods.
84-
*
89+
*
8590
* @privateRemarks
8691
* This doesn't seem to be used?
8792
*/
@@ -90,7 +95,7 @@ export type UpdateTaxLinesWorkflowInput = {
9095
* Whether to force re-calculating tax amounts, which
9196
* may include sending requests to a third-part tax provider, depending
9297
* on the configurations of the cart's tax region.
93-
*
98+
*
9499
* @defaultValue false
95100
*/
96101
force_tax_calculation?: boolean
@@ -100,31 +105,38 @@ export const updateTaxLinesWorkflowId = "update-tax-lines"
100105
/**
101106
* 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
102107
* by the [Calculate Taxes Store API Route](https://docs.medusajs.com/api/store#carts_postcartsidtaxes).
103-
*
108+
*
104109
* 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.
105-
*
110+
*
106111
* @example
107112
* const { result } = await updateTaxLinesWorkflow(container)
108113
* .run({
109114
* input: {
110115
* cart_id: "cart_123",
111116
* }
112117
* })
113-
*
118+
*
114119
* @summary
115-
*
120+
*
116121
* Update a cart's tax lines.
117122
*/
118123
export const updateTaxLinesWorkflow = createWorkflow(
119124
updateTaxLinesWorkflowId,
120125
(input: WorkflowData<UpdateTaxLinesWorkflowInput>): WorkflowData<void> => {
121-
const cart = useRemoteQueryStep({
122-
entry_point: "cart",
123-
fields: cartFields,
124-
variables: {
125-
id: input.cart_id,
126-
},
127-
list: false,
126+
const fetchCart = when({ input }, ({ input }) => {
127+
return !input.cart
128+
}).then(() => {
129+
return useRemoteQueryStep({
130+
entry_point: "cart",
131+
fields: cartFields,
132+
variables: { id: input.cart_id },
133+
throw_if_key_not_found: true,
134+
list: false,
135+
})
136+
})
137+
138+
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {
139+
return input.cart ?? fetchCart
128140
})
129141

130142
const taxLineItems = getItemTaxLinesStep(

0 commit comments

Comments
 (0)