feat(pricing, utils, types): adds money amount to pricing module (#4909)
What: - Adds money amount service / repo / model - Adds money amount to entry service - Adds tests for services - Refreshes schema - Update joiner config to include money amounts RESOLVES CORE-1478 RESOLVES CORE-1479 Co-authored-by: Shahed Nasser <27354907+shahednasser@users.noreply.github.com> Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
co-authored by
Shahed Nasser
Carlos R. L. Rodrigues
parent
0627d06f72
commit
fcb6b4f510
@@ -1,2 +1,3 @@
|
||||
export { default as CurrencyService } from "./currency"
|
||||
export { default as MoneyAmountService } from "./money-amount"
|
||||
export { default as PricingModuleService } from "./pricing-module"
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
import { Context, DAL, FindConfig, PricingTypes } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
ModulesSdkUtils,
|
||||
retrieveEntity,
|
||||
} from "@medusajs/utils"
|
||||
import { MoneyAmount } from "@models"
|
||||
import { MoneyAmountRepository } from "@repositories"
|
||||
|
||||
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
moneyAmountRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class MoneyAmountService<
|
||||
TEntity extends MoneyAmount = MoneyAmount
|
||||
> {
|
||||
protected readonly moneyAmountRepository_: DAL.RepositoryService
|
||||
|
||||
constructor({ moneyAmountRepository }: InjectedDependencies) {
|
||||
this.moneyAmountRepository_ = moneyAmountRepository
|
||||
}
|
||||
|
||||
@InjectManager("moneyAmountRepository_")
|
||||
async retrieve(
|
||||
moneyAmountId: string,
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
return (await retrieveEntity<MoneyAmount, PricingTypes.MoneyAmountDTO>({
|
||||
id: moneyAmountId,
|
||||
entityName: MoneyAmount.name,
|
||||
repository: this.moneyAmountRepository_,
|
||||
config,
|
||||
sharedContext,
|
||||
})) as TEntity
|
||||
}
|
||||
|
||||
@InjectManager("moneyAmountRepository_")
|
||||
async list(
|
||||
filters: PricingTypes.FilterableMoneyAmountProps = {},
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await this.moneyAmountRepository_.find(
|
||||
this.buildQueryForList(filters, config),
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectManager("moneyAmountRepository_")
|
||||
async listAndCount(
|
||||
filters: PricingTypes.FilterableMoneyAmountProps = {},
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
return (await this.moneyAmountRepository_.findAndCount(
|
||||
this.buildQueryForList(filters, config),
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
}
|
||||
|
||||
private buildQueryForList(
|
||||
filters: PricingTypes.FilterableMoneyAmountProps = {},
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {}
|
||||
) {
|
||||
const queryOptions = ModulesSdkUtils.buildQuery<MoneyAmount>(
|
||||
filters,
|
||||
config
|
||||
)
|
||||
|
||||
if (filters.id) {
|
||||
queryOptions.where["id"] = { $in: filters.id }
|
||||
}
|
||||
|
||||
return queryOptions
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "moneyAmountRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.moneyAmountRepository_ as MoneyAmountRepository).create(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "moneyAmountRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
return (await (this.moneyAmountRepository_ as MoneyAmountRepository).update(
|
||||
data,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(doNotForceTransaction, "moneyAmountRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.moneyAmountRepository_.delete(ids, sharedContext)
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,11 @@ import {
|
||||
DAL,
|
||||
FindConfig,
|
||||
InternalModuleDeclaration,
|
||||
JoinerServiceConfig,
|
||||
ModuleJoinerConfig,
|
||||
PricingTypes,
|
||||
} from "@medusajs/types"
|
||||
import { Currency } from "@models"
|
||||
import { CurrencyService } from "@services"
|
||||
import { Currency, MoneyAmount } from "@models"
|
||||
import { CurrencyService, MoneyAmountService } from "@services"
|
||||
|
||||
import {
|
||||
InjectManager,
|
||||
@@ -21,26 +21,142 @@ import { joinerConfig } from "../joiner-config"
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
currencyService: CurrencyService<any>
|
||||
moneyAmountService: MoneyAmountService<any>
|
||||
}
|
||||
|
||||
export default class PricingModuleService<TCurrency extends Currency = Currency>
|
||||
implements PricingTypes.IPricingModuleService
|
||||
export default class PricingModuleService<
|
||||
TMoneyAmount extends MoneyAmount = MoneyAmount,
|
||||
TCurrency extends Currency = Currency
|
||||
> implements PricingTypes.IPricingModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly currencyService_: CurrencyService<TCurrency>
|
||||
protected readonly moneyAmountService_: MoneyAmountService<TMoneyAmount>
|
||||
|
||||
constructor(
|
||||
{ baseRepository, currencyService }: InjectedDependencies,
|
||||
{
|
||||
baseRepository,
|
||||
moneyAmountService,
|
||||
currencyService,
|
||||
}: InjectedDependencies,
|
||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
this.baseRepository_ = baseRepository
|
||||
this.currencyService_ = currencyService
|
||||
this.moneyAmountService_ = moneyAmountService
|
||||
}
|
||||
|
||||
__joinerConfig(): JoinerServiceConfig {
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async retrieve(
|
||||
id: string,
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.MoneyAmountDTO> {
|
||||
const moneyAmount = await this.moneyAmountService_.retrieve(
|
||||
id,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.MoneyAmountDTO>(
|
||||
moneyAmount,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async list(
|
||||
filters: PricingTypes.FilterableMoneyAmountProps = {},
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<PricingTypes.MoneyAmountDTO[]> {
|
||||
const moneyAmounts = await this.moneyAmountService_.list(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.MoneyAmountDTO[]>(
|
||||
moneyAmounts,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async listAndCount(
|
||||
filters: PricingTypes.FilterableMoneyAmountProps = {},
|
||||
config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[PricingTypes.MoneyAmountDTO[], number]> {
|
||||
const [moneyAmounts, count] = await this.moneyAmountService_.listAndCount(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return [
|
||||
await this.baseRepository_.serialize<PricingTypes.MoneyAmountDTO[]>(
|
||||
moneyAmounts,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
),
|
||||
count,
|
||||
]
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const moneyAmounts = await this.moneyAmountService_.create(
|
||||
data,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.MoneyAmountDTO[]>(
|
||||
moneyAmounts,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async update(
|
||||
data: PricingTypes.UpdateMoneyAmountDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const moneyAmounts = await this.moneyAmountService_.update(
|
||||
data,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return this.baseRepository_.serialize<PricingTypes.MoneyAmountDTO[]>(
|
||||
moneyAmounts,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
|
||||
async delete(
|
||||
ids: string[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<void> {
|
||||
await this.moneyAmountService_.delete(ids, sharedContext)
|
||||
}
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async retrieveCurrency(
|
||||
code: string,
|
||||
|
||||
Reference in New Issue
Block a user