feat(pricing) Add Price Set Rule Type (#4977)
* initial * initial service * update pricing module service * add integration test for rule-type * update pricing-module integration tests * update pricing service interface * feat(pricing): PriceSets as entry point to pricing module * chore: add price set money amount * chore: add price set money amount * chore: change name of test * chore: added changeset * chore: use filterable props from money amount in price sets * chore: update migrations * test update integration test * fix weird behavior * Update packages/pricing/integration-tests/__fixtures__/rule-type/index.ts Co-authored-by: Riqwan Thamir <rmthamir@gmail.com> * Apply suggestions from code review Co-authored-by: Riqwan Thamir <rmthamir@gmail.com> * move rule-type to common * chore: reset migration * chore: remove incorrect conflicts * chore: address review * chore: remove ghost price list * Apply suggestions from code review Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> * update id prefix * use persist not persistAndflush * rename key_value to rule_attribute * more renaming --------- Co-authored-by: Riqwan Thamir <rmthamir@gmail.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
@@ -2,3 +2,4 @@ export { default as CurrencyService } from "./currency"
|
||||
export { default as MoneyAmountService } from "./money-amount"
|
||||
export { default as PriceSetService } from "./price-set"
|
||||
export { default as PricingModuleService } from "./pricing-module"
|
||||
export { default as RuleTypeService } from "./rule-type"
|
||||
@@ -8,34 +8,42 @@ import {
|
||||
PricingFilters,
|
||||
PricingTypes,
|
||||
} from "@medusajs/types"
|
||||
import { Currency, MoneyAmount, PriceSet } from "@models"
|
||||
import { CurrencyService, MoneyAmountService, PriceSetService } from "@services"
|
||||
import { Currency, MoneyAmount, PriceSet, RuleType } from "@models"
|
||||
import {
|
||||
CurrencyService,
|
||||
MoneyAmountService,
|
||||
PriceSetService,
|
||||
RuleTypeService,
|
||||
} from "@services"
|
||||
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
shouldForceTransaction,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { shouldForceTransaction } from "@medusajs/utils"
|
||||
import { joinerConfig } from "../joiner-config"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
currencyService: CurrencyService<any>
|
||||
moneyAmountService: MoneyAmountService<any>
|
||||
ruleTypeService: RuleTypeService<any>
|
||||
priceSetService: PriceSetService<any>
|
||||
}
|
||||
|
||||
export default class PricingModuleService<
|
||||
TPriceSet extends PriceSet = PriceSet,
|
||||
TMoneyAmount extends MoneyAmount = MoneyAmount,
|
||||
TCurrency extends Currency = Currency
|
||||
TCurrency extends Currency = Currency,
|
||||
TRuleType extends RuleType = RuleType
|
||||
> implements PricingTypes.IPricingModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly currencyService_: CurrencyService<TCurrency>
|
||||
protected readonly moneyAmountService_: MoneyAmountService<TMoneyAmount>
|
||||
protected readonly ruleTypeService_: RuleTypeService<TRuleType>
|
||||
protected readonly priceSetService_: PriceSetService<TPriceSet>
|
||||
|
||||
constructor(
|
||||
@@ -43,6 +51,7 @@ export default class PricingModuleService<
|
||||
baseRepository,
|
||||
moneyAmountService,
|
||||
currencyService,
|
||||
ruleTypeService,
|
||||
priceSetService,
|
||||
}: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
@@ -50,6 +59,7 @@ export default class PricingModuleService<
|
||||
this.baseRepository_ = baseRepository
|
||||
this.currencyService_ = currencyService
|
||||
this.moneyAmountService_ = moneyAmountService
|
||||
this.ruleTypeService_ = ruleTypeService
|
||||
this.priceSetService_ = priceSetService
|
||||
}
|
||||
|
||||
@@ -412,4 +422,102 @@ export default class PricingModuleService<
|
||||
): Promise<void> {
|
||||
await this.currencyService_.delete(currencyCodes, sharedContext)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async retrieveRuleType(
|
||||
id: string,
|
||||
config: FindConfig<PricingTypes.RuleTypeDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.RuleTypeDTO> {
|
||||
const ruleType = await this.ruleTypeService_.retrieve(
|
||||
id,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.RuleTypeDTO>(ruleType, {
|
||||
populate: true,
|
||||
})
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listRuleTypes(
|
||||
filters: PricingTypes.FilterableRuleTypeProps = {},
|
||||
config: FindConfig<PricingTypes.RuleTypeDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.RuleTypeDTO[]> {
|
||||
const ruleTypes = await this.ruleTypeService_.list(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.RuleTypeDTO[]>(
|
||||
ruleTypes,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listAndCountRuleTypes(
|
||||
filters: PricingTypes.FilterableRuleTypeProps = {},
|
||||
config: FindConfig<PricingTypes.RuleTypeDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[PricingTypes.RuleTypeDTO[], number]> {
|
||||
const [ruleTypes, count] = await this.ruleTypeService_.listAndCount(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return [
|
||||
await this.baseRepository_.serialize<PricingTypes.RuleTypeDTO[]>(
|
||||
ruleTypes,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
),
|
||||
count,
|
||||
]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async createRuleTypes(
|
||||
data: PricingTypes.CreateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.RuleTypeDTO[]> {
|
||||
const ruleTypes = await this.ruleTypeService_.create(data, sharedContext)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.RuleTypeDTO[]>(
|
||||
ruleTypes,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async updateRuleTypes(
|
||||
data: PricingTypes.UpdateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.RuleTypeDTO[]> {
|
||||
const ruleTypes = await this.ruleTypeService_.update(data, sharedContext)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.RuleTypeDTO[]>(
|
||||
ruleTypes,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async deleteRuleTypes(
|
||||
ruleTypes: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.ruleTypeService_.delete(ruleTypes, sharedContext)
|
||||
}
|
||||
}
|
||||
|
||||
106
packages/pricing/src/services/rule-type.ts
Normal file
106
packages/pricing/src/services/rule-type.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { Context, DAL, FindConfig, PricingTypes } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
} from "@medusajs/utils"
|
||||
import { RuleType } from "@models"
|
||||
|
||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
ruleTypeRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class RuleTypeService<TEntity extends RuleType = RuleType> {
|
||||
protected readonly ruleTypeRepository_: DAL.RepositoryService
|
||||
|
||||
constructor({ ruleTypeRepository }: InjectedDependencies) {
|
||||
this.ruleTypeRepository_ = ruleTypeRepository
|
||||
}
|
||||
|
||||
@InjectManager("ruleTypeRepository_")
|
||||
async retrieve(
|
||||
ruleTypeId: string,
|
||||
config: FindConfig<PricingTypes.RuleTypeDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<RuleType, PricingTypes.RuleTypeDTO>({
|
||||
id: ruleTypeId,
|
||||
identifierColumn: "id",
|
||||
entityName: RuleType.name,
|
||||
repository: this.ruleTypeRepository_,
|
||||
config,
|
||||
sharedContext,
|
||||
})) as TEntity
|
||||
}
|
||||
|
||||
@InjectManager("ruleTypeRepository_")
|
||||
async list(
|
||||
filters: PricingTypes.FilterableRuleTypeProps = {},
|
||||
config: FindConfig<PricingTypes.RuleTypeDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await this.ruleTypeRepository_.find(
|
||||
this.buildQueryForList(filters, config),
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectManager("ruleTypeRepository_")
|
||||
async listAndCount(
|
||||
filters: PricingTypes.FilterableRuleTypeProps = {},
|
||||
config: FindConfig<PricingTypes.RuleTypeDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
return (await this.ruleTypeRepository_.findAndCount(
|
||||
this.buildQueryForList(filters, config),
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
private buildQueryForList(
|
||||
filters: PricingTypes.FilterableRuleTypeProps = {},
|
||||
config: FindConfig<PricingTypes.RuleTypeDTO> = {}
|
||||
) {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<RuleType>(filters, config)
|
||||
|
||||
if (filters.id) {
|
||||
queryOptions.where["id"] = { $in: filters.id }
|
||||
}
|
||||
|
||||
return queryOptions
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "ruleTypeRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await this.ruleTypeRepository_.create(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "ruleTypeRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await this.ruleTypeRepository_.update(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "ruleTypeRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.ruleTypeRepository_.delete(ids, sharedContext)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user