feat(pricing, types, utils): Exact match based on context + fallback on rule priority if not (#5214)

* initial

* initial service

* update pricing module service

* add integration test for rule-type

* update pricing-module integration tests

* update pricing service interface

* feat(pricing): PriceSets as entry point to pricing module

* chore: add price set money amount

* chore: add price set money amount

* chore: change name of test

* chore: added changeset

* chore: use filterable props from money amount in price sets

* chore: update migrations

* test update integration test

* fix weird behavior

* Update packages/pricing/integration-tests/__fixtures__/rule-type/index.ts

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* Apply suggestions from code review

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* move rule-type to common

* chore: reset migration

* chore: remove incorrect conflicts

* chore: address review

* chore: remove ghost price list

* Apply suggestions from code review

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* update id prefix

* use persist not persistAndflush

* rename key_value to rule_attribute

* more renaming

* feat(types,pricing): add price set money amount rules to pricing module

* chore: cleanup + add test cases for relationship update

* chore: revert package json

* chore: cleanup

* initial

* update pricing module service

* update pricing-module integration tests

* update pricing service interface

* chore: update migrations

* fix weird behavior

* Apply suggestions from code review

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* move rule-type to common

* chore: delete duplicate migration files

* fix(link-modules): Fix link module initialization (#4990)

**What**
Add a new configuration on the relationship to specify if the relation is consumed from an internal service (from medusa core). In that case do not check if the service is part of the loaded modules

* initial price rule

* rebase develop

* save here

* final changes to create

* update price rule integration test

* add module integraiton tests for price rules

* fix merge

* redo wierd order change

* pr cleanup

* pr cleanup

* pr cleanup

* update pr

* sort out migrations

* [wip]

* wip

* chore: temporarily emulate mikroorm internals

* currency code hard filtering

* before creating subqueries

* chore: wip

* chore: wip

* chore: add exact match multiple contexts

* chore: add one more test

* chore: add query that works with exact match

* chore: qb the thingy

* chore: add some comments

* chore: removed extra filter

* chore: added some more comments + prettify

* chore: test with carlos

* chore: add fallbacks and exact match tests

* chore: cleanup

* feat(types,pricing): add price set money amount rules to pricing module (#5065)

* initial

* initial service

* update pricing module service

* add integration test for rule-type

* update pricing-module integration tests

* update pricing service interface

* feat(pricing): PriceSets as entry point to pricing module

* chore: add price set money amount

* chore: add price set money amount

* chore: change name of test

* chore: added changeset

* chore: use filterable props from money amount in price sets

* chore: update migrations

* test update integration test

* fix weird behavior

* Update packages/pricing/integration-tests/__fixtures__/rule-type/index.ts

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* Apply suggestions from code review

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>

* move rule-type to common

* chore: reset migration

* chore: remove incorrect conflicts

* chore: address review

* chore: remove ghost price list

* Apply suggestions from code review

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* update id prefix

* use persist not persistAndflush

* rename key_value to rule_attribute

* more renaming

* feat(types,pricing): add price set money amount rules to pricing module

* chore: cleanup + add test cases for relationship update

* chore: revert package json

* chore: cleanup

---------

Co-authored-by: Philip Korsholm <philip.korsholm@hotmail.com>
Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* chore: minor cleanup

* chore: added money amount scoping

* chore: added review comments

* chore: update changset and undo test scoping

* chore: introduce group by util + no queries on empty context

* Feat/pricing module methods (#5218)

chore: add removePrices to pricing module

* Revert "Feat/pricing module methods (#5218)" (#5236)

This reverts commit 95c8aaa66423d290a35b6e736e5b187e12d44a36.

* chore: review changes

* chore: update schema

* chore: reset migration

---------

Co-authored-by: Philip Korsholm <philip.korsholm@hotmail.com>
Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
This commit is contained in:
authored and GitHub committed 2023-09-29 13:23:41 +02:00
1 parent dc298acefc
commit 1e7db5a5cb
49 files changed
+2414 -553

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
---
"@medusajs/pricing": patch
"@medusajs/types": patch
"@medusajs/utils": patch
---
feat(types,pricing,utils): Exact match based on context + fallback on rule priority if not
@@ -2,6 +2,8 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Currency } from "@models" import { Currency } from "@models"
import { defaultCurrencyData } from "./data" import { defaultCurrencyData } from "./data"
export * from "./data"
export async function createCurrencies( export async function createCurrencies(
manager: SqlEntityManager, manager: SqlEntityManager,
currencyData: any[] = defaultCurrencyData currencyData: any[] = defaultCurrencyData
@@ -2,6 +2,8 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
import { MoneyAmount } from "@models" import { MoneyAmount } from "@models"
import { defaultMoneyAmountsData } from "./data" import { defaultMoneyAmountsData } from "./data"
export * from "./data"
export async function createMoneyAmounts( export async function createMoneyAmounts(
manager: SqlEntityManager, manager: SqlEntityManager,
moneyAmountsData: any[] = defaultMoneyAmountsData moneyAmountsData: any[] = defaultMoneyAmountsData
@@ -1,24 +1,22 @@
import { CreatePriceRuleDTO } from "@medusajs/types" import { CreatePriceRuleDTO } from "@medusajs/types"
export * from "./data"
export const defaultPriceRuleData = [ export const defaultPriceRuleData = [
{ {
id: "price-rule-1", id: "price-rule-1",
price_set_id: "price-set-1", price_set_id: "price-set-1",
rule_type_id: "rule-type-1", rule_type_id: "rule-type-1",
value: "region_1", value: "USD",
price_list_id: "test", price_list_id: "test",
price_set_money_amount: { price_set_money_amount_id: "price-set-money-amount-USD",
money_amount: { amount: 100, currency_code: "EUR" },
},
}, },
{ {
id: "price-rule-2", id: "price-rule-2",
price_set_id: "price-set-2", price_set_id: "price-set-2",
rule_type_id: "rule-type-1", rule_type_id: "rule-type-2",
value: "region_2", value: "region_1",
price_list_id: "test", price_list_id: "test",
price_set_money_amount: { price_set_money_amount_id: "price-set-money-amount-EUR",
money_amount: { amount: 100, currency_code: "EUR" },
},
}, },
] as unknown as CreatePriceRuleDTO[] ] as unknown as CreatePriceRuleDTO[]
@@ -1,11 +1,11 @@
import { PriceRule, PriceSet, PriceSetMoneyAmount, RuleType } from "@models" import { PriceRule } from "@models"
import { CreatePriceRuleDTO } from "@medusajs/types" import { CreatePriceRuleDTO } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createMoneyAmounts } from "../money-amount"
import { createRuleTypes } from "../rule-type"
import { defaultPriceRuleData } from "./data" import { defaultPriceRuleData } from "./data"
export * from "./data"
export async function createPriceRules( export async function createPriceRules(
manager: SqlEntityManager, manager: SqlEntityManager,
pricesRulesData: CreatePriceRuleDTO[] = defaultPriceRuleData pricesRulesData: CreatePriceRuleDTO[] = defaultPriceRuleData
@@ -13,66 +13,17 @@ export async function createPriceRules(
const priceRules: PriceRule[] = [] const priceRules: PriceRule[] = []
for (let priceRuleData of pricesRulesData) { for (let priceRuleData of pricesRulesData) {
const priceRuleDataClone = { ...priceRuleData } const priceRuleDataClone: any = { ...priceRuleData }
if (priceRuleDataClone.price_set_id) { priceRuleDataClone.price_set = priceRuleDataClone.price_set_id
priceRuleDataClone.price_set = manager.getReference(
PriceSet,
priceRuleDataClone.price_set_id
)
}
let dbRuleType: RuleType | undefined = await manager.findOne(RuleType, { priceRuleDataClone.rule_type = priceRuleDataClone.rule_type_id
id: priceRuleDataClone.rule_type_id,
})
if (!dbRuleType) {
const [createdRuleType] = await createRuleTypes(manager, [
{
id: priceRuleDataClone.rule_type_id,
name: "rule 1",
rule_attribute: "region_id",
},
])
dbRuleType = createdRuleType
}
priceRuleDataClone.rule_type = manager.getReference(
RuleType,
dbRuleType!.id
)
const priceSetMoneyAmountId = const priceSetMoneyAmountId =
priceRuleDataClone.price_set_money_amount_id || priceRuleDataClone.price_set_money_amount_id ||
priceRuleDataClone.price_set_money_amount?.id priceRuleDataClone.price_set_money_amount?.id
let psma: PriceSetMoneyAmount = await manager.findOne(PriceSetMoneyAmount, {id: priceSetMoneyAmountId}) priceRuleDataClone.price_set_money_amount = priceSetMoneyAmountId
if (!psma) {
const [ma] = await createMoneyAmounts(manager, [
priceRuleDataClone.price_set_money_amount.money_amount ?? {
amount: 100,
currency_code: "EUR",
},
])
psma = manager.create(PriceSetMoneyAmount, {
price_set: manager.getReference(
PriceSet,
priceRuleDataClone.price_set.id
),
money_amount: ma.id,
title: "test",
})
await manager.persist(psma).flush()
}
priceRuleDataClone.price_set_money_amount = manager.getReference(
PriceSetMoneyAmount,
psma.id
)
const priceRule = manager.create(PriceRule, priceRuleDataClone) const priceRule = manager.create(PriceRule, priceRuleDataClone)
@@ -0,0 +1,20 @@
export const defaultPriceSetMoneyAmountRulesData = [
{
id: "psmar-1",
value: "EUR",
price_set_money_amount: "price-set-money-amount-USD",
rule_type: "rule-type-1",
},
{
id: "psmar-2",
value: "EU",
price_set_money_amount: "price-set-money-amount-EUR",
rule_type: "rule-type-2",
},
{
id: "psmar-3",
value: "CAD",
price_set_money_amount: "price-set-money-amount-CAD",
rule_type: "rule-type-2",
},
]
@@ -0,0 +1,22 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceSetMoneyAmountRules } from "@models"
import { defaultPriceSetMoneyAmountRulesData } from "./data"
export * from "./data"
export async function createPriceSetMoneyAmountRules(
manager: SqlEntityManager,
psmarData: any[] = defaultPriceSetMoneyAmountRulesData
): Promise<PriceSetMoneyAmountRules[]> {
const priceSetMoneyAmountRules: PriceSetMoneyAmountRules[] = []
for (let data of psmarData) {
const psmar = manager.create(PriceSetMoneyAmountRules, data)
priceSetMoneyAmountRules.push(psmar)
}
await manager.persistAndFlush(priceSetMoneyAmountRules)
return priceSetMoneyAmountRules
}
@@ -0,0 +1,23 @@
export const defaultPriceSetMoneyAmountsData = [
{
id: "price-set-money-amount-USD",
title: "price set money amount USD",
price_set: "price-set-1",
money_amount: "money-amount-USD",
number_rules: 1,
},
{
id: "price-set-money-amount-EUR",
title: "price set money amount EUR",
price_set: "price-set-2",
money_amount: "money-amount-EUR",
number_rules: 1,
},
{
id: "price-set-money-amount-CAD",
title: "price set money amount CAD",
price_set: "price-set-3",
money_amount: "money-amount-CAD",
number_rules: 1,
},
]
@@ -0,0 +1,22 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceSetMoneyAmount } from "@models"
import { defaultPriceSetMoneyAmountsData } from "./data"
export * from "./data"
export async function createPriceSetMoneyAmounts(
manager: SqlEntityManager,
psmaData: any[] = defaultPriceSetMoneyAmountsData
): Promise<PriceSetMoneyAmount[]> {
const priceSetMoneyAmount: PriceSetMoneyAmount[] = []
for (let data of psmaData) {
const psmar = manager.create(PriceSetMoneyAmount, data)
priceSetMoneyAmount.push(psmar)
}
await manager.persistAndFlush(priceSetMoneyAmount)
return priceSetMoneyAmount
}
@@ -3,6 +3,8 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceSet, PriceSetMoneyAmount } from "@models" import { PriceSet, PriceSetMoneyAmount } from "@models"
import { defaultPriceSetsData } from "./data" import { defaultPriceSetsData } from "./data"
export * from "./data"
export async function createPriceSets( export async function createPriceSets(
manager: SqlEntityManager, manager: SqlEntityManager,
priceSetsData: CreatePriceSetDTO[] = defaultPriceSetsData priceSetsData: CreatePriceSetDTO[] = defaultPriceSetsData
@@ -2,11 +2,11 @@ export const defaultRuleTypesData = [
{ {
id: "rule-type-1", id: "rule-type-1",
name: "rule 1", name: "rule 1",
rule_attribute: "region_id", rule_attribute: "currency_code",
}, },
{ {
id: "rule-type-2", id: "rule-type-2",
name: "rule 2", name: "rule 2",
rule_attribute: "currency_code", rule_attribute: "region_id",
}, },
] ]
@@ -1,7 +1,9 @@
import { RuleType } from "@models"
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { RuleType } from "@models"
import { defaultRuleTypesData } from "./data" import { defaultRuleTypesData } from "./data"
export * from "./data"
export async function createRuleTypes( export async function createRuleTypes(
manager: SqlEntityManager, manager: SqlEntityManager,
ruletypesData: any[] = defaultRuleTypesData ruletypesData: any[] = defaultRuleTypesData
@@ -11,9 +13,10 @@ export async function createRuleTypes(
for (let ruleTypeData of ruletypesData) { for (let ruleTypeData of ruletypesData) {
const ruleType = manager.create(RuleType, ruleTypeData) const ruleType = manager.create(RuleType, ruleTypeData)
await manager.persistAndFlush(ruleType)
ruleTypes.push(ruleType) ruleTypes.push(ruleType)
} }
await manager.persistAndFlush(ruleTypes)
return ruleTypes return ruleTypes
} }
@@ -1,15 +1,16 @@
import { PriceSet, PriceSetMoneyAmount } from "@models" import { PriceSetMoneyAmount } from "@models"
import { CreatePriceRuleDTO } from "@medusajs/types" import { CreatePriceRuleDTO } from "@medusajs/types"
import { MikroOrmWrapper } from "../../../utils" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceRuleRepository } from "@repositories" import { PriceRuleRepository } from "@repositories"
import { PriceRuleService } from "@services" import { PriceRuleService } from "@services"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createCurrencies } from "../../../__fixtures__/currency" import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount" import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceRules } from "../../../__fixtures__/price-rule" import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set" import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createRuleTypes } from "../../../__fixtures__/rule-type" import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000) jest.setTimeout(30000)
@@ -32,9 +33,10 @@ describe("PriceRule Service", () => {
}) })
await createCurrencies(testManager) await createCurrencies(testManager)
await createMoneyAmounts(testManager)
await createRuleTypes(testManager) await createRuleTypes(testManager)
await createPriceSets(testManager) await createPriceSets(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceRules(testManager) await createPriceRules(testManager)
}) })
@@ -291,11 +293,14 @@ describe("PriceRule Service", () => {
}, },
]) ])
const psma: PriceSetMoneyAmount = testManager.create(PriceSetMoneyAmount, { const psma: PriceSetMoneyAmount = testManager.create(
price_set: testManager.getReference(PriceSet, "price-set-1"), PriceSetMoneyAmount,
money_amount: ma.id, {
title: "test", price_set: "price-set-1",
}) money_amount: ma.id,
title: "test",
}
)
await testManager.persist(psma).flush() await testManager.persist(psma).flush()
@@ -0,0 +1,300 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceSetMoneyAmountRulesRepository } from "@repositories"
import { PriceSetMoneyAmountRulesService } from "@services"
import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000)
describe("PriceSetMoneyAmountRules Service", () => {
let service: PriceSetMoneyAmountRulesService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
const priceSetMoneyAmountRulesRepository =
new PriceSetMoneyAmountRulesRepository({
manager: repositoryManager,
})
service = new PriceSetMoneyAmountRulesService({
priceSetMoneyAmountRulesRepository,
})
testManager = await MikroOrmWrapper.forkManager()
await createCurrencies(testManager)
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceSetMoneyAmountRules(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("should list psmar records", async () => {
const priceSetMoneyAmountRulesResult = await service.list()
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
it("should list psmar record by id", async () => {
const priceSetMoneyAmountRulesResult = await service.list({
id: ["psmar-1"],
})
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
})
describe("listAndCount", () => {
it("should return psmar records and count", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCount()
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
it("should return psmar records and count when filtered", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCount({
id: ["psmar-1"],
})
expect(count).toEqual(1)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
it("should return psmar and count when using skip and take", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCount({}, { skip: 1, take: 1 })
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-2",
}),
])
})
it("should return requested fields", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCount(
{},
{
take: 1,
select: ["value"],
}
)
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "psmar-1",
value: "EUR",
},
])
})
})
describe("retrieve", () => {
it("should return priceSetMoneyAmountRules for the given id", async () => {
const priceSetMoneyAmountRules = await service.retrieve("psmar-1")
expect(priceSetMoneyAmountRules).toEqual(
expect.objectContaining({
id: "psmar-1",
})
)
})
it("should throw an error when priceSetMoneyAmountRules with id does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceSetMoneyAmountRules with id: does-not-exist was not found"
)
})
it("should throw an error when an id is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual(
'"priceSetMoneyAmountRulesId" must be defined'
)
})
it("should return priceSetMoneyAmountRules based on config select param", async () => {
const priceSetMoneyAmountRulesResult = await service.retrieve("psmar-1", {
select: ["value"],
})
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
expect(serialized).toEqual({
value: "EUR",
id: "psmar-1",
})
})
})
describe("delete", () => {
const id = "psmar-1"
it("should delete the priceSetMoneyAmountRules given an id successfully", async () => {
await service.delete([id])
const priceSetMoneyAmountRules = await service.list({
id: [id],
})
expect(priceSetMoneyAmountRules).toHaveLength(0)
})
})
describe("update", () => {
const id = "psmar-1"
it("should update the value of the priceSetMoneyAmountRules successfully", async () => {
await service.update([
{
id,
value: "New value",
price_set_money_amount: "price-set-money-amount-CAD",
rule_type: "rule-type-2",
},
])
const psmar = await service.retrieve(id, {
relations: ["price_set_money_amount", "rule_type"],
})
expect(psmar).toEqual(
expect.objectContaining({
id,
value: "New value",
price_set_money_amount: expect.objectContaining({
id: "price-set-money-amount-CAD",
}),
rule_type: expect.objectContaining({
id: "rule-type-2",
}),
})
)
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.update([
{
id: "does-not-exist",
value: "random value",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceSetMoneyAmountRules with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should create a priceSetMoneyAmountRules successfully", async () => {
const created = await service.create([
{
price_set_money_amount: "price-set-money-amount-EUR",
rule_type: "rule-type-2",
value: "New priceSetMoneyAmountRule",
},
])
const [priceSetMoneyAmountRules] = await service.list(
{
id: [created[0]?.id],
},
{
relations: ["price_set_money_amount", "rule_type"],
}
)
expect(priceSetMoneyAmountRules).toEqual(
expect.objectContaining({
id: created[0]?.id,
value: "New priceSetMoneyAmountRule",
price_set_money_amount: expect.objectContaining({
id: "price-set-money-amount-EUR",
}),
rule_type: expect.objectContaining({
id: "rule-type-2",
}),
})
)
})
})
})
@@ -0,0 +1,693 @@
import {
CreatePriceRuleDTO,
CreatePriceSetDTO,
IPricingModuleService,
} from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceSet } from "@models"
import { initialize } from "../../../../src"
import {
createCurrencies,
defaultCurrencyData,
} from "../../../__fixtures__/currency"
import {
createMoneyAmounts,
defaultMoneyAmountsData,
} from "../../../__fixtures__/money-amount"
import {
createPriceRules,
defaultPriceRuleData,
} from "../../../__fixtures__/price-rule"
import {
createPriceSets,
defaultPriceSetsData,
} from "../../../__fixtures__/price-set"
import {
createPriceSetMoneyAmounts,
defaultPriceSetMoneyAmountsData,
} from "../../../__fixtures__/price-set-money-amount"
import {
createRuleTypes,
defaultRuleTypesData,
} from "../../../__fixtures__/rule-type"
import { DB_URL, MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000)
async function seedData({
moneyAmountsData = defaultMoneyAmountsData,
priceSetsData = defaultPriceSetsData,
currencyData = defaultCurrencyData,
priceRuleData = defaultPriceRuleData,
priceSetMoneyAmountsData = defaultPriceSetMoneyAmountsData,
ruleTypesData = defaultRuleTypesData,
} = {}) {
const testManager = MikroOrmWrapper.forkManager()
await createCurrencies(testManager, currencyData)
await createMoneyAmounts(testManager, moneyAmountsData)
await createPriceSets(testManager, priceSetsData)
await createPriceSetMoneyAmounts(testManager, priceSetMoneyAmountsData)
await createRuleTypes(testManager, ruleTypesData)
await createPriceRules(testManager, priceRuleData)
}
describe("PricingModule Service - Calculate Price", () => {
let service: IPricingModuleService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let data!: PriceSet[]
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = MikroOrmWrapper.forkManager()
testManager = MikroOrmWrapper.forkManager()
service = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRICING_DB_SCHEMA,
},
})
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("calculatePrices", () => {
beforeEach(async () => {
const currencyData = [
{
symbol: "zł",
name: "Polish Zloty",
symbol_native: "zł",
code: "PLN",
},
{
symbol: "€",
name: "Euro",
symbol_native: "€",
code: "EUR",
},
]
const moneyAmountsData = [
{
id: "money-amount-currency_code-EUR",
currency_code: "EUR",
amount: 500,
min_quantity: 1,
max_quantity: 10,
},
{
id: "money-amount-currency_code-PLN",
currency_code: "PLN",
amount: 400,
min_quantity: 1,
max_quantity: 5,
},
{
id: "money-amount-region_id-PLN",
currency_code: "PLN",
amount: 300,
min_quantity: 1,
max_quantity: 4,
},
{
id: "money-amount-region_id-PL-EUR",
currency_code: "EUR",
amount: 200,
min_quantity: 1,
max_quantity: 3,
},
{
id: "money-amount-region_id-PL-EUR-4-qty",
currency_code: "EUR",
amount: 50,
min_quantity: 4,
max_quantity: 10,
},
{
id: "money-amount-region_id-PL-EUR-customer-group",
currency_code: "EUR",
amount: 100,
min_quantity: 1,
max_quantity: 3,
},
]
const priceSetsData = [
{
id: "price-set-EUR",
},
{
id: "price-set-PLN",
},
] as unknown as CreatePriceSetDTO[]
const priceSetMoneyAmountsData = [
{
id: "psma-currency_code-EUR",
title: "psma EUR - currency_code",
price_set: "price-set-EUR",
money_amount: "money-amount-currency_code-EUR",
number_rules: 1,
},
{
id: "psma-currency_code-PLN",
title: "psma PLN - currency_code",
price_set: "price-set-PLN",
money_amount: "money-amount-currency_code-PLN",
number_rules: 1,
},
{
id: "psma-region_id-PLN",
title: "psma PLN - region_id",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id-PLN",
number_rules: 1,
},
{
id: "psma-region_id_currency_code-PL-EUR",
title: "psma PLN - region_id PL with EUR currency",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id-PL-EUR",
number_rules: 2,
},
{
id: "psma-region_id_currency_code-PL-EUR-4-qty",
title: "psma PLN - region_id PL with EUR currency for quantity 4",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id-PL-EUR-4-qty",
number_rules: 2,
},
{
id: "psma-region_id_currency_code-PL-EUR-customer-group",
title: "psma PLN - region_id PL with EUR currency for customer group",
price_set: "price-set-PLN",
money_amount: "money-amount-region_id-PL-EUR-customer-group",
number_rules: 3,
},
]
const ruleTypesData = [
{
id: "rule-type-currency_code",
name: "rule type currency code",
rule_attribute: "currency_code",
default_priority: 2,
},
{
id: "rule-type-region_id",
name: "rule type region id",
rule_attribute: "region_id",
default_priority: 1,
},
{
id: "rule-type-customer_group_id",
name: "rule type customer group id",
rule_attribute: "customer_group_id",
default_priority: 3,
},
]
const priceRuleData = [
{
id: "price-rule-currency_code-EUR",
price_set_id: "price-set-EUR",
rule_type_id: "rule-type-currency_code",
value: "EUR",
price_list_id: "test",
price_set_money_amount_id: "psma-currency_code-EUR",
},
{
id: "price-rule-currency_code-PLN",
price_set_id: "price-set-PLN",
rule_type_id: "rule-type-currency_code",
value: "PLN",
price_list_id: "test",
price_set_money_amount_id: "psma-currency_code-PLN",
},
{
id: "price-rule-region_id-PLN",
price_set_id: "price-set-PLN",
rule_type_id: "rule-type-region_id",
value: "PL",
price_list_id: "test",
price_set_money_amount_id: "psma-region_id-PLN",
},
{
id: "price-rule-region_id-currency_code-PL",
price_set_id: "price-set-PLN",
rule_type_id: "rule-type-region_id",
value: "PL",
price_list_id: "test",
price_set_money_amount_id: "psma-region_id_currency_code-PL-EUR",
},
{
id: "price-rule-region_id-currency_code-PLN",
price_set_id: "price-set-PLN",
rule_type_id: "rule-type-currency_code",
value: "EUR",
price_list_id: "test",
price_set_money_amount_id: "psma-region_id_currency_code-PL-EUR",
},
{
id: "price-rule-region_id-currency_code-PL-4-qty",
price_set_id: "price-set-PLN",
rule_type_id: "rule-type-region_id",
value: "PL",
price_list_id: "test",
price_set_money_amount_id:
"psma-region_id_currency_code-PL-EUR-4-qty",
},
{
id: "price-rule-region_id-currency_code-PLN-4-qty",
price_set_id: "price-set-PLN",
rule_type_id: "rule-type-currency_code",
value: "EUR",
price_list_id: "test",
price_set_money_amount_id:
"psma-region_id_currency_code-PL-EUR-4-qty",
},
{
id: "price-rule-region_id-currency_customer_group_code-PL",
price_set_id: "price-set-PLN",
rule_type_id: "rule-type-region_id",
value: "PL",
price_list_id: "test",
price_set_money_amount_id:
"psma-region_id_currency_code-PL-EUR-customer-group",
},
{
id: "price-rule-region_id-currency_customer_group_code-PLN",
price_set_id: "price-set-PLN",
rule_type_id: "rule-type-currency_code",
value: "EUR",
price_list_id: "test",
price_set_money_amount_id:
"psma-region_id_currency_code-PL-EUR-customer-group",
},
{
id: "price-rule-region_id-currency_customer_group_code-test_customer_group",
price_set_id: "price-set-PLN",
rule_type_id: "rule-type-customer_group_id",
value: "test-customer-group",
price_list_id: "test",
price_set_money_amount_id:
"psma-region_id_currency_code-PL-EUR-customer-group",
},
] as unknown as CreatePriceRuleDTO[]
await seedData({
currencyData,
moneyAmountsData,
priceSetsData,
priceSetMoneyAmountsData,
priceRuleData,
ruleTypesData,
})
})
it("should return null values when no context is set", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
{
id: "price-set-PLN",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
])
})
it("should return filled prices when 1 context is present and price is setup for EUR", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: { currency_code: "EUR" },
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: "500",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "10",
},
{
id: "price-set-PLN",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
])
})
it("should return filled prices when 1 context is present and price is setup for PLN region_id", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: { region_id: "PL" },
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
{
id: "price-set-PLN",
amount: "300",
currency_code: "PLN",
min_quantity: "1",
max_quantity: "4",
},
])
})
it("should return filled prices when 1 context is present and price is setup for PLN currency_code", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: { currency_code: "PLN" },
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
{
id: "price-set-PLN",
amount: "400",
currency_code: "PLN",
min_quantity: "1",
max_quantity: "5",
},
])
})
it("should return null prices when 1 context is present and price is NOT setup", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: { does_not_exist: "EUR" },
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
{
id: "price-set-PLN",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
])
})
it("should return filled prices when 2 contexts are present and price is setup", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: { currency_code: "EUR", region_id: "PL" },
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: "500",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "10",
},
{
id: "price-set-PLN",
amount: "200",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "3",
},
])
})
it("should return filled prices when 2 contexts are present and price is setup along with declaring quantity", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-PLN"] },
{
context: { currency_code: "EUR", region_id: "PL", quantity: 5 },
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-PLN",
amount: "50",
currency_code: "EUR",
min_quantity: "4",
max_quantity: "10",
},
])
})
it("should return filled prices when 3 contexts are present and price is partially setup for 2", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: {
currency_code: "EUR",
region_id: "PL",
customer_group_id: "test",
},
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: "500",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "10",
},
{
id: "price-set-PLN",
amount: "200",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "3",
},
])
})
it("should return filled prices when 3 contexts are present and price is setup for 3", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: {
currency_code: "EUR",
region_id: "PL",
customer_group_id: "test-customer-group",
},
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: "500",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "10",
},
// Currency Code + Region value + customer group id
{
id: "price-set-PLN",
amount: "100",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "3",
},
])
})
it("should return filled prices when 3 contexts are present and price is setup for 3, but value is incorrect for 1. It falls back to 2 rule context", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: {
currency_code: "EUR",
region_id: "PL",
customer_group_id: "does-not-exist",
},
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: "500",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "10",
},
// Currency Code + Region value
{
id: "price-set-PLN",
amount: "200",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "3",
},
])
})
it("should return filled prices when 3 contexts are present and price is setup for 3, but value is incorrect for 2. It falls back to 1 rule context when 1 rule is not setup", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: {
currency_code: "does-not-exist",
region_id: "PL",
customer_group_id: "does-not-exist",
},
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
// PLN price set is not setup for EUR currency_code so it will default to a null set
{
id: "price-set-PLN",
amount: "300",
currency_code: "PLN",
min_quantity: "1",
max_quantity: "4",
},
])
})
it("should return filled prices when 3 contexts are present and price is setup for 3, but value is incorrect for 2. It falls back to 1 rule context when 1 rule is setup", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: {
currency_code: "EUR",
region_id: "does-not-exist",
customer_group_id: "does-not-exist",
},
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: "500",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "10",
},
// PLN price set is not setup for EUR currency_code so it will default to a null set
{
id: "price-set-PLN",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
])
})
it("should return null prices when 2 context is present and prices are NOT setup", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: { does_not_exist: "EUR", does_not_exist_2: "Berlin" },
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
{
id: "price-set-PLN",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
])
})
it("should return filled prices when 2 context is present and prices are setup, but only for one of the contexts", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-EUR", "price-set-PLN"] },
{
context: { currency_code: "EUR", does_not_exist: "Berlin" },
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-EUR",
amount: "500",
currency_code: "EUR",
min_quantity: "1",
max_quantity: "10",
},
{
id: "price-set-PLN",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
])
})
})
})
@@ -1,21 +1,20 @@
import { CreatePriceRuleDTO, IPricingModuleService } from "@medusajs/types" import { CreatePriceRuleDTO, IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceSet } from "@models"
import { PriceSetMoneyAmount, initialize } from "../../../../src" import { PriceSetMoneyAmount, initialize } from "../../../../src"
import { createCurrencies } from "../../../__fixtures__/currency" import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount" import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { DB_URL, MikroOrmWrapper } from "../../../utils"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { createPriceRules } from "../../../__fixtures__/price-rule" import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { DB_URL, MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000) jest.setTimeout(30000)
describe("PricingModule Service - PriceRule", () => { describe("PricingModule Service - PriceRule", () => {
let service: IPricingModuleService let service: IPricingModuleService
let testManager: SqlEntityManager let testManager: SqlEntityManager
beforeEach(async () => { beforeEach(async () => {
await MikroOrmWrapper.setupDatabase() await MikroOrmWrapper.setupDatabase()
@@ -29,9 +28,10 @@ describe("PricingModule Service - PriceRule", () => {
}) })
await createCurrencies(testManager) await createCurrencies(testManager)
await createMoneyAmounts(testManager)
await createRuleTypes(testManager) await createRuleTypes(testManager)
await createPriceSets(testManager) await createPriceSets(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceRules(testManager) await createPriceRules(testManager)
}) })
@@ -288,11 +288,14 @@ describe("PricingModule Service - PriceRule", () => {
}, },
]) ])
const psma: PriceSetMoneyAmount = testManager.create(PriceSetMoneyAmount, { const psma: PriceSetMoneyAmount = testManager.create(
price_set: testManager.getReference(PriceSet, "price-set-1"), PriceSetMoneyAmount,
money_amount: ma.id, {
title: "test", price_set: "price-set-1",
}) money_amount: ma.id,
title: "test",
}
)
await testManager.persist(psma).flush() await testManager.persist(psma).flush()
@@ -0,0 +1,291 @@
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { initialize } from "../../../../src"
import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
import { createPriceSetMoneyAmountRules } from "../../../__fixtures__/price-set-money-amount-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { DB_URL, MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000)
describe("PricingModule Service - PriceSetMoneyAmountRules", () => {
let service: IPricingModuleService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
service = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PRICING_DB_SCHEMA,
},
})
testManager = await MikroOrmWrapper.forkManager()
await createCurrencies(testManager)
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPriceSetMoneyAmounts(testManager)
await createPriceSetMoneyAmountRules(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("listPriceSetMoneyAmountRules", () => {
it("should list psmar records", async () => {
const priceSetMoneyAmountRulesResult =
await service.listPriceSetMoneyAmountRules()
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
it("should list psmar record by id", async () => {
const priceSetMoneyAmountRulesResult =
await service.listPriceSetMoneyAmountRules({
id: ["psmar-1"],
})
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
})
describe("listAndCount", () => {
it("should return psmar records and count", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules()
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
expect.objectContaining({
id: "psmar-2",
}),
expect.objectContaining({
id: "psmar-3",
}),
])
})
it("should return psmar records and count when filtered", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules({
id: ["psmar-1"],
})
expect(count).toEqual(1)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-1",
}),
])
})
it("should return psmar and count when using skip and take", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(3)
expect(priceSetMoneyAmountRulesResult).toEqual([
expect.objectContaining({
id: "psmar-2",
}),
])
})
it("should return requested fields", async () => {
const [priceSetMoneyAmountRulesResult, count] =
await service.listAndCountPriceSetMoneyAmountRules(
{},
{
take: 1,
select: ["value"],
}
)
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "psmar-1",
value: "EUR",
},
])
})
})
describe("retrievePriceSetMoneyAmountRules", () => {
it("should return priceSetMoneyAmountRules for the given id", async () => {
const priceSetMoneyAmountRules =
await service.retrievePriceSetMoneyAmountRules("psmar-1")
expect(priceSetMoneyAmountRules).toEqual(
expect.objectContaining({
id: "psmar-1",
})
)
})
it("should throw an error when priceSetMoneyAmountRules with id does not exist", async () => {
let error
try {
await service.retrievePriceSetMoneyAmountRules("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceSetMoneyAmountRules with id: does-not-exist was not found"
)
})
it("should throw an error when an id is not provided", async () => {
let error
try {
await service.retrievePriceSetMoneyAmountRules(
undefined as unknown as string
)
} catch (e) {
error = e
}
expect(error.message).toEqual(
'"priceSetMoneyAmountRulesId" must be defined'
)
})
it("should return priceSetMoneyAmountRules based on config select param", async () => {
const priceSetMoneyAmountRulesResult =
await service.retrievePriceSetMoneyAmountRules("psmar-1", {
select: ["value"],
})
const serialized = JSON.parse(
JSON.stringify(priceSetMoneyAmountRulesResult)
)
expect(serialized).toEqual({
value: "EUR",
id: "psmar-1",
})
})
})
describe("deletePriceSetMoneyAmountRules", () => {
const id = "psmar-1"
it("should delete the priceSetMoneyAmountRuless given an id successfully", async () => {
await service.deletePriceSetMoneyAmountRules([id])
const currencies = await service.listPriceSetMoneyAmountRules({
id: [id],
})
expect(currencies).toHaveLength(0)
})
})
describe("updatePriceSetMoneyAmountRules", () => {
const id = "psmar-1"
it("should update the value of the priceSetMoneyAmountRules successfully", async () => {
await service.updatePriceSetMoneyAmountRules([
{
id,
value: "New value",
},
])
const psmar = await service.retrievePriceSetMoneyAmountRules(id)
expect(psmar.value).toEqual("New value")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceSetMoneyAmountRules([
{
id: "does-not-exist",
value: "random value",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceSetMoneyAmountRules with id "does-not-exist" not found'
)
})
})
describe("createPriceSetMoneyAmountRules", () => {
it("should create a priceSetMoneyAmountRules successfully", async () => {
const created = await service.createPriceSetMoneyAmountRules([
{
price_set_money_amount: "price-set-money-amount-EUR",
rule_type: "rule-type-2",
value: "New priceSetMoneyAmountRule",
},
])
const [priceSetMoneyAmountRules] =
await service.listPriceSetMoneyAmountRules(
{
id: [created[0]?.id],
},
{
relations: ["price_set_money_amount", "rule_type"],
}
)
expect(priceSetMoneyAmountRules).toEqual(
expect.objectContaining({
id: created[0]?.id,
value: "New priceSetMoneyAmountRule",
price_set_money_amount: expect.objectContaining({
id: "price-set-money-amount-EUR",
}),
rule_type: expect.objectContaining({
id: "rule-type-2",
}),
})
)
})
})
})
@@ -4,144 +4,62 @@ import { PriceSet } from "@models"
import { initialize } from "../../../../src" import { initialize } from "../../../../src"
import { createCurrencies } from "../../../__fixtures__/currency" import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount" import {
createMoneyAmounts,
defaultMoneyAmountsData,
} from "../../../__fixtures__/money-amount"
import {
createPriceRules,
defaultPriceRuleData,
} from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set" import { createPriceSets } from "../../../__fixtures__/price-set"
import {
createPriceSetMoneyAmounts,
defaultPriceSetMoneyAmountsData,
} from "../../../__fixtures__/price-set-money-amount"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { DB_URL, MikroOrmWrapper } from "../../../utils" import { DB_URL, MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000) jest.setTimeout(30000)
async function seedData({
moneyAmountsData = defaultMoneyAmountsData,
priceRuleData = defaultPriceRuleData,
priceSetMoneyAmountsData = defaultPriceSetMoneyAmountsData,
} = {}) {
const testManager = MikroOrmWrapper.forkManager()
await createCurrencies(testManager)
await createMoneyAmounts(testManager, moneyAmountsData)
await createPriceSets(testManager)
await createPriceSetMoneyAmounts(testManager, priceSetMoneyAmountsData)
await createRuleTypes(testManager)
await createPriceRules(testManager, priceRuleData)
}
describe("PricingModule Service - PriceSet", () => { describe("PricingModule Service - PriceSet", () => {
let service: IPricingModuleService let service: IPricingModuleService
let testManager: SqlEntityManager let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager let repositoryManager: SqlEntityManager
let data!: PriceSet[] let data!: PriceSet[]
const moneyAmountsInputData = [
{
id: "money-amount-USD",
currency_code: "USD",
amount: 500,
min_quantity: 1,
max_quantity: 10,
},
{
id: "money-amount-EUR",
currency_code: "EUR",
amount: 500,
min_quantity: 1,
max_quantity: 10,
},
]
const priceSetInputData = [
{
id: "price-set-1",
money_amounts: [{ id: "money-amount-USD" }],
},
{
id: "price-set-2",
money_amounts: [{ id: "money-amount-EUR" }],
},
{
id: "price-set-3",
money_amounts: [],
},
]
beforeEach(async () => { beforeEach(async () => {
await MikroOrmWrapper.setupDatabase() await MikroOrmWrapper.setupDatabase()
repositoryManager = MikroOrmWrapper.forkManager() repositoryManager = MikroOrmWrapper.forkManager()
service = await initialize({ service = await initialize({
database: { database: {
clientUrl: DB_URL, clientUrl: DB_URL,
schema: process.env.MEDUSA_PRICING_DB_SCHEMA, schema: process.env.MEDUSA_PRICING_DB_SCHEMA,
}, },
}) })
testManager = MikroOrmWrapper.forkManager()
await createCurrencies(testManager)
await createMoneyAmounts(testManager, moneyAmountsInputData)
data = await createPriceSets(testManager, priceSetInputData)
}) })
beforeEach(async () => await seedData())
afterEach(async () => { afterEach(async () => {
await MikroOrmWrapper.clearDatabase() await MikroOrmWrapper.clearDatabase()
}) })
describe("calculatePrices", () => {
it("retrieves the calculated prices when no context is set", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-1", "price-set-2"] },
{}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-1",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
{
id: "price-set-2",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
])
})
it("retrieves the calculated prices when a context is set", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-1", "price-set-2"] },
{
context: {
currency_code: "USD",
},
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-1",
amount: "500",
currency_code: "USD",
min_quantity: "1",
max_quantity: "10",
},
{
id: "price-set-2",
amount: null,
currency_code: null,
min_quantity: null,
max_quantity: null,
},
])
})
it("retrieves the calculated prices only when id exists in the database", async () => {
const priceSetsResult = await service.calculatePrices(
{ id: ["price-set-doesnotexist", "price-set-1"] },
{
context: { currency_code: "USD" },
}
)
expect(priceSetsResult).toEqual([
{
id: "price-set-1",
amount: "500",
currency_code: "USD",
min_quantity: "1",
max_quantity: "10",
},
])
})
})
describe("list", () => { describe("list", () => {
it("list priceSets", async () => { it("list priceSets", async () => {
const priceSetsResult = await service.list() const priceSetsResult = await service.list()
@@ -1,6 +1,3 @@
import { DB_URL, MikroOrmWrapper } from "../../../utils"
import { Currency } from "@models"
import { IPricingModuleService } from "@medusajs/types" import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
@@ -3,8 +3,8 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
import { RuleTypeRepository } from "@repositories" import { RuleTypeRepository } from "@repositories"
import { RuleTypeService } from "@services" import { RuleTypeService } from "@services"
import { MikroOrmWrapper } from "../../../utils"
import { createRuleTypes } from "../../../__fixtures__/rule-type" import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000) jest.setTimeout(30000)
@@ -13,7 +13,6 @@ describe("RuleType Service", () => {
let testManager: SqlEntityManager let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager let repositoryManager: SqlEntityManager
beforeEach(async () => { beforeEach(async () => {
await MikroOrmWrapper.setupDatabase() await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager() repositoryManager = await MikroOrmWrapper.forkManager()
@@ -131,9 +130,8 @@ describe("RuleType Service", () => {
}) })
describe("retrieve", () => { describe("retrieve", () => {
it("should return ruleType for the given id", async () => { it("should return ruleType for the given id", async () => {
const ruleType = await service.retrieve('rule-type-1') const ruleType = await service.retrieve("rule-type-1")
expect(ruleType).toEqual( expect(ruleType).toEqual(
expect.objectContaining({ expect.objectContaining({
@@ -170,15 +168,15 @@ describe("RuleType Service", () => {
}) })
it("should return ruleType based on config select param", async () => { it("should return ruleType based on config select param", async () => {
const ruleTypeResult = await service.retrieve('rule-type-1', { const ruleTypeResult = await service.retrieve("rule-type-1", {
select: ["name"], select: ["name"],
}) })
const serialized = JSON.parse(JSON.stringify(ruleTypeResult)) const serialized = JSON.parse(JSON.stringify(ruleTypeResult))
expect(serialized).toEqual({ expect(serialized).toEqual({
name: 'rule 1', name: "rule 1",
id: 'rule-type-1' id: "rule-type-1",
}) })
}) })
}) })
@@ -189,11 +187,11 @@ describe("RuleType Service", () => {
it("should delete the ruleTypes given an id successfully", async () => { it("should delete the ruleTypes given an id successfully", async () => {
await service.delete([id]) await service.delete([id])
const currencies = await service.list({ const ruleTypes = await service.list({
id: [id], id: [id],
}) })
expect(currencies).toHaveLength(0) expect(ruleTypes).toHaveLength(0)
}) })
}) })
@@ -238,7 +236,7 @@ describe("RuleType Service", () => {
await service.create([ await service.create([
{ {
name: "Test Rule", name: "Test Rule",
rule_attribute: 'region_id', rule_attribute: "region_id",
}, },
]) ])
@@ -249,7 +247,7 @@ describe("RuleType Service", () => {
expect(ruleType).toEqual( expect(ruleType).toEqual(
expect.objectContaining({ expect.objectContaining({
name: "Test Rule", name: "Test Rule",
rule_attribute: 'region_id', rule_attribute: "region_id",
}) })
) )
}) })
+7 -1
View File
@@ -3,8 +3,8 @@ import * as defaultServices from "@services"
import { LoaderOptions } from "@medusajs/modules-sdk" import { LoaderOptions } from "@medusajs/modules-sdk"
import { ModulesSdkTypes } from "@medusajs/types" import { ModulesSdkTypes } from "@medusajs/types"
import { asClass } from "awilix"
import { loadCustomRepositories } from "@medusajs/utils" import { loadCustomRepositories } from "@medusajs/utils"
import { asClass } from "awilix"
export default async ({ export default async ({
container, container,
@@ -22,6 +22,9 @@ export default async ({
moneyAmountService: asClass(defaultServices.MoneyAmountService).singleton(), moneyAmountService: asClass(defaultServices.MoneyAmountService).singleton(),
priceSetService: asClass(defaultServices.PriceSetService).singleton(), priceSetService: asClass(defaultServices.PriceSetService).singleton(),
ruleTypeService: asClass(defaultServices.RuleTypeService).singleton(), ruleTypeService: asClass(defaultServices.RuleTypeService).singleton(),
priceSetMoneyAmountRulesService: asClass(
defaultServices.PriceSetMoneyAmountRulesService
).singleton(),
priceRuleService: asClass(defaultServices.PriceRuleService).singleton(), priceRuleService: asClass(defaultServices.PriceRuleService).singleton(),
}) })
@@ -51,6 +54,9 @@ function loadDefaultRepositories({ container }) {
ruleTypeRepository: asClass( ruleTypeRepository: asClass(
defaultRepositories.RuleTypeRepository defaultRepositories.RuleTypeRepository
).singleton(), ).singleton(),
priceSetMoneyAmountRulesRepository: asClass(
defaultRepositories.PriceSetMoneyAmountRulesRepository
).singleton(),
priceRuleRepository: asClass( priceRuleRepository: asClass(
defaultRepositories.PriceRuleRepository defaultRepositories.PriceRuleRepository
).singleton(), ).singleton(),
@@ -1,150 +0,0 @@
{
"namespaces": [
"public"
],
"name": "public",
"tables": [
{
"columns": {
"code": {
"name": "code",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"symbol": {
"name": "symbol",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"symbol_native": {
"name": "symbol_native",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
}
},
"name": "currency",
"schema": "public",
"indexes": [
{
"keyName": "currency_pkey",
"columnNames": [
"code"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {}
},
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"currency_code": {
"name": "currency_code",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"amount": {
"name": "amount",
"type": "numeric",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "decimal"
},
"min_quantity": {
"name": "min_quantity",
"type": "numeric",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "decimal"
},
"max_quantity": {
"name": "max_quantity",
"type": "numeric",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "decimal"
}
},
"name": "money_amount",
"schema": "public",
"indexes": [
{
"columnNames": [
"currency_code"
],
"composite": false,
"keyName": "IDX_money_amount_currency_code",
"primary": false,
"unique": false
},
{
"keyName": "money_amount_pkey",
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"money_amount_currency_code_foreign": {
"constraintName": "money_amount_currency_code_foreign",
"columnNames": [
"currency_code"
],
"localTableName": "public.money_amount",
"referencedColumnNames": [
"code"
],
"referencedTableName": "public.currency",
"deleteRule": "set null",
"updateRule": "cascade"
}
}
}
]
}
@@ -211,6 +211,16 @@
"primary": false, "primary": false,
"nullable": false, "nullable": false,
"mappedType": "text" "mappedType": "text"
},
"number_rules": {
"name": "number_rules",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "0",
"mappedType": "integer"
} }
}, },
"name": "price_set_money_amount", "name": "price_set_money_amount",
@@ -338,6 +348,104 @@
"checks": [], "checks": [],
"foreignKeys": {} "foreignKeys": {}
}, },
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"price_set_money_amount_id": {
"name": "price_set_money_amount_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"rule_type_id": {
"name": "rule_type_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"value": {
"name": "value",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
}
},
"name": "price_set_money_amount_rules",
"schema": "public",
"indexes": [
{
"columnNames": [
"price_set_money_amount_id"
],
"composite": false,
"keyName": "IDX_price_set_money_amount_rules_price_set_money_amount_id",
"primary": false,
"unique": false
},
{
"columnNames": [
"rule_type_id"
],
"composite": false,
"keyName": "IDX_price_set_money_amount_rules_rule_type_id",
"primary": false,
"unique": false
},
{
"keyName": "price_set_money_amount_rules_pkey",
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {
"price_set_money_amount_rules_price_set_money_amount_id_foreign": {
"constraintName": "price_set_money_amount_rules_price_set_money_amount_id_foreign",
"columnNames": [
"price_set_money_amount_id"
],
"localTableName": "public.price_set_money_amount_rules",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.price_set_money_amount",
"updateRule": "cascade"
},
"price_set_money_amount_rules_rule_type_id_foreign": {
"constraintName": "price_set_money_amount_rules_rule_type_id_foreign",
"columnNames": [
"rule_type_id"
],
"localTableName": "public.price_set_money_amount_rules",
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.rule_type",
"updateRule": "cascade"
}
}
},
{ {
"columns": { "columns": {
"id": { "id": {
@@ -418,14 +526,32 @@
"name": "price_rule", "name": "price_rule",
"schema": "public", "schema": "public",
"indexes": [ "indexes": [
{
"columnNames": [
"price_set_id"
],
"composite": false,
"keyName": "IDX_price_rule_price_set_id",
"primary": false,
"unique": false
},
{ {
"columnNames": [ "columnNames": [
"rule_type_id" "rule_type_id"
], ],
"composite": false, "composite": false,
"keyName": "price_rule_rule_type_id_unique", "keyName": "IDX_price_rule_rule_type_id",
"primary": false, "primary": false,
"unique": true "unique": false
},
{
"columnNames": [
"price_set_money_amount_id"
],
"composite": false,
"keyName": "IDX_price_rule_price_set_money_amount_id",
"primary": false,
"unique": false
}, },
{ {
"keyName": "price_rule_pkey", "keyName": "price_rule_pkey",
@@ -449,6 +575,7 @@
"id" "id"
], ],
"referencedTableName": "public.price_set", "referencedTableName": "public.price_set",
"deleteRule": "cascade",
"updateRule": "cascade" "updateRule": "cascade"
}, },
"price_rule_rule_type_id_foreign": { "price_rule_rule_type_id_foreign": {
@@ -1,23 +0,0 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20230913055746 extends Migration {
async up(): Promise<void> {
this.addSql('create table "price_rule" ("id" text not null, "price_set_id" text not null, "rule_type_id" text not null, "is_dynamic" boolean not null default false, "value" text not null, "priority" integer not null default 0, "price_set_money_amount_id" text not null, "price_list_id" text not null, constraint "price_rule_pkey" primary key ("id"));');
this.addSql('alter table "price_rule" add constraint "price_rule_price_set_id_foreign" foreign key ("price_set_id") references "price_set" ("id") on update cascade;');
this.addSql('alter table "price_rule" add constraint "price_rule_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade;');
this.addSql('alter table "price_rule" add constraint "price_rule_price_set_money_amount_id_foreign" foreign key ("price_set_money_amount_id") references "price_set_money_amount" ("id") on update cascade;');
}
async down(): Promise<void> {
this.addSql('alter table "price_rule" drop constraint "price_rule_price_set_id_foreign";');
this.addSql('alter table "price_rule" drop constraint "price_rule_price_set_money_amount_id_foreign";');
this.addSql('alter table "price_rule" drop constraint "price_rule_rule_type_id_foreign";');
this.addSql('drop table if exists "price_rule" cascade;');
}g
}
@@ -1,52 +0,0 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20230913123118 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table "currency" ("code" text not null, "symbol" text not null, "symbol_native" text not null, "name" text not null, constraint "currency_pkey" primary key ("code"));'
)
this.addSql(
'create table "money_amount" ("id" text not null, "currency_code" text null, "amount" numeric null, "min_quantity" numeric null, "max_quantity" numeric null, constraint "money_amount_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_money_amount_currency_code" on "money_amount" ("currency_code");'
)
this.addSql(
'create table "price_set" ("id" text not null, constraint "price_set_pkey" primary key ("id"));'
)
this.addSql(
'create table "price_set_money_amount" ("id" text not null, "title" text not null, "price_set_id" text not null, "money_amount_id" text not null, constraint "price_set_money_amount_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_price_set_money_amount_price_set_id" on "price_set_money_amount" ("price_set_id");'
)
this.addSql(
'create index "IDX_price_set_money_amount_money_amount_id" on "price_set_money_amount" ("money_amount_id");'
)
this.addSql(
'create table "rule_type" ("id" text not null, "name" text not null, "rule_attribute" text not null, "default_priority" integer not null default 0, constraint "rule_type_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_rule_type_rule_attribute" on "rule_type" ("rule_attribute");'
)
this.addSql(
'alter table "money_amount" add constraint "money_amount_currency_code_foreign" foreign key ("currency_code") references "currency" ("code") on update cascade on delete set null;'
)
this.addSql(
'alter table "price_set_money_amount" add constraint "price_set_money_amount_price_set_id_foreign" foreign key ("price_set_id") references "price_set" ("id") on update cascade on delete cascade;'
)
this.addSql(
'alter table "price_set_money_amount" add constraint "price_set_money_amount_money_amount_id_foreign" foreign key ("money_amount_id") references "money_amount" ("id") on update cascade;'
)
}
}
@@ -0,0 +1,88 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20230928154931 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table "currency" ("code" text not null, "symbol" text not null, "symbol_native" text not null, "name" text not null, constraint "currency_pkey" primary key ("code"));'
)
this.addSql(
'create table "money_amount" ("id" text not null, "currency_code" text null, "amount" numeric null, "min_quantity" numeric null, "max_quantity" numeric null, constraint "money_amount_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_money_amount_currency_code" on "money_amount" ("currency_code");'
)
this.addSql(
'create table "price_set" ("id" text not null, constraint "price_set_pkey" primary key ("id"));'
)
this.addSql(
'create table "price_set_money_amount" ("id" text not null, "title" text not null, "price_set_id" text not null, "money_amount_id" text not null, "number_rules" integer not null default 0, constraint "price_set_money_amount_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_price_set_money_amount_price_set_id" on "price_set_money_amount" ("price_set_id");'
)
this.addSql(
'create index "IDX_price_set_money_amount_money_amount_id" on "price_set_money_amount" ("money_amount_id");'
)
this.addSql(
'create table "rule_type" ("id" text not null, "name" text not null, "rule_attribute" text not null, "default_priority" integer not null default 0, constraint "rule_type_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_rule_type_rule_attribute" on "rule_type" ("rule_attribute");'
)
this.addSql(
'create table "price_set_money_amount_rules" ("id" text not null, "price_set_money_amount_id" text not null, "rule_type_id" text not null, "value" text not null, constraint "price_set_money_amount_rules_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_price_set_money_amount_rules_price_set_money_amount_id" on "price_set_money_amount_rules" ("price_set_money_amount_id");'
)
this.addSql(
'create index "IDX_price_set_money_amount_rules_rule_type_id" on "price_set_money_amount_rules" ("rule_type_id");'
)
this.addSql(
'create table "price_rule" ("id" text not null, "price_set_id" text not null, "rule_type_id" text not null, "is_dynamic" boolean not null default false, "value" text not null, "priority" integer not null default 0, "price_set_money_amount_id" text not null, "price_list_id" text not null, constraint "price_rule_pkey" primary key ("id"));'
)
this.addSql(
'create index "IDX_price_rule_price_set_id" on "price_rule" ("price_set_id");'
)
this.addSql(
'create index "IDX_price_rule_rule_type_id" on "price_rule" ("rule_type_id");'
)
this.addSql(
'create index "IDX_price_rule_price_set_money_amount_id" on "price_rule" ("price_set_money_amount_id");'
)
this.addSql(
'alter table "money_amount" add constraint "money_amount_currency_code_foreign" foreign key ("currency_code") references "currency" ("code") on update cascade on delete set null;'
)
this.addSql(
'alter table "price_set_money_amount" add constraint "price_set_money_amount_price_set_id_foreign" foreign key ("price_set_id") references "price_set" ("id") on update cascade on delete cascade;'
)
this.addSql(
'alter table "price_set_money_amount" add constraint "price_set_money_amount_money_amount_id_foreign" foreign key ("money_amount_id") references "money_amount" ("id") on update cascade;'
)
this.addSql(
'alter table "price_set_money_amount_rules" add constraint "price_set_money_amount_rules_price_set_money_amount_id_foreign" foreign key ("price_set_money_amount_id") references "price_set_money_amount" ("id") on update cascade;'
)
this.addSql(
'alter table "price_set_money_amount_rules" add constraint "price_set_money_amount_rules_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade;'
)
this.addSql(
'alter table "price_rule" add constraint "price_rule_price_set_id_foreign" foreign key ("price_set_id") references "price_set" ("id") on update cascade on delete cascade;'
)
this.addSql(
'alter table "price_rule" add constraint "price_rule_rule_type_id_foreign" foreign key ("rule_type_id") references "rule_type" ("id") on update cascade;'
)
this.addSql(
'alter table "price_rule" add constraint "price_rule_price_set_money_amount_id_foreign" foreign key ("price_set_money_amount_id") references "price_set_money_amount" ("id") on update cascade;'
)
}
}
+2 -1
View File
@@ -1,6 +1,7 @@
export { default as Currency } from "./currency" export { default as Currency } from "./currency"
export { default as MoneyAmount } from "./money-amount" export { default as MoneyAmount } from "./money-amount"
export { default as PriceRule } from "./price-rule"
export { default as PriceSet } from "./price-set" export { default as PriceSet } from "./price-set"
export { default as PriceSetMoneyAmount } from "./price-set-money-amount" export { default as PriceSetMoneyAmount } from "./price-set-money-amount"
export { default as PriceSetMoneyAmountRules } from "./price-set-money-amount-rules"
export { default as RuleType } from "./rule-type" export { default as RuleType } from "./rule-type"
export { default as PriceRule } from "./price-rule"
+5 -6
View File
@@ -1,21 +1,16 @@
import { import {
BeforeCreate, BeforeCreate,
Collection,
Entity, Entity,
ManyToMany,
ManyToOne, ManyToOne,
OneToMany,
OneToOne,
OptionalProps, OptionalProps,
PrimaryKey, PrimaryKey,
Property, Property,
} from "@mikro-orm/core" } from "@mikro-orm/core"
import MoneyAmount from "./money-amount" import { generateEntityId } from "@medusajs/utils"
import PriceSet from "./price-set" import PriceSet from "./price-set"
import PriceSetMoneyAmount from "./price-set-money-amount" import PriceSetMoneyAmount from "./price-set-money-amount"
import RuleType from "./rule-type" import RuleType from "./rule-type"
import { generateEntityId } from "@medusajs/utils"
type OptionalFields = "id" | "is_dynamic" | "priority" type OptionalFields = "id" | "is_dynamic" | "priority"
type OptionalRelations = "price_set" | "rule_type" | "price_set_money_amount" type OptionalRelations = "price_set" | "rule_type" | "price_set_money_amount"
@@ -31,6 +26,8 @@ export default class PriceRule {
entity: () => PriceSet, entity: () => PriceSet,
fieldName: "price_set_id", fieldName: "price_set_id",
name: "price_rule_price_set_id_unique", name: "price_rule_price_set_id_unique",
onDelete: "cascade",
index: "IDX_price_rule_price_set_id",
}) })
price_set: PriceSet price_set: PriceSet
@@ -38,6 +35,7 @@ export default class PriceRule {
entity: () => RuleType, entity: () => RuleType,
fieldName: "rule_type_id", fieldName: "rule_type_id",
name: "price_rule_rule_type_id_unique", name: "price_rule_rule_type_id_unique",
index: "IDX_price_rule_rule_type_id",
}) })
rule_type: RuleType rule_type: RuleType
@@ -54,6 +52,7 @@ export default class PriceRule {
entity: () => PriceSetMoneyAmount, entity: () => PriceSetMoneyAmount,
fieldName: "price_set_money_amount_id", fieldName: "price_set_money_amount_id",
name: "price_set_money_amount_id_unique", name: "price_set_money_amount_id_unique",
index: "IDX_price_rule_price_set_money_amount_id",
}) })
price_set_money_amount: PriceSetMoneyAmount price_set_money_amount: PriceSetMoneyAmount
@@ -0,0 +1,35 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
ManyToOne,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import PriceSetMoneyAmount from "./price-set-money-amount"
import RuleType from "./rule-type"
@Entity()
export default class PriceSetMoneyAmountRules {
@PrimaryKey({ columnType: "text" })
id!: string
@ManyToOne(() => PriceSetMoneyAmount, {
index: "IDX_price_set_money_amount_rules_price_set_money_amount_id",
})
price_set_money_amount?: PriceSetMoneyAmount | string
@ManyToOne(() => RuleType, {
index: "IDX_price_set_money_amount_rules_rule_type_id",
})
rule_type?: RuleType | string
@Property({ columnType: "text" })
value: string
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "psmar")
}
}
@@ -1,15 +1,19 @@
import { generateEntityId } from "@medusajs/utils" import { generateEntityId } from "@medusajs/utils"
import { import {
BeforeCreate, BeforeCreate,
Collection,
Entity, Entity,
ManyToOne, ManyToOne,
OneToMany,
PrimaryKey, PrimaryKey,
PrimaryKeyType, PrimaryKeyType,
Property, Property,
} from "@mikro-orm/core" } from "@mikro-orm/core"
import MoneyAmount from "./money-amount" import MoneyAmount from "./money-amount"
import PriceRule from "./price-rule"
import PriceSet from "./price-set" import PriceSet from "./price-set"
import PriceSetMoneyAmountRules from "./price-set-money-amount-rules"
@Entity() @Entity()
export default class PriceSetMoneyAmount { export default class PriceSetMoneyAmount {
@@ -30,6 +34,21 @@ export default class PriceSetMoneyAmount {
}) })
money_amount?: MoneyAmount money_amount?: MoneyAmount
@Property({ columnType: "integer", default: 0 })
number_rules?: number
@OneToMany({
entity: () => PriceRule,
mappedBy: (pr) => pr.price_set_money_amount,
})
price_rules = new Collection<PriceRule>(this)
@OneToMany({
entity: () => PriceSetMoneyAmountRules,
mappedBy: (psmar) => psmar.price_set_money_amount,
})
price_set_money_amount_rules = new Collection<PriceSetMoneyAmountRules>(this)
@BeforeCreate() @BeforeCreate()
onCreate() { onCreate() {
this.id = generateEntityId(this.id, "psma") this.id = generateEntityId(this.id, "psma")
+16
View File
@@ -1,20 +1,36 @@
import { generateEntityId } from "@medusajs/utils" import { generateEntityId } from "@medusajs/utils"
import { import {
BeforeCreate, BeforeCreate,
Cascade,
Collection, Collection,
Entity, Entity,
ManyToMany, ManyToMany,
OneToMany,
OptionalProps,
PrimaryKey, PrimaryKey,
} from "@mikro-orm/core" } from "@mikro-orm/core"
import MoneyAmount from "./money-amount" import MoneyAmount from "./money-amount"
import PriceRule from "./price-rule"
import PriceSetMoneyAmount from "./price-set-money-amount" import PriceSetMoneyAmount from "./price-set-money-amount"
@Entity() @Entity()
export default class PriceSet { export default class PriceSet {
[OptionalProps]?: "price_set_money_amounts"
@PrimaryKey({ columnType: "text" }) @PrimaryKey({ columnType: "text" })
id!: string id!: string
@OneToMany(() => PriceSetMoneyAmount, (psma) => psma.price_set, {
cascade: [Cascade.REMOVE],
})
price_set_money_amounts = new Collection<PriceSetMoneyAmount>(this)
@OneToMany(() => PriceRule, (pr) => pr.price_set, {
cascade: [Cascade.REMOVE],
})
price_rules = new Collection<PriceRule>(this)
@ManyToMany({ @ManyToMany({
entity: () => MoneyAmount, entity: () => MoneyAmount,
pivotEntity: () => PriceSetMoneyAmount, pivotEntity: () => PriceSetMoneyAmount,
+3 -2
View File
@@ -1,6 +1,7 @@
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils" export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
export { CurrencyRepository } from "./currency" export { CurrencyRepository } from "./currency"
export { MoneyAmountRepository } from "./money-amount" export { MoneyAmountRepository } from "./money-amount"
export { PriceSetRepository } from "./price-set"
export { RuleTypeRepository } from "./rule-type"
export { PriceRuleRepository } from "./price-rule" export { PriceRuleRepository } from "./price-rule"
export { PriceSetRepository } from "./price-set"
export { PriceSetMoneyAmountRulesRepository } from "./price-set-money-amount-rules"
export { RuleTypeRepository } from "./rule-type"
@@ -10,7 +10,7 @@ import {
FilterQuery as MikroFilterQuery, FilterQuery as MikroFilterQuery,
FindOptions as MikroOptions, FindOptions as MikroOptions,
} from "@mikro-orm/core" } from "@mikro-orm/core"
import { PriceRule, PriceSet, PriceSetMoneyAmount, RuleType } from "@models" import { PriceRule } from "@models"
import { SqlEntityManager } from "@mikro-orm/postgresql" import { SqlEntityManager } from "@mikro-orm/postgresql"
@@ -76,20 +76,19 @@ export class PriceRuleRepository extends DALUtils.MikroOrmBaseRepository {
const manager: SqlEntityManager = const manager: SqlEntityManager =
this.getActiveManager<SqlEntityManager>(context) this.getActiveManager<SqlEntityManager>(context)
const toCreate = await Promise.all(data.map(async (ruleData) => { const toCreate = data.map((ruleData) => {
const ruleDataClone = { ...ruleData } as CreatePriceRuleDTO & { const ruleDataClone = { ...ruleData } as CreatePriceRuleDTO & {
rule_type: string rule_type: string
price_set: string price_set: string
price_set_money_amount: string price_set_money_amount: string
} }
ruleDataClone.rule_type = ruleData.rule_type_id ruleDataClone.rule_type = ruleData.rule_type_id
ruleDataClone.price_set = ruleData.price_set_id ruleDataClone.price_set = ruleData.price_set_id
ruleDataClone.price_set_money_amount = ruleData.price_set_money_amount_id
ruleDataClone.price_set_money_amount = ruleData.price_set_money_amount_id
return ruleDataClone return ruleDataClone
})) })
const priceRules = toCreate.map((ruleData) => { const priceRules = toCreate.map((ruleData) => {
return manager.create(PriceRule, ruleData as CreatePriceRuleDTO) return manager.create(PriceRule, ruleData as CreatePriceRuleDTO)
@@ -0,0 +1,127 @@
import {
Context,
CreatePriceSetMoneyAmountRulesDTO,
DAL,
UpdatePriceSetMoneyAmountRulesDTO,
} from "@medusajs/types"
import { DALUtils, MedusaError } from "@medusajs/utils"
import {
LoadStrategy,
FilterQuery as MikroFilterQuery,
FindOptions as MikroOptions,
} from "@mikro-orm/core"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceSetMoneyAmountRules } from "@models"
export class PriceSetMoneyAmountRulesRepository extends DALUtils.MikroOrmBaseRepository {
protected readonly manager_: SqlEntityManager
constructor({ manager }: { manager: SqlEntityManager }) {
// @ts-ignore
// eslint-disable-next-line prefer-rest-params
super(...arguments)
this.manager_ = manager
}
async find(
findOptions: DAL.FindOptions<PriceSetMoneyAmountRules> = { where: {} },
context: Context = {}
): Promise<PriceSetMoneyAmountRules[]> {
const manager = this.getActiveManager<SqlEntityManager>(context)
const findOptions_ = { ...findOptions }
findOptions_.options ??= {}
Object.assign(findOptions_.options, {
strategy: LoadStrategy.SELECT_IN,
})
return await manager.find(
PriceSetMoneyAmountRules,
findOptions_.where as MikroFilterQuery<PriceSetMoneyAmountRules>,
findOptions_.options as MikroOptions<PriceSetMoneyAmountRules>
)
}
async findAndCount(
findOptions: DAL.FindOptions<PriceSetMoneyAmountRules> = { where: {} },
context: Context = {}
): Promise<[PriceSetMoneyAmountRules[], number]> {
const manager = this.getActiveManager<SqlEntityManager>(context)
const findOptions_ = { ...findOptions }
findOptions_.options ??= {}
Object.assign(findOptions_.options, {
strategy: LoadStrategy.SELECT_IN,
})
return await manager.findAndCount(
PriceSetMoneyAmountRules,
findOptions_.where as MikroFilterQuery<PriceSetMoneyAmountRules>,
findOptions_.options as MikroOptions<PriceSetMoneyAmountRules>
)
}
async delete(ids: string[], context: Context = {}): Promise<void> {
const manager = this.getActiveManager<SqlEntityManager>(context)
await manager.nativeDelete(PriceSetMoneyAmountRules, { id: { $in: ids } })
}
async create(
data: CreatePriceSetMoneyAmountRulesDTO[],
context: Context = {}
): Promise<PriceSetMoneyAmountRules[]> {
const manager = this.getActiveManager<SqlEntityManager>(context)
const psmar = data.map((psmarData) => {
return manager.create(PriceSetMoneyAmountRules, psmarData)
})
manager.persistAndFlush(psmar)
return psmar
}
async update(
data: UpdatePriceSetMoneyAmountRulesDTO[],
context: Context = {}
): Promise<PriceSetMoneyAmountRules[]> {
const manager = this.getActiveManager<SqlEntityManager>(context)
const psmarIds = data.map((psmar) => psmar.id)
const existingRecords = await this.find(
{
where: {
id: {
$in: psmarIds,
},
},
},
context
)
const psmarMap = new Map(
existingRecords.map<[string, PriceSetMoneyAmountRules]>((psmar) => [
psmar.id,
psmar,
])
)
const psmar = data.map((psmarData) => {
const existingRecord = psmarMap.get(psmarData.id)
if (!existingRecord) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`PriceSetMoneyAmountRules with id "${psmarData.id}" not found`
)
}
return manager.assign(existingRecord, psmarData)
})
manager.persist(psmar)
return psmar
}
}
+2 -1
View File
@@ -1,6 +1,7 @@
export { default as CurrencyService } from "./currency" export { default as CurrencyService } from "./currency"
export { default as MoneyAmountService } from "./money-amount" export { default as MoneyAmountService } from "./money-amount"
export { default as PriceRuleService } from "./price-rule"
export { default as PriceSetService } from "./price-set" export { default as PriceSetService } from "./price-set"
export { default as PriceSetMoneyAmountRulesService } from "./price-set-money-amount-rules"
export { default as PricingModuleService } from "./pricing-module" export { default as PricingModuleService } from "./pricing-module"
export { default as RuleTypeService } from "./rule-type" export { default as RuleTypeService } from "./rule-type"
export { default as PriceRuleService } from "./price-rule"
+10 -16
View File
@@ -45,8 +45,13 @@ export default class MoneyAmountService<
config: FindConfig<PricingTypes.MoneyAmountDTO> = {}, config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> { ): Promise<TEntity[]> {
const queryOptions = ModulesSdkUtils.buildQuery<MoneyAmount>(
filters,
config
)
return (await this.moneyAmountRepository_.find( return (await this.moneyAmountRepository_.find(
this.buildQueryForList(filters, config), queryOptions,
sharedContext sharedContext
)) as TEntity[] )) as TEntity[]
} }
@@ -57,26 +62,15 @@ export default class MoneyAmountService<
config: FindConfig<PricingTypes.MoneyAmountDTO> = {}, config: FindConfig<PricingTypes.MoneyAmountDTO> = {},
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> { ): Promise<[TEntity[], number]> {
return (await this.moneyAmountRepository_.findAndCount(
this.buildQueryForList(filters, config),
sharedContext
)) as [TEntity[], number]
}
private buildQueryForList(
filters: PricingTypes.FilterableMoneyAmountProps = {},
config: FindConfig<PricingTypes.MoneyAmountDTO> = {}
) {
const queryOptions = ModulesSdkUtils.buildQuery<MoneyAmount>( const queryOptions = ModulesSdkUtils.buildQuery<MoneyAmount>(
filters, filters,
config config
) )
if (filters.id) { return (await this.moneyAmountRepository_.findAndCount(
queryOptions.where["id"] = { $in: filters.id } queryOptions,
} sharedContext
)) as [TEntity[], number]
return queryOptions
} }
@InjectTransactionManager(shouldForceTransaction, "moneyAmountRepository_") @InjectTransactionManager(shouldForceTransaction, "moneyAmountRepository_")
+1 -1
View File
@@ -58,7 +58,7 @@ export default class PriceRuleService<TEntity extends PriceRule = PriceRule> {
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> { ): Promise<[TEntity[], number]> {
const queryConfig = ModulesSdkUtils.buildQuery<PriceRule>(filters, config) const queryConfig = ModulesSdkUtils.buildQuery<PriceRule>(filters, config)
return (await this.priceRuleRepository_.findAndCount( return (await this.priceRuleRepository_.findAndCount(
queryConfig, queryConfig,
sharedContext sharedContext
@@ -0,0 +1,120 @@
import { Context, DAL, FindConfig, PricingTypes } from "@medusajs/types"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
ModulesSdkUtils,
retrieveEntity,
} from "@medusajs/utils"
import { PriceSetMoneyAmountRules } from "@models"
import { doNotForceTransaction, shouldForceTransaction } from "@medusajs/utils"
type InjectedDependencies = {
priceSetMoneyAmountRulesRepository: DAL.RepositoryService
}
export default class PriceSetMoneyAmountRulesService<
TEntity extends PriceSetMoneyAmountRules = PriceSetMoneyAmountRules
> {
protected readonly priceSetMoneyAmountRulesRepository_: DAL.RepositoryService
constructor({ priceSetMoneyAmountRulesRepository }: InjectedDependencies) {
this.priceSetMoneyAmountRulesRepository_ =
priceSetMoneyAmountRulesRepository
}
@InjectManager("priceSetMoneyAmountRulesRepository_")
async retrieve(
priceSetMoneyAmountRulesId: string,
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity> {
return (await retrieveEntity<
PriceSetMoneyAmountRules,
PricingTypes.PriceSetMoneyAmountRulesDTO
>({
id: priceSetMoneyAmountRulesId,
identifierColumn: "id",
entityName: PriceSetMoneyAmountRules.name,
repository: this.priceSetMoneyAmountRulesRepository_,
config,
sharedContext,
})) as TEntity
}
@InjectManager("priceSetMoneyAmountRulesRepository_")
async list(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
return (await this.priceSetMoneyAmountRulesRepository_.find(
this.buildQueryForList(filters, config),
sharedContext
)) as TEntity[]
}
@InjectManager("priceSetMoneyAmountRulesRepository_")
async listAndCount(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> {
return (await this.priceSetMoneyAmountRulesRepository_.findAndCount(
this.buildQueryForList(filters, config),
sharedContext
)) as [TEntity[], number]
}
private buildQueryForList(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {}
) {
const queryOptions = ModulesSdkUtils.buildQuery<PriceSetMoneyAmountRules>(
filters,
config
)
return queryOptions
}
@InjectTransactionManager(
shouldForceTransaction,
"priceSetMoneyAmountRulesRepository_"
)
async create(
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
return (await this.priceSetMoneyAmountRulesRepository_.create(
data,
sharedContext
)) as TEntity[]
}
@InjectTransactionManager(
shouldForceTransaction,
"priceSetMoneyAmountRulesRepository_"
)
async update(
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
return (await this.priceSetMoneyAmountRulesRepository_.update(
data,
sharedContext
)) as TEntity[]
}
@InjectTransactionManager(
doNotForceTransaction,
"priceSetMoneyAmountRulesRepository_"
)
async delete(
ids: string[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
await this.priceSetMoneyAmountRulesRepository_.delete(ids, sharedContext)
}
}
+7 -16
View File
@@ -43,8 +43,10 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
config: FindConfig<PricingTypes.PriceSetDTO> = {}, config: FindConfig<PricingTypes.PriceSetDTO> = {},
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> { ): Promise<TEntity[]> {
const queryOptions = ModulesSdkUtils.buildQuery<PriceSet>(filters, config)
return (await this.priceSetRepository_.find( return (await this.priceSetRepository_.find(
this.buildQueryForList(filters, config), queryOptions,
sharedContext sharedContext
)) as TEntity[] )) as TEntity[]
} }
@@ -55,23 +57,12 @@ export default class PriceSetService<TEntity extends PriceSet = PriceSet> {
config: FindConfig<PricingTypes.PriceSetDTO> = {}, config: FindConfig<PricingTypes.PriceSetDTO> = {},
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> { ): Promise<[TEntity[], number]> {
return (await this.priceSetRepository_.findAndCount(
this.buildQueryForList(filters, config),
sharedContext
)) as [TEntity[], number]
}
private buildQueryForList(
filters: PricingTypes.FilterablePriceSetProps = {},
config: FindConfig<PricingTypes.PriceSetDTO> = {}
) {
const queryOptions = ModulesSdkUtils.buildQuery<PriceSet>(filters, config) const queryOptions = ModulesSdkUtils.buildQuery<PriceSet>(filters, config)
if (filters.id) { return (await this.priceSetRepository_.findAndCount(
queryOptions.where.id = { $in: filters.id } queryOptions,
} sharedContext
)) as [TEntity[], number]
return queryOptions
} }
@InjectTransactionManager(shouldForceTransaction, "priceSetRepository_") @InjectTransactionManager(shouldForceTransaction, "priceSetRepository_")
+207 -38
View File
@@ -8,19 +8,30 @@ import {
PricingFilters, PricingFilters,
PricingTypes, PricingTypes,
} from "@medusajs/types" } from "@medusajs/types"
import {
Currency,
MoneyAmount,
PriceRule,
PriceSet,
PriceSetMoneyAmountRules,
RuleType,
} from "@models"
import { import {
CurrencyService, CurrencyService,
MoneyAmountService, MoneyAmountService,
PriceRuleService,
PriceSetMoneyAmountRulesService,
PriceSetService, PriceSetService,
RuleTypeService, RuleTypeService,
PriceRuleService
} from "@services" } from "@services"
import { Currency, MoneyAmount, PriceRule, PriceSet, RuleType } from "@models"
import { EntityManager } from "@mikro-orm/postgresql"
import { import {
InjectManager, InjectManager,
InjectTransactionManager, InjectTransactionManager,
MedusaContext, MedusaContext,
groupBy,
shouldForceTransaction, shouldForceTransaction,
} from "@medusajs/utils" } from "@medusajs/utils"
@@ -31,6 +42,7 @@ type InjectedDependencies = {
currencyService: CurrencyService<any> currencyService: CurrencyService<any>
moneyAmountService: MoneyAmountService<any> moneyAmountService: MoneyAmountService<any>
priceSetService: PriceSetService<any> priceSetService: PriceSetService<any>
priceSetMoneyAmountRulesService: PriceSetMoneyAmountRulesService<any>
ruleTypeService: RuleTypeService<any> ruleTypeService: RuleTypeService<any>
priceRuleService: PriceRuleService<any> priceRuleService: PriceRuleService<any>
} }
@@ -40,7 +52,8 @@ export default class PricingModuleService<
TMoneyAmount extends MoneyAmount = MoneyAmount, TMoneyAmount extends MoneyAmount = MoneyAmount,
TCurrency extends Currency = Currency, TCurrency extends Currency = Currency,
TRuleType extends RuleType = RuleType, TRuleType extends RuleType = RuleType,
TPriceRule extends PriceRule = PriceRule, TPriceSetMoneyAmountRules extends PriceSetMoneyAmountRules = PriceSetMoneyAmountRules,
TPriceRule extends PriceRule = PriceRule
> implements PricingTypes.IPricingModuleService > implements PricingTypes.IPricingModuleService
{ {
protected baseRepository_: DAL.RepositoryService protected baseRepository_: DAL.RepositoryService
@@ -48,6 +61,7 @@ export default class PricingModuleService<
protected readonly moneyAmountService_: MoneyAmountService<TMoneyAmount> protected readonly moneyAmountService_: MoneyAmountService<TMoneyAmount>
protected readonly ruleTypeService_: RuleTypeService<TRuleType> protected readonly ruleTypeService_: RuleTypeService<TRuleType>
protected readonly priceSetService_: PriceSetService<TPriceSet> protected readonly priceSetService_: PriceSetService<TPriceSet>
protected readonly priceSetMoneyAmountRulesService_: PriceSetMoneyAmountRulesService<TPriceSetMoneyAmountRules>
protected readonly priceRuleService_: PriceRuleService<TPriceRule> protected readonly priceRuleService_: PriceRuleService<TPriceRule>
constructor( constructor(
@@ -57,6 +71,7 @@ export default class PricingModuleService<
currencyService, currencyService,
ruleTypeService, ruleTypeService,
priceSetService, priceSetService,
priceSetMoneyAmountRulesService,
priceRuleService, priceRuleService,
}: InjectedDependencies, }: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration protected readonly moduleDeclaration: InternalModuleDeclaration
@@ -66,6 +81,8 @@ export default class PricingModuleService<
this.moneyAmountService_ = moneyAmountService this.moneyAmountService_ = moneyAmountService
this.ruleTypeService_ = ruleTypeService this.ruleTypeService_ = ruleTypeService
this.priceSetService_ = priceSetService this.priceSetService_ = priceSetService
this.priceSetMoneyAmountRulesService_ = priceSetMoneyAmountRulesService
this.ruleTypeService_ = ruleTypeService
this.priceRuleService_ = priceRuleService this.priceRuleService_ = priceRuleService
} }
@@ -79,46 +96,91 @@ export default class PricingModuleService<
pricingContext: PricingContext = { context: {} }, pricingContext: PricingContext = { context: {} },
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.CalculatedPriceSetDTO> { ): Promise<PricingTypes.CalculatedPriceSetDTO> {
const manager = sharedContext.manager as EntityManager
const knex = manager.getKnex()
// Keeping this whole logic raw in here for now as they will undergo // Keeping this whole logic raw in here for now as they will undergo
// some changes, will abstract them out once we have a final version // some changes, will abstract them out once we have a final version
const context = pricingContext.context || {} const context = pricingContext.context || {}
const priceSetFilters: PricingTypes.FilterablePriceSetProps = {
id: pricingFilters.id, // Quantity is used to scope money amounts based on min_quantity and max_quantity.
// We should potentially think of reserved words in pricingContext that can't be used in rules
// or have a separate pricing options that accept things like quantity, price_list_id and other
// pricing module features
const quantity = context.quantity
delete context.quantity
// Gets all the price set money amounts where rules match for each of the contexts
// that the price set is configured for
const psmaSubQueryKnex = knex({
psma: "price_set_money_amount",
})
.select({
id: "psma.id",
price_set_id: "psma.price_set_id",
money_amount_id: "psma.money_amount_id",
number_rules: "psma.number_rules",
})
.join("price_rule as pr", "pr.price_set_money_amount_id", "psma.id")
.join("rule_type as rt", "rt.id", "pr.rule_type_id")
.orderBy("number_rules", "desc")
for (const [key, value] of Object.entries(context)) {
psmaSubQueryKnex.orWhere({
"rt.rule_attribute": key,
"pr.value": value,
})
} }
const priceSets = await this.list( psmaSubQueryKnex
priceSetFilters, .groupBy("psma.id")
{ .having(knex.raw("count(DISTINCT rt.rule_attribute) = psma.number_rules"))
select: [
"id",
"money_amounts.id",
"money_amounts.currency_code",
"money_amounts.amount",
"money_amounts.min_quantity",
"money_amounts.max_quantity",
],
relations: ["money_amounts"],
},
sharedContext
)
const calculatedPrices = priceSets.map( const priceSetQueryKnex = knex({
(priceSet): PricingTypes.CalculatedPriceSetDTO => { ps: "price_set",
// TODO: This will change with the rules engine selection, })
// making a DB query directly instead .select({
// This should look for a default price when no rules apply id: "ps.id",
// When no price is set, return null values for all cases amount: "ma.amount",
const selectedMoneyAmount = priceSet.money_amounts?.find( min_quantity: "ma.min_quantity",
(ma) => max_quantity: "ma.max_quantity",
context.currency_code && ma.currency_code === context.currency_code currency_code: "ma.currency_code",
) default_priority: "rt.default_priority",
number_rules: "psma.number_rules",
})
.join(psmaSubQueryKnex.as("psma"), "psma.price_set_id", "ps.id")
.join("money_amount as ma", "ma.id", "psma.money_amount_id")
.join("price_rule as pr", "pr.price_set_money_amount_id", "psma.id")
.join("rule_type as rt", "rt.id", "pr.rule_type_id")
.whereIn("ps.id", pricingFilters.id)
.orderBy([
{ column: "number_rules", order: "desc" },
{ column: "default_priority", order: "desc" },
])
if (quantity) {
priceSetQueryKnex.where("ma.min_quantity", "<=", quantity)
priceSetQueryKnex.andWhere("ma.max_quantity", ">=", quantity)
}
const isContextPresent = Object.entries(context).length
// Only if the context is present do we need to query the database.
const queryBuilderResults = isContextPresent ? await priceSetQueryKnex : []
const pricesSetPricesMap = groupBy(queryBuilderResults, "id")
const calculatedPrices = pricingFilters.id.map(
(priceSetId: string): PricingTypes.CalculatedPriceSetDTO => {
// This is where we select prices, for now we just do a first match based on the database results
// which is prioritized by number_rules first for exact match and then deafult_priority of the rule_type
// inject custom price selection here
const price = pricesSetPricesMap.get(priceSetId)?.[0]
return { return {
id: priceSet.id, id: priceSetId,
amount: selectedMoneyAmount?.amount || null, amount: price?.amount || null,
currency_code: selectedMoneyAmount?.currency_code || null, currency_code: price?.currency_code || null,
min_quantity: selectedMoneyAmount?.min_quantity || null, min_quantity: price?.min_quantity || null,
max_quantity: selectedMoneyAmount?.max_quantity || null, max_quantity: price?.max_quantity || null,
} }
} }
) )
@@ -527,6 +589,110 @@ export default class PricingModuleService<
await this.ruleTypeService_.delete(ruleTypes, sharedContext) await this.ruleTypeService_.delete(ruleTypes, sharedContext)
} }
@InjectManager("baseRepository_")
async retrievePriceSetMoneyAmountRules(
id: string,
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceSetMoneyAmountRulesDTO> {
const record = await this.priceSetMoneyAmountRulesService_.retrieve(
id,
config,
sharedContext
)
return this.baseRepository_.serialize<PricingTypes.PriceSetMoneyAmountRulesDTO>(
record,
{
populate: true,
}
)
}
@InjectManager("baseRepository_")
async listPriceSetMoneyAmountRules(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceSetMoneyAmountRulesDTO[]> {
const records = await this.priceSetMoneyAmountRulesService_.list(
filters,
config,
sharedContext
)
return this.baseRepository_.serialize<
PricingTypes.PriceSetMoneyAmountRulesDTO[]
>(records, {
populate: true,
})
}
@InjectManager("baseRepository_")
async listAndCountPriceSetMoneyAmountRules(
filters: PricingTypes.FilterablePriceSetMoneyAmountRulesProps = {},
config: FindConfig<PricingTypes.PriceSetMoneyAmountRulesDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<[PricingTypes.PriceSetMoneyAmountRulesDTO[], number]> {
const [records, count] =
await this.priceSetMoneyAmountRulesService_.listAndCount(
filters,
config,
sharedContext
)
return [
await this.baseRepository_.serialize<
PricingTypes.PriceSetMoneyAmountRulesDTO[]
>(records, {
populate: true,
}),
count,
]
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
async createPriceSetMoneyAmountRules(
data: PricingTypes.CreatePriceSetMoneyAmountRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceSetMoneyAmountRulesDTO[]> {
const records = await this.priceSetMoneyAmountRulesService_.create(
data,
sharedContext
)
return this.baseRepository_.serialize<
PricingTypes.PriceSetMoneyAmountRulesDTO[]
>(records, {
populate: true,
})
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
async updatePriceSetMoneyAmountRules(
data: PricingTypes.UpdatePriceSetMoneyAmountRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceSetMoneyAmountRulesDTO[]> {
const records = await this.priceSetMoneyAmountRulesService_.update(
data,
sharedContext
)
return this.baseRepository_.serialize<
PricingTypes.PriceSetMoneyAmountRulesDTO[]
>(records, {
populate: true,
})
}
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
async deletePriceSetMoneyAmountRules(
ids: string[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
await this.priceSetMoneyAmountRulesService_.delete(ids, sharedContext)
}
@InjectManager("baseRepository_") @InjectManager("baseRepository_")
async retrievePriceRule( async retrievePriceRule(
id: string, id: string,
@@ -539,9 +705,12 @@ export default class PricingModuleService<
sharedContext sharedContext
) )
return this.baseRepository_.serialize<PricingTypes.PriceRuleDTO>(priceRule, { return this.baseRepository_.serialize<PricingTypes.PriceRuleDTO>(
populate: true, priceRule,
}) {
populate: true,
}
)
} }
@InjectManager("baseRepository_") @InjectManager("baseRepository_")
+7 -16
View File
@@ -43,8 +43,10 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
config: FindConfig<PricingTypes.RuleTypeDTO> = {}, config: FindConfig<PricingTypes.RuleTypeDTO> = {},
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> { ): Promise<TEntity[]> {
const queryOptions = ModulesSdkUtils.buildQuery<RuleType>(filters, config)
return (await this.ruleTypeRepository_.find( return (await this.ruleTypeRepository_.find(
this.buildQueryForList(filters, config), queryOptions,
sharedContext sharedContext
)) as TEntity[] )) as TEntity[]
} }
@@ -55,23 +57,12 @@ export default class RuleTypeService<TEntity extends RuleType = RuleType> {
config: FindConfig<PricingTypes.RuleTypeDTO> = {}, config: FindConfig<PricingTypes.RuleTypeDTO> = {},
@MedusaContext() sharedContext: Context = {} @MedusaContext() sharedContext: Context = {}
): Promise<[TEntity[], number]> { ): Promise<[TEntity[], number]> {
return (await this.ruleTypeRepository_.findAndCount(
this.buildQueryForList(filters, config),
sharedContext
)) as [TEntity[], number]
}
private buildQueryForList(
filters: PricingTypes.FilterableRuleTypeProps = {},
config: FindConfig<PricingTypes.RuleTypeDTO> = {}
) {
const queryOptions = ModulesSdkUtils.buildQuery<RuleType>(filters, config) const queryOptions = ModulesSdkUtils.buildQuery<RuleType>(filters, config)
if (filters.id) { return (await this.ruleTypeRepository_.findAndCount(
queryOptions.where["id"] = { $in: filters.id } queryOptions,
} sharedContext
)) as [TEntity[], number]
return queryOptions
} }
@InjectTransactionManager(shouldForceTransaction, "ruleTypeRepository_") @InjectTransactionManager(shouldForceTransaction, "ruleTypeRepository_")
@@ -1,5 +1,7 @@
export * from "./currency" export * from "./currency"
export * from "./money-amount" export * from "./money-amount"
export * from "./price-set" export * from "./price-set"
export * from "./price-set-money-amount"
export * from "./price-set-money-amount-rules"
export * from "./rule-type" export * from "./rule-type"
export * from './price-rule' export * from './price-rule'
@@ -0,0 +1,31 @@
import { BaseFilterable } from "../../dal"
import { PriceSetMoneyAmountDTO } from "./price-set-money-amount"
import { RuleTypeDTO } from "./rule-type"
export interface PriceSetMoneyAmountRulesDTO {
id: string
price_set_money_amount: PriceSetMoneyAmountDTO
rule_type: RuleTypeDTO
value: string
}
export interface CreatePriceSetMoneyAmountRulesDTO {
price_set_money_amount: string
rule_type: string
value: string
}
export interface UpdatePriceSetMoneyAmountRulesDTO {
id: string
price_set_money_amount?: string
rule_type?: string
value?: string
}
export interface FilterablePriceSetMoneyAmountRulesProps
extends BaseFilterable<FilterablePriceSetMoneyAmountRulesProps> {
id?: string[]
rule_type_id?: string[]
price_set_money_amount_id?: string[]
value?: string[]
}
@@ -0,0 +1,9 @@
import { MoneyAmountDTO } from "./money-amount"
import { PriceSetDTO } from "./price-set"
export interface PriceSetMoneyAmountDTO {
id: string
title?: string
price_set?: PriceSetDTO
rule_type?: MoneyAmountDTO
}
@@ -2,9 +2,7 @@ import { BaseFilterable } from "../../dal"
import { FilterableMoneyAmountProps, MoneyAmountDTO } from "./money-amount" import { FilterableMoneyAmountProps, MoneyAmountDTO } from "./money-amount"
export interface PricingContext { export interface PricingContext {
context?: { context?: Record<string, string | number>
currency_code?: string
}
} }
export interface PricingFilters { export interface PricingFilters {
+38 -4
View File
@@ -4,16 +4,19 @@ import {
CreateMoneyAmountDTO, CreateMoneyAmountDTO,
CreatePriceRuleDTO, CreatePriceRuleDTO,
CreatePriceSetDTO, CreatePriceSetDTO,
CreatePriceSetMoneyAmountRulesDTO,
CreateRuleTypeDTO, CreateRuleTypeDTO,
CurrencyDTO, CurrencyDTO,
FilterableCurrencyProps, FilterableCurrencyProps,
FilterableMoneyAmountProps, FilterableMoneyAmountProps,
FilterablePriceRuleProps, FilterablePriceRuleProps,
FilterablePriceSetMoneyAmountRulesProps,
FilterablePriceSetProps, FilterablePriceSetProps,
FilterableRuleTypeProps, FilterableRuleTypeProps,
MoneyAmountDTO, MoneyAmountDTO,
PriceRuleDTO, PriceRuleDTO,
PriceSetDTO, PriceSetDTO,
PriceSetMoneyAmountRulesDTO,
PricingContext, PricingContext,
PricingFilters, PricingFilters,
RuleTypeDTO, RuleTypeDTO,
@@ -21,12 +24,13 @@ import {
UpdateMoneyAmountDTO, UpdateMoneyAmountDTO,
UpdatePriceRuleDTO, UpdatePriceRuleDTO,
UpdatePriceSetDTO, UpdatePriceSetDTO,
UpdatePriceSetMoneyAmountRulesDTO,
UpdateRuleTypeDTO, UpdateRuleTypeDTO,
} from "./common" } from "./common"
import { Context } from "../shared-context"
import { FindConfig } from "../common" import { FindConfig } from "../common"
import { ModuleJoinerConfig } from "../modules-sdk" import { ModuleJoinerConfig } from "../modules-sdk"
import { Context } from "../shared-context"
export interface IPricingModuleService { export interface IPricingModuleService {
__joinerConfig(): ModuleJoinerConfig __joinerConfig(): ModuleJoinerConfig
@@ -157,11 +161,41 @@ export interface IPricingModuleService {
sharedContext?: Context sharedContext?: Context
): Promise<RuleTypeDTO[]> ): Promise<RuleTypeDTO[]>
deleteRuleTypes( deleteRuleTypes(ruleTypeIds: string[], sharedContext?: Context): Promise<void>
ruleTypeIds: string[],
retrievePriceSetMoneyAmountRules(
id: string,
config?: FindConfig<PriceSetMoneyAmountRulesDTO>,
sharedContext?: Context
): Promise<PriceSetMoneyAmountRulesDTO>
listPriceSetMoneyAmountRules(
filters?: FilterablePriceSetMoneyAmountRulesProps,
config?: FindConfig<PriceSetMoneyAmountRulesDTO>,
sharedContext?: Context
): Promise<PriceSetMoneyAmountRulesDTO[]>
listAndCountPriceSetMoneyAmountRules(
filters?: FilterablePriceSetMoneyAmountRulesProps,
config?: FindConfig<PriceSetMoneyAmountRulesDTO>,
sharedContext?: Context
): Promise<[PriceSetMoneyAmountRulesDTO[], number]>
createPriceSetMoneyAmountRules(
data: CreatePriceSetMoneyAmountRulesDTO[],
sharedContext?: Context
): Promise<PriceSetMoneyAmountRulesDTO[]>
updatePriceSetMoneyAmountRules(
data: UpdatePriceSetMoneyAmountRulesDTO[],
sharedContext?: Context
): Promise<PriceSetMoneyAmountRulesDTO[]>
deletePriceSetMoneyAmountRules(
ids: string[],
sharedContext?: Context sharedContext?: Context
): Promise<void> ): Promise<void>
retrievePriceRule( retrievePriceRule(
id: string, id: string,
config?: FindConfig<PriceRuleDTO>, config?: FindConfig<PriceRuleDTO>,
@@ -0,0 +1,50 @@
import { groupBy } from "../group-by"
const array = [
{
id: "test-id-1",
property: "test-id-1-property-1",
},
{
id: "test-id-1",
property: "test-id-1-property-2",
},
{
id: "test-id-2",
property: "test-id-2-property-1",
},
{
id: "test-id-2",
property: "test-id-2-property-2",
},
{
id: "test-id-3",
property: "test-id-3-property-1",
},
]
const mapToObject = (map: Map<any, any>) => Object.fromEntries(map.entries())
describe("groupBy", function () {
it("should return a map grouped by an identifier", function () {
const response = mapToObject(groupBy(array, "id"))
expect(response).toEqual({
"test-id-1": [
{ id: "test-id-1", property: "test-id-1-property-1" },
{ id: "test-id-1", property: "test-id-1-property-2" },
],
"test-id-2": [
{ id: "test-id-2", property: "test-id-2-property-1" },
{ id: "test-id-2", property: "test-id-2-property-2" },
],
"test-id-3": [{ id: "test-id-3", property: "test-id-3-property-1" }],
})
})
it("should return empty map if identifier is not found in array", function () {
const response = mapToObject(groupBy(array, "doesnotexist"))
expect(response).toEqual({})
})
})
+20
View File
@@ -0,0 +1,20 @@
export function groupBy(
array: Record<any, any>[],
attribute: string | number
): Map<any, any> {
return array.reduce<Map<any, any>>((map, obj) => {
const key = obj[attribute]
if (!key) {
return map
}
if (!map.get(key)) {
map.set(key, [])
}
map.get(key).push(obj)
return map
}, new Map())
}
+1
View File
@@ -4,6 +4,7 @@ export * from "./deduplicate"
export * from "./errors" export * from "./errors"
export * from "./generate-entity-id" export * from "./generate-entity-id"
export * from "./get-config-file" export * from "./get-config-file"
export * from "./group-by"
export * from "./handle-postgres-database-error" export * from "./handle-postgres-database-error"
export * from "./is-date" export * from "./is-date"
export * from "./is-defined" export * from "./is-defined"