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:
co-authored by
Riqwan Thamir
parent
ef5024980d
commit
b6ac768698
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user