feat(pricing,utils,types): add operator field to price rule (#10315)

what:

- adds an operator field to price rule with specific operator fields

RESOLVES CMRC-746
This commit is contained in:
Riqwan Thamir
2024-11-28 09:23:26 +00:00
committed by GitHub
parent 2838100efc
commit 3b1a63eca7
8 changed files with 91 additions and 6 deletions
@@ -628,6 +628,17 @@
"nullable": false,
"mappedType": "text"
},
"operator": {
"name": "operator",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "'eq'",
"enumItems": ["gte", "lte", "gt", "lt", "eq"],
"mappedType": "enum"
},
"value": {
"name": "value",
"type": "text",
@@ -693,12 +704,19 @@
"schema": "public",
"indexes": [
{
"keyName": "IDX_price_rule_price_id_attribute_unique",
"columnNames": ["operator"],
"composite": false,
"keyName": "IDX_price_rule_operator",
"primary": false,
"unique": false
},
{
"keyName": "IDX_price_rule_price_id_attribute_operator_unique",
"columnNames": ["price_id"],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_price_rule_price_id_attribute_unique\" ON \"price_rule\" (price_id, attribute) WHERE deleted_at IS NULL"
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_price_rule_price_id_attribute_operator_unique\" ON \"price_rule\" (price_id, attribute, operator) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_rule_deleted_at",
@@ -0,0 +1,25 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20241127114534 extends Migration {
async up(): Promise<void> {
this.addSql(
`alter table if exists "price_rule" add column if not exists "operator" text check ("operator" in ('gte', 'lte', 'gt', 'lt', 'eq'));`
)
this.addSql(
`update "price_rule" set "operator" = 'eq' where "operator" is null;`
)
this.addSql(
`alter table "price_rule" alter column "operator" set not null, alter column "operator" set default 'eq';`
)
this.addSql(
'create index if not exists "IDX_price_rule_operator" on "price_rule" ("operator");'
)
}
async down(): Promise<void> {
this.addSql('drop index if exists "IDX_price_rule_operator";')
this.addSql(
'alter table if exists "price_rule" drop column if exists "operator";'
)
}
}
@@ -0,0 +1,21 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20241127223829 extends Migration {
async up(): Promise<void> {
this.addSql(
'drop index if exists "IDX_price_rule_price_id_attribute_unique";'
)
this.addSql(
'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_price_rule_price_id_attribute_operator_unique" ON "price_rule" (price_id, attribute, operator) WHERE deleted_at IS NULL;'
)
}
async down(): Promise<void> {
this.addSql(
'drop index if exists "IDX_price_rule_price_id_attribute_operator_unique";'
)
this.addSql(
'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_price_rule_price_id_attribute_unique" ON "price_rule" (price_id, attribute) WHERE deleted_at IS NULL;'
)
}
}
@@ -1,13 +1,16 @@
import { DAL } from "@medusajs/framework/types"
import { DAL, PricingRuleOperatorValues } from "@medusajs/framework/types"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
PricingRuleOperator,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Enum,
Filter,
Index,
ManyToOne,
OnInit,
OptionalProps,
@@ -28,7 +31,7 @@ const PriceRuleDeletedAtIndex = createPsqlIndexStatementHelper({
const PriceRulePriceIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: ["price_id", "attribute"],
columns: ["price_id", "attribute", "operator"],
where: "deleted_at IS NULL",
unique: true,
})
@@ -44,6 +47,10 @@ export default class PriceRule {
@Property({ columnType: "text" })
attribute: string
@Index({ name: "IDX_price_rule_operator" })
@Enum({ items: () => PricingRuleOperator, default: PricingRuleOperator.EQ })
operator: PricingRuleOperatorValues = PricingRuleOperator.EQ
@Property({ columnType: "text" })
value: string