chore: Hide repository creation if they are not custom + add upsert support by default (#6127)
This commit is contained in:
committed by
GitHub
parent
8a8a7183b8
commit
5e655dd59b
@@ -1,8 +1,23 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import * as Models from "@models"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
export * from "./scripts"
|
||||
const migrationScriptOptions = {
|
||||
moduleName: Modules.PRICING,
|
||||
models: Models,
|
||||
pathToMigrations: __dirname + "/migrations",
|
||||
}
|
||||
|
||||
export const runMigrations = ModulesSdkUtils.buildMigrationScript(
|
||||
migrationScriptOptions
|
||||
)
|
||||
export const revertMigration = ModulesSdkUtils.buildRevertMigrationScript(
|
||||
migrationScriptOptions
|
||||
)
|
||||
|
||||
export * from "./initialize"
|
||||
export * from "./types"
|
||||
export * from "./loaders"
|
||||
|
||||
@@ -1,95 +1,10 @@
|
||||
import * as defaultRepositories from "@repositories"
|
||||
import * as defaultServices from "@services"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import * as ModuleModels from "@models"
|
||||
import * as ModuleRepositories from "@repositories"
|
||||
import * as ModuleServices from "@services"
|
||||
|
||||
import { LoaderOptions } from "@medusajs/modules-sdk"
|
||||
import { ModulesSdkTypes } from "@medusajs/types"
|
||||
import { loadCustomRepositories } from "@medusajs/utils"
|
||||
import { asClass } from "awilix"
|
||||
|
||||
export default async ({
|
||||
container,
|
||||
options,
|
||||
}: LoaderOptions<
|
||||
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
||||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
>): Promise<void> => {
|
||||
const customRepositories = (
|
||||
options as ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
)?.repositories
|
||||
|
||||
container.register({
|
||||
currencyService: asClass(defaultServices.CurrencyService).singleton(),
|
||||
moneyAmountService: asClass(defaultServices.MoneyAmountService).singleton(),
|
||||
priceSetService: asClass(defaultServices.PriceSetService).singleton(),
|
||||
ruleTypeService: asClass(defaultServices.RuleTypeService).singleton(),
|
||||
priceSetMoneyAmountRulesService: asClass(
|
||||
defaultServices.PriceSetMoneyAmountRulesService
|
||||
).singleton(),
|
||||
priceRuleService: asClass(defaultServices.PriceRuleService).singleton(),
|
||||
priceSetRuleTypeService: asClass(
|
||||
defaultServices.PriceSetRuleTypeService
|
||||
).singleton(),
|
||||
priceSetMoneyAmountService: asClass(
|
||||
defaultServices.PriceSetMoneyAmountService
|
||||
).singleton(),
|
||||
priceListService: asClass(defaultServices.PriceListService).singleton(),
|
||||
priceListRuleService: asClass(
|
||||
defaultServices.PriceListRuleService
|
||||
).singleton(),
|
||||
priceListRuleValueService: asClass(
|
||||
defaultServices.PriceListRuleValueService
|
||||
).singleton(),
|
||||
})
|
||||
|
||||
if (customRepositories) {
|
||||
loadCustomRepositories({
|
||||
defaultRepositories,
|
||||
customRepositories,
|
||||
container,
|
||||
})
|
||||
} else {
|
||||
loadDefaultRepositories({ container })
|
||||
}
|
||||
}
|
||||
|
||||
function loadDefaultRepositories({ container }) {
|
||||
container.register({
|
||||
baseRepository: asClass(defaultRepositories.BaseRepository).singleton(),
|
||||
pricingRepository: asClass(
|
||||
defaultRepositories.PricingRepository
|
||||
).singleton(),
|
||||
currencyRepository: asClass(
|
||||
defaultRepositories.CurrencyRepository
|
||||
).singleton(),
|
||||
moneyAmountRepository: asClass(
|
||||
defaultRepositories.MoneyAmountRepository
|
||||
).singleton(),
|
||||
priceSetRepository: asClass(
|
||||
defaultRepositories.PriceSetRepository
|
||||
).singleton(),
|
||||
ruleTypeRepository: asClass(
|
||||
defaultRepositories.RuleTypeRepository
|
||||
).singleton(),
|
||||
priceSetMoneyAmountRulesRepository: asClass(
|
||||
defaultRepositories.PriceSetMoneyAmountRulesRepository
|
||||
).singleton(),
|
||||
priceRuleRepository: asClass(
|
||||
defaultRepositories.PriceRuleRepository
|
||||
).singleton(),
|
||||
priceSetRuleTypeRepository: asClass(
|
||||
defaultRepositories.PriceSetRuleTypeRepository
|
||||
).singleton(),
|
||||
priceSetMoneyAmountRepository: asClass(
|
||||
defaultRepositories.PriceSetMoneyAmountRepository
|
||||
).singleton(),
|
||||
priceListRepository: asClass(
|
||||
defaultRepositories.PriceListRepository
|
||||
).singleton(),
|
||||
priceListRuleRepository: asClass(
|
||||
defaultRepositories.PriceListRuleRepository
|
||||
).singleton(),
|
||||
priceListRuleValueRepository: asClass(
|
||||
defaultRepositories.PriceListRuleValueRepository
|
||||
).singleton(),
|
||||
})
|
||||
}
|
||||
export default ModulesSdkUtils.moduleContainerLoaderFactory({
|
||||
moduleModels: ModuleModels,
|
||||
moduleRepositories: ModuleRepositories,
|
||||
moduleServices: ModuleServices,
|
||||
})
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { Currency } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class CurrencyRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
Currency,
|
||||
{
|
||||
create: RepositoryTypes.CreateCurrencyDTO
|
||||
update: RepositoryTypes.UpdateCurrencyDTO
|
||||
}
|
||||
>(Currency) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,2 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
export { CurrencyRepository } from "./currency"
|
||||
export { MoneyAmountRepository } from "./money-amount"
|
||||
export { PriceListRepository } from "./price-list"
|
||||
export { PriceListRuleRepository } from "./price-list-rule"
|
||||
export { PriceListRuleValueRepository } from "./price-list-rule-value"
|
||||
export { PriceRuleRepository } from "./price-rule"
|
||||
export { PriceSetRepository } from "./price-set"
|
||||
export { PriceSetMoneyAmountRepository } from "./price-set-money-amount"
|
||||
export { PriceSetMoneyAmountRulesRepository } from "./price-set-money-amount-rules"
|
||||
export { PriceSetRuleTypeRepository } from "./price-set-rule-type"
|
||||
export { PricingRepository } from "./pricing"
|
||||
export { RuleTypeRepository } from "./rule-type"
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { MoneyAmount } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class MoneyAmountRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
MoneyAmount,
|
||||
{
|
||||
create: RepositoryTypes.CreateMoneyAmountDTO
|
||||
update: RepositoryTypes.UpdateMoneyAmountDTO
|
||||
}
|
||||
>(MoneyAmount) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceListRuleValue } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class PriceListRuleValueRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceListRuleValue,
|
||||
{
|
||||
update: RepositoryTypes.UpdatePriceListRuleValueDTO
|
||||
}
|
||||
>(PriceListRuleValue) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceListRuleValueDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceListRuleValue[]> {
|
||||
const priceListRuleValues = data.map((priceRuleValueData) => {
|
||||
const { price_list_rule_id: priceListRuleId, ...priceRuleValue } =
|
||||
priceRuleValueData
|
||||
|
||||
if (priceListRuleId) {
|
||||
priceRuleValue.price_list_rule = priceListRuleId
|
||||
}
|
||||
|
||||
return priceRuleValue
|
||||
})
|
||||
|
||||
return await super.create(priceListRuleValues, context)
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceListRule } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class PriceListRuleRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
PriceListRule
|
||||
) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceListRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceListRule[]> {
|
||||
const priceListRule = data.map((priceListRule) => {
|
||||
const {
|
||||
price_list_id: priceListId,
|
||||
rule_type_id: ruleTypeId,
|
||||
...createData
|
||||
} = priceListRule
|
||||
|
||||
if (priceListId) {
|
||||
createData.price_list = priceListId
|
||||
}
|
||||
|
||||
if (ruleTypeId) {
|
||||
createData.rule_type = ruleTypeId
|
||||
}
|
||||
|
||||
return createData
|
||||
})
|
||||
|
||||
return await super.create(priceListRule, context)
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceListRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceListRule[]> {
|
||||
const priceListRules = data.map((priceListRule) => {
|
||||
const { price_list_id, rule_type_id, ...priceListRuleData } =
|
||||
priceListRule
|
||||
|
||||
if (price_list_id) {
|
||||
priceListRuleData.price_list = price_list_id
|
||||
}
|
||||
|
||||
if (rule_type_id) {
|
||||
priceListRuleData.rule_type = rule_type_id
|
||||
}
|
||||
|
||||
return priceListRuleData
|
||||
})
|
||||
|
||||
return await super.update(priceListRules, context)
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils, GetIsoStringFromDate } from "@medusajs/utils"
|
||||
|
||||
import { PriceList } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class PriceListRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
PriceList
|
||||
) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceListDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceList[]> {
|
||||
const priceLists = data.map((priceListData: any) => {
|
||||
if (!!priceListData.starts_at) {
|
||||
priceListData.starts_at = GetIsoStringFromDate(priceListData.starts_at)
|
||||
}
|
||||
|
||||
if (!!priceListData.ends_at) {
|
||||
priceListData.ends_at = GetIsoStringFromDate(priceListData.ends_at)
|
||||
}
|
||||
|
||||
return priceListData
|
||||
})
|
||||
|
||||
return await super.create(priceLists, context)
|
||||
}
|
||||
|
||||
async update(
|
||||
data: RepositoryTypes.UpdatePriceListDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceList[]> {
|
||||
const priceLists = data.map((priceListData: any) => {
|
||||
if (!!priceListData.starts_at) {
|
||||
priceListData.starts_at = GetIsoStringFromDate(priceListData.starts_at)
|
||||
}
|
||||
|
||||
if (!!priceListData.ends_at) {
|
||||
priceListData.ends_at = GetIsoStringFromDate(priceListData.ends_at)
|
||||
}
|
||||
|
||||
return priceListData
|
||||
})
|
||||
|
||||
return await super.update(priceLists, context)
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceRule } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class PriceRuleRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceRule,
|
||||
{
|
||||
update: RepositoryTypes.UpdatePriceRuleDTO
|
||||
}
|
||||
>(PriceRule) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreatePriceRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<PriceRule[]> {
|
||||
const toCreate = data.map((ruleData) => {
|
||||
const ruleDataClone = { ...ruleData } as any
|
||||
|
||||
ruleDataClone.rule_type ??= ruleData.rule_type_id
|
||||
ruleDataClone.price_set ??= ruleData.price_set_id
|
||||
ruleDataClone.price_set_money_amount ??=
|
||||
ruleData.price_set_money_amount_id
|
||||
|
||||
return ruleDataClone
|
||||
})
|
||||
|
||||
return await super.create(toCreate, context)
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceSetMoneyAmountRules } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class PriceSetMoneyAmountRulesRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceSetMoneyAmountRules,
|
||||
{
|
||||
create: RepositoryTypes.CreatePriceSetMoneyAmountRulesDTO
|
||||
update: RepositoryTypes.UpdatePriceSetMoneyAmountRulesDTO
|
||||
}
|
||||
>(PriceSetMoneyAmountRules) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceSetMoneyAmount } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class PriceSetMoneyAmountRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceSetMoneyAmount,
|
||||
{
|
||||
create: RepositoryTypes.CreatePriceSetMoneyAmountDTO
|
||||
update: RepositoryTypes.UpdatePriceSetMoneyAmountDTO
|
||||
}
|
||||
>(PriceSetMoneyAmount) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceSetRuleType } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class PriceSetRuleTypeRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceSetRuleType,
|
||||
{
|
||||
create: RepositoryTypes.CreatePriceSetRuleTypeDTO
|
||||
update: RepositoryTypes.UpdatePriceSetRuleTypeDTO
|
||||
}
|
||||
>(PriceSetRuleType) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
import { PriceSet } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class PriceSetRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
PriceSet,
|
||||
{
|
||||
create: RepositoryTypes.CreatePriceSetDTO
|
||||
update: RepositoryTypes.UpdatePriceSetDTO
|
||||
}
|
||||
>(PriceSet) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import { RuleType } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
|
||||
export class RuleTypeRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
RuleType,
|
||||
{
|
||||
create: RepositoryTypes.CreateRuleTypeDTO
|
||||
update: RepositoryTypes.UpdateRuleTypeDTO
|
||||
}
|
||||
>(RuleType) {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
export default (async () => {
|
||||
const { revertMigration } = await import("../migration-down")
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
await revertMigration()
|
||||
})()
|
||||
@@ -1,8 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
export default (async () => {
|
||||
const { runMigrations } = await import("../migration-up")
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
await runMigrations()
|
||||
})()
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from "./migration-up"
|
||||
export * from "./migration-down"
|
||||
@@ -1,44 +0,0 @@
|
||||
import * as PricingModels from "@models"
|
||||
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
|
||||
/**
|
||||
* This script is only valid for mikro orm managers. If a user provide a custom manager
|
||||
* he is in charge of reverting the migrations.
|
||||
* @param options
|
||||
* @param logger
|
||||
* @param moduleDeclaration
|
||||
*/
|
||||
export async function revertMigration({
|
||||
options,
|
||||
logger,
|
||||
}: Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
> = {}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig("pricing", options)!
|
||||
const entities = Object.values(PricingModels) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
try {
|
||||
const migrator = orm.getMigrator()
|
||||
await migrator.down()
|
||||
|
||||
logger?.info("Pricing module migration executed")
|
||||
} catch (error) {
|
||||
logger?.error(`Pricing module migration failed to run - Error: ${error}`)
|
||||
}
|
||||
|
||||
await orm.close()
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as PricingModels from "@models"
|
||||
|
||||
/**
|
||||
* This script is only valid for mikro orm managers. If a user provide a custom manager
|
||||
* he is in charge of running the migrations.
|
||||
* @param options
|
||||
* @param logger
|
||||
* @param moduleDeclaration
|
||||
*/
|
||||
export async function runMigrations({
|
||||
options,
|
||||
logger,
|
||||
}: Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
> = {}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig("pricing", options)!
|
||||
const entities = Object.values(PricingModels) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
try {
|
||||
const migrator = orm.getMigrator()
|
||||
|
||||
const pendingMigrations = await migrator.getPendingMigrations()
|
||||
logger.info(
|
||||
`Running pending migrations: ${JSON.stringify(
|
||||
pendingMigrations,
|
||||
null,
|
||||
2
|
||||
)}`
|
||||
)
|
||||
|
||||
await migrator.up({
|
||||
migrations: pendingMigrations.map((m) => m.name),
|
||||
})
|
||||
|
||||
logger.info("Pricing module migration executed")
|
||||
} catch (error) {
|
||||
logger.error(`Pricing module migration failed to run - Error: ${error}`)
|
||||
}
|
||||
|
||||
await orm.close()
|
||||
}
|
||||
@@ -16,7 +16,7 @@ export default class CurrencyService<
|
||||
update: ServiceTypes.UpdateCurrencyDTO
|
||||
}
|
||||
>(Currency)<TEntity> {
|
||||
constructor({ currencyRepository }: InjectedDependencies) {
|
||||
constructor(container: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { PriceListRuleValue } from "@models"
|
||||
import { ServiceTypes } from "@types"
|
||||
@@ -12,7 +12,6 @@ export default class PriceListRuleValueService<
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: ServiceTypes.CreatePriceListRuleValueDTO
|
||||
update: ServiceTypes.UpdatePriceListRuleValueDTO
|
||||
},
|
||||
{
|
||||
@@ -24,4 +23,22 @@ export default class PriceListRuleValueService<
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: ServiceTypes.CreatePriceListRuleValueDTO[],
|
||||
context: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const priceListRuleValues = data.map((priceRuleValueData) => {
|
||||
const { price_list_rule_id: priceListRuleId, ...priceRuleValue } =
|
||||
priceRuleValueData
|
||||
|
||||
if (priceListRuleId) {
|
||||
priceRuleValue.price_list_rule = priceListRuleId
|
||||
}
|
||||
|
||||
return priceRuleValue
|
||||
})
|
||||
|
||||
return await super.create(priceListRuleValues, context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { PriceListRule } from "@models"
|
||||
import { ServiceTypes } from "@types"
|
||||
@@ -24,4 +24,51 @@ export default class PriceListRuleService<
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: ServiceTypes.CreatePriceListRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const priceListRule = data.map((priceListRule) => {
|
||||
const {
|
||||
price_list_id: priceListId,
|
||||
rule_type_id: ruleTypeId,
|
||||
...createData
|
||||
} = priceListRule
|
||||
|
||||
if (priceListId) {
|
||||
createData.price_list = priceListId
|
||||
}
|
||||
|
||||
if (ruleTypeId) {
|
||||
createData.rule_type = ruleTypeId
|
||||
}
|
||||
|
||||
return createData
|
||||
})
|
||||
|
||||
return await super.create(priceListRule, context)
|
||||
}
|
||||
|
||||
async update(
|
||||
data: ServiceTypes.UpdatePriceListRuleDTO[],
|
||||
context: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const priceListRules = data.map((priceListRule) => {
|
||||
const { price_list_id, rule_type_id, ...priceListRuleData } =
|
||||
priceListRule
|
||||
|
||||
if (price_list_id) {
|
||||
priceListRuleData.price_list = price_list_id
|
||||
}
|
||||
|
||||
if (rule_type_id) {
|
||||
priceListRuleData.rule_type = rule_type_id
|
||||
}
|
||||
|
||||
return priceListRuleData
|
||||
})
|
||||
|
||||
return await super.update(priceListRules, context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { GetIsoStringFromDate, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { PriceList } from "@models"
|
||||
import { ServiceTypes } from "@types"
|
||||
|
||||
@@ -11,10 +11,7 @@ export default class PriceListService<
|
||||
TEntity extends PriceList = PriceList
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: ServiceTypes.CreatePriceListDTO
|
||||
update: ServiceTypes.UpdatePriceListDTO
|
||||
},
|
||||
{},
|
||||
{
|
||||
list: ServiceTypes.FilterablePriceListProps
|
||||
listAndCount: ServiceTypes.FilterablePriceListProps
|
||||
@@ -24,4 +21,36 @@ export default class PriceListService<
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: ServiceTypes.CreatePriceListDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]> {
|
||||
const priceLists = this.normalizePriceListDate(data)
|
||||
return await super.create(priceLists, sharedContext)
|
||||
}
|
||||
|
||||
async update(
|
||||
data: ServiceTypes.UpdatePriceListDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]> {
|
||||
const priceLists = this.normalizePriceListDate(data)
|
||||
return await super.update(priceLists, sharedContext)
|
||||
}
|
||||
|
||||
protected normalizePriceListDate(
|
||||
data: (ServiceTypes.UpdatePriceListDTO | ServiceTypes.CreatePriceListDTO)[]
|
||||
) {
|
||||
return data.map((priceListData: any) => {
|
||||
if (!!priceListData.starts_at) {
|
||||
priceListData.starts_at = GetIsoStringFromDate(priceListData.starts_at)
|
||||
}
|
||||
|
||||
if (!!priceListData.ends_at) {
|
||||
priceListData.ends_at = GetIsoStringFromDate(priceListData.ends_at)
|
||||
}
|
||||
|
||||
return priceListData
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { PriceRule } from "@models"
|
||||
|
||||
@@ -13,7 +13,6 @@ export default class PriceRuleService<
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: ServiceTypes.CreatePriceRuleDTO
|
||||
update: ServiceTypes.UpdatePriceRuleDTO
|
||||
},
|
||||
{
|
||||
@@ -25,4 +24,22 @@ export default class PriceRuleService<
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
async create(
|
||||
data: ServiceTypes.CreatePriceRuleDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]> {
|
||||
const toCreate = data.map((ruleData) => {
|
||||
const ruleDataClone = { ...ruleData } as any
|
||||
|
||||
ruleDataClone.rule_type ??= ruleData.rule_type_id
|
||||
ruleDataClone.price_set ??= ruleData.price_set_id
|
||||
ruleDataClone.price_set_money_amount ??=
|
||||
ruleData.price_set_money_amount_id
|
||||
|
||||
return ruleDataClone
|
||||
})
|
||||
|
||||
return await super.create(toCreate, sharedContext)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ export default class PriceSetMoneyAmountRulesService<
|
||||
{
|
||||
create: ServiceTypes.CreatePriceSetMoneyAmountRulesDTO
|
||||
update: ServiceTypes.UpdatePriceSetMoneyAmountRulesDTO
|
||||
},
|
||||
{
|
||||
list: ServiceTypes.FilterablePriceSetMoneyAmountRulesProps
|
||||
listAndCount: ServiceTypes.FilterablePriceSetMoneyAmountRulesProps
|
||||
}
|
||||
>(PriceSetMoneyAmountRules)<TEntity> {
|
||||
constructor({ priceSetMoneyAmountRulesRepository }: InjectedDependencies) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Context, DAL, FindConfig } from "@medusajs/types"
|
||||
import { InjectManager, MedusaContext, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { PriceSetMoneyAmount } from "@models"
|
||||
import { ServiceTypes } from "@types"
|
||||
|
||||
@@ -14,46 +14,14 @@ export default class PriceSetMoneyAmountService<
|
||||
{
|
||||
create: ServiceTypes.CreatePriceSetMoneyAmountDTO
|
||||
update: ServiceTypes.UpdatePriceSetMoneyAmountDTO
|
||||
},
|
||||
{
|
||||
list: ServiceTypes.FilterablePriceSetMoneyAmountProps
|
||||
listAndCount: ServiceTypes.FilterablePriceSetMoneyAmountProps
|
||||
}
|
||||
>(PriceSetMoneyAmount)<TEntity> {
|
||||
protected readonly priceSetMoneyAmountRepository_: DAL.RepositoryService<TEntity>
|
||||
|
||||
constructor({ priceSetMoneyAmountRepository }: InjectedDependencies) {
|
||||
constructor(container: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
this.priceSetMoneyAmountRepository_ = priceSetMoneyAmountRepository
|
||||
}
|
||||
|
||||
@InjectManager("priceSetMoneyAmountRepository_")
|
||||
async list<TEntityMethod = ServiceTypes.PriceSetMoneyAmountDTO>(
|
||||
filters: ServiceTypes.FilterablePriceSetMoneyAmountProps = {},
|
||||
config: FindConfig<TEntityMethod> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return await this.priceSetMoneyAmountRepository_.find(
|
||||
this.buildQueryForList(filters, config),
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("priceSetMoneyAmountRepository_")
|
||||
async listAndCount<TEntityMethod = ServiceTypes.PriceSetMoneyAmountDTO>(
|
||||
filters: ServiceTypes.FilterablePriceSetMoneyAmountProps = {},
|
||||
config: FindConfig<TEntityMethod> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
return await this.priceSetMoneyAmountRepository_.findAndCount(
|
||||
this.buildQueryForList(filters, config),
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
private buildQueryForList<
|
||||
TEntityMethod = ServiceTypes.PriceSetMoneyAmountDTO
|
||||
>(
|
||||
filters: ServiceTypes.FilterablePriceSetMoneyAmountProps = {},
|
||||
config: FindConfig<TEntityMethod> = {}
|
||||
) {
|
||||
return ModulesSdkUtils.buildQuery<TEntity>(filters, config)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Context, DAL, FindConfig } from "@medusajs/types"
|
||||
import { InjectManager, MedusaContext, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { PriceSetRuleType } from "@models"
|
||||
import { ServiceTypes } from "@types"
|
||||
|
||||
@@ -14,46 +14,14 @@ export default class PriceSetRuleTypeService<
|
||||
{
|
||||
create: ServiceTypes.CreatePriceSetRuleTypeDTO
|
||||
update: ServiceTypes.UpdatePriceSetRuleTypeDTO
|
||||
},
|
||||
{
|
||||
list: ServiceTypes.FilterablePriceSetRuleTypeProps
|
||||
listAndCount: ServiceTypes.FilterablePriceSetRuleTypeProps
|
||||
}
|
||||
>(PriceSetRuleType)<TEntity> {
|
||||
protected readonly priceSetRuleTypeRepository_: DAL.RepositoryService<TEntity>
|
||||
|
||||
constructor({ priceSetRuleTypeRepository }: InjectedDependencies) {
|
||||
constructor(container: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
this.priceSetRuleTypeRepository_ = priceSetRuleTypeRepository
|
||||
}
|
||||
|
||||
@InjectManager("priceSetRuleTypeRepository_")
|
||||
async list<TEntityMethod = ServiceTypes.PriceSetMoneyAmountDTO>(
|
||||
filters: ServiceTypes.FilterablePriceSetRuleTypeProps = {},
|
||||
config: FindConfig<TEntityMethod> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return await this.priceSetRuleTypeRepository_.find(
|
||||
this.buildQueryForList(filters, config),
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("priceSetRuleTypeRepository_")
|
||||
async listAndCount<TEntityMethod = ServiceTypes.PriceSetMoneyAmountDTO>(
|
||||
filters: ServiceTypes.FilterablePriceSetRuleTypeProps = {},
|
||||
config: FindConfig<TEntityMethod> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
return await this.priceSetRuleTypeRepository_.findAndCount(
|
||||
this.buildQueryForList(filters, config),
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
private buildQueryForList<
|
||||
TEntityMethod = ServiceTypes.PriceSetMoneyAmountDTO
|
||||
>(
|
||||
filters: ServiceTypes.FilterablePriceSetRuleTypeProps = {},
|
||||
config: FindConfig<TEntityMethod> = {}
|
||||
) {
|
||||
return ModulesSdkUtils.buildQuery<TEntity>(filters, config)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,44 @@
|
||||
import {
|
||||
Currency,
|
||||
MoneyAmount,
|
||||
PriceList,
|
||||
PriceListRule,
|
||||
PriceListRuleValue,
|
||||
PriceRule,
|
||||
PriceSet,
|
||||
PriceSetMoneyAmount,
|
||||
PriceSetMoneyAmountRules,
|
||||
PriceSetRuleType,
|
||||
RuleType,
|
||||
} from "@models"
|
||||
import { DAL } from "@medusajs/types"
|
||||
import { CreateCurrencyDTO, UpdateCurrencyDTO } from "./currency"
|
||||
import { CreateMoneyAmountDTO, UpdateMoneyAmountDTO } from "./money-amount"
|
||||
import {
|
||||
CreatePriceListRuleValueDTO,
|
||||
UpdatePriceListRuleValueDTO,
|
||||
} from "./price-list-rule-value"
|
||||
import {
|
||||
CreatePriceListRuleDTO,
|
||||
UpdatePriceListRuleDTO,
|
||||
} from "./price-list-rule"
|
||||
import { CreatePriceListDTO, UpdatePriceListDTO } from "./price-list"
|
||||
import { CreatePriceRuleDTO, UpdatePriceRuleDTO } from "./price-rule"
|
||||
import {
|
||||
CreatePriceSetMoneyAmountRulesDTO,
|
||||
UpdatePriceSetMoneyAmountRulesDTO,
|
||||
} from "./price-set-money-amount-rules"
|
||||
import {
|
||||
CreatePriceSetMoneyAmountDTO,
|
||||
UpdatePriceSetMoneyAmountDTO,
|
||||
} from "./price-set-money-amount"
|
||||
import {
|
||||
CreatePriceSetRuleTypeDTO,
|
||||
UpdatePriceSetRuleTypeDTO,
|
||||
} from "./price-set-rule-type"
|
||||
import { CreatePriceSetDTO, UpdatePriceSetDTO } from "./price-set"
|
||||
import { CreateRuleTypeDTO, UpdateRuleTypeDTO } from "./rule-type"
|
||||
|
||||
export * from "./currency"
|
||||
export * from "./money-amount"
|
||||
export * from "./price-list-rule-value"
|
||||
@@ -9,3 +50,119 @@ export * from "./price-set-money-amount"
|
||||
export * from "./price-set-rule-type"
|
||||
export * from "./price-set"
|
||||
export * from "./rule-type"
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface ICurrencyRepository<TEntity extends Currency = Currency>
|
||||
extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreateCurrencyDTO
|
||||
update: UpdateCurrencyDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IMoneyAmountRepository<
|
||||
TEntity extends MoneyAmount = MoneyAmount
|
||||
> extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreateMoneyAmountDTO
|
||||
update: UpdateMoneyAmountDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IPriceListRuleValueRepository<
|
||||
TEntity extends PriceListRuleValue = PriceListRuleValue
|
||||
> extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreatePriceListRuleValueDTO
|
||||
update: UpdatePriceListRuleValueDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IPriceListRuleRepository<
|
||||
TEntity extends PriceListRule = PriceListRule
|
||||
> extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreatePriceListRuleDTO
|
||||
update: UpdatePriceListRuleDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IPriceListRepository<TEntity extends PriceList = PriceList>
|
||||
extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreatePriceListDTO
|
||||
update: UpdatePriceListDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IPriceRuleRepository<TEntity extends PriceRule = PriceRule>
|
||||
extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreatePriceRuleDTO
|
||||
update: UpdatePriceRuleDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IPriceSetMoneyAmountRulesRepository<
|
||||
TEntity extends PriceSetMoneyAmountRules = PriceSetMoneyAmountRules
|
||||
> extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreatePriceSetMoneyAmountRulesDTO
|
||||
update: UpdatePriceSetMoneyAmountRulesDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IPriceSetMoneyAmountRepository<
|
||||
TEntity extends PriceSetMoneyAmount = PriceSetMoneyAmount
|
||||
> extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreatePriceSetMoneyAmountDTO
|
||||
update: UpdatePriceSetMoneyAmountDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IPriceSetRuleTypeRepository<
|
||||
TEntity extends PriceSetRuleType = PriceSetRuleType
|
||||
> extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreatePriceSetRuleTypeDTO
|
||||
update: UpdatePriceSetRuleTypeDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IPriceSetRepository<TEntity extends PriceSet = PriceSet>
|
||||
extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreatePriceSetDTO
|
||||
update: UpdatePriceSetDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
export interface IRuleTypeRepository<TEntity extends RuleType = RuleType>
|
||||
extends DAL.RepositoryService<
|
||||
TEntity,
|
||||
{
|
||||
create: CreateRuleTypeDTO
|
||||
update: UpdateRuleTypeDTO
|
||||
}
|
||||
> {}
|
||||
|
||||
Reference in New Issue
Block a user