Chore/rm main entity concept (#7709)

**What**
Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate.
This pr also includes various fixes in different modules

Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2024-06-19 13:02:16 +00:00
committed by GitHub
co-authored by Stevche Radevski Oli Juhl
parent 2895ccfba8
commit 48963f55ef
533 changed files with 6469 additions and 9769 deletions
@@ -31,35 +31,28 @@ type InjectedDependencies = {
[key: `tp_${string}`]: ITaxProvider
}
const generateForModels = { TaxRegion, TaxRateRule, TaxProvider }
const generateForModels = { TaxRate, TaxRegion, TaxRateRule, TaxProvider }
type ItemWithRates = {
rates: TaxRate[]
item: TaxTypes.TaxableItemDTO | TaxTypes.TaxableShippingDTO
}
export default class TaxModuleService<
TTaxRate extends TaxRate = TaxRate,
TTaxRegion extends TaxRegion = TaxRegion,
TTaxRateRule extends TaxRateRule = TaxRateRule,
TTaxProvider extends TaxProvider = TaxProvider
>
extends ModulesSdkUtils.MedusaService<
TaxTypes.TaxRateDTO,
{
TaxRegion: { dto: TaxTypes.TaxRegionDTO }
TaxRateRule: { dto: TaxTypes.TaxRateRuleDTO }
TaxProvider: { dto: TaxTypes.TaxProviderDTO }
}
>(TaxRate, generateForModels, entityNameToLinkableKeysMap)
export default class TaxModuleService
extends ModulesSdkUtils.MedusaService<{
TaxRate: { dto: TaxTypes.TaxRateDTO }
TaxRegion: { dto: TaxTypes.TaxRegionDTO }
TaxRateRule: { dto: TaxTypes.TaxRateRuleDTO }
TaxProvider: { dto: TaxTypes.TaxProviderDTO }
}>(generateForModels, entityNameToLinkableKeysMap)
implements ITaxModuleService
{
protected readonly container_: InjectedDependencies
protected baseRepository_: DAL.RepositoryService
protected taxRateService_: ModulesSdkTypes.IMedusaInternalService<TTaxRate>
protected taxRegionService_: ModulesSdkTypes.IMedusaInternalService<TTaxRegion>
protected taxRateRuleService_: ModulesSdkTypes.IMedusaInternalService<TTaxRateRule>
protected taxProviderService_: ModulesSdkTypes.IMedusaInternalService<TTaxProvider>
protected taxRateService_: ModulesSdkTypes.IMedusaInternalService<TaxRate>
protected taxRegionService_: ModulesSdkTypes.IMedusaInternalService<TaxRegion>
protected taxRateRuleService_: ModulesSdkTypes.IMedusaInternalService<TaxRateRule>
protected taxProviderService_: ModulesSdkTypes.IMedusaInternalService<TaxProvider>
constructor(
{
@@ -86,28 +79,29 @@ export default class TaxModuleService<
return joinerConfig
}
async create(
// @ts-expect-error
async createTaxRates(
data: TaxTypes.CreateTaxRateDTO[],
sharedContext?: Context
): Promise<TaxTypes.TaxRateDTO[]>
async create(
async createTaxRates(
data: TaxTypes.CreateTaxRateDTO,
sharedContext?: Context
): Promise<TaxTypes.TaxRateDTO>
@InjectManager("baseRepository_")
async create(
async createTaxRates(
data: TaxTypes.CreateTaxRateDTO[] | TaxTypes.CreateTaxRateDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<TaxTypes.TaxRateDTO[] | TaxTypes.TaxRateDTO> {
const input = Array.isArray(data) ? data : [data]
const rates = await this.create_(input, sharedContext)
const rates = await this.createTaxRates_(input, sharedContext)
return Array.isArray(data) ? rates : rates[0]
}
@InjectTransactionManager("baseRepository_")
protected async create_(
protected async createTaxRates_(
data: TaxTypes.CreateTaxRateDTO[],
@MedusaContext() sharedContext: Context = {}
) {
@@ -152,29 +146,30 @@ export default class TaxModuleService<
})
}
async update(
// @ts-expect-error
async updateTaxRates(
id: string,
data: TaxTypes.UpdateTaxRateDTO,
sharedContext?: Context
): Promise<TaxTypes.TaxRateDTO>
async update(
async updateTaxRates(
ids: string[],
data: TaxTypes.UpdateTaxRateDTO,
sharedContext?: Context
): Promise<TaxTypes.TaxRateDTO[]>
async update(
async updateTaxRates(
selector: TaxTypes.FilterableTaxRateProps,
data: TaxTypes.UpdateTaxRateDTO,
sharedContext?: Context
): Promise<TaxTypes.TaxRateDTO[]>
@InjectManager("baseRepository_")
async update(
async updateTaxRates(
selector: string | string[] | TaxTypes.FilterableTaxRateProps,
data: TaxTypes.UpdateTaxRateDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<TaxTypes.TaxRateDTO | TaxTypes.TaxRateDTO[]> {
const rates = await this.update_(selector, data, sharedContext)
const rates = await this.updateTaxRates_(selector, data, sharedContext)
const serialized = await this.baseRepository_.serialize<
TaxTypes.TaxRateDTO[]
>(rates, { populate: true })
@@ -182,7 +177,7 @@ export default class TaxModuleService<
}
@InjectTransactionManager("baseRepository_")
protected async update_(
protected async updateTaxRates_(
idOrSelector: string | string[] | TaxTypes.FilterableTaxRateProps,
data: TaxTypes.UpdateTaxRateDTO,
@MedusaContext() sharedContext: Context = {}
@@ -271,17 +266,17 @@ export default class TaxModuleService<
return rates.map((r) => r.id)
}
async upsert(
async upsertTaxRates(
data: TaxTypes.UpsertTaxRateDTO[],
sharedContext?: Context
): Promise<TaxTypes.TaxRateDTO[]>
async upsert(
async upsertTaxRates(
data: TaxTypes.UpsertTaxRateDTO,
sharedContext?: Context
): Promise<TaxTypes.TaxRateDTO>
@InjectTransactionManager("baseRepository_")
async upsert(
async upsertTaxRates(
data: TaxTypes.UpsertTaxRateDTO | TaxTypes.UpsertTaxRateDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TaxTypes.TaxRateDTO | TaxTypes.TaxRateDTO[]> {
@@ -340,7 +335,7 @@ export default class TaxModuleService<
.filter(Boolean) as TaxTypes.CreateTaxRateDTO[]
if (rates.length !== 0) {
await this.create(rates, sharedContext)
await this.createTaxRates(rates, sharedContext)
}
return await this.baseRepository_.serialize<TaxTypes.TaxRegionDTO[]>(
@@ -419,7 +414,7 @@ export default class TaxModuleService<
const toReturn = await promiseAll(
items.map(async (item) => {
const regionIds = regions.map((r) => r.id)
const rateQuery = this.getTaxRateQueryForItem(item, regionIds)
const rateQuery = this.geTaxRateQueryForItem(item, regionIds)
const candidateRates = await this.taxRateService_.list(
rateQuery,
{
@@ -428,7 +423,7 @@ export default class TaxModuleService<
sharedContext
)
const applicableRates = await this.getTaxRatesForItem(
const applicableRates = await this.geTaxRatesForItem(
item,
candidateRates
)
@@ -580,10 +575,10 @@ export default class TaxModuleService<
}
}
private async getTaxRatesForItem(
private async geTaxRatesForItem(
item: TaxTypes.TaxableItemDTO | TaxTypes.TaxableShippingDTO,
rates: TTaxRate[]
): Promise<TTaxRate[]> {
rates: TaxRate[]
): Promise<TaxRate[]> {
if (!rates.length) {
return []
}
@@ -612,7 +607,7 @@ export default class TaxModuleService<
return ratesToReturn
}
private getTaxRateQueryForItem(
private geTaxRateQueryForItem(
item: TaxTypes.TaxableItemDTO | TaxTypes.TaxableShippingDTO,
regionIds: string[]
) {
@@ -644,7 +639,7 @@ export default class TaxModuleService<
}
private checkRuleMatches(
rate: TTaxRate,
rate: TaxRate,
item: TaxTypes.TaxableItemDTO | TaxTypes.TaxableShippingDTO
) {
if (rate.rules.length === 0) {
@@ -684,12 +679,10 @@ export default class TaxModuleService<
}
private prioritizeRates(
rates: TTaxRate[],
rates: TaxRate[],
item: TaxTypes.TaxableItemDTO | TaxTypes.TaxableShippingDTO
) {
const decoratedRates: (TTaxRate & {
priority_score: number
})[] = rates.map((rate) => {
const decoratedRates = rates.map((rate) => {
const { isProductMatch, isProductTypeMatch, isShippingMatch } =
this.checkRuleMatches(rate, item)
@@ -715,7 +708,9 @@ export default class TaxModuleService<
decoratedRate.priority_score = 6
}
return decoratedRate
})
}) as (TaxRate & {
priority_score: number
})[]
return decoratedRates.sort(
(a, b) => (a as any).priority_score - (b as any).priority_score