chore(order): dml (#10292)
* ../../core/types/src/dml/index.ts * ../../core/types/src/dml/index.ts * fix: relationships mapping * handle nullable foreign keys types * handle nullable foreign keys types * handle nullable foreign keys types * continue to update product category repository * fix all product category repositories issues * fix product category service types * fix product module service types * fix product module service types * fix repository template type * refactor: use a singleton DMLToMikroORM factory instance Since the MikroORM MetadataStorage is global, we will also have to turn DML to MikroORM entities conversion use a global bucket as well * refactor: update product module to use DML in tests * wip: tests * WIP product linkable fixes * continue type fixing and start test fixing * test: fix more tests * fix repository * fix pivot table computaion + fix mikro orm repository * fix many to many management and configuration * fix many to many management and configuration * fix many to many management and configuration * update product tag relation configuration * Introduce experimental dml hooks to fix some issues with categories * more fixes * fix product tests * add missing id prefixes * fix product category handle management * test: fix more failing tests * test: make it all green * test: fix breaking tests * fix: build issues * fix: build issues * fix: more breaking tests * refactor: fix issues after merge * refactor: fix issues after merge * refactor: surpress types error * test: fix DML failing tests * improve many to many inference + tests * Wip fix columns from product entity * remove product model before create hook and manage handle validation and transformation at the service level * test: fix breaking unit tests * fix: product module service to not update handle on product update * fix define link and joiner config * test: fix joiner config test * test: fix joiner config test * fix joiner config primary keys * Fix joiner config builder * Fix joiner config builder * test: remove only modifier from test * refactor: remove hooks usage from product collection * refactor: remove hooks usage from product-option * refactor: remove hooks usage for computing category handle * refactor: remove hooks usage from productCategory model * refactor: remove hooks from DML * refactor: remove cruft * order dml * cleanup * re add foerign key indexes * wip * chore: remove unused types * wip * changes * rm raw * autoincrement * wip * rel * refactor: cleanup * migration and models configuration adjustments * cleanup * number searchable * fix random ordering * fix * test: fix product-category tests * test: update breaking DML tests * test: array assertion to not care about ordering * fix: temporarily apply id ordering for products * types * wip * WIP type improvements * update order models * partially fix types temporarely * rel * fix: recursive type issue * improve type inference breaks * improve type inference breaks * update models * rm nullable * default value * repository * update default value handling * fix unit tests * WIP * toMikroORM * fix relations * cascades * fix * experimental dml hooks * rm migration * serial * nullable autoincrement * fix model * model changes * fix one to one DML * order test * fix addresses * fix unit tests * Re align dml entity name inference * update model table name config * update model table name config * revert * update return relation * WIP * hasOne * models * fix * model * initial commit * cart service * order module * utils unit test * index engine * changeset * merge * fix hasOne with fk * update * free text filter per entity * tests * prod category * property string many to many * fix big number * link modules migration set names * merge * shipping option rules * serializer * unit test * fix test mikro orm init * fix test mikro orm init * Maintain merge object properties * fix test mikro orm init * prevent unit test from connecting to db * wip * fix test * fix test * link test * schema * models * auto increment * hook * model hooks * order * wip * orm version * request return field * fix return configuration on order model * workflows * core flows * unit test * test * base repo * test * base repo * test fix * inventory move * locking inventory * test * free text fix * rm timeout mock * migrate fulfillment values * v6.4.3 * cleanup * link-modules update sql * revert test * remove fake timers --------- Co-authored-by: adrien2p <adrien.deperetti@gmail.com> Co-authored-by: Harminder Virk <virk.officials@gmail.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
adrien2p
Harminder Virk
Oli Juhl
parent
05c8a67d8e
commit
cc73802ab3
@@ -1,4 +1,7 @@
|
||||
import { OrderChangeActionDTO } from "@medusajs/framework/types"
|
||||
import {
|
||||
InferEntityType,
|
||||
OrderChangeActionDTO,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
ChangeActionType,
|
||||
MathBN,
|
||||
@@ -22,8 +25,9 @@ export function applyChangesToOrder(
|
||||
addActionReferenceToObject?: boolean
|
||||
}
|
||||
) {
|
||||
const itemsToUpsert: OrderItem[] = []
|
||||
const shippingMethodsToUpsert: OrderShippingMethod[] = []
|
||||
const itemsToUpsert: InferEntityType<typeof OrderItem>[] = []
|
||||
const shippingMethodsToUpsert: InferEntityType<typeof OrderShippingMethod>[] =
|
||||
[]
|
||||
const summariesToUpsert: any[] = []
|
||||
const orderToUpdate: any[] = []
|
||||
|
||||
@@ -69,7 +73,7 @@ export function applyChangesToOrder(
|
||||
const orderItem = isExistingItem ? (item.detail as any) : item
|
||||
const itemId = isExistingItem ? orderItem.item_id : item.id
|
||||
|
||||
itemsToUpsert.push({
|
||||
const itemToUpsert = {
|
||||
id: orderItem.version === version ? orderItem.id : undefined,
|
||||
item_id: itemId,
|
||||
order_id: order.id,
|
||||
@@ -86,7 +90,9 @@ export function applyChangesToOrder(
|
||||
return_dismissed_quantity: orderItem.return_dismissed_quantity ?? 0,
|
||||
written_off_quantity: orderItem.written_off_quantity ?? 0,
|
||||
metadata: orderItem.metadata,
|
||||
} as OrderItem)
|
||||
} as any
|
||||
|
||||
itemsToUpsert.push(itemToUpsert)
|
||||
}
|
||||
|
||||
const orderSummary = order.summary as any
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Constructor, Context, DAL } from "@medusajs/framework/types"
|
||||
import { toMikroORMEntity } from "@medusajs/framework/utils"
|
||||
import { LoadStrategy } from "@mikro-orm/core"
|
||||
import { Order, OrderClaim } from "@models"
|
||||
import { mapRepositoryToOrderModel } from "."
|
||||
@@ -15,7 +16,6 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
const findOptions_ = { ...options } as any
|
||||
findOptions_.options ??= {}
|
||||
findOptions_.where ??= {}
|
||||
findOptions_.populate ??= []
|
||||
|
||||
if (!("strategy" in findOptions_.options)) {
|
||||
if (findOptions_.options.limit != null || findOptions_.options.offset) {
|
||||
@@ -29,12 +29,13 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
}
|
||||
}
|
||||
|
||||
const isRelatedEntity = entity !== Order
|
||||
const isRelatedEntity = entity.name !== Order.name
|
||||
|
||||
const config = mapRepositoryToOrderModel(findOptions_, isRelatedEntity)
|
||||
config.options ??= {}
|
||||
config.options.populate ??= []
|
||||
|
||||
const strategy = config.options.strategy ?? LoadStrategy.JOINED
|
||||
const strategy = findOptions_.options.strategy ?? LoadStrategy.JOINED
|
||||
let orderAlias = "o0"
|
||||
if (isRelatedEntity) {
|
||||
if (entity === OrderClaim) {
|
||||
@@ -65,7 +66,7 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
|
||||
if (strategy === LoadStrategy.SELECT_IN) {
|
||||
const sql = manager
|
||||
.qb(Order, "_sub0")
|
||||
.qb(toMikroORMEntity(Order), "_sub0")
|
||||
.select("version")
|
||||
.where({ id: knex.raw(`"${orderAlias}"."order_id"`) })
|
||||
.getKnexQuery()
|
||||
@@ -74,46 +75,18 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
defaultVersion = knex.raw(`(${sql})`)
|
||||
}
|
||||
|
||||
const version = config.where.version ?? defaultVersion
|
||||
const version = config.where?.version ?? defaultVersion
|
||||
delete config.where?.version
|
||||
|
||||
config.options.populateWhere ??= {}
|
||||
const popWhere = config.options.populateWhere
|
||||
|
||||
if (isRelatedEntity) {
|
||||
popWhere.order ??= {}
|
||||
|
||||
popWhere.shipping_methods ??= {}
|
||||
popWhere.shipping_methods.deleted_at ??= null
|
||||
|
||||
popWhere.shipping_methods.shipping_method ??= {}
|
||||
popWhere.shipping_methods.shipping_method.deleted_at ??= null
|
||||
}
|
||||
|
||||
const orderWhere = isRelatedEntity ? popWhere.order : popWhere
|
||||
|
||||
orderWhere.summary ??= {}
|
||||
orderWhere.summary.version = version
|
||||
|
||||
orderWhere.items ??= {}
|
||||
orderWhere.items.version = version
|
||||
orderWhere.items.deleted_at ??= null
|
||||
|
||||
orderWhere.shipping_methods ??= {}
|
||||
orderWhere.shipping_methods.version = version
|
||||
orderWhere.shipping_methods.deleted_at ??= null
|
||||
|
||||
orderWhere.shipping_methods.shipping_method ??= {}
|
||||
orderWhere.shipping_methods.shipping_method.deleted_at ??= null
|
||||
configurePopulateWhere(config, isRelatedEntity, version)
|
||||
|
||||
if (!config.options.orderBy) {
|
||||
config.options.orderBy = { id: "ASC" }
|
||||
}
|
||||
|
||||
config.where ??= {}
|
||||
config.where.deleted_at ??= null
|
||||
|
||||
return await manager.find(entity, config.where, config.options)
|
||||
return await manager.find(this.entity, config.where, config.options)
|
||||
}
|
||||
|
||||
klass.prototype.findAndCount = async function findAndCount(
|
||||
@@ -134,7 +107,8 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
})
|
||||
}
|
||||
|
||||
const isRelatedEntity = entity !== Order
|
||||
const isRelatedEntity = entity.name !== Order.name
|
||||
|
||||
const config = mapRepositoryToOrderModel(findOptions_, isRelatedEntity)
|
||||
|
||||
let orderAlias = "o0"
|
||||
@@ -148,7 +122,6 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
}
|
||||
}
|
||||
|
||||
// first relation is always order if the entity is not Order
|
||||
const index = config.options.populate.findIndex((p) => p === "order")
|
||||
if (index > -1) {
|
||||
config.options.populate.splice(index, 1)
|
||||
@@ -161,43 +134,107 @@ export function setFindMethods<T>(klass: Constructor<T>, entity: any) {
|
||||
let defaultVersion = knex.raw(`"${orderAlias}"."version"`)
|
||||
const strategy = config.options.strategy ?? LoadStrategy.JOINED
|
||||
if (strategy === LoadStrategy.SELECT_IN) {
|
||||
const sql = manager
|
||||
.qb(Order, "_sub0")
|
||||
.select("version")
|
||||
.where({ id: knex.raw(`"${orderAlias}"."order_id"`) })
|
||||
.getKnexQuery()
|
||||
.toString()
|
||||
|
||||
defaultVersion = knex.raw(`(${sql})`)
|
||||
defaultVersion = getVersionSubQuery(manager, orderAlias)
|
||||
}
|
||||
|
||||
const version = config.where.version ?? defaultVersion
|
||||
delete config.where.version
|
||||
|
||||
config.options.populateWhere ??= {}
|
||||
const popWhere = config.options.populateWhere
|
||||
|
||||
if (isRelatedEntity) {
|
||||
popWhere.order ??= {}
|
||||
}
|
||||
|
||||
const orderWhere = isRelatedEntity ? popWhere.order : popWhere
|
||||
|
||||
orderWhere.summary ??= {}
|
||||
orderWhere.summary.version = version
|
||||
|
||||
orderWhere.items ??= {}
|
||||
orderWhere.items.version = version
|
||||
orderWhere.items.deleted_at ??= null
|
||||
|
||||
popWhere.shipping_methods ??= {}
|
||||
popWhere.shipping_methods.version = version
|
||||
popWhere.shipping_methods.deleted_at ??= null
|
||||
configurePopulateWhere(
|
||||
config,
|
||||
isRelatedEntity,
|
||||
version,
|
||||
strategy === LoadStrategy.SELECT_IN,
|
||||
manager
|
||||
)
|
||||
|
||||
if (!config.options.orderBy) {
|
||||
config.options.orderBy = { id: "ASC" }
|
||||
}
|
||||
|
||||
return await manager.findAndCount(entity, config.where, config.options)
|
||||
return await manager.findAndCount(this.entity, config.where, config.options)
|
||||
}
|
||||
}
|
||||
|
||||
function getVersionSubQuery(manager, alias, field = "order_id") {
|
||||
const knex = manager.getKnex()
|
||||
const sql = manager
|
||||
.qb(toMikroORMEntity(Order), "_sub0")
|
||||
.select("version")
|
||||
.where({ id: knex.raw(`"${alias}"."${field}"`) })
|
||||
.getKnexQuery()
|
||||
.toString()
|
||||
|
||||
return knex.raw(`(${sql})`)
|
||||
}
|
||||
|
||||
function configurePopulateWhere(
|
||||
config: any,
|
||||
isRelatedEntity: boolean,
|
||||
version: any,
|
||||
isSelectIn = false,
|
||||
manager?
|
||||
) {
|
||||
const requestedPopulate = config.options?.populate ?? []
|
||||
const hasRelation = (relation: string) =>
|
||||
requestedPopulate.some(
|
||||
(p) => p === relation || p.startsWith(`${relation}.`)
|
||||
)
|
||||
|
||||
config.options.populateWhere ??= {}
|
||||
const popWhere = config.options.populateWhere
|
||||
|
||||
// isSelectIn && isRelatedEntity - Order is always the FROM clause (field o0.id)
|
||||
if (isRelatedEntity) {
|
||||
popWhere.order ??= {}
|
||||
|
||||
const popWhereOrder = popWhere.order
|
||||
|
||||
popWhereOrder.version = isSelectIn
|
||||
? getVersionSubQuery(manager, "o0", "id")
|
||||
: version
|
||||
|
||||
// related entity shipping method
|
||||
if (hasRelation("shipping_methods")) {
|
||||
popWhere.shipping_methods ??= {}
|
||||
popWhere.shipping_methods.version = isSelectIn
|
||||
? getVersionSubQuery(manager, "s0")
|
||||
: version
|
||||
}
|
||||
|
||||
if (hasRelation("items") || hasRelation("order.items")) {
|
||||
popWhereOrder.items ??= {}
|
||||
popWhereOrder.items.version = isSelectIn
|
||||
? getVersionSubQuery(manager, "o0", "id")
|
||||
: version
|
||||
}
|
||||
|
||||
if (hasRelation("shipping_methods")) {
|
||||
popWhereOrder.shipping_methods ??= {}
|
||||
popWhereOrder.shipping_methods.version = isSelectIn
|
||||
? getVersionSubQuery(manager, "o0", "id")
|
||||
: version
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (isSelectIn) {
|
||||
version = getVersionSubQuery(manager, "o0")
|
||||
}
|
||||
|
||||
if (hasRelation("summary")) {
|
||||
popWhere.summary ??= {}
|
||||
popWhere.summary.version = version
|
||||
}
|
||||
|
||||
if (hasRelation("items") || hasRelation("order.items")) {
|
||||
popWhere.items ??= {}
|
||||
popWhere.items.version = version
|
||||
}
|
||||
|
||||
if (hasRelation("shipping_methods")) {
|
||||
popWhere.shipping_methods ??= {}
|
||||
popWhere.shipping_methods.version = version
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
deduplicate,
|
||||
isDefined,
|
||||
isObject,
|
||||
toMikroORMEntity,
|
||||
} from "@medusajs/framework/utils"
|
||||
|
||||
// Reshape the order object to match the OrderDTO
|
||||
@@ -20,8 +21,9 @@ export function formatOrder<T = any>(
|
||||
|
||||
orders.map((order) => {
|
||||
let mainOrder = order
|
||||
const entity = options?.entity ? toMikroORMEntity(options.entity) : null
|
||||
|
||||
const isRelatedEntity = options?.entity?.name !== "Order"
|
||||
const isRelatedEntity = entity?.name !== "Order"
|
||||
|
||||
// If the entity is a related entity, the original order is located in the order property
|
||||
if (isRelatedEntity) {
|
||||
|
||||
Reference in New Issue
Block a user