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:
1 parent
76bf364440
commit
c54c5ed6de
23 files changed
+481
-288
No files matched your search
@@ -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: {
|
||||
|
||||
+51
-38
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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: "refetch–cart" })
|
||||
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,
|
||||
},
|
||||
|
||||
@@ -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 }) => {
|
||||
|
||||
@@ -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 }) => {
|
||||
|
||||
@@ -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 }) => {
|
||||
|
||||
@@ -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 }) => {
|
||||
|
||||
@@ -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 }) => {
|
||||
|
||||
@@ -98,7 +98,12 @@ export async function mikroOrmCreateConnection(
|
||||
filters: database.filters ?? {},
|
||||
useBatchInserts: true,
|
||||
useBatchUpdates: true,
|
||||
implicitTransactions: false,
|
||||
ignoreUndefinedInQuery: true,
|
||||
batchSize: 100,
|
||||
metadataCache: {
|
||||
enabled: true,
|
||||
},
|
||||
assign: {
|
||||
convertCustomTypes: true,
|
||||
},
|
||||
|
||||
@@ -436,20 +436,23 @@ export function mikroOrmBaseRepositoryFactory<const T extends object>(
|
||||
.getKnexQuery()
|
||||
.toSQL()
|
||||
|
||||
const where = [
|
||||
whereSqlInfo.sql.split("where ")[1],
|
||||
whereSqlInfo.bindings,
|
||||
] as [string, any[]]
|
||||
|
||||
return await (manager.getTransactionContext() ?? manager.getKnex())
|
||||
const builder = (manager.getTransactionContext() ?? manager.getKnex())
|
||||
.queryBuilder()
|
||||
.from(this.tableName)
|
||||
.delete()
|
||||
.where(manager.getKnex().raw(...where))
|
||||
.returning("id")
|
||||
.then((rows: { id: string }[]) => {
|
||||
return rows.map((row: { id: string }) => row.id)
|
||||
})
|
||||
|
||||
const hasWhere = whereSqlInfo.sql.includes("where ")
|
||||
if (hasWhere) {
|
||||
const where = [
|
||||
whereSqlInfo.sql.split("where ")[1],
|
||||
whereSqlInfo.bindings,
|
||||
] as [string, any[]]
|
||||
builder.where(manager.getKnex().raw(...where))
|
||||
}
|
||||
|
||||
return await builder.returning("id").then((rows: { id: string }[]) => {
|
||||
return rows.map((row: { id: string }) => row.id)
|
||||
})
|
||||
}
|
||||
|
||||
async find(
|
||||
|
||||
@@ -13,6 +13,16 @@ export function createPgConnection(options: Options) {
|
||||
options.driverOptions?.ssl ??
|
||||
options.driverOptions?.connection?.ssl ??
|
||||
false
|
||||
const connectionTimeoutMillis =
|
||||
driverOptions?.connectionTimeoutMillis ??
|
||||
driverOptions?.connection?.connectionTimeoutMillis ??
|
||||
5000
|
||||
const keepAliveInitialDelayMillis =
|
||||
driverOptions?.keepAliveInitialDelayMillis ??
|
||||
driverOptions?.connection?.keepAliveInitialDelayMillis ??
|
||||
10000
|
||||
const keepAlive =
|
||||
driverOptions?.keepAlive ?? driverOptions?.connection?.keepAlive ?? true
|
||||
|
||||
return knex<any, any>({
|
||||
client: "pg",
|
||||
@@ -23,11 +33,16 @@ export function createPgConnection(options: Options) {
|
||||
idle_in_transaction_session_timeout:
|
||||
(driverOptions?.idle_in_transaction_session_timeout as number) ??
|
||||
undefined, // prevent null to be passed
|
||||
|
||||
connectionTimeoutMillis: connectionTimeoutMillis as number, // Fail fast on slow connects
|
||||
keepAlive: keepAlive as boolean, // Prevent connections from being dropped
|
||||
keepAliveInitialDelayMillis: keepAliveInitialDelayMillis as number, // Start keepalive probes after 10s
|
||||
},
|
||||
pool: {
|
||||
propagateCreateError: false, // Don't fail entire pool on one bad connection
|
||||
min: (pool?.min as number) ?? 1,
|
||||
// https://knexjs.org/guide/#pool
|
||||
...(pool ?? {}),
|
||||
min: (pool?.min as number) ?? 1,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -37,11 +37,11 @@
|
||||
"build": "rimraf dist && tsc --build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mikro-orm/cli": "6.4.3",
|
||||
"@mikro-orm/core": "6.4.3",
|
||||
"@mikro-orm/knex": "6.4.3",
|
||||
"@mikro-orm/migrations": "6.4.3",
|
||||
"@mikro-orm/postgresql": "6.4.3",
|
||||
"@mikro-orm/cli": "6.4.16",
|
||||
"@mikro-orm/core": "6.4.16",
|
||||
"@mikro-orm/knex": "6.4.16",
|
||||
"@mikro-orm/migrations": "6.4.16",
|
||||
"@mikro-orm/postgresql": "6.4.16",
|
||||
"@opentelemetry/instrumentation-pg": "^0.44.0",
|
||||
"@opentelemetry/resources": "^1.26.0",
|
||||
"@opentelemetry/sdk-node": "^0.53.0",
|
||||
|
||||
@@ -280,12 +280,8 @@ export default class CartModuleService
|
||||
|
||||
const carts = await this.createCarts_(input, sharedContext)
|
||||
|
||||
const result = await this.listCarts(
|
||||
{ id: carts.map((p) => p!.id) },
|
||||
{
|
||||
relations: ["shipping_address", "billing_address"],
|
||||
},
|
||||
sharedContext
|
||||
const result = await this.baseRepository_.serialize<CartTypes.CartDTO[]>(
|
||||
carts
|
||||
)
|
||||
|
||||
return (Array.isArray(data) ? result : result[0]) as
|
||||
@@ -297,23 +293,30 @@ export default class CartModuleService
|
||||
protected async createCarts_(
|
||||
data: CartTypes.CreateCartDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
): Promise<InferEntityType<typeof Cart>[]> {
|
||||
const cartsWithItems = data.map(({ items, ...cart }) => {
|
||||
const cartId = generateEntityId((cart as any).id, "cart")
|
||||
return {
|
||||
cart: { ...cart, id: cartId },
|
||||
items: items || [],
|
||||
}
|
||||
})
|
||||
|
||||
// Batch create all carts a once instead of sequentially
|
||||
const createdCarts = await this.cartService_.create(
|
||||
cartsWithItems.map((c) => c.cart),
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const lineItemsToCreate: CreateLineItemDTO[] = []
|
||||
const createdCarts: InferEntityType<typeof Cart>[] = []
|
||||
for (const { items, ...cart } of data) {
|
||||
const [created] = await this.cartService_.create([cart], sharedContext)
|
||||
|
||||
createdCarts.push(created)
|
||||
|
||||
if (items?.length) {
|
||||
const cartItems = items.map((item) => {
|
||||
return {
|
||||
for (const { cart, items } of cartsWithItems) {
|
||||
if (items.length) {
|
||||
lineItemsToCreate.push(
|
||||
...items.map((item) => ({
|
||||
...item,
|
||||
cart_id: created.id,
|
||||
}
|
||||
})
|
||||
|
||||
lineItemsToCreate.push(...cartItems)
|
||||
cart_id: cart.id,
|
||||
}))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,7 +324,19 @@ export default class CartModuleService
|
||||
await this.addLineItemsBulk_(lineItemsToCreate, sharedContext)
|
||||
}
|
||||
|
||||
return createdCarts
|
||||
const fullCarts = await this.cartService_.list(
|
||||
{ id: createdCarts.map((c) => c.id) },
|
||||
{
|
||||
relations: ["shipping_address", "billing_address"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
// Return in the same input order
|
||||
const orderedInputId = cartsWithItems.map((c) => c.cart.id)
|
||||
return orderedInputId.map((id) =>
|
||||
fullCarts.find((c) => c.id === id)
|
||||
) as InferEntityType<typeof Cart>[]
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
|
||||
@@ -136,6 +136,15 @@
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_deleted_at\" ON \"price_list\" (deleted_at) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "IDX_price_list_id_status_starts_at_ends_at",
|
||||
"columnNames": [],
|
||||
"composite": false,
|
||||
"constraint": false,
|
||||
"primary": false,
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_id_status_starts_at_ends_at\" ON \"price_list\" (id, status, starts_at, ends_at) WHERE deleted_at IS NULL AND status = 'active'"
|
||||
},
|
||||
{
|
||||
"keyName": "price_list_pkey",
|
||||
"columnNames": [
|
||||
@@ -252,6 +261,15 @@
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_attribute\" ON \"price_list_rule\" (attribute) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "IDX_price_list_rule_value",
|
||||
"columnNames": [],
|
||||
"composite": false,
|
||||
"constraint": false,
|
||||
"primary": false,
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_value\" ON \"price_list_rule\" USING gin (value) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "price_list_rule_pkey",
|
||||
"columnNames": [
|
||||
@@ -625,6 +643,15 @@
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_currency_code\" ON \"price\" (currency_code) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "IDX_price_currency_code_price_set_id_min_quantity",
|
||||
"columnNames": [],
|
||||
"composite": false,
|
||||
"constraint": false,
|
||||
"primary": false,
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_currency_code_price_set_id_min_quantity\" ON \"price\" (currency_code, price_set_id, min_quantity) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "price_pkey",
|
||||
"columnNames": [
|
||||
@@ -822,6 +849,15 @@
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_operator_value\" ON \"price_rule\" (operator, value) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "IDX_price_rule_attribute_value_price_id",
|
||||
"columnNames": [],
|
||||
"composite": false,
|
||||
"constraint": false,
|
||||
"primary": false,
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_attribute_value_price_id\" ON \"price_rule\" (attribute, value, price_id) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "price_rule_pkey",
|
||||
"columnNames": [
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Migration } from "@mikro-orm/migrations"
|
||||
|
||||
export class Migration20251009110625 extends Migration {
|
||||
override async up(): Promise<void> {
|
||||
this.addSql(
|
||||
`CREATE INDEX IF NOT EXISTS "IDX_price_list_id_status_starts_at_ends_at" ON "price_list" (id, status, starts_at, ends_at) WHERE deleted_at IS NULL AND status = 'active';`
|
||||
)
|
||||
|
||||
this.addSql(
|
||||
`CREATE INDEX IF NOT EXISTS "IDX_price_list_rule_value" ON "price_list_rule" USING gin (value) WHERE deleted_at IS NULL;`
|
||||
)
|
||||
this.addSql(
|
||||
`CREATE INDEX IF NOT EXISTS "IDX_price_rule_attribute_value_price_id" ON "price_rule" (attribute, value, price_id) WHERE deleted_at IS NULL;`
|
||||
)
|
||||
}
|
||||
|
||||
override async down(): Promise<void> {
|
||||
this.addSql(
|
||||
`drop index if exists "IDX_price_list_id_status_starts_at_ends_at";`
|
||||
)
|
||||
|
||||
this.addSql(`drop index if exists "IDX_price_list_rule_value";`)
|
||||
|
||||
this.addSql(
|
||||
`drop index if exists "IDX_price_rule_attribute_value_price_id";`
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,11 @@ const PriceListRule = model
|
||||
on: ["attribute"],
|
||||
where: "deleted_at IS NULL",
|
||||
},
|
||||
{
|
||||
on: ["value"],
|
||||
where: "deleted_at IS NULL",
|
||||
type: "gin",
|
||||
},
|
||||
])
|
||||
|
||||
export default PriceListRule
|
||||
@@ -26,5 +26,11 @@ const PriceList = model
|
||||
.cascades({
|
||||
delete: ["price_list_rules", "prices"],
|
||||
})
|
||||
.indexes([
|
||||
{
|
||||
on: ["id", "status", "starts_at", "ends_at"],
|
||||
where: "deleted_at IS NULL AND status = 'active'",
|
||||
},
|
||||
])
|
||||
|
||||
export default PriceList
|
||||
@@ -30,6 +30,10 @@ const PriceRule = model
|
||||
on: ["operator", "value"],
|
||||
where: "deleted_at IS NULL",
|
||||
},
|
||||
{
|
||||
on: ["attribute", "value", "price_id"],
|
||||
where: "deleted_at IS NULL",
|
||||
},
|
||||
])
|
||||
|
||||
export default PriceRule
|
||||
@@ -6,6 +6,10 @@ import {
|
||||
PriceListStatus,
|
||||
} from "@medusajs/framework/utils"
|
||||
|
||||
import {
|
||||
Knex,
|
||||
SqlEntityManager,
|
||||
} from "@medusajs/framework/mikro-orm/postgresql"
|
||||
import {
|
||||
CalculatedPriceSetDTO,
|
||||
Context,
|
||||
@@ -13,10 +17,6 @@ import {
|
||||
PricingFilters,
|
||||
PricingRepositoryService,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
Knex,
|
||||
SqlEntityManager,
|
||||
} from "@medusajs/framework/mikro-orm/postgresql"
|
||||
|
||||
export class PricingRepository
|
||||
extends MikroOrmBase
|
||||
@@ -172,101 +172,108 @@ export class PricingRepository
|
||||
})
|
||||
|
||||
if (hasComplexContext) {
|
||||
const priceRuleConditions = knex.raw(
|
||||
`
|
||||
(
|
||||
price.rules_count = 0 OR
|
||||
(
|
||||
/* Count all matching rules and compare to total rule count */
|
||||
SELECT COUNT(*)
|
||||
FROM price_rule pr
|
||||
WHERE pr.price_id = price.id
|
||||
AND pr.deleted_at IS NULL
|
||||
AND (
|
||||
${flattenedContext
|
||||
.map(([key, value]) => {
|
||||
if (typeof value === "number") {
|
||||
return `
|
||||
(pr.attribute = ? AND (
|
||||
(pr.operator = 'eq' AND pr.value = ?) OR
|
||||
(pr.operator = 'gt' AND ? > pr.value::numeric) OR
|
||||
(pr.operator = 'gte' AND ? >= pr.value::numeric) OR
|
||||
(pr.operator = 'lt' AND ? < pr.value::numeric) OR
|
||||
(pr.operator = 'lte' AND ? <= pr.value::numeric)
|
||||
))
|
||||
`
|
||||
} else {
|
||||
const normalizeValue = Array.isArray(value) ? value : [value]
|
||||
const placeholders = normalizeValue.map(() => "?").join(",")
|
||||
return `(pr.attribute = ? AND pr.value IN (${placeholders}))`
|
||||
}
|
||||
})
|
||||
.join(" OR ")})
|
||||
) = (
|
||||
/* Get total rule count */
|
||||
SELECT COUNT(*)
|
||||
FROM price_rule pr
|
||||
WHERE pr.price_id = price.id
|
||||
AND pr.deleted_at IS NULL
|
||||
)
|
||||
)
|
||||
`,
|
||||
flattenedContext.flatMap(([key, value]) => {
|
||||
// Build match conditions for LATERAL join
|
||||
const priceRuleMatchConditions = flattenedContext
|
||||
.map(([_key, value]) => {
|
||||
if (typeof value === "number") {
|
||||
return [key, value.toString(), value, value, value, value]
|
||||
return `
|
||||
(pr.attribute = ? AND (
|
||||
(pr.operator = 'eq' AND pr.value = ?) OR
|
||||
(pr.operator = 'gt' AND ? > pr.value::numeric) OR
|
||||
(pr.operator = 'gte' AND ? >= pr.value::numeric) OR
|
||||
(pr.operator = 'lt' AND ? < pr.value::numeric) OR
|
||||
(pr.operator = 'lte' AND ? <= pr.value::numeric)
|
||||
))
|
||||
`
|
||||
} else {
|
||||
const normalizeValue = Array.isArray(value) ? value : [value]
|
||||
return [key, ...normalizeValue]
|
||||
const placeholders = normalizeValue.map(() => "?").join(",")
|
||||
return `(pr.attribute = ? AND pr.value IN (${placeholders}))`
|
||||
}
|
||||
})
|
||||
)
|
||||
.join(" OR ")
|
||||
|
||||
const priceListRuleConditions = knex.raw(
|
||||
`
|
||||
(
|
||||
pl.rules_count = 0 OR
|
||||
(
|
||||
/* Count all matching rules and compare to total rule count */
|
||||
SELECT COUNT(*)
|
||||
FROM price_list_rule plr
|
||||
WHERE plr.price_list_id = pl.id
|
||||
AND plr.deleted_at IS NULL
|
||||
AND (
|
||||
${flattenedContext
|
||||
.map(([key, value]) => {
|
||||
if (Array.isArray(value)) {
|
||||
return value
|
||||
.map((v) => `(plr.attribute = ? AND plr.value @> ?)`)
|
||||
.join(" OR ")
|
||||
}
|
||||
return `(plr.attribute = ? AND plr.value @> ?)`
|
||||
})
|
||||
.join(" OR ")}
|
||||
)
|
||||
) = (
|
||||
/* Get total rule count */
|
||||
SELECT COUNT(*)
|
||||
FROM price_list_rule plr
|
||||
WHERE plr.price_list_id = pl.id
|
||||
AND plr.deleted_at IS NULL
|
||||
)
|
||||
)
|
||||
`,
|
||||
flattenedContext.flatMap(([key, value]) => {
|
||||
const priceRuleMatchParams = flattenedContext.flatMap(([key, value]) => {
|
||||
if (typeof value === "number") {
|
||||
return [key, value.toString(), value, value, value, value]
|
||||
} else {
|
||||
const normalizeValue = Array.isArray(value) ? value : [value]
|
||||
return [key, ...normalizeValue]
|
||||
}
|
||||
})
|
||||
|
||||
const priceListRuleMatchConditions = flattenedContext
|
||||
.map(([_key, value]) => {
|
||||
if (Array.isArray(value)) {
|
||||
return value
|
||||
.map((_v) => `(plr.attribute = ? AND plr.value @> ?)`)
|
||||
.join(" OR ")
|
||||
}
|
||||
return `(plr.attribute = ? AND plr.value @> ?)`
|
||||
})
|
||||
.join(" OR ")
|
||||
|
||||
const priceListRuleMatchParams = flattenedContext.flatMap(
|
||||
([key, value]) => {
|
||||
const valueAsArray = Array.isArray(value) ? value : [value]
|
||||
return valueAsArray.flatMap((v) => [key, JSON.stringify(v)])
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
// Use LATERAL joins to compute matched and total counts in one go
|
||||
query
|
||||
.leftJoin(
|
||||
knex.raw(
|
||||
`LATERAL (
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE ${priceRuleMatchConditions}) as matched_count,
|
||||
COUNT(*) as total_count
|
||||
FROM price_rule pr
|
||||
WHERE pr.price_id = price.id
|
||||
AND pr.deleted_at IS NULL
|
||||
) pr_stats`,
|
||||
priceRuleMatchParams
|
||||
),
|
||||
knex.raw("true")
|
||||
)
|
||||
.leftJoin(
|
||||
knex.raw(
|
||||
`LATERAL (
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE ${priceListRuleMatchConditions}) as matched_count,
|
||||
COUNT(*) as total_count
|
||||
FROM price_list_rule plr
|
||||
WHERE plr.price_list_id = pl.id
|
||||
AND plr.deleted_at IS NULL
|
||||
) plr_stats`,
|
||||
priceListRuleMatchParams
|
||||
),
|
||||
knex.raw("true")
|
||||
)
|
||||
|
||||
query.where((qb) => {
|
||||
qb.whereNull("price.price_list_id")
|
||||
.andWhereRaw(priceRuleConditions)
|
||||
.orWhere((qb2) => {
|
||||
qb2
|
||||
.whereNotNull("price.price_list_id")
|
||||
.whereRaw(priceListRuleConditions)
|
||||
.andWhereRaw(priceRuleConditions)
|
||||
qb.where((qb2) => {
|
||||
// No price list: price rules must match or be zero
|
||||
qb2.whereNull("price.price_list_id").andWhere((qb3) => {
|
||||
qb3
|
||||
.where("price.rules_count", 0)
|
||||
.orWhereRaw("pr_stats.matched_count = price.rules_count")
|
||||
})
|
||||
}).orWhere((qb2) => {
|
||||
// Has price list: both price rules and price list rules must match
|
||||
qb2
|
||||
.whereNotNull("price.price_list_id")
|
||||
.andWhere((qb3) => {
|
||||
qb3
|
||||
.where("price.rules_count", 0)
|
||||
.orWhereRaw("pr_stats.matched_count = price.rules_count")
|
||||
})
|
||||
.andWhere((qb3) => {
|
||||
qb3
|
||||
.where("pl.rules_count", 0)
|
||||
.orWhereRaw("plr_stats.matched_count = pl.rules_count")
|
||||
})
|
||||
})
|
||||
})
|
||||
} else {
|
||||
query.where(function (this: Knex.QueryBuilder) {
|
||||
|
||||
Reference in new issue
Block a user