fix: Accept filters in softDelete + fulfillment location clean-up (#7198)
This commit is contained in:
@@ -14,7 +14,6 @@ import {
|
||||
LoadStrategy,
|
||||
ReferenceType,
|
||||
RequiredEntityData,
|
||||
wrap,
|
||||
} from "@mikro-orm/core"
|
||||
import { FindOptions as MikroOptions } from "@mikro-orm/core/drivers/IDatabaseDriver"
|
||||
import {
|
||||
@@ -25,9 +24,9 @@ import {
|
||||
} from "@mikro-orm/core/typings"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
MedusaError,
|
||||
arrayDifference,
|
||||
isString,
|
||||
MedusaError,
|
||||
promiseAll,
|
||||
} from "../../common"
|
||||
import { buildQuery } from "../../modules-sdk"
|
||||
@@ -35,9 +34,9 @@ import {
|
||||
getSoftDeletedCascadedEntitiesIdsMappedBy,
|
||||
transactionWrapper,
|
||||
} from "../utils"
|
||||
import { mikroOrmUpdateDeletedAtRecursively } from "./utils"
|
||||
import { mikroOrmSerializer } from "./mikro-orm-serializer"
|
||||
import { dbErrorMapper } from "./db-error-mapper"
|
||||
import { mikroOrmSerializer } from "./mikro-orm-serializer"
|
||||
import { mikroOrmUpdateDeletedAtRecursively } from "./utils"
|
||||
|
||||
export class MikroOrmBase<T = any> {
|
||||
readonly manager_: any
|
||||
@@ -101,6 +100,17 @@ export class MikroOrmBaseRepository<T extends object = object>
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
static buildUniqueCompositeKeyValue(keys: string[], data: object) {
|
||||
return keys.map((k) => data[k]).join("_")
|
||||
}
|
||||
|
||||
static retrievePrimaryKeys(entity: EntityClass<any> | EntitySchema) {
|
||||
return (
|
||||
(entity as EntitySchema).meta?.primaryKeys ??
|
||||
(entity as EntityClass<any>).prototype.__meta.primaryKeys ?? ["id"]
|
||||
)
|
||||
}
|
||||
|
||||
create(data: unknown[], context?: Context): Promise<T[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
@@ -142,21 +152,14 @@ export class MikroOrmBaseRepository<T extends object = object>
|
||||
}
|
||||
|
||||
async softDelete(
|
||||
idsOrFilter: string[] | InternalFilterQuery,
|
||||
filters:
|
||||
| string
|
||||
| string[]
|
||||
| (FilterQuery<T> & BaseFilterable<FilterQuery<T>>)
|
||||
| (FilterQuery<T> & BaseFilterable<FilterQuery<T>>)[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<[T[], Record<string, unknown[]>]> {
|
||||
const isArray = Array.isArray(idsOrFilter)
|
||||
// TODO handle composite keys
|
||||
const filter =
|
||||
isArray || isString(idsOrFilter)
|
||||
? {
|
||||
id: {
|
||||
$in: isArray ? idsOrFilter : [idsOrFilter],
|
||||
},
|
||||
}
|
||||
: idsOrFilter
|
||||
|
||||
const entities = await this.find({ where: filter as any }, sharedContext)
|
||||
const entities = await this.find({ where: filters as any }, sharedContext)
|
||||
const date = new Date()
|
||||
|
||||
const manager = this.getActiveManager<SqlEntityManager>(sharedContext)
|
||||
@@ -286,17 +289,6 @@ export function mikroOrmBaseRepositoryFactory<T extends object = object>(
|
||||
})
|
||||
}
|
||||
|
||||
static buildUniqueCompositeKeyValue(keys: string[], data: object) {
|
||||
return keys.map((k) => data[k]).join("_")
|
||||
}
|
||||
|
||||
static retrievePrimaryKeys(entity: EntityClass<T> | EntitySchema<T>) {
|
||||
return (
|
||||
(entity as EntitySchema<T>).meta?.primaryKeys ??
|
||||
(entity as EntityClass<T>).prototype.__meta.primaryKeys ?? ["id"]
|
||||
)
|
||||
}
|
||||
|
||||
async create(data: any[], context?: Context): Promise<T[]> {
|
||||
const manager = this.getActiveManager<EntityManager>(context)
|
||||
|
||||
@@ -768,6 +760,32 @@ export function mikroOrmBaseRepositoryFactory<T extends object = object>(
|
||||
|
||||
return orderedEntities
|
||||
}
|
||||
|
||||
async softDelete(
|
||||
filters:
|
||||
| string
|
||||
| string[]
|
||||
| (FilterQuery<T> & BaseFilterable<FilterQuery<T>>)
|
||||
| (FilterQuery<T> & BaseFilterable<FilterQuery<T>>)[],
|
||||
sharedContext: Context = {}
|
||||
): Promise<[T[], Record<string, unknown[]>]> {
|
||||
const primaryKeys =
|
||||
MikroOrmAbstractBaseRepository_.retrievePrimaryKeys(entity)
|
||||
|
||||
const filterArray = Array.isArray(filters) ? filters : [filters]
|
||||
const normalizedFilters: FilterQuery = {
|
||||
$or: filterArray.map((filter) => {
|
||||
// TODO: add support for composite keys
|
||||
if (isString(filter)) {
|
||||
return { [primaryKeys[0]]: filter }
|
||||
}
|
||||
|
||||
return filter
|
||||
}),
|
||||
}
|
||||
|
||||
return await super.softDelete(normalizedFilters, sharedContext)
|
||||
}
|
||||
}
|
||||
|
||||
return MikroOrmAbstractBaseRepository_ as unknown as {
|
||||
|
||||
@@ -44,11 +44,11 @@ export const LINKS = {
|
||||
Modules.STOCK_LOCATION,
|
||||
"location_id"
|
||||
),
|
||||
FulfillmentSetLocation: composeLinkName(
|
||||
Modules.FULFILLMENT,
|
||||
"fulfillment_set_id",
|
||||
LocationFulfillmentSet: composeLinkName(
|
||||
Modules.STOCK_LOCATION,
|
||||
"location_id"
|
||||
"stock_location_id",
|
||||
Modules.FULFILLMENT,
|
||||
"fulfillment_set_id"
|
||||
),
|
||||
OrderPromotion: composeLinkName(
|
||||
Modules.ORDER,
|
||||
|
||||
@@ -12,11 +12,11 @@ import {
|
||||
SoftDeleteReturn,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
MapToConfig,
|
||||
isString,
|
||||
kebabCase,
|
||||
lowerCaseFirst,
|
||||
mapObjectTo,
|
||||
MapToConfig,
|
||||
pluralize,
|
||||
upperCaseFirst,
|
||||
} from "../common"
|
||||
|
||||
@@ -19,13 +19,13 @@ import {
|
||||
MedusaError,
|
||||
shouldForceTransaction,
|
||||
} from "../common"
|
||||
import { FreeTextSearchFilterKey } from "../dal"
|
||||
import { buildQuery } from "./build-query"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
} from "./decorators"
|
||||
import { FreeTextSearchFilterKey } from "../dal"
|
||||
|
||||
type SelectorAndData = {
|
||||
selector: FilterQuery<any> | BaseFilterable<FilterQuery<any>>
|
||||
@@ -440,10 +440,17 @@ export function internalModuleServiceFactory<
|
||||
|
||||
@InjectTransactionManager(propertyRepositoryName)
|
||||
async softDelete(
|
||||
idsOrFilter: string[] | InternalFilterQuery,
|
||||
idsOrFilter:
|
||||
| string
|
||||
| string[]
|
||||
| InternalFilterQuery
|
||||
| InternalFilterQuery[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], Record<string, unknown[]>]> {
|
||||
if (Array.isArray(idsOrFilter) && !idsOrFilter.length) {
|
||||
if (
|
||||
(Array.isArray(idsOrFilter) && !idsOrFilter.length) ||
|
||||
(!Array.isArray(idsOrFilter) && !idsOrFilter)
|
||||
) {
|
||||
return [[], {}]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user