From 3b1a63eca7adcf09a8e9634593b483b70b38aa44 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 28 Nov 2024 10:23:26 +0100 Subject: [PATCH] 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 --- .../types/src/pricing/common/price-rule.ts | 5 ++++ packages/core/utils/src/bundles.ts | 3 ++- packages/core/utils/src/pricing/enums.ts | 7 ++++++ packages/core/utils/src/pricing/index.ts | 3 ++- .../migrations/.snapshot-medusa-pricing.json | 22 ++++++++++++++-- .../src/migrations/Migration20241127114534.ts | 25 +++++++++++++++++++ .../src/migrations/Migration20241127223829.ts | 21 ++++++++++++++++ .../modules/pricing/src/models/price-rule.ts | 11 ++++++-- 8 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 packages/core/utils/src/pricing/enums.ts create mode 100644 packages/modules/pricing/src/migrations/Migration20241127114534.ts create mode 100644 packages/modules/pricing/src/migrations/Migration20241127223829.ts diff --git a/packages/core/types/src/pricing/common/price-rule.ts b/packages/core/types/src/pricing/common/price-rule.ts index 7b29301295..49a33bbd7c 100644 --- a/packages/core/types/src/pricing/common/price-rule.ts +++ b/packages/core/types/src/pricing/common/price-rule.ts @@ -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" diff --git a/packages/core/utils/src/bundles.ts b/packages/core/utils/src/bundles.ts index acad41bdc6..cee955c1de 100644 --- a/packages/core/utils/src/bundles.ts +++ b/packages/core/utils/src/bundles.ts @@ -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" diff --git a/packages/core/utils/src/pricing/enums.ts b/packages/core/utils/src/pricing/enums.ts new file mode 100644 index 0000000000..e4bc8f6dbf --- /dev/null +++ b/packages/core/utils/src/pricing/enums.ts @@ -0,0 +1,7 @@ +export enum PricingRuleOperator { + GTE = "gte", + LTE = "lte", + GT = "gt", + LT = "lt", + EQ = "eq", +} diff --git a/packages/core/utils/src/pricing/index.ts b/packages/core/utils/src/pricing/index.ts index 7e0a00cb5e..509364cef4 100644 --- a/packages/core/utils/src/pricing/index.ts +++ b/packages/core/utils/src/pricing/index.ts @@ -1,3 +1,4 @@ export * from "./builders" -export * from "./price-list" +export * from "./enums" export * from "./events" +export * from "./price-list" diff --git a/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json b/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json index 46f12f4a56..fb6ac7c677 100644 --- a/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json +++ b/packages/modules/pricing/src/migrations/.snapshot-medusa-pricing.json @@ -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", diff --git a/packages/modules/pricing/src/migrations/Migration20241127114534.ts b/packages/modules/pricing/src/migrations/Migration20241127114534.ts new file mode 100644 index 0000000000..aab0492d8b --- /dev/null +++ b/packages/modules/pricing/src/migrations/Migration20241127114534.ts @@ -0,0 +1,25 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20241127114534 extends Migration { + async up(): Promise { + 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 { + this.addSql('drop index if exists "IDX_price_rule_operator";') + this.addSql( + 'alter table if exists "price_rule" drop column if exists "operator";' + ) + } +} diff --git a/packages/modules/pricing/src/migrations/Migration20241127223829.ts b/packages/modules/pricing/src/migrations/Migration20241127223829.ts new file mode 100644 index 0000000000..ba3ceedb91 --- /dev/null +++ b/packages/modules/pricing/src/migrations/Migration20241127223829.ts @@ -0,0 +1,21 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20241127223829 extends Migration { + async up(): Promise { + 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 { + 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;' + ) + } +} diff --git a/packages/modules/pricing/src/models/price-rule.ts b/packages/modules/pricing/src/models/price-rule.ts index c814bff7e6..76955bd275 100644 --- a/packages/modules/pricing/src/models/price-rule.ts +++ b/packages/modules/pricing/src/models/price-rule.ts @@ -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