This reverts commit a095245d71.
This commit is contained in:
1 parent
ccff121691
commit
0b55295fc7
60 files changed
+711
-862
No files matched your search
@@ -18,7 +18,6 @@ import {
|
||||
CampaignBudgetType,
|
||||
ComputedActions,
|
||||
deduplicate,
|
||||
EmitEvents,
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
isDefined,
|
||||
@@ -140,26 +139,11 @@ export default class PromotionModuleService
|
||||
}
|
||||
|
||||
@InjectManager()
|
||||
async listActivePromotions(
|
||||
listActivePromotions(
|
||||
filters?: FilterablePromotionProps,
|
||||
config?: FindConfig<PromotionDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<PromotionDTO[]> {
|
||||
const activePromotions = await this.listActivePromotions_(
|
||||
filters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return this.baseRepository_.serialize<PromotionDTO[]>(activePromotions)
|
||||
}
|
||||
|
||||
@InjectManager()
|
||||
protected async listActivePromotions_(
|
||||
filters?: FilterablePromotionProps,
|
||||
config?: FindConfig<PromotionDTO>,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<InferEntityType<typeof Promotion>[]> {
|
||||
// Ensure we share the same now date across all filters
|
||||
const now = new Date()
|
||||
const activeFilters = {
|
||||
@@ -186,11 +170,7 @@ export default class PromotionModuleService
|
||||
],
|
||||
}
|
||||
|
||||
return await this.promotionService_.list(
|
||||
activeFilters,
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
return this.listPromotions(activeFilters, config, sharedContext)
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
@@ -205,7 +185,7 @@ export default class PromotionModuleService
|
||||
const campaignBudgetMap = new Map<string, UpdateCampaignBudgetDTO>()
|
||||
const promotionCodeUsageMap = new Map<string, boolean>()
|
||||
|
||||
const existingPromotions = await this.listActivePromotions_(
|
||||
const existingPromotions = await this.listActivePromotions(
|
||||
{ code: promotionCodes },
|
||||
{ relations: ["campaign", "campaign.budget"] },
|
||||
sharedContext
|
||||
@@ -220,10 +200,9 @@ export default class PromotionModuleService
|
||||
}
|
||||
}
|
||||
|
||||
const existingPromotionsMap = new Map<
|
||||
string,
|
||||
InferEntityType<typeof Promotion>
|
||||
>(existingPromotions.map((promotion) => [promotion.code!, promotion]))
|
||||
const existingPromotionsMap = new Map<string, PromotionTypes.PromotionDTO>(
|
||||
existingPromotions.map((promotion) => [promotion.code!, promotion])
|
||||
)
|
||||
|
||||
for (let computedAction of computedActions) {
|
||||
const promotion = existingPromotionsMap.get(computedAction.code)
|
||||
@@ -314,7 +293,7 @@ export default class PromotionModuleService
|
||||
const promotionCodeUsageMap = new Map<string, boolean>()
|
||||
const campaignBudgetMap = new Map<string, UpdateCampaignBudgetDTO>()
|
||||
|
||||
const existingPromotions = await this.listActivePromotions_(
|
||||
const existingPromotions = await this.listActivePromotions(
|
||||
{
|
||||
code: computedActions
|
||||
.map((computedAction) => computedAction.code)
|
||||
@@ -333,10 +312,9 @@ export default class PromotionModuleService
|
||||
}
|
||||
}
|
||||
|
||||
const existingPromotionsMap = new Map<
|
||||
string,
|
||||
InferEntityType<typeof Promotion>
|
||||
>(existingPromotions.map((promotion) => [promotion.code!, promotion]))
|
||||
const existingPromotionsMap = new Map<string, PromotionTypes.PromotionDTO>(
|
||||
existingPromotions.map((promotion) => [promotion.code!, promotion])
|
||||
)
|
||||
|
||||
for (let computedAction of computedActions) {
|
||||
const promotion = existingPromotionsMap.get(computedAction.code)
|
||||
@@ -455,18 +433,25 @@ export default class PromotionModuleService
|
||||
|
||||
const methodIdPromoValueMap = new Map<string, number>()
|
||||
|
||||
const promotionCodesToApply = [...promotionCodes, ...appliedCodes]
|
||||
const automaticPromotions = preventAutoPromotions
|
||||
? []
|
||||
: await this.listActivePromotions(
|
||||
{ is_automatic: true },
|
||||
{ select: ["code"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const automaticPromotionCodes = automaticPromotions.map((p) => p.code!)
|
||||
const promotionCodesToApply = [
|
||||
...promotionCodes,
|
||||
...automaticPromotionCodes,
|
||||
...appliedCodes,
|
||||
]
|
||||
|
||||
const uniquePromotionCodes = Array.from(new Set(promotionCodesToApply))
|
||||
|
||||
const queryFilter = preventAutoPromotions
|
||||
? { code: uniquePromotionCodes }
|
||||
: {
|
||||
$or: [{ code: uniquePromotionCodes }, { is_automatic: true }],
|
||||
}
|
||||
|
||||
const promotions = await this.listActivePromotions(
|
||||
queryFilter,
|
||||
{ code: uniquePromotionCodes },
|
||||
{
|
||||
take: null,
|
||||
order: { application_method: { value: "DESC" } },
|
||||
@@ -489,9 +474,6 @@ export default class PromotionModuleService
|
||||
promotions.map((promotion) => [promotion.code!, promotion])
|
||||
)
|
||||
|
||||
const automaticPromotions = promotions.filter((p) => p.is_automatic)
|
||||
const automaticPromotionCodes = automaticPromotions.map((p) => p.code!)
|
||||
|
||||
for (const [code, adjustments] of codeAdjustmentMap.entries()) {
|
||||
for (const adjustment of adjustments.items) {
|
||||
computedActions.push({
|
||||
@@ -646,9 +628,7 @@ export default class PromotionModuleService
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<
|
||||
PromotionTypes.PromotionDTO | PromotionTypes.PromotionDTO[]
|
||||
>(Array.isArray(data) ? promotions : promotions[0])
|
||||
return Array.isArray(data) ? promotions : promotions[0]
|
||||
}
|
||||
|
||||
@InjectTransactionManager()
|
||||
@@ -907,7 +887,6 @@ export default class PromotionModuleService
|
||||
): Promise<PromotionTypes.PromotionDTO[]>
|
||||
|
||||
@InjectManager()
|
||||
@EmitEvents()
|
||||
// @ts-expect-error
|
||||
async updatePromotions(
|
||||
data:
|
||||
@@ -1061,7 +1040,7 @@ export default class PromotionModuleService
|
||||
) {
|
||||
const promotionRuleIds = data.map((d) => d.id)
|
||||
|
||||
const promotionRules = await this.promotionRuleService_.list(
|
||||
const promotionRules = await this.listPromotionRules(
|
||||
{ id: promotionRuleIds },
|
||||
{ relations: ["values"] },
|
||||
sharedContext
|
||||
@@ -1118,7 +1097,6 @@ export default class PromotionModuleService
|
||||
}
|
||||
|
||||
@InjectManager()
|
||||
@EmitEvents()
|
||||
async addPromotionRules(
|
||||
promotionId: string,
|
||||
rulesData: PromotionTypes.CreatePromotionRuleDTO[],
|
||||
@@ -1240,8 +1218,6 @@ export default class PromotionModuleService
|
||||
|
||||
validatePromotionRuleAttributes(rulesData)
|
||||
|
||||
const promotionRuleValuesDataToCreate: CreatePromotionRuleValueDTO[] = []
|
||||
|
||||
for (const ruleData of rulesData) {
|
||||
const { values, ...rest } = ruleData
|
||||
const promotionRuleData: CreatePromotionRuleDTO = {
|
||||
@@ -1262,14 +1238,12 @@ export default class PromotionModuleService
|
||||
promotion_rule: createdPromotionRule,
|
||||
}))
|
||||
|
||||
promotionRuleValuesDataToCreate.push(...promotionRuleValuesData)
|
||||
await this.promotionRuleValueService_.create(
|
||||
promotionRuleValuesData,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
await this.promotionRuleValueService_.create(
|
||||
promotionRuleValuesDataToCreate,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return createdPromotionRules
|
||||
}
|
||||
|
||||
@@ -1515,16 +1489,15 @@ export default class PromotionModuleService
|
||||
const updateBudgetData: UpdateCampaignBudgetDTO[] = []
|
||||
const createBudgetData: CreateCampaignBudgetDTO[] = []
|
||||
|
||||
const existingCampaigns = await this.campaignService_.list(
|
||||
const existingCampaigns = await this.listCampaigns(
|
||||
{ id: campaignIds },
|
||||
{ relations: ["budget"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
const existingCampaignsMap = new Map<
|
||||
string,
|
||||
InferEntityType<typeof Campaign>
|
||||
>(existingCampaigns.map((campaign) => [campaign.id, campaign]))
|
||||
const existingCampaignsMap = new Map<string, PromotionTypes.CampaignDTO>(
|
||||
existingCampaigns.map((campaign) => [campaign.id, campaign])
|
||||
)
|
||||
|
||||
for (const updateCampaignData of data) {
|
||||
const { budget: budgetData, ...campaignData } = updateCampaignData
|
||||
@@ -1581,7 +1554,7 @@ export default class PromotionModuleService
|
||||
@InjectManager()
|
||||
async addPromotionsToCampaign(
|
||||
data: PromotionTypes.AddPromotionsToCampaignDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
sharedContext?: Context
|
||||
): Promise<{ ids: string[] }> {
|
||||
const ids = await this.addPromotionsToCampaign_(data, sharedContext)
|
||||
|
||||
@@ -1648,7 +1621,7 @@ export default class PromotionModuleService
|
||||
@InjectManager()
|
||||
async removePromotionsFromCampaign(
|
||||
data: PromotionTypes.AddPromotionsToCampaignDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
sharedContext?: Context
|
||||
): Promise<{ ids: string[] }> {
|
||||
const ids = await this.removePromotionsFromCampaign_(data, sharedContext)
|
||||
|
||||
|
||||
Reference in new issue
Block a user