feat: Completely revamp the pricing module (#7852)
* feat: Completely revamp the pricing module * chore: Update all places to the new pricing interfaces * fix: Remove unnecessary join to itself * chore: Add data migration for existing users * fix: Apply the correct index to price rule
This commit is contained in:
@@ -2,11 +2,13 @@ export const defaultPriceListRuleData = [
|
||||
{
|
||||
id: "price-list-rule-1",
|
||||
price_list_id: "price-list-1",
|
||||
rule_type_id: "rule-type-1",
|
||||
attribute: "currency_code",
|
||||
value: [],
|
||||
},
|
||||
{
|
||||
id: "price-list-rule-2",
|
||||
price_list_id: "price-list-1",
|
||||
rule_type_id: "rule-type-2",
|
||||
attribute: "region_id",
|
||||
value: [],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -6,7 +6,7 @@ export const defaultPriceRuleData = [
|
||||
{
|
||||
id: "price-rule-1",
|
||||
price_set_id: "price-set-1",
|
||||
rule_type_id: "rule-type-1",
|
||||
attribute: "currency_code",
|
||||
value: "USD",
|
||||
price_list_id: "test",
|
||||
price_id: "price-set-money-amount-USD",
|
||||
@@ -14,7 +14,7 @@ export const defaultPriceRuleData = [
|
||||
{
|
||||
id: "price-rule-2",
|
||||
price_set_id: "price-set-2",
|
||||
rule_type_id: "rule-type-2",
|
||||
attribute: "region_id",
|
||||
value: "region_1",
|
||||
price_list_id: "test",
|
||||
price_id: "price-set-money-amount-EUR",
|
||||
|
||||
@@ -16,7 +16,7 @@ export async function createPriceRules(
|
||||
const priceRuleDataClone: CreatePriceRuleDTO = { ...priceRuleData }
|
||||
|
||||
priceRuleDataClone.price_set_id = priceRuleDataClone.price_set_id
|
||||
priceRuleDataClone.rule_type_id = priceRuleDataClone.rule_type_id
|
||||
priceRuleDataClone.attribute = priceRuleDataClone.attribute
|
||||
priceRuleDataClone.price_id = priceRuleDataClone.price_id
|
||||
|
||||
const priceRule = manager.create(PriceRule, priceRuleDataClone)
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
export const defaultRuleTypesData = [
|
||||
{
|
||||
id: "rule-type-1",
|
||||
name: "rule 1",
|
||||
rule_attribute: "currency_code",
|
||||
},
|
||||
{
|
||||
id: "rule-type-2",
|
||||
name: "rule 2",
|
||||
rule_attribute: "region_id",
|
||||
},
|
||||
]
|
||||
@@ -1,22 +0,0 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { RuleType } from "@models"
|
||||
import { defaultRuleTypesData } from "./data"
|
||||
|
||||
export * from "./data"
|
||||
|
||||
export async function createRuleTypes(
|
||||
manager: SqlEntityManager,
|
||||
ruletypesData: any[] = defaultRuleTypesData
|
||||
): Promise<RuleType[]> {
|
||||
const ruleTypes: RuleType[] = []
|
||||
|
||||
for (let ruleTypeData of ruletypesData) {
|
||||
const ruleType = manager.create(RuleType, ruleTypeData)
|
||||
|
||||
ruleTypes.push(ruleType)
|
||||
}
|
||||
|
||||
await manager.persistAndFlush(ruleTypes)
|
||||
|
||||
return ruleTypes
|
||||
}
|
||||
@@ -2,7 +2,6 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { createPrices, defaultPricesData } from "./price"
|
||||
import { createPriceRules, defaultPriceRuleData } from "./price-rule"
|
||||
import { createPriceSets, defaultPriceSetsData } from "./price-set"
|
||||
import { createRuleTypes, defaultRuleTypesData } from "./rule-type"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -12,11 +11,9 @@ export async function seedPriceData(
|
||||
priceSetsData = defaultPriceSetsData,
|
||||
priceRuleData = defaultPriceRuleData,
|
||||
pricesData = defaultPricesData,
|
||||
ruleTypesData = defaultRuleTypesData,
|
||||
} = {}
|
||||
) {
|
||||
await createPriceSets(testManager, priceSetsData)
|
||||
await createPrices(testManager, pricesData)
|
||||
await createRuleTypes(testManager, ruleTypesData)
|
||||
await createPriceRules(testManager, priceRuleData)
|
||||
}
|
||||
|
||||
+13
-35
@@ -154,32 +154,11 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
},
|
||||
]
|
||||
|
||||
const ruleTypesData = [
|
||||
{
|
||||
id: "rule-type-company_id",
|
||||
name: "rule type company id",
|
||||
rule_attribute: "company_id",
|
||||
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-company_id-EUR",
|
||||
price_set_id: "price-set-EUR",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
attribute: "company_id",
|
||||
value: "EUR",
|
||||
price_list_id: "test",
|
||||
price_id: "price-company_id-EUR",
|
||||
@@ -187,7 +166,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-company_id-PLN",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
attribute: "company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_id: "price-company_id-PLN",
|
||||
@@ -195,7 +174,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id-PLN",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-region_id",
|
||||
attribute: "region_id",
|
||||
value: "PL",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id-PLN",
|
||||
@@ -203,7 +182,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id+company_id-PL",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-region_id",
|
||||
attribute: "region_id",
|
||||
value: "PL",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id+company_id-PLN",
|
||||
@@ -211,7 +190,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id+company_id-medusa-company-id",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
attribute: "company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id+company_id-PLN",
|
||||
@@ -219,7 +198,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id-PLN-5-qty",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-region_id",
|
||||
attribute: "region_id",
|
||||
value: "PL",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id-PLN-5-qty",
|
||||
@@ -227,7 +206,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id-company_id-PL",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-region_id",
|
||||
attribute: "region_id",
|
||||
value: "PL",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id_company_id-PL-EUR",
|
||||
@@ -235,7 +214,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id-company_id-PLN",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
attribute: "company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id_company_id-PL-EUR",
|
||||
@@ -243,7 +222,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id-company_id-PL-4-qty",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-region_id",
|
||||
attribute: "region_id",
|
||||
value: "PL",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id_company_id-PL-EUR-4-qty",
|
||||
@@ -251,7 +230,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id-company_id-PLN-4-qty",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
attribute: "company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id_company_id-PL-EUR-4-qty",
|
||||
@@ -259,7 +238,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id-currency_customer_group_code-PL",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-region_id",
|
||||
attribute: "region_id",
|
||||
value: "PL",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id_company_id-PL-EUR-customer-group",
|
||||
@@ -267,7 +246,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
id: "price-rule-region_id-currency_customer_group_code-PLN",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
attribute: "company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id_company_id-PL-EUR-customer-group",
|
||||
@@ -275,7 +254,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
{
|
||||
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",
|
||||
attribute: "customer_group_id",
|
||||
value: "test-customer-group",
|
||||
price_list_id: "test",
|
||||
price_id: "price-region_id_company_id-PL-EUR-customer-group",
|
||||
@@ -286,7 +265,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
priceSetsData,
|
||||
pricesData,
|
||||
priceRuleData,
|
||||
ruleTypesData,
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
+7
-36
@@ -3,7 +3,6 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { createPriceLists } from "../../../__fixtures__/price-list"
|
||||
import { createPriceListRules } from "../../../__fixtures__/price-list-rules"
|
||||
import { createRuleTypes } from "../../../__fixtures__/rule-type"
|
||||
import { Modules } from "@medusajs/utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
@@ -15,7 +14,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
let testManager: SqlEntityManager
|
||||
beforeEach(async () => {
|
||||
testManager = await MikroOrmWrapper.forkManager()
|
||||
await createRuleTypes(testManager)
|
||||
await createPriceLists(testManager)
|
||||
await createPriceListRules(testManager)
|
||||
})
|
||||
@@ -166,14 +164,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
|
||||
describe("setPriceListRules", () => {
|
||||
it("should add a price list rule to a price list", async () => {
|
||||
await createRuleTypes(testManager, [
|
||||
{
|
||||
id: "rule-type-3",
|
||||
name: "test",
|
||||
rule_attribute: "sales_channel",
|
||||
},
|
||||
])
|
||||
|
||||
await service.setPriceListRules({
|
||||
price_list_id: "price-list-1",
|
||||
rules: {
|
||||
@@ -184,34 +174,21 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
const [priceList] = await service.listPriceLists(
|
||||
{ id: ["price-list-1"] },
|
||||
{
|
||||
relations: [
|
||||
"price_list_rules",
|
||||
"price_list_rules.price_list_rule_values",
|
||||
],
|
||||
relations: ["price_list_rules"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(priceList.price_list_rules).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
rule_type: { id: "rule-type-3" },
|
||||
price_list_rule_values: [
|
||||
expect.objectContaining({ value: "sc-1" }),
|
||||
],
|
||||
attribute: "sales_channel",
|
||||
value: "sc-1",
|
||||
}),
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("should multiple priceListRules to a priceList", async () => {
|
||||
await createRuleTypes(testManager, [
|
||||
{
|
||||
id: "rule-type-3",
|
||||
name: "test",
|
||||
rule_attribute: "sales_channel",
|
||||
},
|
||||
])
|
||||
|
||||
await service.setPriceListRules({
|
||||
price_list_id: "price-list-1",
|
||||
rules: {
|
||||
@@ -224,21 +201,15 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
id: ["price-list-1"],
|
||||
},
|
||||
{
|
||||
relations: [
|
||||
"price_list_rules",
|
||||
"price_list_rules.price_list_rule_values",
|
||||
],
|
||||
relations: ["price_list_rules"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(priceList.price_list_rules).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
rule_type: { id: "rule-type-3" },
|
||||
price_list_rule_values: expect.arrayContaining([
|
||||
expect.objectContaining({ value: "sc-1" }),
|
||||
expect.objectContaining({ value: "sc-2" }),
|
||||
]),
|
||||
attribute: "sales_channel",
|
||||
value: ["sc-1", "sc-2"],
|
||||
}),
|
||||
])
|
||||
)
|
||||
@@ -262,7 +233,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
)
|
||||
|
||||
expect(priceList.price_list_rules).toEqual([
|
||||
expect.objectContaining({ rule_type: { id: "rule-type-2" } }),
|
||||
expect.objectContaining({ attribute: "region_id" }),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
+41
-163
@@ -32,16 +32,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
await createPriceSets(testManager)
|
||||
await createPriceLists(testManager)
|
||||
await service.createRuleTypes([
|
||||
{
|
||||
name: "Region ID",
|
||||
rule_attribute: "region_id",
|
||||
},
|
||||
{
|
||||
name: "Customer Group ID",
|
||||
rule_attribute: "customer_group_id",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
@@ -287,20 +277,15 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
const [priceList] = await service.listPriceLists(
|
||||
{ id: [createdId] },
|
||||
{
|
||||
relations: [
|
||||
"prices",
|
||||
"prices.price_set",
|
||||
"price_list_rules.price_list_rule_values",
|
||||
"price_list_rules.rule_type",
|
||||
],
|
||||
relations: ["prices", "price_list_rules"],
|
||||
select: [
|
||||
"id",
|
||||
"starts_at",
|
||||
"prices.amount",
|
||||
"prices.currency_code",
|
||||
"prices.price_list_id",
|
||||
"price_list_rules.price_list_rule_values.value",
|
||||
"price_list_rules.rule_type.rule_attribute",
|
||||
"price_list_rules.value",
|
||||
"price_list_rules.attribute",
|
||||
],
|
||||
}
|
||||
)
|
||||
@@ -317,17 +302,8 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
]),
|
||||
price_list_rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_type: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_attribute: "new_rule",
|
||||
}),
|
||||
price_list_rule_values: [
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: "new-rule-value",
|
||||
}),
|
||||
],
|
||||
attribute: "new_rule",
|
||||
value: ["new-rule-value"],
|
||||
}),
|
||||
]),
|
||||
})
|
||||
@@ -349,7 +325,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'PriceList with id "does-not-exist" not found'
|
||||
"Price lists with ids: 'does-not-exist' not found"
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -439,19 +415,14 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
id: [created.id],
|
||||
},
|
||||
{
|
||||
relations: [
|
||||
"prices",
|
||||
"prices.price_set",
|
||||
"price_list_rules.price_list_rule_values",
|
||||
"price_list_rules.rule_type",
|
||||
],
|
||||
relations: ["prices", "prices.price_set", "price_list_rules"],
|
||||
select: [
|
||||
"id",
|
||||
"prices.amount",
|
||||
"prices.currency_code",
|
||||
"prices.price_list_id",
|
||||
"price_list_rules.price_list_rule_values.value",
|
||||
"price_list_rules.rule_type.rule_attribute",
|
||||
"price_list_rules.value",
|
||||
"price_list_rules.attribute",
|
||||
],
|
||||
}
|
||||
)
|
||||
@@ -468,37 +439,16 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
price_list_rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_type: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_attribute: "customer_group_id",
|
||||
}),
|
||||
price_list_rule_values: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: "vip-customer-group-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: "another-vip-customer-group-id",
|
||||
}),
|
||||
]),
|
||||
attribute: "customer_group_id",
|
||||
value: [
|
||||
"vip-customer-group-id",
|
||||
"another-vip-customer-group-id",
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_type: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
price_list_rule_values: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: "DE",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: "DK",
|
||||
}),
|
||||
]),
|
||||
attribute: "region_id",
|
||||
value: ["DE", "DK"],
|
||||
}),
|
||||
]),
|
||||
})
|
||||
@@ -581,8 +531,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
"prices",
|
||||
"prices.price_set",
|
||||
"prices.price_rules",
|
||||
"price_list_rules.price_list_rule_values",
|
||||
"price_list_rules.rule_type",
|
||||
"price_list_rules",
|
||||
],
|
||||
select: [
|
||||
"id",
|
||||
@@ -591,8 +540,8 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
"prices.amount",
|
||||
"prices.currency_code",
|
||||
"prices.price_list_id",
|
||||
"price_list_rules.price_list_rule_values.value",
|
||||
"price_list_rules.rule_type.rule_attribute",
|
||||
"price_list_rules.value",
|
||||
"price_list_rules.attribute",
|
||||
],
|
||||
}
|
||||
)
|
||||
@@ -622,72 +571,21 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
price_list_rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_type: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_attribute: "customer_group_id",
|
||||
}),
|
||||
price_list_rule_values: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: "vip-customer-group-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: "another-vip-customer-group-id",
|
||||
}),
|
||||
]),
|
||||
attribute: "customer_group_id",
|
||||
value: [
|
||||
"vip-customer-group-id",
|
||||
"another-vip-customer-group-id",
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_type: expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
price_list_rule_values: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: "DE",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
value: "DK",
|
||||
}),
|
||||
]),
|
||||
attribute: "region_id",
|
||||
value: ["DE", "DK"],
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw error when rule type does not exist", async () => {
|
||||
const error = await service
|
||||
.createPriceLists([
|
||||
{
|
||||
title: "test",
|
||||
description: "test",
|
||||
rules: {
|
||||
region_id: ["DE", "DK"],
|
||||
missing_1: ["test-missing-1"],
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
amount: 400,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "price-set-1",
|
||||
rules: {
|
||||
region_id: "DE",
|
||||
missing_2: "test-missing-2",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"Cannot find RuleTypes with rule_attribute - missing_1, missing_2"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("addPriceListPrices", () => {
|
||||
@@ -714,8 +612,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
"prices",
|
||||
"prices.price_set",
|
||||
"prices.price_rules",
|
||||
"price_list_rules.price_list_rule_values",
|
||||
"price_list_rules.rule_type",
|
||||
"price_list_rules",
|
||||
],
|
||||
select: [
|
||||
"id",
|
||||
@@ -724,8 +621,8 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
"prices.amount",
|
||||
"prices.currency_code",
|
||||
"prices.price_list_id",
|
||||
"price_list_rules.price_list_rule_values.value",
|
||||
"price_list_rules.rule_type.rule_attribute",
|
||||
"price_list_rules.value",
|
||||
"price_list_rules.attribute",
|
||||
],
|
||||
}
|
||||
)
|
||||
@@ -746,13 +643,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
})
|
||||
|
||||
it("should add a price with rules to a priceList successfully", async () => {
|
||||
await service.createRuleTypes([
|
||||
{
|
||||
name: "region_id",
|
||||
rule_attribute: "region_id",
|
||||
},
|
||||
])
|
||||
|
||||
await service.addPriceListPrices([
|
||||
{
|
||||
price_list_id: "price-list-1",
|
||||
@@ -778,20 +668,18 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
"prices",
|
||||
"prices.price_set",
|
||||
"prices.price_rules",
|
||||
"prices.price_rules.rule_type",
|
||||
"price_list_rules.price_list_rule_values",
|
||||
"price_list_rules.rule_type",
|
||||
"price_list_rules",
|
||||
],
|
||||
select: [
|
||||
"id",
|
||||
"prices.price_rules.value",
|
||||
"prices.price_rules.rule_type.rule_attribute",
|
||||
"prices.price_rules.attribute",
|
||||
"prices.rules_count",
|
||||
"prices.amount",
|
||||
"prices.currency_code",
|
||||
"prices.price_list_id",
|
||||
"price_list_rules.price_list_rule_values.value",
|
||||
"price_list_rules.rule_type.rule_attribute",
|
||||
"price_list_rules.value",
|
||||
"price_list_rules.attribute",
|
||||
],
|
||||
}
|
||||
)
|
||||
@@ -805,9 +693,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
price_rules: [
|
||||
expect.objectContaining({
|
||||
value: "EU",
|
||||
rule_type: expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
attribute: "region_id",
|
||||
}),
|
||||
],
|
||||
amount: 123,
|
||||
@@ -884,20 +770,18 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
"prices",
|
||||
"prices.price_set",
|
||||
"prices.price_rules",
|
||||
"prices.price_rules.rule_type",
|
||||
"price_list_rules.price_list_rule_values",
|
||||
"price_list_rules.rule_type",
|
||||
"price_list_rules",
|
||||
],
|
||||
select: [
|
||||
"id",
|
||||
"prices.price_rules.value",
|
||||
"prices.price_rules.rule_type.rule_attribute",
|
||||
"prices.price_rules.attribute",
|
||||
"prices.rules_count",
|
||||
"prices.amount",
|
||||
"prices.currency_code",
|
||||
"prices.price_list_id",
|
||||
"price_list_rules.price_list_rule_values.value",
|
||||
"price_list_rules.rule_type.rule_attribute",
|
||||
"price_list_rules.value",
|
||||
"price_list_rules.attribute",
|
||||
],
|
||||
}
|
||||
)
|
||||
@@ -911,15 +795,11 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
price_rules: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
value: "new test",
|
||||
rule_type: expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
attribute: "region_id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
value: "new test",
|
||||
rule_type: expect.objectContaining({
|
||||
rule_attribute: "customer_group_id",
|
||||
}),
|
||||
attribute: "customer_group_id",
|
||||
}),
|
||||
]),
|
||||
amount: 123,
|
||||
@@ -934,9 +814,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
|
||||
describe("removePrices", () => {
|
||||
it("should remove prices from a priceList successfully", async () => {
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{ rules: [{ rule_attribute: "region_id" }] },
|
||||
])
|
||||
const [priceSet] = await service.createPriceSets([{}])
|
||||
|
||||
await service.addPriceListPrices([
|
||||
{
|
||||
|
||||
+11
-13
@@ -5,7 +5,6 @@ import { Price } from "../../../../src/models"
|
||||
import { createPrices } from "../../../__fixtures__/price"
|
||||
import { createPriceRules } from "../../../__fixtures__/price-rule"
|
||||
import { createPriceSets } from "../../../__fixtures__/price-set"
|
||||
import { createRuleTypes } from "../../../__fixtures__/rule-type"
|
||||
import { Modules } from "@medusajs/utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
@@ -19,7 +18,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
await createPriceSets(testManager)
|
||||
await createRuleTypes(testManager)
|
||||
await createPrices(testManager)
|
||||
await createPriceRules(testManager)
|
||||
})
|
||||
@@ -57,8 +55,8 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
id: ["price-rule-1"],
|
||||
},
|
||||
{
|
||||
select: ["id", "price_set.id"],
|
||||
relations: ["price_set"],
|
||||
select: ["id", "price.id"],
|
||||
relations: ["price"],
|
||||
}
|
||||
)
|
||||
|
||||
@@ -67,10 +65,10 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
expect(serialized).toEqual([
|
||||
{
|
||||
id: "price-rule-1",
|
||||
price_set: {
|
||||
id: "price-set-1",
|
||||
price: {
|
||||
id: "price-set-money-amount-USD",
|
||||
},
|
||||
price_set_id: "price-set-1",
|
||||
price_id: "price-set-money-amount-USD",
|
||||
},
|
||||
])
|
||||
})
|
||||
@@ -112,8 +110,8 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
id: ["price-rule-1"],
|
||||
},
|
||||
{
|
||||
select: ["id", "price_set.id"],
|
||||
relations: ["price_set"],
|
||||
select: ["id", "price.id"],
|
||||
relations: ["price"],
|
||||
}
|
||||
)
|
||||
|
||||
@@ -123,10 +121,10 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
expect(serialized).toEqual([
|
||||
{
|
||||
id: "price-rule-1",
|
||||
price_set: {
|
||||
id: "price-set-1",
|
||||
price: {
|
||||
id: "price-set-money-amount-USD",
|
||||
},
|
||||
price_set_id: "price-set-1",
|
||||
price_id: "price-set-money-amount-USD",
|
||||
},
|
||||
])
|
||||
})
|
||||
@@ -283,7 +281,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
await service.createPriceRules({
|
||||
id: "price-rule-new",
|
||||
price_set_id: "price-set-1",
|
||||
rule_type_id: "rule-type-1",
|
||||
attribute: "region_id",
|
||||
value: "region_1",
|
||||
price_list_id: "test",
|
||||
price_id: price.id,
|
||||
|
||||
+2
-83
@@ -1,39 +1,18 @@
|
||||
import {
|
||||
CreatePriceSetDTO,
|
||||
CreatePriceSetRuleTypeDTO,
|
||||
IPricingModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { CreatePriceSetDTO, IPricingModuleService } from "@medusajs/types"
|
||||
import {
|
||||
CommonEvents,
|
||||
composeMessage,
|
||||
Modules,
|
||||
PricingEvents,
|
||||
} from "@medusajs/utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
MockEventBusService,
|
||||
moduleIntegrationTestRunner,
|
||||
} from "medusa-test-utils"
|
||||
import { PriceSetRuleType } from "../../../../src/models"
|
||||
import { seedPriceData } from "../../../__fixtures__/seed-price-data"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
async function createPriceSetPriceRules(
|
||||
manager: SqlEntityManager,
|
||||
priceSetRulesData: CreatePriceSetRuleTypeDTO[]
|
||||
): Promise<void> {
|
||||
const priceSetRules: PriceSetRuleType[] = []
|
||||
|
||||
for (let priceSetRuleData of priceSetRulesData) {
|
||||
const priceRule = manager.create(PriceSetRuleType, priceSetRuleData as any)
|
||||
|
||||
priceSetRules.push(priceRule)
|
||||
}
|
||||
|
||||
await manager.persistAndFlush(priceSetRules)
|
||||
}
|
||||
|
||||
moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
moduleName: Modules.PRICING,
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
@@ -51,16 +30,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
beforeEach(async () => {
|
||||
const testManager = await MikroOrmWrapper.forkManager()
|
||||
await seedPriceData(testManager)
|
||||
await createPriceSetPriceRules(testManager, [
|
||||
{
|
||||
price_set_id: "price-set-1",
|
||||
rule_type_id: "rule-type-1",
|
||||
},
|
||||
{
|
||||
price_set_id: "price-set-2",
|
||||
rule_type_id: "rule-type-2",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
@@ -346,32 +315,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should fail to create a price set with rule types and money amounts with rule types that don't exits", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
currency_code: "USD",
|
||||
rules: {
|
||||
city: "Berlin",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
expect(error.message).toEqual(
|
||||
"Rule types don't exist for prices with rule attribute: city"
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a price set with rule types and money amounts", async () => {
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{
|
||||
@@ -399,10 +342,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
})
|
||||
)
|
||||
|
||||
const [priceRules] = await service.listPriceRules({
|
||||
price_set_id: [priceSet.id],
|
||||
})
|
||||
|
||||
const events = eventBusEmitSpy.mock.calls[0][0]
|
||||
expect(events).toHaveLength(3)
|
||||
expect(events[0]).toEqual(
|
||||
@@ -429,7 +368,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
action: CommonEvents.CREATED,
|
||||
object: "price_rule",
|
||||
data: {
|
||||
id: priceRules.id,
|
||||
id: (priceSet as any).prices![0].price_rules[0].id,
|
||||
},
|
||||
})
|
||||
)
|
||||
@@ -591,26 +530,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should fail with an appropriate error when trying to add a price with rule that doesn't exist", async () => {
|
||||
let error
|
||||
try {
|
||||
await service.addPrices({
|
||||
priceSetId: "price-set-1",
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
currency_code: "USD",
|
||||
rules: { city: "Paris" },
|
||||
},
|
||||
],
|
||||
})
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("Rule types don't exist for: city")
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
-239
@@ -1,239 +0,0 @@
|
||||
import { createRuleTypes } from "../../../__fixtures__/rule-type"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
moduleName: Modules.PRICING,
|
||||
testSuite: ({ MikroOrmWrapper, service }) => {
|
||||
describe("PricingModuleService ruleType", () => {
|
||||
beforeEach(async () => {
|
||||
const testManager = MikroOrmWrapper.forkManager()
|
||||
await createRuleTypes(testManager)
|
||||
})
|
||||
|
||||
describe("listRuleTypes", () => {
|
||||
it("should list rule types", async () => {
|
||||
const ruleTypeResult = await service.listRuleTypes()
|
||||
|
||||
expect(ruleTypeResult).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "rule-type-1",
|
||||
name: "rule 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "rule-type-2",
|
||||
name: "rule 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list rule types by id", async () => {
|
||||
const ruleTypeResult = await service.listRuleTypes({
|
||||
id: ["rule-type-1"],
|
||||
})
|
||||
|
||||
expect(ruleTypeResult).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "rule-type-1",
|
||||
name: "rule 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountRuleTypes", () => {
|
||||
it("should return rule types and count", async () => {
|
||||
const [ruleTypeResult, count] = await service.listAndCountRuleTypes()
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(ruleTypeResult).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "rule-type-1",
|
||||
name: "rule 1",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "rule-type-2",
|
||||
name: "rule 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return rule types and count when filtered", async () => {
|
||||
const [ruleTypeResult, count] = await service.listAndCountRuleTypes({
|
||||
id: ["rule-type-1"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(ruleTypeResult).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "rule-type-1",
|
||||
name: "rule 1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return rule types and count when using skip and take", async () => {
|
||||
const [ruleTypeResult, count] = await service.listAndCountRuleTypes(
|
||||
{},
|
||||
{ skip: 1, take: 1 }
|
||||
)
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(ruleTypeResult).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "rule-type-2",
|
||||
name: "rule 2",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [ruleTypeResult, count] = await service.listAndCountRuleTypes(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["name"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(ruleTypeResult))
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(serialized).toEqual([
|
||||
{
|
||||
id: "rule-type-1",
|
||||
name: "rule 1",
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveRuleType", () => {
|
||||
it("should return ruleType for the given id", async () => {
|
||||
const ruleType = await service.retrieveRuleType("rule-type-1")
|
||||
|
||||
expect(ruleType).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "rule-type-1",
|
||||
name: "rule 1",
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when ruleType with id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieveRuleType("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"RuleType 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.retrieveRuleType(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("ruleType - id must be defined")
|
||||
})
|
||||
|
||||
it("should return ruleType based on config select param", async () => {
|
||||
const ruleTypeResult = await service.retrieveRuleType("rule-type-1", {
|
||||
select: ["name"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(ruleTypeResult))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
name: "rule 1",
|
||||
id: "rule-type-1",
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteRuleTypes", () => {
|
||||
const id = "rule-type-1"
|
||||
|
||||
it("should delete the ruleTypes given an id successfully", async () => {
|
||||
await service.deleteRuleTypes([id])
|
||||
|
||||
const currencies = await service.listRuleTypes({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(currencies).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateRuleTypes", () => {
|
||||
const id = "rule-type-1"
|
||||
|
||||
it("should update the name of the ruleType successfully", async () => {
|
||||
await service.updateRuleTypes([
|
||||
{
|
||||
id,
|
||||
name: "rule 3",
|
||||
},
|
||||
])
|
||||
|
||||
const ruletype = await service.retrieveRuleType(id)
|
||||
|
||||
expect(ruletype.name).toEqual("rule 3")
|
||||
})
|
||||
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.updateRuleTypes([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
name: "rule 3",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'RuleType with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createRuleTypes", () => {
|
||||
it("should create a ruleType successfully", async () => {
|
||||
await service.createRuleTypes([
|
||||
{
|
||||
name: "Test Rule",
|
||||
rule_attribute: "region_id",
|
||||
},
|
||||
])
|
||||
|
||||
const [ruleType] = await service.listRuleTypes({
|
||||
name: ["Test Rule"],
|
||||
})
|
||||
|
||||
expect(ruleType).toEqual(
|
||||
expect.objectContaining({
|
||||
name: "Test Rule",
|
||||
rule_attribute: "region_id",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user