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 10:23:26 +01:00
committed by GitHub
parent 2838100efc
commit 3b1a63eca7
8 changed files with 91 additions and 6 deletions
@@ -133,3 +133,8 @@ export interface FilterablePriceRuleProps
*/
price_set_id?: string[]
}
/**
* The possible operators to use in a price rule.
*/
export type PricingRuleOperatorValues = "gt" | "lt" | "eq" | "lte" | "gte"
+2 -1
View File
@@ -6,14 +6,15 @@ export * as DMLUtils from "./dml"
export * as EventBusUtils from "./event-bus"
export * as FeatureFlagUtils from "./feature-flags"
export * as FulfillmentUtils from "./fulfillment"
export * as GraphQLUtils from "./graphql"
export * as InventoryUtils from "./inventory"
export * as LinkUtils from "./link"
export * as ModulesSdkUtils from "./modules-sdk"
export * as OrchestrationUtils from "./orchestration"
export * as OrderUtils from "./order"
export * as PricingUtils from "./pricing"
export * as ProductUtils from "./product"
export * as PromotionUtils from "./promotion"
export * as SearchUtils from "./search"
export * as ShippingProfileUtils from "./shipping"
export * as UserUtils from "./user"
export * as GraphQLUtils from "./graphql"
+7
View File
@@ -0,0 +1,7 @@
export enum PricingRuleOperator {
GTE = "gte",
LTE = "lte",
GT = "gt",
LT = "lt",
EQ = "eq",
}
+2 -1
View File
@@ -1,3 +1,4 @@
export * from "./builders"
export * from "./price-list"
export * from "./enums"
export * from "./events"
export * from "./price-list"
@@ -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