chore: abstract the modules repository (#6035)
**What** Reduce the work effort to create repositories when building new modules by abstracting the most common cases into the base class default implementation returned by a factory - [x] Migrate all modules Co-authored-by: Riqwan Thamir <5105988+riqwan@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
ef5024980d
commit
b6ac768698
9
.changeset/swift-mice-leave.md
Normal file
9
.changeset/swift-mice-leave.md
Normal file
@@ -0,0 +1,9 @@
|
||||
---
|
||||
"@medusajs/authentication": patch
|
||||
"@medusajs/link-modules": patch
|
||||
"@medusajs/pricing": patch
|
||||
"@medusajs/product": patch
|
||||
"@medusajs/utils": patch
|
||||
---
|
||||
|
||||
chore: Attempt to abstract the modules repository
|
||||
@@ -1,86 +1,16 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { AuthProvider } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Context } from "@medusajs/types"
|
||||
|
||||
export class AuthProviderRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class AuthProviderRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
AuthProvider,
|
||||
{
|
||||
create: RepositoryTypes.CreateAuthProviderDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<AuthProvider> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<AuthProvider[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
AuthProvider,
|
||||
findOptions_.where as MikroFilterQuery<AuthProvider>,
|
||||
findOptions_.options as MikroOptions<AuthProvider>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<AuthProvider> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[AuthProvider[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
AuthProvider,
|
||||
findOptions_.where as MikroFilterQuery<AuthProvider>,
|
||||
findOptions_.options as MikroOptions<AuthProvider>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(AuthProvider, { provider: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreateAuthProviderDTO[],
|
||||
context: Context = {}
|
||||
): Promise<AuthProvider[]> {
|
||||
const manager: SqlEntityManager =
|
||||
this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const authProviders = data.map((authProviderData) => {
|
||||
return manager.create(AuthProvider, authProviderData)
|
||||
})
|
||||
|
||||
manager.persist(authProviders)
|
||||
|
||||
return authProviders
|
||||
}
|
||||
|
||||
>(AuthProvider, "provider") {
|
||||
async update(
|
||||
data: RepositoryTypes.UpdateAuthProviderDTO[],
|
||||
context: Context = {}
|
||||
|
||||
@@ -1,77 +1,17 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { AuthUser } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class AuthUserRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<AuthUser> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<AuthUser[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
AuthUser,
|
||||
findOptions_.where as MikroFilterQuery<AuthUser>,
|
||||
findOptions_.options as MikroOptions<AuthUser>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<AuthUser> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[AuthUser[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
AuthUser,
|
||||
findOptions_.where as MikroFilterQuery<AuthUser>,
|
||||
findOptions_.options as MikroOptions<AuthUser>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(AuthUser, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
export class AuthUserRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
AuthUser
|
||||
) {
|
||||
async create(
|
||||
data: RepositoryTypes.CreateAuthUserDTO[],
|
||||
context: Context = {}
|
||||
): Promise<AuthUser[]> {
|
||||
const manager: SqlEntityManager =
|
||||
this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const toCreate = data.map((authUser) => {
|
||||
const authUserClone = { ...authUser } as any
|
||||
|
||||
@@ -80,13 +20,7 @@ export class AuthUserRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
return authUserClone
|
||||
})
|
||||
|
||||
const authUsers = toCreate.map((authUserData) => {
|
||||
return manager.create(AuthUser, authUserData)
|
||||
})
|
||||
|
||||
manager.persist(authUsers)
|
||||
|
||||
return authUsers
|
||||
return await super.create(toCreate, context)
|
||||
}
|
||||
|
||||
async update(
|
||||
|
||||
@@ -1,77 +1,22 @@
|
||||
import { Context, FindOptions, ModuleJoinerConfig } from "@medusajs/types"
|
||||
import {
|
||||
EntitySchema,
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { Context, ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
|
||||
import {
|
||||
MikroOrmAbstractBaseRepository,
|
||||
generateEntityId,
|
||||
mikroOrmBaseRepositoryFactory,
|
||||
} from "@medusajs/utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export function getLinkRepository(model: EntitySchema) {
|
||||
return class LinkRepository extends MikroOrmAbstractBaseRepository<object> {
|
||||
readonly manager_: SqlEntityManager
|
||||
readonly model_: EntitySchema
|
||||
return class LinkRepository extends mikroOrmBaseRepositoryFactory(model) {
|
||||
readonly joinerConfig_: ModuleJoinerConfig
|
||||
|
||||
constructor({
|
||||
manager,
|
||||
joinerConfig,
|
||||
}: {
|
||||
manager: SqlEntityManager
|
||||
joinerConfig: ModuleJoinerConfig
|
||||
}) {
|
||||
constructor({ joinerConfig }: { joinerConfig: ModuleJoinerConfig }) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
this.model_ = model
|
||||
this.joinerConfig_ = joinerConfig
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: FindOptions<unknown> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<unknown[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
this.model_,
|
||||
findOptions_.where as MikroFilterQuery<unknown>,
|
||||
findOptions_.options as MikroOptions<any>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: FindOptions<object> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[object[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
this.model_,
|
||||
findOptions_.where as MikroFilterQuery<unknown>,
|
||||
findOptions_.options as MikroOptions<any>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(data: any, context: Context = {}): Promise<void> {
|
||||
const filter = {}
|
||||
for (const key in data) {
|
||||
@@ -81,7 +26,7 @@ export function getLinkRepository(model: EntitySchema) {
|
||||
}
|
||||
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(this.model_, data, {})
|
||||
await manager.nativeDelete(model, data, {})
|
||||
}
|
||||
|
||||
async create(data: object[], context: Context = {}): Promise<object[]> {
|
||||
@@ -93,10 +38,10 @@ export function getLinkRepository(model: EntitySchema) {
|
||||
this.joinerConfig_.databaseConfig?.idPrefix ?? "link"
|
||||
)
|
||||
|
||||
return manager.create(this.model_, link)
|
||||
return manager.create(model, link)
|
||||
})
|
||||
|
||||
await manager.upsertMany(this.model_, links)
|
||||
await manager.upsertMany(model, links)
|
||||
|
||||
return links
|
||||
}
|
||||
|
||||
@@ -212,7 +212,7 @@ describe("PriceListRule Service", () => {
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'PriceListRule with id(s) "does-not-exist" not found'
|
||||
'PriceListRule with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -283,7 +283,7 @@ describe("PriceRule Service", () => {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('PriceRule with id "undefined" not found')
|
||||
expect(error.message).toEqual('PriceRule with id "" not found')
|
||||
})
|
||||
|
||||
it("should create a priceRule successfully", async () => {
|
||||
|
||||
@@ -370,7 +370,7 @@ describe("PriceSet Service", () => {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('PriceSet with id "undefined" not found')
|
||||
expect(error.message).toEqual('PriceSet with id "" not found')
|
||||
})
|
||||
|
||||
it("should create a priceSet successfully", async () => {
|
||||
|
||||
@@ -213,7 +213,7 @@ describe("PriceListRule Service", () => {
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'PriceListRule with id(s) "does-not-exist" not found'
|
||||
'PriceListRule with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { CreatePriceRuleDTO, IPricingModuleService } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { PriceSetMoneyAmount, initialize } from "../../../../src"
|
||||
import { initialize, PriceSetMoneyAmount } from "../../../../src"
|
||||
import { createCurrencies } from "../../../__fixtures__/currency"
|
||||
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
|
||||
import { createPriceRules } from "../../../__fixtures__/price-rule"
|
||||
@@ -279,7 +279,7 @@ describe("PricingModule Service - PriceRule", () => {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('PriceRule with id "undefined" not found')
|
||||
expect(error.message).toEqual('PriceRule with id "" not found')
|
||||
})
|
||||
|
||||
it("should create a PriceRule successfully", async () => {
|
||||
|
||||
@@ -1,124 +1,12 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { Currency } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class CurrencyRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class CurrencyRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
Currency,
|
||||
{
|
||||
create: RepositoryTypes.CreateCurrencyDTO
|
||||
update: RepositoryTypes.UpdateCurrencyDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<Currency> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<Currency[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
Currency,
|
||||
findOptions_.where as MikroFilterQuery<Currency>,
|
||||
findOptions_.options as MikroOptions<Currency>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<Currency> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[Currency[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
Currency,
|
||||
findOptions_.where as MikroFilterQuery<Currency>,
|
||||
findOptions_.options as MikroOptions<Currency>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(codes: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(Currency, { code: { $in: codes } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreateCurrencyDTO[],
|
||||
context: Context = {}
|
||||
): Promise<Currency[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const currencies = data.map((currencyData) => {
|
||||
return manager.create(Currency, currencyData)
|
||||
})
|
||||
|
||||
manager.persist(currencies)
|
||||
|
||||
return currencies
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdateCurrencyDTO[],
|
||||
context: Context = {}
|
||||
): Promise<Currency[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const currencyCodes = data.map((currencyData) => currencyData.code)
|
||||
const existingCurrencies = await this.find(
|
||||
{
|
||||
where: {
|
||||
code: {
|
||||
$in: currencyCodes,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingCurrencyMap = new Map(
|
||||
existingCurrencies.map<[string, Currency]>((currency) => [
|
||||
currency.code,
|
||||
currency,
|
||||
])
|
||||
)
|
||||
|
||||
const currencies = data.map((currencyData) => {
|
||||
const existingCurrency = existingCurrencyMap.get(currencyData.code)
|
||||
|
||||
if (!existingCurrency) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Currency with code "${currencyData.code}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingCurrency, currencyData)
|
||||
})
|
||||
|
||||
manager.persist(currencies)
|
||||
|
||||
return currencies
|
||||
}
|
||||
}
|
||||
>(Currency, "code") {}
|
||||
|
||||
@@ -1,124 +1,12 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { MoneyAmount } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class MoneyAmountRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class MoneyAmountRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
MoneyAmount,
|
||||
{
|
||||
create: RepositoryTypes.CreateMoneyAmountDTO
|
||||
update: RepositoryTypes.UpdateMoneyAmountDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<MoneyAmount> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<MoneyAmount[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
MoneyAmount,
|
||||
findOptions_.where as MikroFilterQuery<MoneyAmount>,
|
||||
findOptions_.options as MikroOptions<MoneyAmount>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<MoneyAmount> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[MoneyAmount[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
MoneyAmount,
|
||||
findOptions_.where as MikroFilterQuery<MoneyAmount>,
|
||||
findOptions_.options as MikroOptions<MoneyAmount>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(MoneyAmount, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreateMoneyAmountDTO[],
|
||||
context: Context = {}
|
||||
): Promise<MoneyAmount[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const moneyAmounts = data.map((moneyAmountData) => {
|
||||
return manager.create(MoneyAmount, moneyAmountData as any)
|
||||
})
|
||||
|
||||
manager.persist(moneyAmounts)
|
||||
|
||||
return moneyAmounts
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdateMoneyAmountDTO[],
|
||||
context: Context = {}
|
||||
): Promise<MoneyAmount[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const moneyAmountIds = data.map((moneyAmountData) => moneyAmountData.id)
|
||||
const existingMoneyAmounts = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: moneyAmountIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingMoneyAmountMap = new Map(
|
||||
existingMoneyAmounts.map<[string, MoneyAmount]>((moneyAmount) => [
|
||||
moneyAmount.id,
|
||||
moneyAmount,
|
||||
])
|
||||
)
|
||||
|
||||
const moneyAmounts = data.map((moneyAmountData) => {
|
||||
const existingMoneyAmount = existingMoneyAmountMap.get(moneyAmountData.id)
|
||||
|
||||
if (!existingMoneyAmount) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`MoneyAmount with id "${moneyAmountData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingMoneyAmount, moneyAmountData)
|
||||
})
|
||||
|
||||
manager.persist(moneyAmounts)
|
||||
|
||||
return moneyAmounts
|
||||
}
|
||||
}
|
||||
>(MoneyAmount) {}
|
||||
|
||||
@@ -1,77 +1,19 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError, arrayDifference } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceListRuleValue } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class PriceListRuleValueRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class PriceListRuleValueRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceListRuleValue,
|
||||
{
|
||||
update: RepositoryTypes.UpdatePriceListRuleValueDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PriceListRuleValue> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PriceListRuleValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
PriceListRuleValue,
|
||||
findOptions_.where as MikroFilterQuery<PriceListRuleValue>,
|
||||
findOptions_.options as MikroOptions<PriceListRuleValue>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PriceListRuleValue> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PriceListRuleValue[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
PriceListRuleValue,
|
||||
findOptions_.where as MikroFilterQuery<PriceListRuleValue>,
|
||||
findOptions_.options as MikroOptions<PriceListRuleValue>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
await manager.nativeDelete(PriceListRuleValue, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
>(PriceListRuleValue) {
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceListRuleValueDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceListRuleValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const priceListRuleValues = data.map((priceRuleValueData) => {
|
||||
const { price_list_rule_id: priceListRuleId, ...priceRuleValue } =
|
||||
priceRuleValueData
|
||||
@@ -80,62 +22,9 @@ export class PriceListRuleValueRepository extends DALUtils.MikroOrmBaseRepositor
|
||||
priceRuleValue.price_list_rule = priceListRuleId
|
||||
}
|
||||
|
||||
return manager.create(PriceListRuleValue, priceRuleValue)
|
||||
return priceRuleValue
|
||||
})
|
||||
|
||||
manager.persist(priceListRuleValues)
|
||||
|
||||
return priceListRuleValues
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceListRuleValueDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceListRuleValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const priceValueIds = data.map(
|
||||
(priceRuleValueData) => priceRuleValueData.id
|
||||
)
|
||||
const existingPriceValues = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: priceValueIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const dataAndExistingIdDifference = arrayDifference(
|
||||
data.map((d) => d.id),
|
||||
existingPriceValues.map((pv) => pv.id)
|
||||
)
|
||||
|
||||
if (dataAndExistingIdDifference.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PriceListRuleValue with id(s) "${dataAndExistingIdDifference.join(
|
||||
", "
|
||||
)}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
const existingValuesMap = new Map(
|
||||
existingPriceValues.map<[string, PriceListRuleValue]>((priceValue) => [
|
||||
priceValue.id,
|
||||
priceValue,
|
||||
])
|
||||
)
|
||||
|
||||
const priceValues = data.map((priceRuleValueData) => {
|
||||
const existingPriceValue = existingValuesMap.get(priceRuleValueData.id)!
|
||||
|
||||
return manager.assign(existingPriceValue, priceRuleValueData)
|
||||
})
|
||||
|
||||
manager.persist(priceValues)
|
||||
|
||||
return priceValues
|
||||
return await super.create(priceListRuleValues, context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,16 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError, arrayDifference } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceListRule } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class PriceListRuleRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PriceListRule> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PriceListRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
PriceListRule,
|
||||
findOptions_.where as MikroFilterQuery<PriceListRule>,
|
||||
findOptions_.options as MikroOptions<PriceListRule>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PriceListRule> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PriceListRule[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
PriceListRule,
|
||||
findOptions_.where as MikroFilterQuery<PriceListRule>,
|
||||
findOptions_.options as MikroOptions<PriceListRule>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PriceListRule, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
export class PriceListRuleRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
PriceListRule
|
||||
) {
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceListRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceListRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const priceListRule = data.map((priceListRule) => {
|
||||
const {
|
||||
price_list_id: priceListId,
|
||||
@@ -86,60 +26,20 @@ export class PriceListRuleRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
createData.rule_type = ruleTypeId
|
||||
}
|
||||
|
||||
return manager.create(PriceListRule, createData)
|
||||
return createData
|
||||
})
|
||||
|
||||
manager.persist(priceListRule)
|
||||
|
||||
return priceListRule
|
||||
return await super.create(priceListRule, context)
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceListRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceListRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const priceListIds = data.map((priceListRule) => priceListRule.id)
|
||||
const existingPriceListRules = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: priceListIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const dataAndExistingIdDifference = arrayDifference(
|
||||
data.map((d) => d.id),
|
||||
existingPriceListRules.map((plr) => plr.id)
|
||||
)
|
||||
|
||||
if (dataAndExistingIdDifference.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PriceListRule with id(s) "${dataAndExistingIdDifference.join(
|
||||
", "
|
||||
)}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
const existingPriceListRuleMap = new Map(
|
||||
existingPriceListRules.map<[string, PriceListRule]>((priceList) => [
|
||||
priceList.id,
|
||||
priceList,
|
||||
])
|
||||
)
|
||||
|
||||
const priceListRules = data.map((priceListRule) => {
|
||||
const { price_list_id, rule_type_id, ...priceListRuleData } =
|
||||
priceListRule
|
||||
|
||||
const existingPriceListRule = existingPriceListRuleMap.get(
|
||||
priceListRule.id
|
||||
)!
|
||||
|
||||
if (price_list_id) {
|
||||
priceListRuleData.price_list = price_list_id
|
||||
}
|
||||
@@ -148,11 +48,9 @@ export class PriceListRuleRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
priceListRuleData.rule_type = rule_type_id
|
||||
}
|
||||
|
||||
return manager.assign(existingPriceListRule, priceListRuleData)
|
||||
return priceListRuleData
|
||||
})
|
||||
|
||||
manager.persist(priceListRules)
|
||||
|
||||
return priceListRules
|
||||
return await super.update(priceListRules, context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,16 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, GetIsoStringFromDate, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils, GetIsoStringFromDate } from "@medusajs/utils"
|
||||
|
||||
import { PriceList } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class PriceListRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PriceList> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PriceList[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
PriceList,
|
||||
findOptions_.where as MikroFilterQuery<PriceList>,
|
||||
findOptions_.options as MikroOptions<PriceList>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PriceList> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PriceList[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
PriceList,
|
||||
findOptions_.where as MikroFilterQuery<PriceList>,
|
||||
findOptions_.options as MikroOptions<PriceList>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PriceList, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
export class PriceListRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
PriceList
|
||||
) {
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceListDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceList[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const priceLists = data.map((priceListData: any) => {
|
||||
if (!!priceListData.starts_at) {
|
||||
priceListData.starts_at = GetIsoStringFromDate(priceListData.starts_at)
|
||||
@@ -80,48 +20,17 @@ export class PriceListRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
priceListData.ends_at = GetIsoStringFromDate(priceListData.ends_at)
|
||||
}
|
||||
|
||||
return manager.create(PriceList, priceListData)
|
||||
return priceListData
|
||||
})
|
||||
|
||||
manager.persist(priceLists)
|
||||
|
||||
return priceLists
|
||||
return await super.create(priceLists, context)
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceListDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceList[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const priceListIds = data.map((priceListData) => priceListData.id)
|
||||
const existingPriceLists = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: priceListIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingPriceListMap = new Map(
|
||||
existingPriceLists.map<[string, PriceList]>((priceList) => [
|
||||
priceList.id,
|
||||
priceList,
|
||||
])
|
||||
)
|
||||
|
||||
const priceLists = data.map((priceListData: any) => {
|
||||
const existingPriceList = existingPriceListMap.get(priceListData.id)
|
||||
|
||||
if (!existingPriceList) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PriceList with id "${priceListData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
if (!!priceListData.starts_at) {
|
||||
priceListData.starts_at = GetIsoStringFromDate(priceListData.starts_at)
|
||||
}
|
||||
@@ -130,11 +39,9 @@ export class PriceListRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
priceListData.ends_at = GetIsoStringFromDate(priceListData.ends_at)
|
||||
}
|
||||
|
||||
return manager.assign(existingPriceList, priceListData)
|
||||
return priceListData
|
||||
})
|
||||
|
||||
manager.persist(priceLists)
|
||||
|
||||
return priceLists
|
||||
return await super.update(priceLists, context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,77 +1,19 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceRule } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class PriceRuleRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class PriceRuleRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceRule,
|
||||
{
|
||||
update: RepositoryTypes.UpdatePriceRuleDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PriceRule> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PriceRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
PriceRule,
|
||||
findOptions_.where as MikroFilterQuery<PriceRule>,
|
||||
findOptions_.options as MikroOptions<PriceRule>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PriceRule> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PriceRule[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
PriceRule,
|
||||
findOptions_.where as MikroFilterQuery<PriceRule>,
|
||||
findOptions_.options as MikroOptions<PriceRule>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PriceRule, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
>(PriceRule) {
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceRule[]> {
|
||||
const manager: SqlEntityManager =
|
||||
this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const toCreate = data.map((ruleData) => {
|
||||
const ruleDataClone = { ...ruleData } as any
|
||||
|
||||
@@ -83,54 +25,6 @@ export class PriceRuleRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
return ruleDataClone
|
||||
})
|
||||
|
||||
const priceRules = toCreate.map((ruleData) => {
|
||||
return manager.create(PriceRule, ruleData)
|
||||
})
|
||||
|
||||
manager.persist(priceRules)
|
||||
|
||||
return priceRules
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const priceRuleIds = data.map((priceRuleData) => priceRuleData.id)
|
||||
const existingPriceRules = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: priceRuleIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingPriceRulesMap = new Map(
|
||||
existingPriceRules.map<[string, PriceRule]>((priceRule) => [
|
||||
priceRule.id,
|
||||
priceRule,
|
||||
])
|
||||
)
|
||||
|
||||
const priceRules = data.map((priceRuleData) => {
|
||||
const existingPriceRule = existingPriceRulesMap.get(priceRuleData.id)
|
||||
|
||||
if (!existingPriceRule) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PriceRule with id "${priceRuleData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingPriceRule, priceRuleData)
|
||||
})
|
||||
|
||||
manager.persist(priceRules)
|
||||
|
||||
return priceRules
|
||||
return await super.create(toCreate, context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,124 +1,12 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceSetMoneyAmountRules } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class PriceSetMoneyAmountRulesRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class PriceSetMoneyAmountRulesRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceSetMoneyAmountRules,
|
||||
{
|
||||
create: RepositoryTypes.CreatePriceSetMoneyAmountRulesDTO
|
||||
update: RepositoryTypes.UpdatePriceSetMoneyAmountRulesDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PriceSetMoneyAmountRules> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PriceSetMoneyAmountRules[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
PriceSetMoneyAmountRules,
|
||||
findOptions_.where as MikroFilterQuery<PriceSetMoneyAmountRules>,
|
||||
findOptions_.options as MikroOptions<PriceSetMoneyAmountRules>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PriceSetMoneyAmountRules> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PriceSetMoneyAmountRules[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
PriceSetMoneyAmountRules,
|
||||
findOptions_.where as MikroFilterQuery<PriceSetMoneyAmountRules>,
|
||||
findOptions_.options as MikroOptions<PriceSetMoneyAmountRules>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PriceSetMoneyAmountRules, { id: { $in: ids } })
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceSetMoneyAmountRulesDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceSetMoneyAmountRules[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const psmar = data.map((psmarData) => {
|
||||
return manager.create(PriceSetMoneyAmountRules, psmarData as any)
|
||||
})
|
||||
|
||||
manager.persist(psmar)
|
||||
|
||||
return psmar
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceSetMoneyAmountRulesDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceSetMoneyAmountRules[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const psmarIds = data.map((psmar) => psmar.id)
|
||||
const existingRecords = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: psmarIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const psmarMap = new Map(
|
||||
existingRecords.map<[string, PriceSetMoneyAmountRules]>((psmar) => [
|
||||
psmar.id,
|
||||
psmar,
|
||||
])
|
||||
)
|
||||
|
||||
const psmar = data.map((psmarData) => {
|
||||
const existingRecord = psmarMap.get(psmarData.id)
|
||||
|
||||
if (!existingRecord) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PriceSetMoneyAmountRules with id "${psmarData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingRecord, psmarData as any)
|
||||
})
|
||||
|
||||
manager.persist(psmar)
|
||||
|
||||
return psmar
|
||||
}
|
||||
}
|
||||
>(PriceSetMoneyAmountRules) {}
|
||||
|
||||
@@ -1,126 +1,12 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceSetMoneyAmount } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class PriceSetMoneyAmountRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class PriceSetMoneyAmountRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceSetMoneyAmount,
|
||||
{
|
||||
create: RepositoryTypes.CreatePriceSetMoneyAmountDTO
|
||||
update: RepositoryTypes.UpdatePriceSetMoneyAmountDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PriceSetMoneyAmount> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PriceSetMoneyAmount[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
PriceSetMoneyAmount,
|
||||
findOptions_.where as MikroFilterQuery<PriceSetMoneyAmount>,
|
||||
findOptions_.options as MikroOptions<PriceSetMoneyAmount>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PriceSetMoneyAmount> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PriceSetMoneyAmount[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
PriceSetMoneyAmount,
|
||||
findOptions_.where as MikroFilterQuery<PriceSetMoneyAmount>,
|
||||
findOptions_.options as MikroOptions<PriceSetMoneyAmount>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PriceSetMoneyAmount, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceSetMoneyAmountDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceSetMoneyAmount[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const psma = data.map((psmaData) => {
|
||||
return manager.create(
|
||||
PriceSetMoneyAmount,
|
||||
psmaData as unknown as PriceSetMoneyAmount
|
||||
)
|
||||
})
|
||||
|
||||
manager.persist(psma)
|
||||
|
||||
return psma
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceSetMoneyAmountDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceSetMoneyAmount[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const ids = data.map((psmaData) => psmaData.id)
|
||||
const existingPriceSetMoneyAmounts = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: ids,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingPSMAMap = new Map(
|
||||
existingPriceSetMoneyAmounts.map<[string, PriceSetMoneyAmount]>(
|
||||
(psma) => [psma.id, psma]
|
||||
)
|
||||
)
|
||||
|
||||
const priceSetMoneyAmounts = data.map((psmaData) => {
|
||||
const existingPSMA = existingPSMAMap.get(psmaData.id)
|
||||
|
||||
if (!existingPSMA) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PriceSetMoneyAmount with id "${psmaData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingPSMA, psmaData)
|
||||
})
|
||||
|
||||
manager.persist(priceSetMoneyAmounts)
|
||||
|
||||
return priceSetMoneyAmounts
|
||||
}
|
||||
}
|
||||
>(PriceSetMoneyAmount) {}
|
||||
|
||||
@@ -1,124 +1,12 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceSetRuleType } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class PriceSetRuleTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class PriceSetRuleTypeRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceSetRuleType,
|
||||
{
|
||||
create: RepositoryTypes.CreatePriceSetRuleTypeDTO
|
||||
update: RepositoryTypes.UpdatePriceSetRuleTypeDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PriceSetRuleType> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PriceSetRuleType[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
PriceSetRuleType,
|
||||
findOptions_.where as MikroFilterQuery<PriceSetRuleType>,
|
||||
findOptions_.options as MikroOptions<PriceSetRuleType>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PriceSetRuleType> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PriceSetRuleType[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
PriceSetRuleType,
|
||||
findOptions_.where as MikroFilterQuery<PriceSetRuleType>,
|
||||
findOptions_.options as MikroOptions<PriceSetRuleType>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PriceSetRuleType, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceSetRuleTypeDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceSetRuleType[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const priceSets = data.map((priceSetData) => {
|
||||
return manager.create(PriceSetRuleType, priceSetData)
|
||||
})
|
||||
|
||||
manager.persist(priceSets)
|
||||
|
||||
return priceSets
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceSetRuleTypeDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceSetRuleType[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const priceSetIds = data.map((priceSetData) => priceSetData.id)
|
||||
const existingPriceSets = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: priceSetIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingPriceSetMap = new Map(
|
||||
existingPriceSets.map<[string, PriceSetRuleType]>((priceSet) => [
|
||||
priceSet.id,
|
||||
priceSet,
|
||||
])
|
||||
)
|
||||
|
||||
const priceSets = data.map((priceSetData) => {
|
||||
const existingPriceSet = existingPriceSetMap.get(priceSetData.id)
|
||||
|
||||
if (!existingPriceSet) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PriceSetRuleType with id "${priceSetData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingPriceSet, priceSetData)
|
||||
})
|
||||
|
||||
manager.persist(priceSets)
|
||||
|
||||
return priceSets
|
||||
}
|
||||
}
|
||||
>(PriceSetRuleType) {}
|
||||
|
||||
@@ -1,124 +1,12 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceSet } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class PriceSetRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class PriceSetRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceSet,
|
||||
{
|
||||
create: RepositoryTypes.CreatePriceSetDTO
|
||||
update: RepositoryTypes.UpdatePriceSetDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PriceSet> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PriceSet[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
PriceSet,
|
||||
findOptions_.where as MikroFilterQuery<PriceSet>,
|
||||
findOptions_.options as MikroOptions<PriceSet>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PriceSet> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PriceSet[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
PriceSet,
|
||||
findOptions_.where as MikroFilterQuery<PriceSet>,
|
||||
findOptions_.options as MikroOptions<PriceSet>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PriceSet, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceSetDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceSet[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const priceSets = data.map((priceSetData) => {
|
||||
return manager.create(PriceSet, priceSetData)
|
||||
})
|
||||
|
||||
manager.persist(priceSets)
|
||||
|
||||
return priceSets
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceSetDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceSet[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const priceSetIds = data.map((priceSetData) => priceSetData.id)
|
||||
const existingPriceSets = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: priceSetIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingPriceSetMap = new Map(
|
||||
existingPriceSets.map<[string, PriceSet]>((priceSet) => [
|
||||
priceSet.id,
|
||||
priceSet,
|
||||
])
|
||||
)
|
||||
|
||||
const priceSets = data.map((priceSetData) => {
|
||||
const existingPriceSet = existingPriceSetMap.get(priceSetData.id)
|
||||
|
||||
if (!existingPriceSet) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PriceSet with id "${priceSetData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingPriceSet, priceSetData)
|
||||
})
|
||||
|
||||
manager.persist(priceSets)
|
||||
|
||||
return priceSets
|
||||
}
|
||||
}
|
||||
>(PriceSet) {}
|
||||
|
||||
@@ -13,14 +13,10 @@ export class PricingRepository
|
||||
extends MikroOrmBase
|
||||
implements PricingRepositoryService
|
||||
{
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
constructor() {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async calculatePrices(
|
||||
|
||||
@@ -1,128 +1,11 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError, validateRuleAttributes } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { RuleType } from "@models"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class RuleTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class RuleTypeRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
RuleType,
|
||||
{
|
||||
create: RepositoryTypes.CreateRuleTypeDTO
|
||||
update: RepositoryTypes.UpdateRuleTypeDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<RuleType> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<RuleType[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
RuleType,
|
||||
findOptions_.where as MikroFilterQuery<RuleType>,
|
||||
findOptions_.options as MikroOptions<RuleType>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<RuleType> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[RuleType[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
RuleType,
|
||||
findOptions_.where as MikroFilterQuery<RuleType>,
|
||||
findOptions_.options as MikroOptions<RuleType>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(RuleType, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreateRuleTypeDTO[],
|
||||
context: Context = {}
|
||||
): Promise<RuleType[]> {
|
||||
validateRuleAttributes(data.map((d) => d.rule_attribute))
|
||||
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const ruleTypes = data.map((ruleTypeData) => {
|
||||
return manager.create(RuleType, ruleTypeData)
|
||||
})
|
||||
|
||||
manager.persist(ruleTypes)
|
||||
|
||||
return ruleTypes
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdateRuleTypeDTO[],
|
||||
context: Context = {}
|
||||
): Promise<RuleType[]> {
|
||||
validateRuleAttributes(data.map((d) => d.rule_attribute))
|
||||
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const ruleTypeIds = data.map((ruleType) => ruleType.id)
|
||||
const existingRuleTypes = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: ruleTypeIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingRuleTypesMap = new Map(
|
||||
existingRuleTypes.map<[string, RuleType]>((ruleType) => [
|
||||
ruleType.id,
|
||||
ruleType,
|
||||
])
|
||||
)
|
||||
|
||||
const ruleTypes = data.map((ruleTypeData) => {
|
||||
const existingRuleType = existingRuleTypesMap.get(ruleTypeData.id)
|
||||
|
||||
if (!existingRuleType) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`RuleType with id "${ruleTypeData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingRuleType, ruleTypeData)
|
||||
})
|
||||
|
||||
manager.persist(ruleTypes)
|
||||
|
||||
return ruleTypes
|
||||
}
|
||||
}
|
||||
>(RuleType) {}
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
validateRuleAttributes,
|
||||
} from "@medusajs/utils"
|
||||
import { RuleType } from "@models"
|
||||
import { ServiceTypes } from "@types"
|
||||
@@ -69,6 +70,7 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
|
||||
data: ServiceTypes.CreateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
validateRuleAttributes(data.map((d) => d.rule_attribute))
|
||||
return (await this.ruleTypeRepository_.create(
|
||||
data,
|
||||
sharedContext
|
||||
@@ -80,6 +82,7 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
|
||||
data: ServiceTypes.UpdateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
validateRuleAttributes(data.map((d) => d.rule_attribute))
|
||||
return (await this.ruleTypeRepository_.update(
|
||||
data,
|
||||
sharedContext
|
||||
|
||||
@@ -26,22 +26,13 @@ export type ReorderConditions = {
|
||||
export const tempReorderRank = 99999
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeRepository<ProductCategory> {
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<ProductCategory> = { where: {} },
|
||||
transformOptions: ProductCategoryTransformOptions = {},
|
||||
context: Context = {}
|
||||
): Promise<ProductCategory[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const manager = super.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
const { includeDescendantsTree } = transformOptions
|
||||
@@ -81,7 +72,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
findOptions: DAL.FindOptions<ProductCategory> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<ProductCategory[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const manager = super.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
for (let productCategory of productCategories) {
|
||||
const whereOptions = {
|
||||
@@ -132,7 +123,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
transformOptions: ProductCategoryTransformOptions = {},
|
||||
context: Context = {}
|
||||
): Promise<[ProductCategory[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const manager = super.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
const { includeDescendantsTree } = transformOptions
|
||||
@@ -171,7 +162,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
}
|
||||
|
||||
async delete(id: string, context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const manager = super.getActiveManager<SqlEntityManager>(context)
|
||||
const productCategory = await manager.findOneOrFail(
|
||||
ProductCategory,
|
||||
{ id },
|
||||
@@ -197,11 +188,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
)
|
||||
|
||||
await this.performReordering(manager, conditions)
|
||||
await (manager as SqlEntityManager).nativeDelete(
|
||||
ProductCategory,
|
||||
{ id: id },
|
||||
{}
|
||||
)
|
||||
await manager.nativeDelete(ProductCategory, { id: id }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
@@ -209,7 +196,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
context: Context = {}
|
||||
): Promise<ProductCategory> {
|
||||
const categoryData = { ...data }
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const manager = super.getActiveManager<SqlEntityManager>(context)
|
||||
const siblings = await manager.find(ProductCategory, {
|
||||
parent_category_id: categoryData?.parent_category_id || null,
|
||||
})
|
||||
@@ -231,7 +218,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
context: Context = {}
|
||||
): Promise<ProductCategory> {
|
||||
const categoryData = { ...data }
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const manager = super.getActiveManager<SqlEntityManager>(context)
|
||||
const productCategory = await manager.findOneOrFail(ProductCategory, { id })
|
||||
|
||||
const conditions = this.fetchReorderConditions(
|
||||
@@ -403,7 +390,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
|
||||
throw new Error("sibling rank is not defined")
|
||||
}
|
||||
|
||||
const rank = shouldIncrementRank ? ++sibling.rank : --sibling.rank
|
||||
const rank = shouldIncrementRank ? ++sibling.rank! : --sibling.rank!
|
||||
|
||||
manager.assign(sibling, { rank })
|
||||
manager.persist(sibling)
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Context, ProductTypes } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { ProductCollection } from "@models"
|
||||
|
||||
type UpdateProductCollection = ProductTypes.UpdateProductCollectionDTO & {
|
||||
@@ -17,71 +11,13 @@ type CreateProductCollection = ProductTypes.CreateProductCollectionDTO & {
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<ProductCollection> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<ProductCollection[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
ProductCollection,
|
||||
findOptions_.where as MikroFilterQuery<ProductCollection>,
|
||||
findOptions_.options as MikroOptions<ProductCollection>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<ProductCollection> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[ProductCollection[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
ProductCollection,
|
||||
findOptions_.where as MikroFilterQuery<ProductCollection>,
|
||||
findOptions_.options as MikroOptions<ProductCollection>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(collectionIds: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(
|
||||
ProductCollection,
|
||||
{ id: { $in: collectionIds } },
|
||||
{}
|
||||
)
|
||||
}
|
||||
|
||||
export class ProductCollectionRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
ProductCollection
|
||||
) {
|
||||
async create(
|
||||
data: CreateProductCollection[],
|
||||
context: Context = {}
|
||||
): Promise<ProductCollection[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const productCollections = data.map((collectionData) => {
|
||||
if (collectionData.product_ids) {
|
||||
collectionData.products = collectionData.product_ids
|
||||
@@ -89,59 +25,26 @@ export class ProductCollectionRepository extends DALUtils.MikroOrmBaseRepository
|
||||
delete collectionData.product_ids
|
||||
}
|
||||
|
||||
return manager.create(ProductCollection, collectionData)
|
||||
return collectionData
|
||||
})
|
||||
|
||||
manager.persist(productCollections)
|
||||
|
||||
return productCollections
|
||||
return await super.create(productCollections, context)
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdateProductCollection[],
|
||||
context: Context = {}
|
||||
): Promise<ProductCollection[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const collectionIds = data.map((collectionData) => collectionData.id)
|
||||
const existingCollections = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: collectionIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingCollectionsMap = new Map(
|
||||
existingCollections.map<[string, ProductCollection]>((collection) => [
|
||||
collection.id,
|
||||
collection,
|
||||
])
|
||||
)
|
||||
|
||||
const productCollections = data.map((collectionData) => {
|
||||
const existingCollection = existingCollectionsMap.get(collectionData.id)
|
||||
|
||||
if (!existingCollection) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`ProductCollection with id "${collectionData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
if (collectionData.product_ids) {
|
||||
collectionData.products = collectionData.product_ids
|
||||
|
||||
delete collectionData.product_ids
|
||||
}
|
||||
|
||||
return manager.assign(existingCollection, collectionData)
|
||||
return collectionData
|
||||
})
|
||||
|
||||
manager.persist(productCollections)
|
||||
|
||||
return productCollections
|
||||
return await super.update(productCollections, context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,12 @@
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { Image, Product } from "@models"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { Image } from "@models"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductImageRepository extends DALUtils.MikroOrmAbstractBaseRepository<Image> {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<Image> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<Image[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
Image,
|
||||
findOptions_.where as MikroFilterQuery<Image>,
|
||||
findOptions_.options as MikroOptions<Image>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<Image> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[Image[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
Image,
|
||||
findOptions_.where as MikroFilterQuery<Image>,
|
||||
findOptions_.options as MikroOptions<Image>
|
||||
)
|
||||
}
|
||||
|
||||
export class ProductImageRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
Image
|
||||
) {
|
||||
async upsert(urls: string[], context: Context = {}): Promise<Image[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
@@ -73,7 +21,7 @@ export class ProductImageRepository extends DALUtils.MikroOrmAbstractBaseReposit
|
||||
context
|
||||
)
|
||||
|
||||
const existingImagesMap = new Map(
|
||||
const existingImagesMap = new Map<string, Image>(
|
||||
existingImages.map<[string, Image]>((img) => [img.url, img])
|
||||
)
|
||||
|
||||
@@ -97,13 +45,4 @@ export class ProductImageRepository extends DALUtils.MikroOrmAbstractBaseReposit
|
||||
|
||||
return upsertedImgs
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(Product, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(data: unknown[], context: Context = {}): Promise<Image[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,16 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { Context } from "@medusajs/types"
|
||||
import {
|
||||
CreateProductOptionValueDTO,
|
||||
UpdateProductOptionValueDTO,
|
||||
} from "../types/services/product-option-value"
|
||||
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { FilterQuery as MikroFilterQuery } from "@mikro-orm/core/typings"
|
||||
import { FindOptions as MikroOptions } from "@mikro-orm/core/drivers/IDatabaseDriver"
|
||||
import { ProductOptionValue } from "@models"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class ProductOptionValueRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<ProductOptionValue> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<ProductOptionValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const findOptions_ = { ...findOptions }
|
||||
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.find(
|
||||
ProductOptionValue,
|
||||
findOptions_.where as MikroFilterQuery<ProductOptionValue>,
|
||||
findOptions_.options as MikroOptions<ProductOptionValue>
|
||||
)
|
||||
}
|
||||
|
||||
export class ProductOptionValueRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
ProductOptionValue
|
||||
) {
|
||||
async upsert(
|
||||
optionValues: (UpdateProductOptionValueDTO | CreateProductOptionValueDTO)[],
|
||||
context: Context = {}
|
||||
@@ -106,9 +81,4 @@ export class ProductOptionValueRepository extends DALUtils.MikroOrmBaseRepositor
|
||||
|
||||
return upsertedOptionValues
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(ProductOptionValue, { id: { $in: ids } }, {})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,74 +1,15 @@
|
||||
import { Context, DAL, ProductTypes } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
import { Context, ProductTypes } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Product, ProductOption } from "@models"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseRepository<ProductOption> {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class ProductOptionRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
ProductOption,
|
||||
{
|
||||
update: ProductTypes.UpdateProductOptionDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<ProductOption> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<ProductOption[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
ProductOption,
|
||||
findOptions_.where as MikroFilterQuery<ProductOption>,
|
||||
findOptions_.options as MikroOptions<ProductOption>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<ProductOption> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[ProductOption[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
ProductOption,
|
||||
findOptions_.where as MikroFilterQuery<ProductOption>,
|
||||
findOptions_.options as MikroOptions<ProductOption>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
await (manager as SqlEntityManager).nativeDelete(
|
||||
ProductOption,
|
||||
{ id: { $in: ids } },
|
||||
{}
|
||||
)
|
||||
}
|
||||
|
||||
>(ProductOption) {
|
||||
async create(
|
||||
data: ProductTypes.CreateProductOptionDTO[],
|
||||
context: Context = {}
|
||||
@@ -82,7 +23,7 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi
|
||||
id: { $in: productIds },
|
||||
})
|
||||
|
||||
const existingProductsMap = new Map(
|
||||
const existingProductsMap = new Map<string, Product>(
|
||||
existingProducts.map<[string, Product]>((product) => [
|
||||
product.id,
|
||||
product,
|
||||
@@ -100,54 +41,10 @@ export class ProductOptionRepository extends DALUtils.MikroOrmAbstractBaseReposi
|
||||
optionData.product_id = product?.id
|
||||
}
|
||||
|
||||
return manager.create(ProductOption, optionData)
|
||||
return optionData
|
||||
})
|
||||
|
||||
manager.persist(productOptions)
|
||||
|
||||
return productOptions
|
||||
}
|
||||
|
||||
async update(
|
||||
data: ProductTypes.UpdateProductOptionDTO[],
|
||||
context: Context = {}
|
||||
): Promise<ProductOption[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const optionIds = data.map((optionData) => optionData.id)
|
||||
const existingOptions = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: optionIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingOptionsMap = new Map(
|
||||
existingOptions.map<[string, ProductOption]>((option) => [
|
||||
option.id,
|
||||
option,
|
||||
])
|
||||
)
|
||||
|
||||
const productOptions = data.map((optionData) => {
|
||||
const existingOption = existingOptionsMap.get(optionData.id)
|
||||
|
||||
if (!existingOption) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`ProductOption with id "${optionData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingOption, optionData)
|
||||
})
|
||||
|
||||
manager.persist(productOptions)
|
||||
|
||||
return productOptions
|
||||
return await super.create(productOptions, context)
|
||||
}
|
||||
|
||||
async upsert(
|
||||
|
||||
@@ -1,123 +1,20 @@
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
import { ProductTag } from "@models"
|
||||
import {
|
||||
Context,
|
||||
CreateProductTagDTO,
|
||||
DAL,
|
||||
UpdateProductTagDTO,
|
||||
UpsertProductTagDTO,
|
||||
} from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
export class ProductTagRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class ProductTagRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
ProductTag,
|
||||
{
|
||||
create: CreateProductTagDTO
|
||||
update: UpdateProductTagDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<ProductTag> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<ProductTag[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const findOptions_ = { ...findOptions }
|
||||
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
ProductTag,
|
||||
findOptions_.where as MikroFilterQuery<ProductTag>,
|
||||
findOptions_.options as MikroOptions<ProductTag>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<ProductTag> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[ProductTag[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
ProductTag,
|
||||
findOptions_.where as MikroFilterQuery<ProductTag>,
|
||||
findOptions_.options as MikroOptions<ProductTag>
|
||||
)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreateProductTagDTO[],
|
||||
context: Context = {}
|
||||
): Promise<ProductTag[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const productTags = data.map((tagData) => {
|
||||
return manager.create(ProductTag, tagData)
|
||||
})
|
||||
|
||||
manager.persist(productTags)
|
||||
|
||||
return productTags
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdateProductTagDTO[],
|
||||
context: Context = {}
|
||||
): Promise<ProductTag[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const tagIds = data.map((tagData) => tagData.id)
|
||||
const existingTags = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: tagIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingTagsMap = new Map(
|
||||
existingTags.map<[string, ProductTag]>((tag) => [tag.id, tag])
|
||||
)
|
||||
|
||||
const productTags = data.map((tagData) => {
|
||||
const existingTag = existingTagsMap.get(tagData.id)
|
||||
|
||||
if (!existingTag) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`ProductTag with id "${tagData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingTag, tagData)
|
||||
})
|
||||
|
||||
manager.persist(productTags)
|
||||
|
||||
return productTags
|
||||
}
|
||||
|
||||
>(ProductTag) {
|
||||
async upsert(
|
||||
tags: UpsertProductTagDTO[],
|
||||
context: Context = {}
|
||||
@@ -166,10 +63,4 @@ export class ProductTagRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
|
||||
return upsertedTags
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
await manager.nativeDelete(ProductTag, { id: { $in: ids } }, {})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,68 +1,19 @@
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
import { ProductType } from "@models"
|
||||
import {
|
||||
Context,
|
||||
CreateProductTypeDTO,
|
||||
DAL,
|
||||
UpdateProductTypeDTO,
|
||||
} from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class ProductTypeRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
ProductType,
|
||||
{
|
||||
create: CreateProductTypeDTO
|
||||
update: UpdateProductTypeDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<ProductType> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<ProductType[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
ProductType,
|
||||
findOptions_.where as MikroFilterQuery<ProductType>,
|
||||
findOptions_.options as MikroOptions<ProductType>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<ProductType> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[ProductType[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
ProductType,
|
||||
findOptions_.where as MikroFilterQuery<ProductType>,
|
||||
findOptions_.options as MikroOptions<ProductType>
|
||||
)
|
||||
}
|
||||
|
||||
>(ProductType) {
|
||||
async upsert(
|
||||
types: CreateProductTypeDTO[],
|
||||
context: Context = {}
|
||||
@@ -112,63 +63,4 @@ export class ProductTypeRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
|
||||
return upsertedTypes
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(ProductType, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreateProductTypeDTO[],
|
||||
context: Context = {}
|
||||
): Promise<ProductType[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const productTypes = data.map((typeData) => {
|
||||
return manager.create(ProductType, typeData)
|
||||
})
|
||||
|
||||
manager.persist(productTypes)
|
||||
|
||||
return productTypes
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdateProductTypeDTO[],
|
||||
context: Context = {}
|
||||
): Promise<ProductType[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const typeIds = data.map((typeData) => typeData.id)
|
||||
const existingTypes = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: typeIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingTypesMap = new Map(
|
||||
existingTypes.map<[string, ProductType]>((type) => [type.id, type])
|
||||
)
|
||||
|
||||
const productTypes = data.map((typeData) => {
|
||||
const existingType = existingTypesMap.get(typeData.id)
|
||||
|
||||
if (!existingType) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`ProductType with id "${typeData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingType, typeData)
|
||||
})
|
||||
|
||||
manager.persist(productTypes)
|
||||
|
||||
return productTypes
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,131 +1,17 @@
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
RequiredEntityData,
|
||||
} from "@mikro-orm/core"
|
||||
import { ProductVariant } from "@models"
|
||||
import { Context, DAL, WithRequiredProperty } from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
DALUtils,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { RequiredEntityData } from "@mikro-orm/core"
|
||||
import { WithRequiredProperty } from "@medusajs/types"
|
||||
import { ProductVariantServiceTypes } from "../types/services"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductVariantRepository extends DALUtils.MikroOrmAbstractBaseRepository<ProductVariant> {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<ProductVariant> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<ProductVariant[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
ProductVariant,
|
||||
findOptions_.where as MikroFilterQuery<ProductVariant>,
|
||||
findOptions_.options as MikroOptions<ProductVariant>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<ProductVariant> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[ProductVariant[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
ProductVariant,
|
||||
findOptions_.where as MikroFilterQuery<ProductVariant>,
|
||||
findOptions_.options as MikroOptions<ProductVariant>
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
): Promise<void> {
|
||||
await (manager as SqlEntityManager).nativeDelete(
|
||||
ProductVariant,
|
||||
{ id: { $in: ids } },
|
||||
{}
|
||||
)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RequiredEntityData<ProductVariant>[],
|
||||
context: Context = {}
|
||||
): Promise<ProductVariant[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const variants = data.map((variant) => {
|
||||
return (manager as SqlEntityManager).create(ProductVariant, variant)
|
||||
})
|
||||
|
||||
manager.persist(variants)
|
||||
|
||||
return variants
|
||||
}
|
||||
|
||||
async update(
|
||||
data: WithRequiredProperty<
|
||||
export class ProductVariantRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
ProductVariant,
|
||||
{
|
||||
create: RequiredEntityData<ProductVariant>
|
||||
update: WithRequiredProperty<
|
||||
ProductVariantServiceTypes.UpdateProductVariantDTO,
|
||||
"id"
|
||||
>[],
|
||||
context: Context = {}
|
||||
): Promise<ProductVariant[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const productVariantsToUpdate = await manager.find(ProductVariant, {
|
||||
id: data.map((updateData) => updateData.id),
|
||||
})
|
||||
|
||||
const productVariantsToUpdateMap = new Map<string, ProductVariant>(
|
||||
productVariantsToUpdate.map((variant) => [variant.id, variant])
|
||||
)
|
||||
|
||||
const variants = data.map((variantData) => {
|
||||
const productVariant = productVariantsToUpdateMap.get(variantData.id)
|
||||
|
||||
if (!productVariant) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`ProductVariant with id "${variantData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(productVariant, variantData)
|
||||
})
|
||||
|
||||
manager.persist(variants)
|
||||
|
||||
return variants
|
||||
>
|
||||
}
|
||||
}
|
||||
>(ProductVariant) {}
|
||||
|
||||
@@ -6,12 +6,6 @@ import {
|
||||
ProductType,
|
||||
} from "@models"
|
||||
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import {
|
||||
Context,
|
||||
DAL,
|
||||
@@ -24,29 +18,19 @@ import { DALUtils, isDefined, MedusaError, promiseAll } from "@medusajs/utils"
|
||||
import { ProductServiceTypes } from "../types/services"
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<Product> {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class ProductRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
Product,
|
||||
{
|
||||
create: WithRequiredProperty<ProductTypes.CreateProductOnlyDTO, "status">
|
||||
}
|
||||
|
||||
>(Product) {
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<Product & { q?: string }> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<Product[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
await this.mutateNotInCategoriesConstraints(findOptions_)
|
||||
|
||||
this.applyFreeTextSearchFilters<Product>(
|
||||
@@ -54,26 +38,16 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<P
|
||||
this.getFreeTextSearchConstraints
|
||||
)
|
||||
|
||||
return await manager.find(
|
||||
Product,
|
||||
findOptions_.where as MikroFilterQuery<Product>,
|
||||
findOptions_.options as MikroOptions<Product>
|
||||
)
|
||||
return await super.find(findOptions_, context)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<Product & { q?: string }> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[Product[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
await this.mutateNotInCategoriesConstraints(findOptions_)
|
||||
|
||||
this.applyFreeTextSearchFilters<Product>(
|
||||
@@ -81,12 +55,9 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<P
|
||||
this.getFreeTextSearchConstraints
|
||||
)
|
||||
|
||||
return await manager.findAndCount(
|
||||
Product,
|
||||
findOptions_.where as MikroFilterQuery<Product>,
|
||||
findOptions_.options as MikroOptions<Product>
|
||||
)
|
||||
return await super.findAndCount(findOptions_, context)
|
||||
}
|
||||
|
||||
/**
|
||||
* In order to be able to have a strict not in categories, and prevent a product
|
||||
* to be return in the case it also belongs to other categories, we need to
|
||||
@@ -127,26 +98,6 @@ export class ProductRepository extends DALUtils.MikroOrmAbstractBaseRepository<P
|
||||
}
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(Product, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: WithRequiredProperty<ProductTypes.CreateProductOnlyDTO, "status">[],
|
||||
context: Context = {}
|
||||
): Promise<Product[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const products = data.map((product) => {
|
||||
return (manager as SqlEntityManager).create(Product, product)
|
||||
})
|
||||
|
||||
manager.persist(products)
|
||||
|
||||
return products
|
||||
}
|
||||
|
||||
async update(
|
||||
data: WithRequiredProperty<ProductServiceTypes.UpdateProductDTO, "id">[],
|
||||
context: Context = {}
|
||||
|
||||
@@ -359,7 +359,7 @@ describe("Promotion Service", () => {
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toContain('Promotion with id "undefined" not found')
|
||||
expect(error.message).toContain('Promotion with id "" not found')
|
||||
})
|
||||
|
||||
it("should update the attributes of a promotion successfully", async () => {
|
||||
|
||||
@@ -1,121 +1,11 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { ApplicationMethod } from "@models"
|
||||
import {
|
||||
CreateApplicationMethodDTO,
|
||||
UpdateApplicationMethodDTO,
|
||||
} from "../types"
|
||||
import { CreateApplicationMethodDTO, UpdateApplicationMethodDTO } from "@types"
|
||||
|
||||
export class ApplicationMethodRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class ApplicationMethodRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
ApplicationMethod,
|
||||
{
|
||||
create: CreateApplicationMethodDTO
|
||||
update: UpdateApplicationMethodDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<ApplicationMethod> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<ApplicationMethod[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.find(
|
||||
ApplicationMethod,
|
||||
findOptions_.where as MikroFilterQuery<ApplicationMethod>,
|
||||
findOptions_.options as MikroOptions<ApplicationMethod>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<ApplicationMethod> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[ApplicationMethod[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.findAndCount(
|
||||
ApplicationMethod,
|
||||
findOptions_.where as MikroFilterQuery<ApplicationMethod>,
|
||||
findOptions_.options as MikroOptions<ApplicationMethod>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
await manager.nativeDelete(ApplicationMethod, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreateApplicationMethodDTO[],
|
||||
context: Context = {}
|
||||
): Promise<ApplicationMethod[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const applicationMethods = data.map((applicationMethodData) => {
|
||||
return manager.create(ApplicationMethod, applicationMethodData)
|
||||
})
|
||||
|
||||
manager.persist(applicationMethods)
|
||||
|
||||
return applicationMethods
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdateApplicationMethodDTO[],
|
||||
context: Context = {}
|
||||
): Promise<ApplicationMethod[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const applicationMethodIds = data.map(
|
||||
(applicationMethodData) => applicationMethodData.id
|
||||
)
|
||||
const existingApplicationMethods = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: applicationMethodIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingApplicationMethodMap = new Map(
|
||||
existingApplicationMethods.map<[string, ApplicationMethod]>(
|
||||
(applicationMethod) => [applicationMethod.id, applicationMethod]
|
||||
)
|
||||
)
|
||||
|
||||
const applicationMethods = data.map((applicationMethodData) => {
|
||||
const existingApplicationMethod = existingApplicationMethodMap.get(
|
||||
applicationMethodData.id
|
||||
)
|
||||
|
||||
if (!existingApplicationMethod) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`ApplicationMethod with id "${applicationMethodData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingApplicationMethod, applicationMethodData)
|
||||
})
|
||||
|
||||
manager.persist(applicationMethods)
|
||||
|
||||
return applicationMethods
|
||||
}
|
||||
}
|
||||
>(ApplicationMethod) {}
|
||||
|
||||
@@ -1,128 +1,14 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError, arrayDifference } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { PromotionRuleValue } from "@models"
|
||||
import {
|
||||
CreatePromotionRuleValueDTO,
|
||||
UpdatePromotionRuleValueDTO,
|
||||
} from "@types"
|
||||
|
||||
export class PromotionRuleValueRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class PromotionRuleValueRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PromotionRuleValue,
|
||||
{
|
||||
create: CreatePromotionRuleValueDTO
|
||||
update: UpdatePromotionRuleValueDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PromotionRuleValue> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PromotionRuleValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.find(
|
||||
PromotionRuleValue,
|
||||
findOptions_.where as MikroFilterQuery<PromotionRuleValue>,
|
||||
findOptions_.options as MikroOptions<PromotionRuleValue>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PromotionRuleValue> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PromotionRuleValue[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.findAndCount(
|
||||
PromotionRuleValue,
|
||||
findOptions_.where as MikroFilterQuery<PromotionRuleValue>,
|
||||
findOptions_.options as MikroOptions<PromotionRuleValue>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PromotionRuleValue, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreatePromotionRuleValueDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PromotionRuleValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const promotionRuleValue = data.map((promotionRuleValue) => {
|
||||
return manager.create(PromotionRuleValue, promotionRuleValue)
|
||||
})
|
||||
|
||||
manager.persist(promotionRuleValue)
|
||||
|
||||
return promotionRuleValue
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdatePromotionRuleValueDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PromotionRuleValue[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const promotionRuleValueIds = data.map(
|
||||
(promotionRuleValue) => promotionRuleValue.id
|
||||
)
|
||||
const existingPromotionRuleValues = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: promotionRuleValueIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const dataAndExistingIdDifference = arrayDifference(
|
||||
data.map((d) => d.id),
|
||||
existingPromotionRuleValues.map((plr) => plr.id)
|
||||
)
|
||||
|
||||
if (dataAndExistingIdDifference.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PromotionRuleValue with id(s) "${dataAndExistingIdDifference.join(
|
||||
", "
|
||||
)}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
const existingPromotionRuleValueMap = new Map(
|
||||
existingPromotionRuleValues.map<[string, PromotionRuleValue]>(
|
||||
(promotionRuleValue) => [promotionRuleValue.id, promotionRuleValue]
|
||||
)
|
||||
)
|
||||
|
||||
const promotionRuleValue = data.map((promotionRuleValueData) => {
|
||||
const existingPromotionRuleValue = existingPromotionRuleValueMap.get(
|
||||
promotionRuleValueData.id
|
||||
)!
|
||||
|
||||
return manager.assign(existingPromotionRuleValue, promotionRuleValueData)
|
||||
})
|
||||
|
||||
manager.persist(promotionRuleValue)
|
||||
|
||||
return promotionRuleValue
|
||||
}
|
||||
}
|
||||
>(PromotionRuleValue) {}
|
||||
|
||||
@@ -1,124 +1,11 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError, arrayDifference } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { PromotionRule } from "@models"
|
||||
import { CreatePromotionRuleDTO, UpdatePromotionRuleDTO } from "@types"
|
||||
|
||||
export class PromotionRuleRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class PromotionRuleRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PromotionRule,
|
||||
{
|
||||
create: CreatePromotionRuleDTO
|
||||
update: UpdatePromotionRuleDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<PromotionRule> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<PromotionRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.find(
|
||||
PromotionRule,
|
||||
findOptions_.where as MikroFilterQuery<PromotionRule>,
|
||||
findOptions_.options as MikroOptions<PromotionRule>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<PromotionRule> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[PromotionRule[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
return await manager.findAndCount(
|
||||
PromotionRule,
|
||||
findOptions_.where as MikroFilterQuery<PromotionRule>,
|
||||
findOptions_.options as MikroOptions<PromotionRule>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(PromotionRule, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreatePromotionRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PromotionRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const promotionRule = data.map((promotionRule) => {
|
||||
return manager.create(PromotionRule, promotionRule)
|
||||
})
|
||||
|
||||
manager.persist(promotionRule)
|
||||
|
||||
return promotionRule
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdatePromotionRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PromotionRule[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const promotionRuleIds = data.map((promotionRule) => promotionRule.id)
|
||||
const existingPromotionRules = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: promotionRuleIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const dataAndExistingIdDifference = arrayDifference(
|
||||
data.map((d) => d.id),
|
||||
existingPromotionRules.map((plr) => plr.id)
|
||||
)
|
||||
|
||||
if (dataAndExistingIdDifference.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`PromotionRule with id(s) "${dataAndExistingIdDifference.join(
|
||||
", "
|
||||
)}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
const existingPromotionRuleMap = new Map(
|
||||
existingPromotionRules.map<[string, PromotionRule]>((promotionRule) => [
|
||||
promotionRule.id,
|
||||
promotionRule,
|
||||
])
|
||||
)
|
||||
|
||||
const promotionRule = data.map((promotionRuleData) => {
|
||||
const existingPromotionRule = existingPromotionRuleMap.get(
|
||||
promotionRuleData.id
|
||||
)!
|
||||
|
||||
return manager.assign(existingPromotionRule, promotionRuleData)
|
||||
})
|
||||
|
||||
manager.persist(promotionRule)
|
||||
|
||||
return promotionRule
|
||||
}
|
||||
}
|
||||
>(PromotionRule) {}
|
||||
|
||||
@@ -1,124 +1,11 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils, MedusaError } from "@medusajs/utils"
|
||||
import {
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { Promotion } from "@models"
|
||||
import { CreatePromotionDTO, UpdatePromotionDTO } from "../types"
|
||||
import { CreatePromotionDTO, UpdatePromotionDTO } from "@types"
|
||||
|
||||
export class PromotionRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class PromotionRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
Promotion,
|
||||
{
|
||||
create: CreatePromotionDTO
|
||||
Update: UpdatePromotionDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<Promotion> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<Promotion[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
Promotion,
|
||||
findOptions_.where as MikroFilterQuery<Promotion>,
|
||||
findOptions_.options as MikroOptions<Promotion>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<Promotion> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[Promotion[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
Promotion,
|
||||
findOptions_.where as MikroFilterQuery<Promotion>,
|
||||
findOptions_.options as MikroOptions<Promotion>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
await manager.nativeDelete(Promotion, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: CreatePromotionDTO[],
|
||||
context: Context = {}
|
||||
): Promise<Promotion[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const promotions = data.map((promotionData) => {
|
||||
return manager.create(Promotion, promotionData)
|
||||
})
|
||||
|
||||
manager.persist(promotions)
|
||||
|
||||
return promotions
|
||||
}
|
||||
|
||||
async update(
|
||||
data: UpdatePromotionDTO[],
|
||||
context: Context = {}
|
||||
): Promise<Promotion[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
const promotionIds = data.map((promotionData) => promotionData.id)
|
||||
const existingPromotions = await this.find(
|
||||
{
|
||||
where: {
|
||||
id: {
|
||||
$in: promotionIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
context
|
||||
)
|
||||
|
||||
const existingPromotionMap = new Map(
|
||||
existingPromotions.map<[string, Promotion]>((promotion) => [
|
||||
promotion.id,
|
||||
promotion,
|
||||
])
|
||||
)
|
||||
|
||||
const promotions = data.map((promotionData) => {
|
||||
const existingPromotion = existingPromotionMap.get(promotionData.id)
|
||||
|
||||
if (!existingPromotion) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Promotion with id "${promotionData.id}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
return manager.assign(existingPromotion, promotionData)
|
||||
})
|
||||
|
||||
manager.persist(promotions)
|
||||
|
||||
return promotions
|
||||
}
|
||||
}
|
||||
>(Promotion) {}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import {
|
||||
Context,
|
||||
DAL,
|
||||
FilterQuery,
|
||||
FilterQuery as InternalFilerQuery,
|
||||
RepositoryTransformOptions,
|
||||
} from "@medusajs/types"
|
||||
import { isString } from "../../common"
|
||||
import { arrayDifference, isString, MedusaError } from "../../common"
|
||||
import { MedusaContext } from "../../decorators"
|
||||
import { buildQuery, InjectTransactionManager } from "../../modules-sdk"
|
||||
import {
|
||||
@@ -12,9 +12,22 @@ import {
|
||||
transactionWrapper,
|
||||
} from "../utils"
|
||||
import { mikroOrmSerializer, mikroOrmUpdateDeletedAtRecursively } from "./utils"
|
||||
import {
|
||||
EntityManager,
|
||||
EntitySchema,
|
||||
FilterQuery,
|
||||
LoadStrategy,
|
||||
RequiredEntityData,
|
||||
} from "@mikro-orm/core"
|
||||
import {
|
||||
EntityClass,
|
||||
EntityName,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
} from "@mikro-orm/core/typings"
|
||||
import { FindOptions as MikroOptions } from "@mikro-orm/core/drivers/IDatabaseDriver"
|
||||
|
||||
export class MikroOrmBase<T = any> {
|
||||
protected readonly manager_: any
|
||||
readonly manager_: any
|
||||
|
||||
protected constructor({ manager }) {
|
||||
this.manager_ = manager
|
||||
@@ -53,28 +66,47 @@ export class MikroOrmBase<T = any> {
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class MikroOrmAbstractBaseRepository<T = any>
|
||||
extends MikroOrmBase
|
||||
implements DAL.RepositoryService<T>
|
||||
{
|
||||
abstract find(options?: DAL.FindOptions<T>, context?: Context)
|
||||
/**
|
||||
* Privileged extends of the abstract classes unless most of the methods can't be implemented
|
||||
* in your repository. This base repository is also used to provide a base repository
|
||||
* injection if needed to be able to use the common methods without being related to an entity.
|
||||
* In this case, none of the method will be implemented except the manager and transaction
|
||||
* related ones.
|
||||
*/
|
||||
|
||||
abstract findAndCount(
|
||||
options?: DAL.FindOptions<T>,
|
||||
context?: Context
|
||||
): Promise<[T[], number]>
|
||||
|
||||
abstract create(data: unknown[], context?: Context): Promise<T[]>
|
||||
export class MikroOrmBaseRepository<
|
||||
T extends object = object
|
||||
> extends MikroOrmBase<T> {
|
||||
constructor() {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
create(data: unknown[], context?: Context): Promise<T[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
update(data: unknown[], context?: Context): Promise<T[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
abstract delete(ids: string[], context?: Context): Promise<void>
|
||||
delete(ids: string[], context?: Context): Promise<void> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
find(options?: DAL.FindOptions<T>, context?: Context): Promise<T[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
findAndCount(
|
||||
options?: DAL.FindOptions<T>,
|
||||
context?: Context
|
||||
): Promise<[T[], number]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
async softDelete(
|
||||
idsOrFilter: string[] | FilterQuery,
|
||||
idsOrFilter: string[] | InternalFilerQuery,
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
): Promise<[T[], Record<string, unknown[]>]> {
|
||||
@@ -91,7 +123,11 @@ export abstract class MikroOrmAbstractBaseRepository<T = any>
|
||||
const entities = await this.find({ where: filter as any })
|
||||
const date = new Date()
|
||||
|
||||
await mikroOrmUpdateDeletedAtRecursively(manager, entities, date)
|
||||
await mikroOrmUpdateDeletedAtRecursively<T>(
|
||||
manager,
|
||||
entities as any[],
|
||||
date
|
||||
)
|
||||
|
||||
const softDeletedEntitiesMap = getSoftDeletedCascadedEntitiesIdsMappedBy({
|
||||
entities,
|
||||
@@ -102,7 +138,7 @@ export abstract class MikroOrmAbstractBaseRepository<T = any>
|
||||
|
||||
@InjectTransactionManager()
|
||||
async restore(
|
||||
idsOrFilter: string[] | FilterQuery,
|
||||
idsOrFilter: string[] | InternalFilerQuery,
|
||||
@MedusaContext()
|
||||
{ transactionManager: manager }: Context = {}
|
||||
): Promise<[T[], Record<string, unknown[]>]> {
|
||||
@@ -122,7 +158,7 @@ export abstract class MikroOrmAbstractBaseRepository<T = any>
|
||||
|
||||
const entities = await this.find(query)
|
||||
|
||||
await mikroOrmUpdateDeletedAtRecursively(manager, entities, null)
|
||||
await mikroOrmUpdateDeletedAtRecursively(manager, entities as any[], null)
|
||||
|
||||
const softDeletedEntitiesMap = getSoftDeletedCascadedEntitiesIdsMappedBy({
|
||||
entities,
|
||||
@@ -151,72 +187,10 @@ export abstract class MikroOrmAbstractBaseRepository<T = any>
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class MikroOrmAbstractTreeRepositoryBase<T = any>
|
||||
extends MikroOrmBase<T>
|
||||
implements DAL.TreeRepositoryService<T>
|
||||
{
|
||||
protected constructor({ manager }) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
abstract find(
|
||||
options?: DAL.FindOptions<T>,
|
||||
transformOptions?: RepositoryTransformOptions,
|
||||
context?: Context
|
||||
)
|
||||
|
||||
abstract findAndCount(
|
||||
options?: DAL.FindOptions<T>,
|
||||
transformOptions?: RepositoryTransformOptions,
|
||||
context?: Context
|
||||
): Promise<[T[], number]>
|
||||
|
||||
abstract create(data: unknown, context?: Context): Promise<T>
|
||||
|
||||
abstract delete(id: string, context?: Context): Promise<void>
|
||||
}
|
||||
|
||||
/**
|
||||
* Privileged extends of the abstract classes unless most of the methods can't be implemented
|
||||
* in your repository. This base repository is also used to provide a base repository
|
||||
* injection if needed to be able to use the common methods without being related to an entity.
|
||||
* In this case, none of the method will be implemented except the manager and transaction
|
||||
* related ones.
|
||||
*/
|
||||
|
||||
export class MikroOrmBaseRepository extends MikroOrmAbstractBaseRepository {
|
||||
constructor({ manager }) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
create(data: unknown[], context?: Context): Promise<any[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
update(data: unknown[], context?: Context): Promise<any[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
delete(ids: string[], context?: Context): Promise<void> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
find(options?: DAL.FindOptions, context?: Context): Promise<any[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
findAndCount(
|
||||
options?: DAL.FindOptions,
|
||||
context?: Context
|
||||
): Promise<[any[], number]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
}
|
||||
|
||||
export class MikroOrmBaseTreeRepository extends MikroOrmAbstractTreeRepositoryBase {
|
||||
constructor({ manager }) {
|
||||
export class MikroOrmBaseTreeRepository<
|
||||
T extends object = object
|
||||
> extends MikroOrmBase<T> {
|
||||
constructor() {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
@@ -225,7 +199,7 @@ export class MikroOrmBaseTreeRepository extends MikroOrmAbstractTreeRepositoryBa
|
||||
options?: DAL.FindOptions,
|
||||
transformOptions?: RepositoryTransformOptions,
|
||||
context?: Context
|
||||
): Promise<any[]> {
|
||||
): Promise<T[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
@@ -233,11 +207,11 @@ export class MikroOrmBaseTreeRepository extends MikroOrmAbstractTreeRepositoryBa
|
||||
options?: DAL.FindOptions,
|
||||
transformOptions?: RepositoryTransformOptions,
|
||||
context?: Context
|
||||
): Promise<[any[], number]> {
|
||||
): Promise<[T[], number]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
create(data: unknown, context?: Context): Promise<any> {
|
||||
create(data: unknown, context?: Context): Promise<T> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
@@ -245,3 +219,127 @@ export class MikroOrmBaseTreeRepository extends MikroOrmAbstractTreeRepositoryBa
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
}
|
||||
|
||||
type DtoBasedMutationMethods = "create" | "update"
|
||||
|
||||
export function mikroOrmBaseRepositoryFactory<
|
||||
T extends object = object,
|
||||
TDTos extends { [K in DtoBasedMutationMethods]?: any } = {
|
||||
[K in DtoBasedMutationMethods]?: any
|
||||
}
|
||||
>(
|
||||
entity: EntityClass<T> | EntitySchema<T> | string,
|
||||
primaryKey: string = "id"
|
||||
) {
|
||||
class MikroOrmAbstractBaseRepository_ extends MikroOrmBaseRepository<T> {
|
||||
async create(data: TDTos["create"][], context?: Context): Promise<T[]> {
|
||||
const manager = this.getActiveManager<EntityManager>(context)
|
||||
|
||||
const entities = data.map((data_) => {
|
||||
return manager.create(
|
||||
entity as EntityName<T>,
|
||||
data_ as RequiredEntityData<T>
|
||||
)
|
||||
})
|
||||
|
||||
manager.persist(entities)
|
||||
|
||||
return entities
|
||||
}
|
||||
|
||||
async update(data: TDTos["update"][], context?: Context): Promise<T[]> {
|
||||
const manager = this.getActiveManager<EntityManager>(context)
|
||||
|
||||
const primaryKeyValues: string[] = data.map((data_) => data_[primaryKey])
|
||||
const existingEntities = await this.find(
|
||||
{
|
||||
where: {
|
||||
[primaryKey]: {
|
||||
$in: primaryKeyValues,
|
||||
},
|
||||
},
|
||||
} as DAL.FindOptions<T>,
|
||||
context
|
||||
)
|
||||
|
||||
const missingEntities = arrayDifference(
|
||||
data.map((d) => d[primaryKey]),
|
||||
existingEntities.map((d: any) => d[primaryKey])
|
||||
)
|
||||
|
||||
if (missingEntities.length) {
|
||||
const entityName = (entity as EntityClass<T>).name ?? entity
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`${entityName} with ${[primaryKey]} "${missingEntities.join(
|
||||
", "
|
||||
)}" not found`
|
||||
)
|
||||
}
|
||||
|
||||
const existingEntitiesMap = new Map(
|
||||
existingEntities.map<[string, T]>((entity_: any) => [
|
||||
entity_[primaryKey],
|
||||
entity_,
|
||||
])
|
||||
)
|
||||
|
||||
const entities = data.map((data_) => {
|
||||
const existingEntity = existingEntitiesMap.get(data_[primaryKey])!
|
||||
return manager.assign(existingEntity, data_ as RequiredEntityData<T>)
|
||||
})
|
||||
|
||||
manager.persist(entities)
|
||||
|
||||
return entities
|
||||
}
|
||||
|
||||
async delete(primaryKeyValues: string[], context?: Context): Promise<void> {
|
||||
const manager = this.getActiveManager<EntityManager>(context)
|
||||
|
||||
await manager.nativeDelete<T>(
|
||||
entity as EntityName<T>,
|
||||
{ [primaryKey]: { $in: primaryKeyValues } } as unknown as FilterQuery<T>
|
||||
)
|
||||
}
|
||||
|
||||
async find(options?: DAL.FindOptions<T>, context?: Context): Promise<T[]> {
|
||||
const manager = this.getActiveManager<EntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...options }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
entity as EntityName<T>,
|
||||
findOptions_.where as MikroFilterQuery<T>,
|
||||
findOptions_.options as MikroOptions<T>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<T> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[T[], number]> {
|
||||
const manager = this.getActiveManager<EntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
entity as EntityName<T>,
|
||||
findOptions_.where as MikroFilterQuery<T>,
|
||||
findOptions_.options as MikroOptions<T>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return MikroOrmAbstractBaseRepository_
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { MedusaError } from "../common"
|
||||
|
||||
type RuleAttributeInput = string | undefined
|
||||
|
||||
export const ReservedPricingRuleAttributes = [
|
||||
|
||||
Reference in New Issue
Block a user