Skip to content

Commit c54c5ed

Browse files
adrien2polivermrbl
andauthored
chore(): improve cart operations + Mikro orm 6.4.16 (#13712)
* chore(): Mikro orm 6.4.16 * Create small-ghosts-draw.md * update config * update config * fix delete * update config * update workflows * order improvements * test pricing quuery * test pricing quuery * configurable connection options * configurable connection options * configurable connection options * Update packages/modules/pricing/src/models/price.ts Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> --------- Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
1 parent 76bf364 commit c54c5ed

23 files changed

Lines changed: 478 additions & 285 deletions

.changeset/small-ghosts-draw.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@medusajs/deps": patch
3+
"@medusajs/utils": patch
4+
"@medusajs/cart": patch
5+
"@medusajs/pricing": patch
6+
"@medusajs/core-flows": patch
7+
---
8+
9+
chore(): improve cart operations + Mikro orm 6.4.16

integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4515,7 +4515,7 @@ medusaIntegrationTestRunner({
45154515

45164516
expect(
45174517
// @ts-ignore
4518-
transaction.context.invoke["use-remote-query"].output.output
4518+
transaction.context.invoke["fetch-cart"].output.output.data
45194519
.shipping_address.metadata
45204520
).toEqual({
45214521
testing_tax: true,

packages/core/core-flows/src/cart/steps/get-variants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface GetVariantsStepInput {
1818
export const getVariantsStepId = "get-variants"
1919
/**
2020
* This step retrieves variants matching the specified filters.
21-
*
21+
*
2222
* @example
2323
* const data = getVariantsStep({
2424
* filter: {

packages/core/core-flows/src/cart/steps/prepare-adjustments-from-promotion-actions.ts

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import {
22
AddItemAdjustmentAction,
33
AddShippingMethodAdjustment,
44
ComputeActions,
5-
IPromotionModuleService,
65
PromotionDTO,
76
RemoveItemAdjustmentAction,
87
RemoveShippingMethodAdjustment,
98
} from "@medusajs/framework/types"
10-
import { ComputedActions, Modules } from "@medusajs/framework/utils"
9+
import {
10+
ComputedActions,
11+
ContainerRegistrationKeys,
12+
} from "@medusajs/framework/utils"
1113
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
1214

1315
/**
@@ -102,9 +104,7 @@ export const prepareAdjustmentsFromPromotionActionsStep = createStep(
102104
data: PrepareAdjustmentsFromPromotionActionsStepInput,
103105
{ container }
104106
) => {
105-
const promotionModuleService: IPromotionModuleService = container.resolve(
106-
Modules.PROMOTION
107-
)
107+
const query = container.resolve(ContainerRegistrationKeys.QUERY)
108108

109109
const { actions = [] } = data
110110

@@ -118,46 +118,59 @@ export const prepareAdjustmentsFromPromotionActionsStep = createStep(
118118
} as PrepareAdjustmentsFromPromotionActionsStepOutput)
119119
}
120120

121-
const promotions = await promotionModuleService.listPromotions(
122-
{ code: actions.map((a) => a.code) },
123-
{ select: ["id", "code"] }
121+
const { data: promotions } = await query.graph(
122+
{
123+
entity: "promotion",
124+
fields: ["id", "code"],
125+
filters: { code: actions.map((a) => a.code) },
126+
},
127+
{ cache: { enable: true } }
124128
)
125129

126130
const promotionsMap = new Map<string, PromotionDTO>(
127131
promotions.map((promotion) => [promotion.code!, promotion])
128132
)
129133

130-
const lineItemAdjustmentsToCreate = actions
131-
.filter((a) => a.action === ComputedActions.ADD_ITEM_ADJUSTMENT)
132-
.map((action) => ({
133-
code: action.code,
134-
amount: (action as AddItemAdjustmentAction).amount,
135-
is_tax_inclusive: (action as AddItemAdjustmentAction).is_tax_inclusive,
136-
item_id: (action as AddItemAdjustmentAction).item_id,
137-
promotion_id: promotionsMap.get(action.code)?.id,
138-
}))
139-
140-
const lineItemAdjustmentIdsToRemove = actions
141-
.filter((a) => a.action === ComputedActions.REMOVE_ITEM_ADJUSTMENT)
142-
.map((a) => (a as RemoveItemAdjustmentAction).adjustment_id)
134+
const lineItemAdjustmentsToCreate: PrepareAdjustmentsFromPromotionActionsStepOutput["lineItemAdjustmentsToCreate"] =
135+
[]
136+
const lineItemAdjustmentIdsToRemove: string[] = []
137+
const shippingMethodAdjustmentsToCreate: PrepareAdjustmentsFromPromotionActionsStepOutput["shippingMethodAdjustmentsToCreate"] =
138+
[]
139+
const shippingMethodAdjustmentIdsToRemove: string[] = []
143140

144-
const shippingMethodAdjustmentsToCreate = actions
145-
.filter(
146-
(a) => a.action === ComputedActions.ADD_SHIPPING_METHOD_ADJUSTMENT
147-
)
148-
.map((action) => ({
149-
code: action.code,
150-
amount: (action as AddShippingMethodAdjustment).amount,
151-
shipping_method_id: (action as AddShippingMethodAdjustment)
152-
.shipping_method_id,
153-
promotion_id: promotionsMap.get(action.code)?.id,
154-
}))
155-
156-
const shippingMethodAdjustmentIdsToRemove = actions
157-
.filter(
158-
(a) => a.action === ComputedActions.REMOVE_SHIPPING_METHOD_ADJUSTMENT
159-
)
160-
.map((a) => (a as RemoveShippingMethodAdjustment).adjustment_id)
141+
for (const action of actions) {
142+
switch (action.action) {
143+
case ComputedActions.ADD_ITEM_ADJUSTMENT:
144+
const itemAction = action as AddItemAdjustmentAction
145+
lineItemAdjustmentsToCreate.push({
146+
code: action.code,
147+
amount: itemAction.amount as number,
148+
is_tax_inclusive: itemAction.is_tax_inclusive, // TODO: there is a discrepeancy between the type and the actual data
149+
item_id: itemAction.item_id,
150+
promotion_id: promotionsMap.get(action.code)?.id,
151+
} as PrepareAdjustmentsFromPromotionActionsStepOutput["lineItemAdjustmentsToCreate"][number])
152+
break
153+
case ComputedActions.REMOVE_ITEM_ADJUSTMENT:
154+
lineItemAdjustmentIdsToRemove.push(
155+
(action as RemoveItemAdjustmentAction).adjustment_id
156+
)
157+
break
158+
case ComputedActions.ADD_SHIPPING_METHOD_ADJUSTMENT:
159+
const shippingAction = action as AddShippingMethodAdjustment
160+
shippingMethodAdjustmentsToCreate.push({
161+
code: action.code,
162+
amount: shippingAction.amount as number,
163+
shipping_method_id: shippingAction.shipping_method_id,
164+
promotion_id: promotionsMap.get(action.code)?.id,
165+
})
166+
break
167+
case ComputedActions.REMOVE_SHIPPING_METHOD_ADJUSTMENT:
168+
shippingMethodAdjustmentIdsToRemove.push(
169+
(action as RemoveShippingMethodAdjustment).adjustment_id
170+
)
171+
break
172+
}
173+
}
161174

162175
const computedPromotionCodes = [
163176
...lineItemAdjustmentsToCreate,

packages/core/core-flows/src/cart/workflows/complete-cart.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,31 @@ export const completeCartWorkflow = createWorkflow(
104104
ttl: TWO_MINUTES,
105105
})
106106

107-
const orderCart = useQueryGraphStep({
108-
entity: "order_cart",
109-
fields: ["cart_id", "order_id"],
110-
filters: { cart_id: input.id },
111-
options: {
112-
isList: false,
113-
},
114-
})
107+
const [orderCart, cartData] = parallelize(
108+
useQueryGraphStep({
109+
entity: "order_cart",
110+
fields: ["cart_id", "order_id"],
111+
filters: { cart_id: input.id },
112+
options: {
113+
isList: false,
114+
},
115+
}),
116+
useQueryGraphStep({
117+
entity: "cart",
118+
fields: completeCartFields,
119+
filters: { id: input.id },
120+
options: {
121+
isList: false,
122+
},
123+
}).config({
124+
name: "cart-query",
125+
})
126+
)
115127

116128
const orderId = transform({ orderCart }, ({ orderCart }) => {
117129
return orderCart?.data?.order_id
118130
})
119131

120-
const cartData = useQueryGraphStep({
121-
entity: "cart",
122-
fields: completeCartFields,
123-
filters: { id: input.id },
124-
options: {
125-
isList: false,
126-
},
127-
}).config({
128-
name: "cart-query",
129-
})
130-
131132
// this needs to be before the validation step
132133
const paymentSessions = validateCartPaymentsStep({ cart: cartData.data })
133134
// purpose of this step is to run compensation if cart completion fails

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
WorkflowResponse,
1010
} from "@medusajs/framework/workflows-sdk"
1111
import { useQueryGraphStep } from "../../common"
12-
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
1312
import { acquireLockStep, releaseLockStep } from "../../locking"
1413
import { updateLineItemsStep } from "../steps"
1514
import { cartFieldsForRefreshSteps } from "../utils/fields"
@@ -169,15 +168,18 @@ export const refreshCartItemsWorkflow = createWorkflow(
169168
})
170169
})
171170

172-
const refetchedCart = useRemoteQueryStep({
173-
entry_point: "cart",
171+
const { data: refetchedCart } = useQueryGraphStep({
172+
entity: "cart",
174173
fields: cartFieldsForRefreshSteps,
175-
variables: { id: input.cart_id },
176-
list: false,
177-
}).config({ name: "refetchcart" })
174+
filters: { id: input.cart_id },
175+
options: { isList: false },
176+
}).config({ name: "refetch-cart" })
178177

179178
refreshCartShippingMethodsWorkflow.runAsStep({
180-
input: { cart: refetchedCart, additional_data: input.additional_data },
179+
input: {
180+
cart: refetchedCart, // Pass cart to avoid refetch
181+
additional_data: input.additional_data,
182+
},
181183
})
182184

183185
when("force-refresh-update-tax-lines", { input }, ({ input }) => {
@@ -223,6 +225,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
223225
updateCartPromotionsWorkflow.runAsStep({
224226
input: {
225227
cart_id: input.cart_id,
228+
cart: refetchedCart, // Pass cart to avoid refetch in updateCartPromotionsWorkflow
226229
promo_codes: cartPromoCodes,
227230
action: PromotionActions.REPLACE,
228231
},

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
WorkflowResponse,
1010
} from "@medusajs/framework/workflows-sdk"
1111
import { AdditionalData } from "@medusajs/types"
12-
import { useRemoteQueryStep } from "../../common"
12+
import { useQueryGraphStep } from "../../common"
1313
import { acquireLockStep, releaseLockStep } from "../../locking"
1414
import { removeShippingMethodFromCartStep } from "../steps"
1515
import { updateShippingMethodsStep } from "../steps/update-shipping-methods"
@@ -62,10 +62,11 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
6262
>
6363
) => {
6464
const shouldExecute = transform({ input }, ({ input }) => {
65-
return (
66-
!!input.cart_id ||
67-
(!!input.cart && !!input.cart.shipping_methods?.length)
68-
)
65+
if (input.cart) {
66+
return !!input.cart.shipping_methods?.length
67+
}
68+
69+
return !!input.cart_id
6970
})
7071

7172
const cartId = transform({ input }, ({ input }) => {
@@ -79,8 +80,8 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
7980
return shouldExecute
8081
}
8182
).then(() => {
82-
return useRemoteQueryStep({
83-
entry_point: "cart",
83+
const { data: cart } = useQueryGraphStep({
84+
entity: "cart",
8485
fields: [
8586
"id",
8687
"sales_channel_id",
@@ -94,10 +95,14 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
9495
"shipping_methods.data",
9596
"total",
9697
],
97-
variables: { id: cartId },
98-
throw_if_key_not_found: true,
99-
list: false,
98+
filters: { id: cartId },
99+
options: {
100+
throwIfKeyNotFound: true,
101+
isList: false,
102+
},
100103
}).config({ name: "get-cart" })
104+
105+
return cart
101106
})
102107

103108
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {

packages/core/core-flows/src/cart/workflows/refresh-payment-collection.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
transform,
99
when,
1010
} from "@medusajs/framework/workflows-sdk"
11-
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
11+
import { useQueryGraphStep } from "../../common"
1212
import { acquireLockStep, releaseLockStep } from "../../locking"
1313
import { updatePaymentCollectionStep } from "../../payment-collection"
1414
import { deletePaymentSessionsWorkflow } from "../../payment-collection/workflows/delete-payment-sessions"
@@ -61,20 +61,26 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
6161
},
6262
(input: WorkflowData<RefreshPaymentCollectionForCartWorklowInput>) => {
6363
const shouldExecute = transform({ input }, ({ input }) => {
64-
return (
65-
!!input.cart_id || (!!input.cart && !!input.cart.payment_collection)
66-
)
64+
if (input.cart) {
65+
return !!input.cart.payment_collection
66+
}
67+
68+
return !!input.cart_id
6769
})
6870

6971
const cartId = transform({ input }, ({ input }) => {
7072
return input.cart_id ?? input.cart?.id
7173
})
7274

73-
const fetchCart = when("should-fetch-cart", { input }, ({ input }) => {
74-
return shouldExecute
75-
}).then(() => {
76-
return useRemoteQueryStep({
77-
entry_point: "cart",
75+
const fetchCart = when(
76+
"should-fetch-cart",
77+
{ shouldExecute },
78+
({ shouldExecute }) => {
79+
return shouldExecute
80+
}
81+
).then(() => {
82+
const { data: cart } = useQueryGraphStep({
83+
entity: "cart",
7884
fields: [
7985
"id",
8086
"region_id",
@@ -87,10 +93,14 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
8793
"payment_collection.currency_code",
8894
"payment_collection.payment_sessions.id",
8995
],
90-
variables: { id: cartId },
91-
throw_if_key_not_found: true,
92-
list: false,
93-
})
96+
filters: { id: cartId },
97+
options: {
98+
throwIfKeyNotFound: true,
99+
isList: false,
100+
},
101+
}).config({ name: "fetch-cart" })
102+
103+
return cart
94104
})
95105

96106
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
WorkflowData,
99
WorkflowResponse,
1010
} from "@medusajs/framework/workflows-sdk"
11-
import { useRemoteQueryStep } from "../../common"
11+
import { useQueryGraphStep } from "../../common"
1212
import { acquireLockStep, releaseLockStep } from "../../locking"
1313
import {
1414
createLineItemAdjustmentsStep,
@@ -82,12 +82,14 @@ export const updateCartPromotionsWorkflow = createWorkflow(
8282
const fetchCart = when("should-fetch-cart", { input }, ({ input }) => {
8383
return !input.cart
8484
}).then(() => {
85-
return useRemoteQueryStep({
86-
entry_point: "cart",
85+
const { data: cart } = useQueryGraphStep({
86+
entity: "cart",
8787
fields: cartFieldsForRefreshSteps,
88-
variables: { id: input.cart_id },
89-
list: false,
90-
})
88+
filters: { id: input.cart_id },
89+
options: { isList: false },
90+
}).config({ name: "fetch-cart" })
91+
92+
return cart
9193
})
9294

9395
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {

0 commit comments

Comments
 (0)