feat(promotion,dashboard,types,utils,medusa): Add statuses to promotions (#10950)
what: - adds a status column to promotion table - introduce active promotion query - scope revert, register and compute actions to active promotions - admin to create and update promotion with statuses RESOLVES CMRC-845 RESOLVES CMRC-846 RESOLVES CMRC-847 RESOLVES CMRC-848 RESOLVES CMRC-849 RESOLVES CMRC-850
This commit is contained in:
@@ -345,6 +345,21 @@
|
||||
],
|
||||
"mappedType": "enum"
|
||||
},
|
||||
"status": {
|
||||
"name": "status",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"default": "'draft'",
|
||||
"enumItems": [
|
||||
"draft",
|
||||
"active",
|
||||
"inactive"
|
||||
],
|
||||
"mappedType": "enum"
|
||||
},
|
||||
"campaign_id": {
|
||||
"name": "campaign_id",
|
||||
"type": "text",
|
||||
@@ -414,6 +429,14 @@
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_promotion_type\" ON \"promotion\" (type) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "IDX_promotion_status",
|
||||
"columnNames": [],
|
||||
"composite": false,
|
||||
"primary": false,
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_promotion_status\" ON \"promotion\" (status) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "IDX_promotion_campaign_id",
|
||||
"columnNames": [],
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Migration } from "@mikro-orm/migrations"
|
||||
|
||||
export class Migration20250113094144 extends Migration {
|
||||
async up(): Promise<void> {
|
||||
this.addSql(
|
||||
"alter table if exists \"promotion\" add column if not exists \"status\" text check (\"status\" in ('draft', 'active', 'inactive')) not null default 'draft';"
|
||||
)
|
||||
|
||||
this.addSql(
|
||||
'CREATE INDEX IF NOT EXISTS "IDX_promotion_status" ON "promotion" (status) WHERE deleted_at IS NULL;'
|
||||
)
|
||||
|
||||
// Data Migration
|
||||
this.addSql(`UPDATE promotion SET status = 'active';`)
|
||||
}
|
||||
|
||||
async down(): Promise<void> {
|
||||
this.addSql('drop index if exists "IDX_promotion_status";')
|
||||
this.addSql(
|
||||
'alter table if exists "promotion" drop column if exists "status";'
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,10 @@ const Promotion = model
|
||||
.index("IDX_promotion_code"),
|
||||
is_automatic: model.boolean().default(false),
|
||||
type: model.enum(PromotionUtils.PromotionType).index("IDX_promotion_type"),
|
||||
status: model
|
||||
.enum(PromotionUtils.PromotionStatus)
|
||||
.index("IDX_promotion_status")
|
||||
.default(PromotionUtils.PromotionStatus.DRAFT),
|
||||
campaign: model
|
||||
.belongsTo(() => Campaign, {
|
||||
mappedBy: "promotions",
|
||||
|
||||
@@ -2,10 +2,13 @@ import {
|
||||
CampaignBudgetTypeValues,
|
||||
Context,
|
||||
DAL,
|
||||
FilterablePromotionProps,
|
||||
FindConfig,
|
||||
InferEntityType,
|
||||
InternalModuleDeclaration,
|
||||
ModuleJoinerConfig,
|
||||
ModulesSdkTypes,
|
||||
PromotionDTO,
|
||||
PromotionTypes,
|
||||
} from "@medusajs/framework/types"
|
||||
import {
|
||||
@@ -24,6 +27,7 @@ import {
|
||||
MedusaContext,
|
||||
MedusaError,
|
||||
MedusaService,
|
||||
PromotionStatus,
|
||||
PromotionType,
|
||||
toMikroORMEntity,
|
||||
transformPropertiesToBigNumber,
|
||||
@@ -134,6 +138,40 @@ export default class PromotionModuleService
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
@InjectManager()
|
||||
listActivePromotions(
|
||||
filters?: FilterablePromotionProps,
|
||||
config?: FindConfig<PromotionDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<PromotionDTO[]> {
|
||||
const activeFilters = {
|
||||
$or: [
|
||||
{
|
||||
status: PromotionStatus.ACTIVE,
|
||||
campaign_id: null,
|
||||
...filters,
|
||||
},
|
||||
{
|
||||
status: PromotionStatus.ACTIVE,
|
||||
...filters,
|
||||
campaign: {
|
||||
...filters?.campaign,
|
||||
$and: [
|
||||
{
|
||||
$or: [{ starts_at: null }, { starts_at: { $lte: new Date() } }],
|
||||
},
|
||||
{
|
||||
$or: [{ ends_at: null }, { ends_at: { $gt: new Date() } }],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
return this.listPromotions(activeFilters, config, sharedContext)
|
||||
}
|
||||
|
||||
@InjectManager()
|
||||
async registerUsage(
|
||||
computedActions: PromotionTypes.UsageComputedActions[],
|
||||
@@ -148,11 +186,9 @@ export default class PromotionModuleService
|
||||
const campaignBudgetMap = new Map<string, UpdateCampaignBudgetDTO>()
|
||||
const promotionCodeUsageMap = new Map<string, boolean>()
|
||||
|
||||
const existingPromotions = await this.listPromotions(
|
||||
const existingPromotions = await this.listActivePromotions(
|
||||
{ code: promotionCodes },
|
||||
{
|
||||
relations: ["campaign", "campaign.budget"],
|
||||
},
|
||||
{ relations: ["campaign", "campaign.budget"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
@@ -250,15 +286,13 @@ export default class PromotionModuleService
|
||||
const promotionCodeUsageMap = new Map<string, boolean>()
|
||||
const campaignBudgetMap = new Map<string, UpdateCampaignBudgetDTO>()
|
||||
|
||||
const existingPromotions = await this.listPromotions(
|
||||
const existingPromotions = await this.listActivePromotions(
|
||||
{
|
||||
code: computedActions
|
||||
.map((computedAction) => computedAction.code)
|
||||
.filter(Boolean),
|
||||
},
|
||||
{
|
||||
relations: ["campaign", "campaign.budget"],
|
||||
},
|
||||
{ relations: ["campaign", "campaign.budget"] },
|
||||
sharedContext
|
||||
)
|
||||
|
||||
@@ -362,7 +396,7 @@ export default class PromotionModuleService
|
||||
>()
|
||||
const automaticPromotions = preventAutoPromotions
|
||||
? []
|
||||
: await this.listPromotions(
|
||||
: await this.listActivePromotions(
|
||||
{ is_automatic: true },
|
||||
{ select: ["code"] },
|
||||
sharedContext
|
||||
@@ -402,7 +436,7 @@ export default class PromotionModuleService
|
||||
})
|
||||
})
|
||||
|
||||
const promotions = await this.listPromotions(
|
||||
const promotions = await this.listActivePromotions(
|
||||
{
|
||||
code: [
|
||||
...promotionCodesToApply,
|
||||
@@ -412,6 +446,7 @@ export default class PromotionModuleService
|
||||
},
|
||||
{
|
||||
take: null,
|
||||
order: { application_method: { value: "DESC" } },
|
||||
relations: [
|
||||
"application_method",
|
||||
"application_method.target_rules",
|
||||
|
||||
Reference in New Issue
Block a user