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
@@ -22,6 +22,7 @@ import {
MathBN,
MedusaContext,
MedusaError,
MedusaService,
ModulesSdkUtils,
PromotionType,
transformPropertiesToBigNumber,
@@ -66,41 +67,34 @@ type InjectedDependencies = {
campaignBudgetService: ModulesSdkTypes.IMedusaInternalService<any>
}
const generateMethodForModels = {
ApplicationMethod,
Campaign,
CampaignBudget,
PromotionRule,
PromotionRuleValue,
}
export default class PromotionModuleService<
TApplicationMethod extends ApplicationMethod = ApplicationMethod,
TPromotion extends Promotion = Promotion,
TPromotionRule extends PromotionRule = PromotionRule,
TPromotionRuleValue extends PromotionRuleValue = PromotionRuleValue,
TCampaign extends Campaign = Campaign,
TCampaignBudget extends CampaignBudget = CampaignBudget
>
extends ModulesSdkUtils.MedusaService<
PromotionTypes.PromotionDTO,
export default class PromotionModuleService
extends MedusaService<{
Promotion: { dto: PromotionTypes.PromotionDTO }
ApplicationMethod: { dto: PromotionTypes.ApplicationMethodDTO }
Campaign: { dto: PromotionTypes.CampaignDTO }
CampaignBudget: { dto: PromotionTypes.CampaignBudgetDTO }
PromotionRule: { dto: PromotionTypes.PromotionRuleDTO }
PromotionRuleValue: { dto: PromotionTypes.PromotionRuleValueDTO }
}>(
{
ApplicationMethod: { dto: PromotionTypes.ApplicationMethodDTO }
Campaign: { dto: PromotionTypes.CampaignDTO }
CampaignBudget: { dto: PromotionTypes.CampaignBudgetDTO }
PromotionRule: { dto: PromotionTypes.PromotionRuleDTO }
PromotionRuleValue: { dto: PromotionTypes.PromotionRuleValueDTO }
}
>(Promotion, generateMethodForModels, entityNameToLinkableKeysMap)
Promotion,
ApplicationMethod,
Campaign,
CampaignBudget,
PromotionRule,
PromotionRuleValue,
},
entityNameToLinkableKeysMap
)
implements PromotionTypes.IPromotionModuleService
{
protected baseRepository_: DAL.RepositoryService
protected promotionService_: ModulesSdkTypes.IMedusaInternalService<TPromotion>
protected applicationMethodService_: ModulesSdkTypes.IMedusaInternalService<TApplicationMethod>
protected promotionRuleService_: ModulesSdkTypes.IMedusaInternalService<TPromotionRule>
protected promotionRuleValueService_: ModulesSdkTypes.IMedusaInternalService<TPromotionRuleValue>
protected campaignService_: ModulesSdkTypes.IMedusaInternalService<TCampaign>
protected campaignBudgetService_: ModulesSdkTypes.IMedusaInternalService<TCampaignBudget>
protected promotionService_: ModulesSdkTypes.IMedusaInternalService<Promotion>
protected applicationMethodService_: ModulesSdkTypes.IMedusaInternalService<ApplicationMethod>
protected promotionRuleService_: ModulesSdkTypes.IMedusaInternalService<PromotionRule>
protected promotionRuleValueService_: ModulesSdkTypes.IMedusaInternalService<PromotionRuleValue>
protected campaignService_: ModulesSdkTypes.IMedusaInternalService<Campaign>
protected campaignBudgetService_: ModulesSdkTypes.IMedusaInternalService<CampaignBudget>
constructor(
{
@@ -145,7 +139,7 @@ export default class PromotionModuleService<
>()
const promotionCodeUsageMap = new Map<string, boolean>()
const existingPromotions = await this.list(
const existingPromotions = await this.listPromotions(
{ code: promotionCodes },
{ relations: ["application_method", "campaign", "campaign.budget"] },
sharedContext
@@ -256,7 +250,7 @@ export default class PromotionModuleService<
const methodIdPromoValueMap = new Map<string, number>()
const automaticPromotions = preventAutoPromotions
? []
: await this.list(
: await this.listPromotions(
{ is_automatic: true },
{ select: ["code"], take: null },
sharedContext
@@ -296,7 +290,7 @@ export default class PromotionModuleService<
})
})
const promotions = await this.list(
const promotions = await this.listPromotions(
{
code: [
...promotionCodesToApply,
@@ -432,27 +426,28 @@ export default class PromotionModuleService<
return computedActions
}
async create(
// @ts-expect-error
async createPromotions(
data: PromotionTypes.CreatePromotionDTO,
sharedContext?: Context
): Promise<PromotionTypes.PromotionDTO>
async create(
async createPromotions(
data: PromotionTypes.CreatePromotionDTO[],
sharedContext?: Context
): Promise<PromotionTypes.PromotionDTO[]>
@InjectManager("baseRepository_")
async create(
async createPromotions(
data:
| PromotionTypes.CreatePromotionDTO
| PromotionTypes.CreatePromotionDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PromotionTypes.PromotionDTO | PromotionTypes.PromotionDTO[]> {
const input = Array.isArray(data) ? data : [data]
const createdPromotions = await this.create_(input, sharedContext)
const createdPromotions = await this.createPromotions_(input, sharedContext)
const promotions = await this.list(
const promotions = await this.listPromotions(
{ id: createdPromotions.map((p) => p!.id) },
{
relations: [
@@ -475,7 +470,7 @@ export default class PromotionModuleService<
}
@InjectTransactionManager("baseRepository_")
protected async create_(
protected async createPromotions_(
data: PromotionTypes.CreatePromotionDTO[],
@MedusaContext() sharedContext: Context = {}
) {
@@ -699,27 +694,28 @@ export default class PromotionModuleService<
return createdPromotions
}
async update(
// @ts-expect-error
async updatePromotions(
data: PromotionTypes.UpdatePromotionDTO,
sharedContext?: Context
): Promise<PromotionTypes.PromotionDTO>
async update(
async updatePromotions(
data: PromotionTypes.UpdatePromotionDTO[],
sharedContext?: Context
): Promise<PromotionTypes.PromotionDTO[]>
@InjectManager("baseRepository_")
async update(
async updatePromotions(
data:
| PromotionTypes.UpdatePromotionDTO
| PromotionTypes.UpdatePromotionDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PromotionTypes.PromotionDTO | PromotionTypes.PromotionDTO[]> {
const input = Array.isArray(data) ? data : [data]
const updatedPromotions = await this.update_(input, sharedContext)
const updatedPromotions = await this.updatePromotions_(input, sharedContext)
const promotions = await this.list(
const promotions = await this.listPromotions(
{ id: updatedPromotions.map((p) => p!.id) },
{
relations: [
@@ -740,7 +736,7 @@ export default class PromotionModuleService<
}
@InjectTransactionManager("baseRepository_")
protected async update_(
protected async updatePromotions_(
data: PromotionTypes.UpdatePromotionDTO[],
@MedusaContext() sharedContext: Context = {}
) {
@@ -1014,8 +1010,8 @@ export default class PromotionModuleService<
relationName: "promotions" | "method_target_rules" | "method_buy_rules",
relation: Promotion | ApplicationMethod,
@MedusaContext() sharedContext: Context = {}
): Promise<TPromotionRule[]> {
const createdPromotionRules: TPromotionRule[] = []
): Promise<PromotionRule[]> {
const createdPromotionRules: PromotionRule[] = []
const promotion =
relation instanceof ApplicationMethod ? relation.promotion : relation