refactor: migrate pricing entities to DML models (#10335)

Fixes: FRMW-2810

## Breaking changes
There is only one breaking change

- The `min_quantity` and `max_quantity` properties are now consistently typed as numbers. Earlier, it was [typed as numbers](https://github.com/medusajs/medusa/blob/develop/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts#L68-L69) in some API responses and as [string in others](https://github.com/medusajs/medusa/blob/develop/integration-tests/http/__tests__/price-list/admin/price-list.spec.ts#L186-L187). I did not go to the bottom of this inconsistency, but the tests reveals them.

Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
Harminder Virk
2024-12-02 15:14:41 +05:30
committed by GitHub
parent b4d6a4b3f0
commit 913cf15e2b
18 changed files with 387 additions and 726 deletions

View File

@@ -183,8 +183,8 @@ medusaIntegrationTestRunner({
amount: 100,
currency_code: "usd",
// BREAKING: Min and max quantity are returned as string
min_quantity: "1",
max_quantity: "100",
min_quantity: 1,
max_quantity: 100,
variant_id: product1.variants[0].id,
created_at: expect.any(String),
updated_at: expect.any(String),
@@ -194,8 +194,8 @@ medusaIntegrationTestRunner({
id: expect.any(String),
amount: 80,
currency_code: "usd",
min_quantity: "101",
max_quantity: "500",
min_quantity: 101,
max_quantity: 500,
variant_id: product1.variants[0].id,
created_at: expect.any(String),
updated_at: expect.any(String),
@@ -360,15 +360,15 @@ medusaIntegrationTestRunner({
amount: 100,
currency_code: "usd",
id: expect.any(String),
max_quantity: "100",
min_quantity: "1",
max_quantity: 100,
min_quantity: 1,
}),
expect.objectContaining({
amount: 80,
currency_code: "usd",
id: expect.any(String),
max_quantity: "500",
min_quantity: "101",
max_quantity: 500,
min_quantity: 101,
}),
]),
rules: {
@@ -409,15 +409,15 @@ medusaIntegrationTestRunner({
amount: 100,
currency_code: "usd",
id: expect.any(String),
max_quantity: "100",
min_quantity: "1",
max_quantity: 100,
min_quantity: 1,
}),
expect.objectContaining({
amount: 250,
currency_code: "eur",
id: expect.any(String),
max_quantity: "500",
min_quantity: "101",
max_quantity: 500,
min_quantity: 101,
}),
])
})
@@ -470,16 +470,16 @@ medusaIntegrationTestRunner({
id: expect.any(String),
amount: 100,
currency_code: "usd",
min_quantity: "1",
max_quantity: "100",
min_quantity: 1,
max_quantity: 100,
variant_id: product1.variants[0].id,
}),
expect.objectContaining({
id: expect.any(String),
amount: 80,
currency_code: "usd",
min_quantity: "101",
max_quantity: "500",
min_quantity: 101,
max_quantity: 500,
variant_id: product1.variants[0].id,
}),
expect.objectContaining({
@@ -487,24 +487,24 @@ medusaIntegrationTestRunner({
amount: 45,
currency_code: "usd",
variant_id: product1.variants[0].id,
min_quantity: "1001",
max_quantity: "2000",
min_quantity: 1001,
max_quantity: 2000,
}),
expect.objectContaining({
id: expect.any(String),
amount: 35,
currency_code: "usd",
variant_id: product1.variants[0].id,
min_quantity: "2001",
max_quantity: "3000",
min_quantity: 2001,
max_quantity: 3000,
}),
expect.objectContaining({
id: expect.any(String),
amount: 25,
currency_code: "usd",
variant_id: product1.variants[0].id,
min_quantity: "3001",
max_quantity: "4000",
min_quantity: 3001,
max_quantity: 4000,
}),
])
)
@@ -551,16 +551,16 @@ medusaIntegrationTestRunner({
id: expect.any(String),
amount: 45,
currency_code: "usd",
min_quantity: "1",
max_quantity: "100",
min_quantity: 1,
max_quantity: 100,
variant_id: product1.variants[0].id,
}),
expect.objectContaining({
id: expect.any(String),
amount: 35,
currency_code: "usd",
min_quantity: "101",
max_quantity: "500",
min_quantity: 101,
max_quantity: 500,
variant_id: product1.variants[0].id,
}),
])
@@ -605,16 +605,16 @@ medusaIntegrationTestRunner({
id: expect.any(String),
amount: 100,
currency_code: "usd",
min_quantity: "1",
max_quantity: "100",
min_quantity: 1,
max_quantity: 100,
variant_id: product1.variants[0].id,
}),
expect.objectContaining({
id: expect.any(String),
amount: 80,
currency_code: "usd",
min_quantity: "101",
max_quantity: "500",
min_quantity: 101,
max_quantity: 500,
variant_id: product1.variants[0].id,
}),
expect.objectContaining({
@@ -698,8 +698,8 @@ medusaIntegrationTestRunner({
expect.objectContaining({
amount: 80,
currency_code: "usd",
min_quantity: "101",
max_quantity: "500",
min_quantity: 101,
max_quantity: 500,
variant_id: product1.variants[0].id,
}),
])

View File

@@ -191,6 +191,7 @@ medusaIntegrationTestRunner({
max_quantity: null,
min_quantity: null,
price_list: null,
price_list_id: null,
price_set_id: expect.any(String),
raw_amount: {
precision: 20,
@@ -210,6 +211,7 @@ medusaIntegrationTestRunner({
max_quantity: null,
min_quantity: null,
price_list: null,
price_list_id: null,
price_set_id: expect.any(String),
raw_amount: {
precision: 20,

View File

@@ -1,5 +1,6 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceListRule } from "@models"
import { toMikroORMEntity } from "@medusajs/framework/utils"
import { defaultPriceListRuleData } from "./data"
export * from "./data"
@@ -11,7 +12,7 @@ export async function createPriceListRules(
const priceListRules: PriceListRule[] = []
for (let data of priceListRuleData) {
const plr = manager.create(PriceListRule, data)
const plr = manager.create(toMikroORMEntity(PriceListRule), data)
priceListRules.push(plr)
}

View File

@@ -1,5 +1,6 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceList } from "@models"
import { toMikroORMEntity } from "@medusajs/framework/utils"
import { defaultPriceListData } from "./data"
export * from "./data"
@@ -11,7 +12,7 @@ export async function createPriceLists(
const priceLists: PriceList[] = []
for (let data of priceListData) {
const pl = manager.create(PriceList, data)
const pl = manager.create(toMikroORMEntity(PriceList), data)
priceLists.push(pl)
}

View File

@@ -2,6 +2,7 @@ import { PriceRule } from "@models"
import { CreatePriceRuleDTO } from "@medusajs/framework/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { toMikroORMEntity } from "@medusajs/framework/utils"
import { defaultPriceRuleData } from "./data"
export * from "./data"
@@ -20,7 +21,10 @@ export async function createPriceRules(
priceRuleDataClone.attribute = priceRuleDataClone.attribute
priceRuleDataClone.price_id = priceRuleDataClone.price_id
const priceRule = manager.create(PriceRule, priceRuleDataClone)
const priceRule = manager.create(
toMikroORMEntity(PriceRule),
priceRuleDataClone
)
priceRules.push(priceRule)
}

View File

@@ -1,6 +1,7 @@
import { CreatePriceSetDTO } from "@medusajs/framework/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Price, PriceSet } from "@models"
import { toMikroORMEntity } from "@medusajs/framework/utils"
import { defaultPriceSetsData } from "./data"
export * from "./data"
@@ -16,12 +17,15 @@ export async function createPriceSets(
const prices = priceSetDataClone.prices || []
delete priceSetDataClone.prices
let priceSet = manager.create(PriceSet, priceSetDataClone) as PriceSet
let priceSet = manager.create(
toMikroORMEntity(PriceSet),
priceSetDataClone
) as PriceSet
manager.persist(priceSet)
for (let priceData of prices) {
const price = manager.create(Price, {
const price = manager.create(toMikroORMEntity(Price), {
...priceData,
price_set_id: priceSet.id,
title: "test",

View File

@@ -1,6 +1,7 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Price } from "@models"
import { defaultPricesData } from "./data"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { toMikroORMEntity } from "@medusajs/framework/utils"
export * from "./data"
@@ -11,7 +12,7 @@ export async function createPrices(
const prices: Price[] = []
for (let data of pricesData) {
const price = manager.create(Price, data)
const price = manager.create(toMikroORMEntity(Price), data)
prices.push(price)
}

View File

@@ -254,11 +254,14 @@ moduleIntegrationTestRunner<IPricingModuleService>({
ends_at: "10/20/2030",
},
])
expect(priceList).toEqual(
expect.objectContaining({
starts_at: new Date("10/10/2010").toISOString(),
ends_at: new Date("10/20/2030").toISOString(),
})
expect(priceList).toHaveProperty("starts_at")
expect(priceList).toHaveProperty("ends_at")
expect(priceList.starts_at?.toString()).toEqual(
new Date("10/10/2010").toISOString()
)
expect(priceList.ends_at?.toString()).toEqual(
new Date("10/20/2030").toISOString()
)
})
@@ -378,11 +381,14 @@ moduleIntegrationTestRunner<IPricingModuleService>({
ends_at: "10/20/2030",
},
])
expect(priceList).toEqual(
expect.objectContaining({
starts_at: new Date("10/10/2010").toISOString(),
ends_at: new Date("10/20/2030").toISOString(),
})
expect(priceList).toHaveProperty("starts_at")
expect(priceList).toHaveProperty("ends_at")
expect(priceList.starts_at?.toString()).toEqual(
new Date("10/10/2010").toString()
)
expect(priceList.ends_at?.toString()).toEqual(
new Date("10/20/2030").toString()
)
})
@@ -562,7 +568,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
currency_code: "EUR",
}),
expect.objectContaining({
rules_count: 0,
price_rules: [],
amount: 600,
currency_code: "EUR",

View File

@@ -5,7 +5,7 @@ import { Price } from "../../../../src/models"
import { createPrices } from "../../../__fixtures__/price"
import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { Modules } from "@medusajs/framework/utils"
import { Modules, toMikroORMEntity } from "@medusajs/framework/utils"
jest.setTimeout(30000)
@@ -268,13 +268,13 @@ moduleIntegrationTestRunner<IPricingModuleService>({
})
it("should create a PriceRule successfully", async () => {
const price: Price = testManager.create(Price, {
const price = testManager.create(toMikroORMEntity(Price), {
currency_code: "EUR",
amount: 100,
price_set_id: "price-set-1",
title: "test",
rules_count: 0,
} as Price)
})
await testManager.persist(price).flush()

View File

@@ -1,5 +1,7 @@
{
"namespaces": ["public"],
"namespaces": [
"public"
],
"name": "public",
"tables": [
{
@@ -39,7 +41,10 @@
"primary": false,
"nullable": false,
"default": "'draft'",
"enumItems": ["active", "draft"],
"enumItems": [
"active",
"draft"
],
"mappedType": "enum"
},
"type": {
@@ -50,7 +55,10 @@
"primary": false,
"nullable": false,
"default": "'sale'",
"enumItems": ["sale", "override"],
"enumItems": [
"sale",
"override"
],
"mappedType": "enum"
},
"starts_at": {
@@ -79,7 +87,7 @@
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"nullable": true,
"default": "0",
"mappedType": "integer"
},
@@ -121,15 +129,17 @@
"indexes": [
{
"keyName": "IDX_price_list_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_deleted_at\" ON \"price_list\" (deleted_at) WHERE deleted_at IS NOT NULL"
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_deleted_at\" ON \"price_list\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "price_list_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -214,7 +224,7 @@
"indexes": [
{
"keyName": "IDX_price_list_rule_price_list_id",
"columnNames": ["price_list_id"],
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
@@ -222,15 +232,17 @@
},
{
"keyName": "IDX_price_list_rule_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_deleted_at\" ON \"price_list_rule\" (deleted_at) WHERE deleted_at IS NOT NULL"
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_list_rule_deleted_at\" ON \"price_list_rule\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "price_list_rule_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -240,9 +252,13 @@
"foreignKeys": {
"price_list_rule_price_list_id_foreign": {
"constraintName": "price_list_rule_price_list_id_foreign",
"columnNames": ["price_list_id"],
"columnNames": [
"price_list_id"
],
"localTableName": "public.price_list_rule",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_list",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -326,11 +342,11 @@
"indexes": [
{
"keyName": "IDX_price_preference_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_preference_deleted_at\" ON \"price_preference\" (deleted_at) WHERE deleted_at IS NOT NULL"
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_preference_deleted_at\" ON \"price_preference\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_preference_attribute_value",
@@ -342,7 +358,9 @@
},
{
"keyName": "price_preference_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -400,15 +418,17 @@
"indexes": [
{
"keyName": "IDX_price_set_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_deleted_at\" ON \"price_set\" (deleted_at) WHERE deleted_at IS NOT NULL"
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_set_deleted_at\" ON \"price_set\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "price_set_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -455,32 +475,33 @@
"nullable": false,
"mappedType": "decimal"
},
"raw_amount": {
"name": "raw_amount",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "json"
},
"min_quantity": {
"name": "min_quantity",
"type": "numeric",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "decimal"
"mappedType": "integer"
},
"max_quantity": {
"name": "max_quantity",
"type": "numeric",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "decimal"
"mappedType": "integer"
},
"rules_count": {
"name": "rules_count",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"default": "0",
"mappedType": "integer"
},
"price_set_id": {
"name": "price_set_id",
@@ -491,16 +512,6 @@
"nullable": false,
"mappedType": "text"
},
"rules_count": {
"name": "rules_count",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "0",
"mappedType": "integer"
},
"price_list_id": {
"name": "price_list_id",
"type": "text",
@@ -510,6 +521,15 @@
"nullable": true,
"mappedType": "text"
},
"raw_amount": {
"name": "raw_amount",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "json"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
@@ -546,17 +566,9 @@
"name": "price",
"schema": "public",
"indexes": [
{
"keyName": "IDX_price_currency_code",
"columnNames": ["currency_code"],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_currency_code\" ON \"price\" (currency_code) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_price_set_id",
"columnNames": ["price_set_id"],
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
@@ -564,7 +576,7 @@
},
{
"keyName": "IDX_price_price_list_id",
"columnNames": ["price_list_id"],
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
@@ -572,15 +584,25 @@
},
{
"keyName": "IDX_price_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_deleted_at\" ON \"price\" (deleted_at) WHERE deleted_at IS NOT NULL"
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_deleted_at\" ON \"price\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_currency_code",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_currency_code\" ON \"price\" (currency_code) WHERE deleted_at IS NULL"
},
{
"keyName": "price_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -590,18 +612,26 @@
"foreignKeys": {
"price_price_set_id_foreign": {
"constraintName": "price_price_set_id_foreign",
"columnNames": ["price_set_id"],
"columnNames": [
"price_set_id"
],
"localTableName": "public.price",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"price_price_list_id_foreign": {
"constraintName": "price_price_list_id_foreign",
"columnNames": ["price_list_id"],
"columnNames": [
"price_list_id"
],
"localTableName": "public.price",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_list",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -628,17 +658,6 @@
"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",
@@ -648,6 +667,23 @@
"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"
},
"priority": {
"name": "priority",
"type": "integer",
@@ -704,31 +740,34 @@
"schema": "public",
"indexes": [
{
"columnNames": ["operator"],
"keyName": "IDX_price_rule_price_id",
"columnNames": [],
"composite": false,
"keyName": "IDX_price_rule_operator",
"primary": false,
"unique": false
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_price_id\" ON \"price_rule\" (price_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_rule_deleted_at",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_deleted_at\" ON \"price_rule\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_price_rule_price_id_attribute_operator_unique",
"columnNames": ["price_id"],
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"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",
"columnNames": ["deleted_at"],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_price_rule_deleted_at\" ON \"price_rule\" (deleted_at) WHERE deleted_at IS NOT NULL"
},
{
"keyName": "price_rule_pkey",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -738,9 +777,13 @@
"foreignKeys": {
"price_rule_price_id_foreign": {
"constraintName": "price_rule_price_id_foreign",
"columnNames": ["price_id"],
"columnNames": [
"price_id"
],
"localTableName": "public.price_rule",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price",
"deleteRule": "cascade",
"updateRule": "cascade"

View File

@@ -0,0 +1,29 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20241128055359 extends Migration {
async up(): Promise<void> {
this.addSql('alter table if exists "price_list" alter column "rules_count" type integer using ("rules_count"::integer);');
this.addSql('alter table if exists "price_list" alter column "rules_count" drop not null;');
this.addSql('alter table if exists "price" alter column "min_quantity" type integer using ("min_quantity"::integer);');
this.addSql('alter table if exists "price" alter column "max_quantity" type integer using ("max_quantity"::integer);');
this.addSql('alter table if exists "price" alter column "rules_count" type integer using ("rules_count"::integer);');
this.addSql('alter table if exists "price" alter column "rules_count" drop not null;');
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_price_rule_price_id" ON "price_rule" (price_id) WHERE deleted_at IS NULL;');
}
async down(): Promise<void> {
this.addSql('alter table if exists "price_list" alter column "rules_count" type integer using ("rules_count"::integer);');
this.addSql('alter table if exists "price_list" alter column "rules_count" set not null;');
this.addSql('alter table if exists "price" alter column "min_quantity" type numeric using ("min_quantity"::numeric);');
this.addSql('alter table if exists "price" alter column "max_quantity" type numeric using ("max_quantity"::numeric);');
this.addSql('alter table if exists "price" alter column "rules_count" type integer using ("rules_count"::integer);');
this.addSql('alter table if exists "price" alter column "rules_count" set not null;');
this.addSql('drop index if exists "IDX_price_rule_price_id";');
}
}

View File

@@ -1,91 +1,20 @@
import { DAL } from "@medusajs/framework/types"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import PriceList from "./price-list"
type OptionalFields = DAL.SoftDeletableModelDateColumns
const tableName = "price_list_rule"
const PriceListRuleDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const PriceListRulePriceListIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_list_id",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceListRule {
[OptionalProps]: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text" })
attribute: string
@Property({ columnType: "jsonb", nullable: true })
value: string | string[] | null = null
@PriceListRulePriceListIdIndex.MikroORMIndex()
@ManyToOne(() => PriceList, {
columnType: "text",
mapToPk: true,
fieldName: "price_list_id",
onDelete: "cascade",
const PriceListRule = model
.define("PriceListRule", {
id: model.id({ prefix: "prule" }).primaryKey(),
attribute: model.text(),
value: model.json().nullable(),
price_list: model.belongsTo(() => PriceList, {
mappedBy: "price_list_rules",
}),
})
price_list_id: string
.indexes([
{
on: ["price_list_id"],
where: "deleted_at IS NULL",
},
])
@ManyToOne(() => PriceList, { persist: false })
price_list: Rel<PriceList>
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@PriceListRuleDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "plrule")
this.price_list_id ??= this.price_list?.id!
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "plrule")
this.price_list_id ??= this.price_list?.id!
}
}
export default PriceListRule

View File

@@ -1,116 +1,30 @@
import { DAL } from "@medusajs/framework/types"
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
model,
PriceListStatus,
PriceListType,
Searchable,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
Filter,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import Price from "./price"
import PriceListRule from "./price-list-rule"
type OptionalFields =
| "starts_at"
| "ends_at"
| DAL.SoftDeletableModelDateColumns
const tableName = "price_list"
const PriceListDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
export const PriceListIdPrefix = "plist"
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceList {
[OptionalProps]: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@Searchable()
@Property({ columnType: "text" })
title: string
@Searchable()
@Property({ columnType: "text" })
description: string
@Enum({ items: () => PriceListStatus, default: PriceListStatus.DRAFT })
status: PriceListStatus
@Enum({ items: () => PriceListType, default: PriceListType.SALE })
type: PriceListType
@Property({
columnType: "timestamptz",
nullable: true,
const PriceList = model
.define("PriceList", {
id: model.id({ prefix: "plist" }).primaryKey(),
title: model.text().searchable(),
description: model.text().searchable(),
status: model.enum(PriceListStatus).default(PriceListStatus.DRAFT),
type: model.enum(PriceListType).default(PriceListType.SALE),
starts_at: model.dateTime().nullable(),
ends_at: model.dateTime().nullable(),
rules_count: model.number().default(0).nullable(),
prices: model.hasMany(() => Price, {
mappedBy: "price_list",
}),
price_list_rules: model.hasMany(() => PriceListRule, {
mappedBy: "price_list",
}),
})
starts_at: Date | null = null
@Property({
columnType: "timestamptz",
nullable: true,
.cascades({
delete: ["price_list_rules", "prices"],
})
ends_at: Date | null = null
@OneToMany(() => Price, (price) => price.price_list, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
})
prices = new Collection<Rel<Price>>(this)
@OneToMany(() => PriceListRule, (pr) => pr.price_list, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
})
price_list_rules = new Collection<Rel<PriceListRule>>(this)
@Property({ columnType: "integer", default: 0 })
rules_count: number = 0
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@PriceListDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, PriceListIdPrefix)
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, PriceListIdPrefix)
}
}
export default PriceList

View File

@@ -1,75 +1,19 @@
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
export const uniquePreferenceRuleIndexName =
"IDX_price_preference_attribute_value"
const UniquePreferenceRuleIndexStatement = createPsqlIndexStatementHelper({
name: uniquePreferenceRuleIndexName,
tableName: "price_preference",
columns: ["attribute", "value"],
unique: true,
where: "deleted_at IS NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "price_preference",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@UniquePreferenceRuleIndexStatement.MikroORMIndex()
export default class PricePreference {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
attribute: string
@Property({ columnType: "text", nullable: true })
value: string | null = null
@Property({ default: false })
is_tax_inclusive: boolean
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
const PricePreference = model
.define("PricePreference", {
id: model.id({ prefix: "prpref" }).primaryKey(),
attribute: model.text(),
value: model.text().nullable(),
is_tax_inclusive: model.boolean().default(false),
})
created_at: Date
.indexes([
{
name: "IDX_price_preference_attribute_value",
on: ["attribute", "value"],
unique: true,
where: "deleted_at IS NULL",
},
])
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@DeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "prpref")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "prpref")
}
}
export default PricePreference

View File

@@ -1,102 +1,23 @@
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,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model, PricingRuleOperator } from "@medusajs/framework/utils"
import Price from "./price"
type OptionalFields = DAL.SoftDeletableModelDateColumns
const tableName = "price_rule"
const PriceRuleDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const PriceRulePriceIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: ["price_id", "attribute", "operator"],
where: "deleted_at IS NULL",
unique: true,
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceRule {
[OptionalProps]?: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@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
@Property({ columnType: "integer", default: 0 })
priority: number = 0
@PriceRulePriceIdIndex.MikroORMIndex()
@ManyToOne(() => Price, {
columnType: "text",
mapToPk: true,
fieldName: "price_id",
onDelete: "cascade",
const PriceRule = model
.define("PriceRule", {
id: model.id({ prefix: "prule" }).primaryKey(),
attribute: model.text(),
value: model.text(),
operator: model.enum(PricingRuleOperator).default(PricingRuleOperator.EQ),
priority: model.number().default(0),
price: model.belongsTo(() => Price, {
mappedBy: "price_rules",
}),
})
price_id: string
.indexes([
{
on: ["price_id", "attribute", "operator"],
where: "deleted_at IS NULL",
unique: true,
},
])
@ManyToOne(() => Price, { persist: false })
price: Rel<Price>
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@PriceRuleDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
beforeCreate() {
this.id = generateEntityId(this.id, "prule")
this.price_id ??= this.price?.id!
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "prule")
this.price_id ??= this.price?.id!
}
}
export default PriceRule

View File

@@ -1,68 +1,15 @@
import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
OneToMany,
OnInit,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import Price from "./price"
const tableName = "price_set"
const PriceSetDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
export const PriceSetIdPrefix = "pset"
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PriceSet {
@PrimaryKey({ columnType: "text" })
id!: string
@OneToMany(() => Price, (price) => price.price_set, {
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
const PriceSet = model
.define("PriceSet", {
id: model.id({ prefix: "pset" }).primaryKey(),
prices: model.hasMany(() => Price, {
mappedBy: "price_set",
}),
})
prices = new Collection<Rel<Price>>(this)
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
.cascades({
delete: ["prices"],
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@PriceSetDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, PriceSetIdPrefix)
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, PriceSetIdPrefix)
}
}
export default PriceSet

View File

@@ -1,148 +1,45 @@
import { DAL } from "@medusajs/framework/types"
import {
BigNumber,
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
MikroOrmBigNumberProperty,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
ManyToOne,
OneToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import PriceList from "./price-list"
import PriceRule from "./price-rule"
import PriceSet from "./price-set"
type OptionalFields = DAL.SoftDeletableModelDateColumns
const tableName = "price"
const PriceDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const PricePriceSetIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_set_id",
where: "deleted_at IS NULL",
})
const PricePriceListIdIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "price_list_id",
where: "deleted_at IS NULL",
})
const PriceCurrencyCodeIndex = createPsqlIndexStatementHelper({
tableName: tableName,
columns: "currency_code",
where: "deleted_at IS NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Price {
[OptionalProps]?: OptionalFields
@PrimaryKey({ columnType: "text" })
id!: string
@Property({ columnType: "text", nullable: true })
title: string | null = null
@PriceCurrencyCodeIndex.MikroORMIndex()
@Property({ columnType: "text" })
currency_code: string
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: Record<string, unknown>
@Property({ columnType: "numeric", nullable: true })
min_quantity: number | null = null
@Property({ columnType: "numeric", nullable: true })
max_quantity: number | null = null
@PricePriceSetIdIndex.MikroORMIndex()
@ManyToOne(() => PriceSet, {
columnType: "text",
mapToPk: true,
fieldName: "price_set_id",
onDelete: "cascade",
const Price = model
.define("Price", {
id: model.id({ prefix: "price" }).primaryKey(),
title: model.text().nullable(),
currency_code: model.text(),
amount: model.bigNumber(),
min_quantity: model.number().nullable(),
max_quantity: model.number().nullable(),
rules_count: model.number().default(0).nullable(),
price_set: model.belongsTo(() => PriceSet, {
mappedBy: "prices",
}),
price_rules: model.hasMany(() => PriceRule, {
mappedBy: "price",
}),
price_list: model
.belongsTo(() => PriceList, {
mappedBy: "prices",
})
.nullable(),
})
price_set_id: string
@ManyToOne(() => PriceSet, { persist: false })
price_set?: Rel<PriceSet>
@Property({ columnType: "integer", default: 0 })
rules_count: number = 0
@OneToMany({
entity: () => PriceRule,
mappedBy: (pr) => pr.price,
cascade: [Cascade.PERSIST, "soft-remove" as Cascade],
.cascades({
delete: ["price_rules"],
})
price_rules = new Collection<Rel<PriceRule>>(this)
.indexes([
{
on: ["price_set_id"],
where: "deleted_at IS NULL",
},
{
on: ["price_list_id"],
where: "deleted_at IS NULL",
},
{
on: ["currency_code"],
where: "deleted_at IS NULL",
},
])
@PricePriceListIdIndex.MikroORMIndex()
@ManyToOne(() => PriceList, {
columnType: "text",
mapToPk: true,
nullable: true,
fieldName: "price_list_id",
onDelete: "cascade",
})
price_list_id: string | null = null
@ManyToOne(() => PriceList, { persist: false, nullable: true })
price_list: Rel<PriceList> | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@PriceDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "price")
this.price_set_id ??= this.price_set?.id!
this.price_list_id ??= this.price_list?.id!
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "price")
this.price_set_id ??= this.price_set?.id!
this.price_list_id ??= this.price_list?.id!
}
}
export default Price

View File

@@ -7,6 +7,7 @@ import {
CreatePriceSetDTO,
DAL,
FindConfig,
InferEntityType,
InternalModuleDeclaration,
ModuleJoinerConfig,
ModulesSdkTypes,
@@ -52,6 +53,7 @@ import {
import { ServiceTypes } from "@types"
import { eventBuilders, validatePriceListDates } from "@utils"
import { joinerConfig } from "../joiner-config"
import { Collection } from "@mikro-orm/core"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
@@ -91,12 +93,24 @@ export default class PricingModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly pricingRepository_: PricingRepositoryService
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<PriceSet>
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceRule>
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<Price>
protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService<PriceList>
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceListRule>
protected readonly pricePreferenceService_: ModulesSdkTypes.IMedusaInternalService<PricePreference>
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof PriceSet>
>
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof PriceRule>
>
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof Price>
>
protected readonly priceListService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof PriceList>
>
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof PriceListRule>
>
protected readonly pricePreferenceService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof PricePreference>
>
constructor(
{
@@ -446,7 +460,7 @@ export default class PricingModuleService
(priceSet): priceSet is CreatePriceSetDTO => !priceSet.id
)
const operations: Promise<PriceSet[]>[] = []
const operations: Promise<InferEntityType<typeof PriceSet>[]>[] = []
if (forCreate.length) {
operations.push(this.createPriceSets_(forCreate, sharedContext))
@@ -512,7 +526,7 @@ export default class PricingModuleService
protected async updatePriceSets_(
data: ServiceTypes.UpdatePriceSetInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<PriceSet[]> {
): Promise<InferEntityType<typeof PriceSet>[]> {
// TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
// We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
const normalizedData = await this.normalizeUpdateData(data)
@@ -828,7 +842,7 @@ export default class PricingModuleService
!pricePreference.id
)
const operations: Promise<PricePreference[]>[] = []
const operations: Promise<InferEntityType<typeof PricePreference>[]>[] = []
if (forCreate.length) {
operations.push(this.createPricePreferences_(forCreate, sharedContext))
@@ -1249,7 +1263,7 @@ export default class PricingModuleService
protected async updatePriceListPrices_(
data: PricingTypes.UpdatePriceListPricesDTO[],
sharedContext: Context = {}
): Promise<Price[]> {
): Promise<InferEntityType<typeof Price>[]> {
const priceLists = await this.listPriceLists(
{ id: data.map((p) => p.price_list_id) },
{ relations: ["prices", "prices.price_rules"] },
@@ -1305,7 +1319,7 @@ export default class PricingModuleService
protected async addPriceListPrices_(
data: PricingTypes.AddPriceListPricesDTO[],
sharedContext: Context = {}
): Promise<Price[]> {
): Promise<InferEntityType<typeof Price>[]> {
const priceLists = await this.listPriceLists(
{ id: data.map((p) => p.price_list_id) },
{ relations: ["prices", "prices.price_rules"] },
@@ -1379,7 +1393,7 @@ export default class PricingModuleService
protected async setPriceListRules_(
data: PricingTypes.SetPriceListRulesDTO[],
sharedContext: Context = {}
): Promise<PriceList[]> {
): Promise<InferEntityType<typeof PriceList>[]> {
// TODO: re think this method
const priceLists = await this.priceListService_.list(
{ id: data.map((d) => d.price_list_id) },
@@ -1402,10 +1416,12 @@ export default class PricingModuleService
const priceListsUpsert = priceLists
.map((priceList) => {
const priceListRules =
priceList.price_list_rules as unknown as Collection<
InferEntityType<typeof PriceListRule>
>
const allRules = new Map(
priceList.price_list_rules
.toArray()
.map((r) => [r.attribute, r.value])
priceListRules.toArray().map((r) => [r.attribute, r.value])
)
const rules = rulesMap.get(priceList.id)
@@ -1441,7 +1457,7 @@ export default class PricingModuleService
protected async removePriceListRules_(
data: PricingTypes.RemovePriceListRulesDTO[],
sharedContext: Context = {}
): Promise<PriceList[]> {
): Promise<InferEntityType<typeof PriceList>[]> {
// TODO: re think this method
const priceLists = await this.priceListService_.list(
{ id: data.map((d) => d.price_list_id) },
@@ -1464,10 +1480,13 @@ export default class PricingModuleService
const priceListsUpsert = priceLists
.map((priceList) => {
const priceListRules =
priceList.price_list_rules as unknown as Collection<
InferEntityType<typeof PriceListRule>
>
const allRules = new Map(
priceList.price_list_rules
.toArray()
.map((r) => [r.attribute, r.value])
priceListRules.toArray().map((r) => [r.attribute, r.value])
)
const rules = rulesMap.get(priceList.id)
@@ -1536,8 +1555,8 @@ export default class PricingModuleService
}
const isTaxInclusive = (
priceRules: PriceRule[],
preferences: PricePreference[],
priceRules: InferEntityType<typeof PriceRule>[],
preferences: InferEntityType<typeof PricePreference>[],
currencyCode: string,
regionId?: string
) => {