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>
This commit is contained in:
Adrien de Peretti
2025-10-10 08:58:19 +02:00
committed by GitHub
parent 76bf364440
commit c54c5ed6de
23 changed files with 481 additions and 288 deletions

View File

@@ -18,7 +18,7 @@ export interface GetVariantsStepInput {
export const getVariantsStepId = "get-variants"
/**
* This step retrieves variants matching the specified filters.
*
*
* @example
* const data = getVariantsStep({
* filter: {

View File

@@ -2,12 +2,14 @@ import {
AddItemAdjustmentAction,
AddShippingMethodAdjustment,
ComputeActions,
IPromotionModuleService,
PromotionDTO,
RemoveItemAdjustmentAction,
RemoveShippingMethodAdjustment,
} from "@medusajs/framework/types"
import { ComputedActions, Modules } from "@medusajs/framework/utils"
import {
ComputedActions,
ContainerRegistrationKeys,
} from "@medusajs/framework/utils"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
/**
@@ -102,9 +104,7 @@ export const prepareAdjustmentsFromPromotionActionsStep = createStep(
data: PrepareAdjustmentsFromPromotionActionsStepInput,
{ container }
) => {
const promotionModuleService: IPromotionModuleService = container.resolve(
Modules.PROMOTION
)
const query = container.resolve(ContainerRegistrationKeys.QUERY)
const { actions = [] } = data
@@ -118,46 +118,59 @@ export const prepareAdjustmentsFromPromotionActionsStep = createStep(
} as PrepareAdjustmentsFromPromotionActionsStepOutput)
}
const promotions = await promotionModuleService.listPromotions(
{ code: actions.map((a) => a.code) },
{ select: ["id", "code"] }
const { data: promotions } = await query.graph(
{
entity: "promotion",
fields: ["id", "code"],
filters: { code: actions.map((a) => a.code) },
},
{ cache: { enable: true } }
)
const promotionsMap = new Map<string, PromotionDTO>(
promotions.map((promotion) => [promotion.code!, promotion])
)
const lineItemAdjustmentsToCreate = actions
.filter((a) => a.action === ComputedActions.ADD_ITEM_ADJUSTMENT)
.map((action) => ({
code: action.code,
amount: (action as AddItemAdjustmentAction).amount,
is_tax_inclusive: (action as AddItemAdjustmentAction).is_tax_inclusive,
item_id: (action as AddItemAdjustmentAction).item_id,
promotion_id: promotionsMap.get(action.code)?.id,
}))
const lineItemAdjustmentsToCreate: PrepareAdjustmentsFromPromotionActionsStepOutput["lineItemAdjustmentsToCreate"] =
[]
const lineItemAdjustmentIdsToRemove: string[] = []
const shippingMethodAdjustmentsToCreate: PrepareAdjustmentsFromPromotionActionsStepOutput["shippingMethodAdjustmentsToCreate"] =
[]
const shippingMethodAdjustmentIdsToRemove: string[] = []
const lineItemAdjustmentIdsToRemove = actions
.filter((a) => a.action === ComputedActions.REMOVE_ITEM_ADJUSTMENT)
.map((a) => (a as RemoveItemAdjustmentAction).adjustment_id)
const shippingMethodAdjustmentsToCreate = actions
.filter(
(a) => a.action === ComputedActions.ADD_SHIPPING_METHOD_ADJUSTMENT
)
.map((action) => ({
code: action.code,
amount: (action as AddShippingMethodAdjustment).amount,
shipping_method_id: (action as AddShippingMethodAdjustment)
.shipping_method_id,
promotion_id: promotionsMap.get(action.code)?.id,
}))
const shippingMethodAdjustmentIdsToRemove = actions
.filter(
(a) => a.action === ComputedActions.REMOVE_SHIPPING_METHOD_ADJUSTMENT
)
.map((a) => (a as RemoveShippingMethodAdjustment).adjustment_id)
for (const action of actions) {
switch (action.action) {
case ComputedActions.ADD_ITEM_ADJUSTMENT:
const itemAction = action as AddItemAdjustmentAction
lineItemAdjustmentsToCreate.push({
code: action.code,
amount: itemAction.amount as number,
is_tax_inclusive: itemAction.is_tax_inclusive, // TODO: there is a discrepeancy between the type and the actual data
item_id: itemAction.item_id,
promotion_id: promotionsMap.get(action.code)?.id,
} as PrepareAdjustmentsFromPromotionActionsStepOutput["lineItemAdjustmentsToCreate"][number])
break
case ComputedActions.REMOVE_ITEM_ADJUSTMENT:
lineItemAdjustmentIdsToRemove.push(
(action as RemoveItemAdjustmentAction).adjustment_id
)
break
case ComputedActions.ADD_SHIPPING_METHOD_ADJUSTMENT:
const shippingAction = action as AddShippingMethodAdjustment
shippingMethodAdjustmentsToCreate.push({
code: action.code,
amount: shippingAction.amount as number,
shipping_method_id: shippingAction.shipping_method_id,
promotion_id: promotionsMap.get(action.code)?.id,
})
break
case ComputedActions.REMOVE_SHIPPING_METHOD_ADJUSTMENT:
shippingMethodAdjustmentIdsToRemove.push(
(action as RemoveShippingMethodAdjustment).adjustment_id
)
break
}
}
const computedPromotionCodes = [
...lineItemAdjustmentsToCreate,

View File

@@ -104,30 +104,31 @@ export const completeCartWorkflow = createWorkflow(
ttl: TWO_MINUTES,
})
const orderCart = useQueryGraphStep({
entity: "order_cart",
fields: ["cart_id", "order_id"],
filters: { cart_id: input.id },
options: {
isList: false,
},
})
const [orderCart, cartData] = parallelize(
useQueryGraphStep({
entity: "order_cart",
fields: ["cart_id", "order_id"],
filters: { cart_id: input.id },
options: {
isList: false,
},
}),
useQueryGraphStep({
entity: "cart",
fields: completeCartFields,
filters: { id: input.id },
options: {
isList: false,
},
}).config({
name: "cart-query",
})
)
const orderId = transform({ orderCart }, ({ orderCart }) => {
return orderCart?.data?.order_id
})
const cartData = useQueryGraphStep({
entity: "cart",
fields: completeCartFields,
filters: { id: input.id },
options: {
isList: false,
},
}).config({
name: "cart-query",
})
// this needs to be before the validation step
const paymentSessions = validateCartPaymentsStep({ cart: cartData.data })
// purpose of this step is to run compensation if cart completion fails

View File

@@ -9,7 +9,6 @@ import {
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { useQueryGraphStep } from "../../common"
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
import { acquireLockStep, releaseLockStep } from "../../locking"
import { updateLineItemsStep } from "../steps"
import { cartFieldsForRefreshSteps } from "../utils/fields"
@@ -169,15 +168,18 @@ export const refreshCartItemsWorkflow = createWorkflow(
})
})
const refetchedCart = useRemoteQueryStep({
entry_point: "cart",
const { data: refetchedCart } = useQueryGraphStep({
entity: "cart",
fields: cartFieldsForRefreshSteps,
variables: { id: input.cart_id },
list: false,
}).config({ name: "refetchcart" })
filters: { id: input.cart_id },
options: { isList: false },
}).config({ name: "refetch-cart" })
refreshCartShippingMethodsWorkflow.runAsStep({
input: { cart: refetchedCart, additional_data: input.additional_data },
input: {
cart: refetchedCart, // Pass cart to avoid refetch
additional_data: input.additional_data,
},
})
when("force-refresh-update-tax-lines", { input }, ({ input }) => {
@@ -223,6 +225,7 @@ export const refreshCartItemsWorkflow = createWorkflow(
updateCartPromotionsWorkflow.runAsStep({
input: {
cart_id: input.cart_id,
cart: refetchedCart, // Pass cart to avoid refetch in updateCartPromotionsWorkflow
promo_codes: cartPromoCodes,
action: PromotionActions.REPLACE,
},

View File

@@ -9,7 +9,7 @@ import {
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { AdditionalData } from "@medusajs/types"
import { useRemoteQueryStep } from "../../common"
import { useQueryGraphStep } from "../../common"
import { acquireLockStep, releaseLockStep } from "../../locking"
import { removeShippingMethodFromCartStep } from "../steps"
import { updateShippingMethodsStep } from "../steps/update-shipping-methods"
@@ -62,10 +62,11 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
>
) => {
const shouldExecute = transform({ input }, ({ input }) => {
return (
!!input.cart_id ||
(!!input.cart && !!input.cart.shipping_methods?.length)
)
if (input.cart) {
return !!input.cart.shipping_methods?.length
}
return !!input.cart_id
})
const cartId = transform({ input }, ({ input }) => {
@@ -79,8 +80,8 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
return shouldExecute
}
).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
const { data: cart } = useQueryGraphStep({
entity: "cart",
fields: [
"id",
"sales_channel_id",
@@ -94,10 +95,14 @@ export const refreshCartShippingMethodsWorkflow = createWorkflow(
"shipping_methods.data",
"total",
],
variables: { id: cartId },
throw_if_key_not_found: true,
list: false,
filters: { id: cartId },
options: {
throwIfKeyNotFound: true,
isList: false,
},
}).config({ name: "get-cart" })
return cart
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {

View File

@@ -8,7 +8,7 @@ import {
transform,
when,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common/steps/use-remote-query"
import { useQueryGraphStep } from "../../common"
import { acquireLockStep, releaseLockStep } from "../../locking"
import { updatePaymentCollectionStep } from "../../payment-collection"
import { deletePaymentSessionsWorkflow } from "../../payment-collection/workflows/delete-payment-sessions"
@@ -61,20 +61,26 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
},
(input: WorkflowData<RefreshPaymentCollectionForCartWorklowInput>) => {
const shouldExecute = transform({ input }, ({ input }) => {
return (
!!input.cart_id || (!!input.cart && !!input.cart.payment_collection)
)
if (input.cart) {
return !!input.cart.payment_collection
}
return !!input.cart_id
})
const cartId = transform({ input }, ({ input }) => {
return input.cart_id ?? input.cart?.id
})
const fetchCart = when("should-fetch-cart", { input }, ({ input }) => {
return shouldExecute
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
const fetchCart = when(
"should-fetch-cart",
{ shouldExecute },
({ shouldExecute }) => {
return shouldExecute
}
).then(() => {
const { data: cart } = useQueryGraphStep({
entity: "cart",
fields: [
"id",
"region_id",
@@ -87,10 +93,14 @@ export const refreshPaymentCollectionForCartWorkflow = createWorkflow(
"payment_collection.currency_code",
"payment_collection.payment_sessions.id",
],
variables: { id: cartId },
throw_if_key_not_found: true,
list: false,
})
filters: { id: cartId },
options: {
throwIfKeyNotFound: true,
isList: false,
},
}).config({ name: "fetch-cart" })
return cart
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {

View File

@@ -8,7 +8,7 @@ import {
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { useQueryGraphStep } from "../../common"
import { acquireLockStep, releaseLockStep } from "../../locking"
import {
createLineItemAdjustmentsStep,
@@ -82,12 +82,14 @@ export const updateCartPromotionsWorkflow = createWorkflow(
const fetchCart = when("should-fetch-cart", { input }, ({ input }) => {
return !input.cart
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
const { data: cart } = useQueryGraphStep({
entity: "cart",
fields: cartFieldsForRefreshSteps,
variables: { id: input.cart_id },
list: false,
})
filters: { id: input.cart_id },
options: { isList: false },
}).config({ name: "fetch-cart" })
return cart
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {

View File

@@ -8,7 +8,7 @@ import {
transform,
when,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { useQueryGraphStep } from "../../common"
import { acquireLockStep, releaseLockStep } from "../../locking"
import { getItemTaxLinesStep } from "../../tax/steps/get-item-tax-lines"
import { setTaxLinesForItemsStep } from "../steps"
@@ -127,13 +127,17 @@ export const updateTaxLinesWorkflow = createWorkflow(
const fetchCart = when("should-fetch-cart", { input }, ({ input }) => {
return !input.cart
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
const { data: cart } = useQueryGraphStep({
entity: "cart",
fields: cartFields,
variables: { id: input.cart_id },
throw_if_key_not_found: true,
list: false,
})
filters: { id: input.cart_id },
options: {
throwIfKeyNotFound: true,
isList: false,
},
}).config({ name: "fetch-cart" })
return cart
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {

View File

@@ -8,7 +8,7 @@ import {
transform,
when,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { useQueryGraphStep } from "../../common"
import { getItemTaxLinesStep } from "../../tax/steps/get-item-tax-lines"
import { upsertTaxLinesForItemsStep } from "../steps/upsert-tax-lines-for-items"
@@ -124,13 +124,17 @@ export const upsertTaxLinesWorkflow = createWorkflow(
const fetchCart = when("should-fetch-cart", { input }, ({ input }) => {
return !input.cart
}).then(() => {
return useRemoteQueryStep({
entry_point: "cart",
const { data: cart } = useQueryGraphStep({
entity: "cart",
fields: cartFields,
variables: { id: input.cart_id },
throw_if_key_not_found: true,
list: false,
})
filters: { id: input.cart_id },
options: {
throwIfKeyNotFound: true,
isList: false,
},
}).config({ name: "fetch-cart" })
return cart
})
const cart = transform({ fetchCart, input }, ({ fetchCart, input }) => {