chore: Abstract module services (#6087)
**What** Create a service abstraction for the modules internal service layer. The objective is to reduce the effort of building new modules when the logic is the same or otherwise allow to override the default behavior. Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Oli Juhl
parent
80feb972cb
commit
130c641e5c
@@ -1,17 +1,7 @@
|
||||
import { Context, DAL, FindConfig, PromotionTypes } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
} from "@medusajs/utils"
|
||||
import { DAL, PromotionTypes } from "@medusajs/types"
|
||||
import { ApplicationMethod } from "@models"
|
||||
import { ApplicationMethodRepository } from "@repositories"
|
||||
import {
|
||||
CreateApplicationMethodDTO,
|
||||
UpdateApplicationMethodDTO,
|
||||
} from "../types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { CreateApplicationMethodDTO, UpdateApplicationMethodDTO } from "@types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
applicationMethodRepository: DAL.RepositoryService
|
||||
@@ -19,90 +9,19 @@ type InjectedDependencies = {
|
||||
|
||||
export default class ApplicationMethodService<
|
||||
TEntity extends ApplicationMethod = ApplicationMethod
|
||||
> {
|
||||
protected readonly applicationMethodRepository_: DAL.RepositoryService
|
||||
|
||||
constructor({ applicationMethodRepository }: InjectedDependencies) {
|
||||
this.applicationMethodRepository_ = applicationMethodRepository
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: CreateApplicationMethodDTO
|
||||
update: UpdateApplicationMethodDTO
|
||||
},
|
||||
{
|
||||
list: PromotionTypes.FilterableApplicationMethodProps
|
||||
listAndCount: PromotionTypes.FilterableApplicationMethodProps
|
||||
}
|
||||
|
||||
@InjectManager("applicationMethodRepository_")
|
||||
async retrieve(
|
||||
applicationMethodId: string,
|
||||
config: FindConfig<PromotionTypes.ApplicationMethodDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<
|
||||
ApplicationMethod,
|
||||
PromotionTypes.ApplicationMethodDTO
|
||||
>({
|
||||
id: applicationMethodId,
|
||||
entityName: ApplicationMethod.name,
|
||||
repository: this.applicationMethodRepository_,
|
||||
config,
|
||||
sharedContext,
|
||||
})) as TEntity
|
||||
}
|
||||
|
||||
@InjectManager("applicationMethodRepository_")
|
||||
async list(
|
||||
filters: PromotionTypes.FilterableApplicationMethodProps = {},
|
||||
config: FindConfig<PromotionTypes.ApplicationMethodDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<ApplicationMethod>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
return (await this.applicationMethodRepository_.find(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectManager("applicationMethodRepository_")
|
||||
async listAndCount(
|
||||
filters: PromotionTypes.FilterableApplicationMethodProps = {},
|
||||
config: FindConfig<PromotionTypes.ApplicationMethodDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<ApplicationMethod>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
return (await this.applicationMethodRepository_.findAndCount(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("applicationMethodRepository_")
|
||||
async create(
|
||||
data: CreateApplicationMethodDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.applicationMethodRepository_ as ApplicationMethodRepository
|
||||
).create(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("applicationMethodRepository_")
|
||||
async update(
|
||||
data: UpdateApplicationMethodDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.applicationMethodRepository_ as ApplicationMethodRepository
|
||||
).update(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("applicationMethodRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.applicationMethodRepository_.delete(ids, sharedContext)
|
||||
>(ApplicationMethod)<TEntity> {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
import { Context, DAL, FindConfig, PromotionTypes } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
} from "@medusajs/utils"
|
||||
import { DAL, PromotionTypes } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { CampaignBudget } from "@models"
|
||||
import { CampaignBudgetRepository } from "@repositories"
|
||||
import { CreateCampaignBudgetDTO, UpdateCampaignBudgetDTO } from "../types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
@@ -16,90 +9,19 @@ type InjectedDependencies = {
|
||||
|
||||
export default class CampaignBudgetService<
|
||||
TEntity extends CampaignBudget = CampaignBudget
|
||||
> {
|
||||
protected readonly campaignBudgetRepository_: DAL.RepositoryService
|
||||
|
||||
constructor({ campaignBudgetRepository }: InjectedDependencies) {
|
||||
this.campaignBudgetRepository_ = campaignBudgetRepository
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: CreateCampaignBudgetDTO
|
||||
update: UpdateCampaignBudgetDTO
|
||||
},
|
||||
{
|
||||
list: PromotionTypes.FilterableCampaignBudgetProps
|
||||
listAndCount: PromotionTypes.FilterableCampaignBudgetProps
|
||||
}
|
||||
|
||||
@InjectManager("campaignBudgetRepository_")
|
||||
async retrieve(
|
||||
campaignBudgetId: string,
|
||||
config: FindConfig<PromotionTypes.CampaignBudgetDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<
|
||||
CampaignBudget,
|
||||
PromotionTypes.CampaignBudgetDTO
|
||||
>({
|
||||
id: campaignBudgetId,
|
||||
entityName: CampaignBudget.name,
|
||||
repository: this.campaignBudgetRepository_,
|
||||
config,
|
||||
sharedContext,
|
||||
})) as TEntity
|
||||
}
|
||||
|
||||
@InjectManager("campaignBudgetRepository_")
|
||||
async list(
|
||||
filters: PromotionTypes.FilterableCampaignBudgetProps = {},
|
||||
config: FindConfig<PromotionTypes.CampaignBudgetDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<CampaignBudget>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
return (await this.campaignBudgetRepository_.find(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectManager("campaignBudgetRepository_")
|
||||
async listAndCount(
|
||||
filters: PromotionTypes.FilterableCampaignBudgetProps = {},
|
||||
config: FindConfig<PromotionTypes.CampaignBudgetDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<CampaignBudget>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
return (await this.campaignBudgetRepository_.findAndCount(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("campaignBudgetRepository_")
|
||||
async create(
|
||||
data: CreateCampaignBudgetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.campaignBudgetRepository_ as CampaignBudgetRepository
|
||||
).create(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("campaignBudgetRepository_")
|
||||
async update(
|
||||
data: UpdateCampaignBudgetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.campaignBudgetRepository_ as CampaignBudgetRepository
|
||||
).update(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("campaignBudgetRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.campaignBudgetRepository_.delete(ids, sharedContext)
|
||||
>(CampaignBudget)<TEntity> {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,96 +1,27 @@
|
||||
import { Context, DAL, FindConfig, PromotionTypes } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
} from "@medusajs/utils"
|
||||
import { DAL, PromotionTypes } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Campaign } from "@models"
|
||||
import { CampaignRepository } from "@repositories"
|
||||
import { CreateCampaignDTO, UpdateCampaignDTO } from "../types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
campaignRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class CampaignService<TEntity extends Campaign = Campaign> {
|
||||
protected readonly campaignRepository_: DAL.RepositoryService
|
||||
|
||||
constructor({ campaignRepository }: InjectedDependencies) {
|
||||
this.campaignRepository_ = campaignRepository
|
||||
export default class CampaignService<
|
||||
TEntity extends Campaign = Campaign
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: CreateCampaignDTO
|
||||
update: UpdateCampaignDTO
|
||||
},
|
||||
{
|
||||
list: PromotionTypes.FilterableCampaignProps
|
||||
listAndCount: PromotionTypes.FilterableCampaignProps
|
||||
}
|
||||
|
||||
@InjectManager("campaignRepository_")
|
||||
async retrieve(
|
||||
campaignId: string,
|
||||
config: FindConfig<PromotionTypes.CampaignDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<Campaign, PromotionTypes.CampaignDTO>({
|
||||
id: campaignId,
|
||||
entityName: Campaign.name,
|
||||
repository: this.campaignRepository_,
|
||||
config,
|
||||
sharedContext,
|
||||
})) as TEntity
|
||||
}
|
||||
|
||||
@InjectManager("campaignRepository_")
|
||||
async list(
|
||||
filters: PromotionTypes.FilterableCampaignProps = {},
|
||||
config: FindConfig<PromotionTypes.CampaignDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<Campaign>(filters, config)
|
||||
|
||||
return (await this.campaignRepository_.find(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectManager("campaignRepository_")
|
||||
async listAndCount(
|
||||
filters: PromotionTypes.FilterableCampaignProps = {},
|
||||
config: FindConfig<PromotionTypes.CampaignDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<Campaign>(filters, config)
|
||||
|
||||
return (await this.campaignRepository_.findAndCount(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("campaignRepository_")
|
||||
async create(
|
||||
data: CreateCampaignDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.campaignRepository_ as CampaignRepository).create(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("campaignRepository_")
|
||||
async update(
|
||||
data: UpdateCampaignDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.campaignRepository_ as CampaignRepository).update(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("campaignRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.campaignRepository_.delete(ids, sharedContext)
|
||||
>(Campaign)<TEntity> {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
import { Context, DAL, FindConfig, PromotionTypes } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
} from "@medusajs/utils"
|
||||
import { DAL, PromotionTypes } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { PromotionRuleValue } from "@models"
|
||||
import { PromotionRuleValueRepository } from "@repositories"
|
||||
import {
|
||||
CreatePromotionRuleValueDTO,
|
||||
UpdatePromotionRuleValueDTO,
|
||||
@@ -19,90 +12,19 @@ type InjectedDependencies = {
|
||||
|
||||
export default class PromotionRuleValueService<
|
||||
TEntity extends PromotionRuleValue = PromotionRuleValue
|
||||
> {
|
||||
protected readonly promotionRuleValueRepository_: DAL.RepositoryService
|
||||
|
||||
constructor({ promotionRuleValueRepository }: InjectedDependencies) {
|
||||
this.promotionRuleValueRepository_ = promotionRuleValueRepository
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: CreatePromotionRuleValueDTO
|
||||
update: UpdatePromotionRuleValueDTO
|
||||
},
|
||||
{
|
||||
list: PromotionTypes.FilterablePromotionRuleValueProps
|
||||
listAndCount: PromotionTypes.FilterablePromotionRuleValueProps
|
||||
}
|
||||
|
||||
@InjectManager("promotionRuleValueRepository_")
|
||||
async retrieve(
|
||||
promotionRuleValueId: string,
|
||||
config: FindConfig<PromotionTypes.PromotionRuleValueDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<
|
||||
PromotionRuleValue,
|
||||
PromotionTypes.PromotionRuleValueDTO
|
||||
>({
|
||||
id: promotionRuleValueId,
|
||||
entityName: PromotionRuleValue.name,
|
||||
repository: this.promotionRuleValueRepository_,
|
||||
config,
|
||||
sharedContext,
|
||||
})) as TEntity
|
||||
}
|
||||
|
||||
@InjectManager("promotionRuleValueRepository_")
|
||||
async list(
|
||||
filters: PromotionTypes.FilterablePromotionRuleValueProps = {},
|
||||
config: FindConfig<PromotionTypes.PromotionRuleValueDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<PromotionRuleValue>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
return (await this.promotionRuleValueRepository_.find(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectManager("promotionRuleValueRepository_")
|
||||
async listAndCount(
|
||||
filters: PromotionTypes.FilterablePromotionRuleValueProps = {},
|
||||
config: FindConfig<PromotionTypes.PromotionRuleValueDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<PromotionRuleValue>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
return (await this.promotionRuleValueRepository_.findAndCount(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("promotionRuleValueRepository_")
|
||||
async create(
|
||||
data: CreatePromotionRuleValueDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.promotionRuleValueRepository_ as PromotionRuleValueRepository
|
||||
).create(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("promotionRuleValueRepository_")
|
||||
async update(
|
||||
data: UpdatePromotionRuleValueDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.promotionRuleValueRepository_ as PromotionRuleValueRepository
|
||||
).update(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("promotionRuleValueRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.promotionRuleValueRepository_.delete(ids, sharedContext)
|
||||
>(PromotionRuleValue)<TEntity> {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
import { Context, DAL, FindConfig, PromotionTypes } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
} from "@medusajs/utils"
|
||||
import { DAL, PromotionTypes } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { PromotionRule } from "@models"
|
||||
import { PromotionRuleRepository } from "@repositories"
|
||||
import { CreatePromotionRuleDTO, UpdatePromotionRuleDTO } from "../types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
@@ -16,90 +9,19 @@ type InjectedDependencies = {
|
||||
|
||||
export default class PromotionRuleService<
|
||||
TEntity extends PromotionRule = PromotionRule
|
||||
> {
|
||||
protected readonly promotionRuleRepository_: DAL.RepositoryService
|
||||
|
||||
constructor({ promotionRuleRepository }: InjectedDependencies) {
|
||||
this.promotionRuleRepository_ = promotionRuleRepository
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: CreatePromotionRuleDTO
|
||||
update: UpdatePromotionRuleDTO
|
||||
},
|
||||
{
|
||||
list: PromotionTypes.FilterablePromotionRuleProps
|
||||
listAndCount: PromotionTypes.FilterablePromotionRuleProps
|
||||
}
|
||||
|
||||
@InjectManager("promotionRuleRepository_")
|
||||
async retrieve(
|
||||
promotionRuleId: string,
|
||||
config: FindConfig<PromotionTypes.PromotionRuleDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<
|
||||
PromotionRule,
|
||||
PromotionTypes.PromotionRuleDTO
|
||||
>({
|
||||
id: promotionRuleId,
|
||||
entityName: PromotionRule.name,
|
||||
repository: this.promotionRuleRepository_,
|
||||
config,
|
||||
sharedContext,
|
||||
})) as TEntity
|
||||
}
|
||||
|
||||
@InjectManager("promotionRuleRepository_")
|
||||
async list(
|
||||
filters: PromotionTypes.FilterablePromotionRuleProps = {},
|
||||
config: FindConfig<PromotionTypes.PromotionRuleDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<PromotionRule>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
return (await this.promotionRuleRepository_.find(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectManager("promotionRuleRepository_")
|
||||
async listAndCount(
|
||||
filters: PromotionTypes.FilterablePromotionRuleProps = {},
|
||||
config: FindConfig<PromotionTypes.PromotionRuleDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<PromotionRule>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
return (await this.promotionRuleRepository_.findAndCount(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("promotionRuleRepository_")
|
||||
async create(
|
||||
data: CreatePromotionRuleDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.promotionRuleRepository_ as PromotionRuleRepository
|
||||
).create(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("promotionRuleRepository_")
|
||||
async update(
|
||||
data: UpdatePromotionRuleDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (
|
||||
this.promotionRuleRepository_ as PromotionRuleRepository
|
||||
).update(data, sharedContext)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("promotionRuleRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.promotionRuleRepository_.delete(ids, sharedContext)
|
||||
>(PromotionRule)<TEntity> {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,96 +1,27 @@
|
||||
import { Context, DAL, FindConfig, PromotionTypes } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
} from "@medusajs/utils"
|
||||
import { DAL, PromotionTypes } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Promotion } from "@models"
|
||||
import { PromotionRepository } from "@repositories"
|
||||
import { CreatePromotionDTO, UpdatePromotionDTO } from "../types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
promotionRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class PromotionService<TEntity extends Promotion = Promotion> {
|
||||
protected readonly promotionRepository_: DAL.RepositoryService
|
||||
|
||||
constructor({ promotionRepository }: InjectedDependencies) {
|
||||
this.promotionRepository_ = promotionRepository
|
||||
export default class PromotionService<
|
||||
TEntity extends Promotion = Promotion
|
||||
> extends ModulesSdkUtils.abstractServiceFactory<
|
||||
InjectedDependencies,
|
||||
{
|
||||
create: CreatePromotionDTO
|
||||
update: UpdatePromotionDTO
|
||||
},
|
||||
{
|
||||
list: PromotionTypes.FilterablePromotionProps
|
||||
listAndCount: PromotionTypes.FilterablePromotionProps
|
||||
}
|
||||
|
||||
@InjectManager("promotionRepository_")
|
||||
async retrieve(
|
||||
promotionId: string,
|
||||
config: FindConfig<PromotionTypes.PromotionDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<Promotion, PromotionTypes.PromotionDTO>({
|
||||
id: promotionId,
|
||||
entityName: Promotion.name,
|
||||
repository: this.promotionRepository_,
|
||||
config,
|
||||
sharedContext,
|
||||
})) as TEntity
|
||||
}
|
||||
|
||||
@InjectManager("promotionRepository_")
|
||||
async list(
|
||||
filters: PromotionTypes.FilterablePromotionProps = {},
|
||||
config: FindConfig<PromotionTypes.PromotionDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<Promotion>(filters, config)
|
||||
|
||||
return (await this.promotionRepository_.find(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectManager("promotionRepository_")
|
||||
async listAndCount(
|
||||
filters: PromotionTypes.FilterablePromotionProps = {},
|
||||
config: FindConfig<PromotionTypes.PromotionDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<Promotion>(filters, config)
|
||||
|
||||
return (await this.promotionRepository_.findAndCount(
|
||||
queryOptions,
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("promotionRepository_")
|
||||
async create(
|
||||
data: CreatePromotionDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.promotionRepository_ as PromotionRepository).create(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("promotionRepository_")
|
||||
async update(
|
||||
data: UpdatePromotionDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.promotionRepository_ as PromotionRepository).update(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("promotionRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.promotionRepository_.delete(ids, sharedContext)
|
||||
>(Promotion)<TEntity> {
|
||||
constructor(...args: any[]) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user