Skip to content

Commit

Permalink
refactor: migrate pricing entities to DML models (#10335)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
thetutlage and adrien2p authored Dec 2, 2024
1 parent b4d6a4b commit 913cf15
Show file tree
Hide file tree
Showing 18 changed files with 386 additions and 725 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
}),
])
})
Expand Down Expand Up @@ -470,41 +470,41 @@ 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({
id: expect.any(String),
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,
}),
])
)
Expand Down Expand Up @@ -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,
}),
])
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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,
}),
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
})

Expand Down Expand Up @@ -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()
)
})

Expand Down Expand Up @@ -562,7 +568,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
currency_code: "EUR",
}),
expect.objectContaining({
rules_count: 0,
price_rules: [],
amount: 600,
currency_code: "EUR",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
Loading

0 comments on commit 913cf15

Please sign in to comment.