feat(pricing, types): PriceSets as entry point to pricing module (#4978)
What: - Adds PriceSet, PriceSetMoneyAmount, updates schema - Adds service/repo for PriceSet - Shifts entry point to use PriceSet - Updates link/joiner config RESOLVES CORE-1495
This commit is contained in:
@@ -6,8 +6,8 @@ import {
|
||||
ModuleJoinerConfig,
|
||||
PricingTypes,
|
||||
} from "@medusajs/types"
|
||||
import { Currency, MoneyAmount } from "@models"
|
||||
import { CurrencyService, MoneyAmountService } from "@services"
|
||||
import { Currency, MoneyAmount, PriceSet } from "@models"
|
||||
import { CurrencyService, MoneyAmountService, PriceSetService } from "@services"
|
||||
|
||||
import {
|
||||
InjectManager,
|
||||
@@ -22,9 +22,15 @@ type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
currencyService: CurrencyService<any>
|
||||
moneyAmountService: MoneyAmountService<any>
|
||||
priceSetService: PriceSetService<any>
|
||||
}
|
||||
|
||||
type PricingContext = {
|
||||
currency_code?: string
|
||||
}
|
||||
|
||||
export default class PricingModuleService<
|
||||
TPriceSet extends PriceSet = PriceSet,
|
||||
TMoneyAmount extends MoneyAmount = MoneyAmount,
|
||||
TCurrency extends Currency = Currency
|
||||
> implements PricingTypes.IPricingModuleService
|
||||
@@ -32,26 +38,180 @@ export default class PricingModuleService<
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly currencyService_: CurrencyService<TCurrency>
|
||||
protected readonly moneyAmountService_: MoneyAmountService<TMoneyAmount>
|
||||
protected readonly priceSetService_: PriceSetService<TPriceSet>
|
||||
|
||||
constructor(
|
||||
{
|
||||
baseRepository,
|
||||
moneyAmountService,
|
||||
currencyService,
|
||||
priceSetService,
|
||||
}: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
this.baseRepository_ = baseRepository
|
||||
this.currencyService_ = currencyService
|
||||
this.moneyAmountService_ = moneyAmountService
|
||||
this.priceSetService_ = priceSetService
|
||||
}
|
||||
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async calculatePrices(
|
||||
priceSetIds: string[],
|
||||
pricingContext: PricingContext,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.CalculatedPriceSetDTO> {
|
||||
// Keeping this whole logic raw in here for now as they will undergo
|
||||
// some changes, will abstract them out once we have a final version
|
||||
const priceSetFilters: PricingTypes.FilterablePriceSetProps = {
|
||||
id: priceSetIds,
|
||||
}
|
||||
|
||||
const priceSets = await this.list(
|
||||
priceSetFilters,
|
||||
{
|
||||
select: [
|
||||
"id",
|
||||
"money_amounts.id",
|
||||
"money_amounts.currency_code",
|
||||
"money_amounts.amount",
|
||||
"money_amounts.min_quantity",
|
||||
"money_amounts.max_quantity",
|
||||
],
|
||||
relations: ["money_amounts"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const calculatedPrices = priceSets.map(
|
||||
(priceSet): PricingTypes.CalculatedPriceSetDTO => {
|
||||
// TODO: This will change with the rules engine selection,
|
||||
// making a DB query directly instead
|
||||
// This should look for a default price when no rules apply
|
||||
// When no price is set, return null values for all cases
|
||||
const selectedMoneyAmount = priceSet.money_amounts?.find(
|
||||
(ma) =>
|
||||
pricingContext.currency_code &&
|
||||
ma.currency_code === pricingContext.currency_code
|
||||
)
|
||||
|
||||
return {
|
||||
id: priceSet.id,
|
||||
amount: selectedMoneyAmount?.amount || null,
|
||||
currency_code: selectedMoneyAmount?.currency_code || null,
|
||||
min_quantity: selectedMoneyAmount?.min_quantity || null,
|
||||
max_quantity: selectedMoneyAmount?.max_quantity || null,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return JSON.parse(JSON.stringify(calculatedPrices))
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async retrieve(
|
||||
id: string,
|
||||
config: FindConfig<PricingTypes.PriceSetDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.PriceSetDTO> {
|
||||
const priceSet = await this.priceSetService_.retrieve(
|
||||
id,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.PriceSetDTO>(priceSet, {
|
||||
populate: true,
|
||||
})
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async list(
|
||||
filters: PricingTypes.FilterablePriceSetProps = {},
|
||||
config: FindConfig<PricingTypes.PriceSetDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.PriceSetDTO[]> {
|
||||
const priceSets = await this.priceSetService_.list(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.PriceSetDTO[]>(
|
||||
priceSets,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listAndCount(
|
||||
filters: PricingTypes.FilterablePriceSetProps = {},
|
||||
config: FindConfig<PricingTypes.PriceSetDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[PricingTypes.PriceSetDTO[], number]> {
|
||||
const [priceSets, count] = await this.priceSetService_.listAndCount(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return [
|
||||
await this.baseRepository_.serialize<PricingTypes.PriceSetDTO[]>(
|
||||
priceSets,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
),
|
||||
count,
|
||||
]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreatePriceSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const priceSets = await this.priceSetService_.create(data, sharedContext)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.PriceSetDTO[]>(
|
||||
priceSets,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdatePriceSetDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const priceSets = await this.priceSetService_.update(data, sharedContext)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.PriceSetDTO[]>(
|
||||
priceSets,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.priceSetService_.delete(ids, sharedContext)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async retrieveMoneyAmount(
|
||||
id: string,
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -71,7 +231,7 @@ export default class PricingModuleService<
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async list(
|
||||
async listMoneyAmounts(
|
||||
filters: PricingTypes.FilterableMoneyAmountProps = {},
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -91,7 +251,7 @@ export default class PricingModuleService<
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listAndCount(
|
||||
async listAndCountMoneyAmounts(
|
||||
filters: PricingTypes.FilterableMoneyAmountProps = {},
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
@@ -114,7 +274,7 @@ export default class PricingModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async create(
|
||||
async createMoneyAmounts(
|
||||
data: PricingTypes.CreateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
@@ -132,7 +292,7 @@ export default class PricingModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async update(
|
||||
async updateMoneyAmounts(
|
||||
data: PricingTypes.UpdateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
@@ -150,7 +310,7 @@ export default class PricingModuleService<
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async delete(
|
||||
async deleteMoneyAmounts(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user