feat(pricing,types): addPrice and removePricreRules APIs (#5237)
* 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 * add migration for price_set_rule_type table * update types * initial add * update tests * working create with rule-types * initial testing for money amount creation * add price set money amount repo and service * fix broken build * create price list with prices and rule types * create price set with info not being rules and money amounts * create price set initial working implementation * chore: introduce group by util + no queries on empty context * addPrices * remove comments * Feat/pricing module methods (#5218) chore: add removePrices to pricing module * fix broken integration test * Revert "Feat/pricing module methods (#5218)" (#5236) This reverts commit 95c8aaa66423d290a35b6e736e5b187e12d44a36. * feat(types,pricing): remove prices from a price set (#5235) feat(types,pricing): remove prices from a price set * add addRules * typing * add validation of price set ids for addRules * add rule_attribute check for addRules method * chore: review changes * chore: update schema * chore: first part of reviews * chore: reset migration * remove unnecessary init * update interface * use persist not persistAndflush * chore: added money amount scoping * chore: update schema * fix * fix 2 * add default pricing * addPrices * create * update pricing service interface * chore: rename money amounts to prices * chore: cleanup + changelog * chore: update package.json * chore: cleanup persistAndFlushes from services * jsdoc * chore: add js docs for price rules * chore: added js doc for psma and rule types * chore: added jsdoc for currencies * more jsdocs * jsdoc money amounts * chore: move jsdoc to interface * chore: remove persist and flush to persist * change overload --------- 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:
co-authored by
Oli Juhl
Philip Korsholm
Philip Korsholm
Adrien de Peretti
parent
cf18211848
commit
c5703a4765
@@ -9,6 +9,7 @@ import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
|
||||
import { createPriceRules } from "../../../__fixtures__/price-rule"
|
||||
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"
|
||||
|
||||
@@ -31,12 +32,12 @@ describe("PriceRule Service", () => {
|
||||
service = new PriceRuleService({
|
||||
priceRuleRepository: priceRuleRepository,
|
||||
})
|
||||
|
||||
await createCurrencies(testManager)
|
||||
await createMoneyAmounts(testManager)
|
||||
await createRuleTypes(testManager)
|
||||
await createPriceSets(testManager)
|
||||
await createRuleTypes(testManager)
|
||||
await createPriceSetMoneyAmounts(testManager)
|
||||
await createPriceSetMoneyAmountRules(testManager)
|
||||
await createPriceRules(testManager)
|
||||
})
|
||||
|
||||
|
||||
+2
-12
@@ -3,12 +3,7 @@ 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 { seedPriceData } from "../../../__fixtures__/seed-price-data"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
@@ -33,12 +28,7 @@ describe("PriceSetMoneyAmountRules Service", () => {
|
||||
|
||||
testManager = await MikroOrmWrapper.forkManager()
|
||||
|
||||
await createCurrencies(testManager)
|
||||
await createMoneyAmounts(testManager)
|
||||
await createPriceSets(testManager)
|
||||
await createRuleTypes(testManager)
|
||||
await createPriceSetMoneyAmounts(testManager)
|
||||
await createPriceSetMoneyAmountRules(testManager)
|
||||
await seedPriceData(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -31,15 +31,22 @@ describe("PriceSet Service", () => {
|
||||
const priceSetInputData = [
|
||||
{
|
||||
id: "price-set-1",
|
||||
money_amounts: [{ id: "money-amount-USD" }],
|
||||
prices: [
|
||||
{
|
||||
id: "money-amount-USD",
|
||||
currency_code: "EUR",
|
||||
amount: 100,
|
||||
rules: {},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "price-set-2",
|
||||
money_amounts: [],
|
||||
prices: [],
|
||||
},
|
||||
{
|
||||
id: "price-set-3",
|
||||
money_amounts: [],
|
||||
prices: [],
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
+209
-189
@@ -7,53 +7,13 @@ 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"
|
||||
|
||||
import { seedPriceData } from "../../../__fixtures__/seed-price-data"
|
||||
|
||||
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
|
||||
@@ -95,14 +55,21 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
|
||||
const moneyAmountsData = [
|
||||
{
|
||||
id: "money-amount-currency_code-EUR",
|
||||
id: "money-amount-PLN",
|
||||
currency_code: "PLN",
|
||||
amount: 1000,
|
||||
min_quantity: 1,
|
||||
max_quantity: 10,
|
||||
},
|
||||
{
|
||||
id: "money-amount-company_id-EUR",
|
||||
currency_code: "EUR",
|
||||
amount: 500,
|
||||
min_quantity: 1,
|
||||
max_quantity: 10,
|
||||
},
|
||||
{
|
||||
id: "money-amount-currency_code-PLN",
|
||||
id: "money-amount-company_id-PLN",
|
||||
currency_code: "PLN",
|
||||
amount: 400,
|
||||
min_quantity: 1,
|
||||
@@ -115,6 +82,20 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
min_quantity: 1,
|
||||
max_quantity: 4,
|
||||
},
|
||||
{
|
||||
id: "money-amount-region_id+company_id-PLN",
|
||||
currency_code: "PLN",
|
||||
amount: 999,
|
||||
min_quantity: 1,
|
||||
max_quantity: 10,
|
||||
},
|
||||
{
|
||||
id: "money-amount-region_id-PLN-5-qty",
|
||||
currency_code: "PLN",
|
||||
amount: 250,
|
||||
min_quantity: 4,
|
||||
max_quantity: 10,
|
||||
},
|
||||
{
|
||||
id: "money-amount-region_id-PL-EUR",
|
||||
currency_code: "EUR",
|
||||
@@ -149,17 +130,24 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
|
||||
const priceSetMoneyAmountsData = [
|
||||
{
|
||||
id: "psma-currency_code-EUR",
|
||||
title: "psma EUR - currency_code",
|
||||
id: "psma-PLN",
|
||||
title: "psma PLN",
|
||||
price_set: "price-set-PLN",
|
||||
money_amount: "money-amount-PLN",
|
||||
number_rules: 0,
|
||||
},
|
||||
{
|
||||
id: "psma-company_id-EUR",
|
||||
title: "psma EUR - company_id",
|
||||
price_set: "price-set-EUR",
|
||||
money_amount: "money-amount-currency_code-EUR",
|
||||
money_amount: "money-amount-company_id-EUR",
|
||||
number_rules: 1,
|
||||
},
|
||||
{
|
||||
id: "psma-currency_code-PLN",
|
||||
title: "psma PLN - currency_code",
|
||||
id: "psma-company_id-PLN",
|
||||
title: "psma PLN - company_id",
|
||||
price_set: "price-set-PLN",
|
||||
money_amount: "money-amount-currency_code-PLN",
|
||||
money_amount: "money-amount-company_id-PLN",
|
||||
number_rules: 1,
|
||||
},
|
||||
{
|
||||
@@ -170,21 +158,35 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
number_rules: 1,
|
||||
},
|
||||
{
|
||||
id: "psma-region_id_currency_code-PL-EUR",
|
||||
id: "psma-region_id+company_id-PLN",
|
||||
title: "psma region_id + company_id",
|
||||
price_set: "price-set-PLN",
|
||||
money_amount: "money-amount-region_id+company_id-PLN",
|
||||
number_rules: 2,
|
||||
},
|
||||
{
|
||||
id: "psma-region_id-PLN-5-qty",
|
||||
title: "psma PLN - region_id 5 qty",
|
||||
price_set: "price-set-PLN",
|
||||
money_amount: "money-amount-region_id-PLN-5-qty",
|
||||
number_rules: 1,
|
||||
},
|
||||
{
|
||||
id: "psma-region_id_company_id-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",
|
||||
id: "psma-region_id_company_id-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",
|
||||
id: "psma-region_id_company_id-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",
|
||||
@@ -194,9 +196,9 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
|
||||
const ruleTypesData = [
|
||||
{
|
||||
id: "rule-type-currency_code",
|
||||
name: "rule type currency code",
|
||||
rule_attribute: "currency_code",
|
||||
id: "rule-type-company_id",
|
||||
name: "rule type company id",
|
||||
rule_attribute: "company_id",
|
||||
default_priority: 2,
|
||||
},
|
||||
{
|
||||
@@ -215,20 +217,20 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
|
||||
const priceRuleData = [
|
||||
{
|
||||
id: "price-rule-currency_code-EUR",
|
||||
id: "price-rule-company_id-EUR",
|
||||
price_set_id: "price-set-EUR",
|
||||
rule_type_id: "rule-type-currency_code",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
value: "EUR",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount_id: "psma-currency_code-EUR",
|
||||
price_set_money_amount_id: "psma-company_id-EUR",
|
||||
},
|
||||
{
|
||||
id: "price-rule-currency_code-PLN",
|
||||
id: "price-rule-company_id-PLN",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-currency_code",
|
||||
value: "PLN",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount_id: "psma-currency_code-PLN",
|
||||
price_set_money_amount_id: "psma-company_id-PLN",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-PLN",
|
||||
@@ -239,38 +241,60 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
price_set_money_amount_id: "psma-region_id-PLN",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-currency_code-PL",
|
||||
id: "price-rule-region_id+company_id-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",
|
||||
price_set_money_amount_id: "psma-region_id+company_id-PLN",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-currency_code-PLN",
|
||||
id: "price-rule-region_id+company_id-medusa-company-id",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-currency_code",
|
||||
value: "EUR",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount_id: "psma-region_id_currency_code-PL-EUR",
|
||||
price_set_money_amount_id: "psma-region_id+company_id-PLN",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-currency_code-PL-4-qty",
|
||||
id: "price-rule-region_id-PLN-5-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",
|
||||
price_set_money_amount_id: "psma-region_id-PLN-5-qty",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-currency_code-PLN-4-qty",
|
||||
id: "price-rule-region_id-company_id-PL",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-currency_code",
|
||||
value: "EUR",
|
||||
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",
|
||||
price_set_money_amount_id: "psma-region_id_company_id-PL-EUR",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-company_id-PLN",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount_id: "psma-region_id_company_id-PL-EUR",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-company_id-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_company_id-PL-EUR-4-qty",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-company_id-PLN-4-qty",
|
||||
price_set_id: "price-set-PLN",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount_id: "psma-region_id_company_id-PL-EUR-4-qty",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-currency_customer_group_code-PL",
|
||||
@@ -279,16 +303,16 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
value: "PL",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount_id:
|
||||
"psma-region_id_currency_code-PL-EUR-customer-group",
|
||||
"psma-region_id_company_id-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",
|
||||
rule_type_id: "rule-type-company_id",
|
||||
value: "medusa-company-id",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount_id:
|
||||
"psma-region_id_currency_code-PL-EUR-customer-group",
|
||||
"psma-region_id_company_id-PL-EUR-customer-group",
|
||||
},
|
||||
{
|
||||
id: "price-rule-region_id-currency_customer_group_code-test_customer_group",
|
||||
@@ -297,67 +321,64 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
value: "test-customer-group",
|
||||
price_list_id: "test",
|
||||
price_set_money_amount_id:
|
||||
"psma-region_id_currency_code-PL-EUR-customer-group",
|
||||
"psma-region_id_company_id-PL-EUR-customer-group",
|
||||
},
|
||||
] as unknown as CreatePriceRuleDTO[]
|
||||
|
||||
await seedData({
|
||||
await seedPriceData(MikroOrmWrapper.forkManager(), {
|
||||
currencyData,
|
||||
moneyAmountsData,
|
||||
priceSetsData,
|
||||
priceSetMoneyAmountsData,
|
||||
priceSetMoneyAmountRulesData: [],
|
||||
priceRuleData,
|
||||
ruleTypesData,
|
||||
})
|
||||
})
|
||||
|
||||
it("should return null values when no context is set", async () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
it("should throw an error when currency code is not set", async () => {
|
||||
let result = 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,
|
||||
},
|
||||
])
|
||||
expect(result).rejects.toThrow(
|
||||
"currency_code is a required input in the pricing context"
|
||||
)
|
||||
|
||||
result = service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{ context: { region_id: "DE" } }
|
||||
)
|
||||
|
||||
expect(result).rejects.toThrow(
|
||||
"currency_code is a required input in the pricing context"
|
||||
)
|
||||
})
|
||||
|
||||
it("should return filled prices when 1 context is present and price is setup for EUR", async () => {
|
||||
it("should return filled prices when 1 context is present and price is setup for PLN", async () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: { currency_code: "EUR" },
|
||||
context: { currency_code: "PLN" },
|
||||
}
|
||||
)
|
||||
|
||||
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,
|
||||
},
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
amount: "1000",
|
||||
currency_code: "PLN",
|
||||
min_quantity: "1",
|
||||
max_quantity: "10",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
@@ -365,7 +386,7 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: { region_id: "PL" },
|
||||
context: { currency_code: "PLN", region_id: "PL" },
|
||||
}
|
||||
)
|
||||
|
||||
@@ -405,10 +426,10 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
},
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
amount: "400",
|
||||
amount: "1000",
|
||||
currency_code: "PLN",
|
||||
min_quantity: "1",
|
||||
max_quantity: "5",
|
||||
max_quantity: "10",
|
||||
},
|
||||
])
|
||||
})
|
||||
@@ -417,7 +438,7 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: { does_not_exist: "EUR" },
|
||||
context: { currency_code: "EUR", does_not_exist: "EUR" },
|
||||
}
|
||||
)
|
||||
|
||||
@@ -443,24 +464,50 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: { currency_code: "EUR", region_id: "PL" },
|
||||
context: { currency_code: "PLN", region_id: "PL" },
|
||||
}
|
||||
)
|
||||
|
||||
expect(priceSetsResult).toEqual([
|
||||
{
|
||||
id: "price-set-EUR",
|
||||
amount: "500",
|
||||
currency_code: "EUR",
|
||||
min_quantity: "1",
|
||||
max_quantity: "10",
|
||||
amount: null,
|
||||
currency_code: null,
|
||||
min_quantity: null,
|
||||
max_quantity: null,
|
||||
},
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
amount: "200",
|
||||
currency_code: "EUR",
|
||||
amount: "300",
|
||||
currency_code: "PLN",
|
||||
min_quantity: "1",
|
||||
max_quantity: "3",
|
||||
max_quantity: "4",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should return filled prices when 2 contexts are present and price is not setup", async () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: { currency_code: "PLN", company_id: "doesnotexist" },
|
||||
}
|
||||
)
|
||||
|
||||
expect(priceSetsResult).toEqual([
|
||||
{
|
||||
id: "price-set-EUR",
|
||||
amount: null,
|
||||
currency_code: null,
|
||||
min_quantity: null,
|
||||
max_quantity: null,
|
||||
},
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
amount: "1000",
|
||||
currency_code: "PLN",
|
||||
min_quantity: "1",
|
||||
max_quantity: "10",
|
||||
},
|
||||
])
|
||||
})
|
||||
@@ -469,15 +516,15 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-PLN"] },
|
||||
{
|
||||
context: { currency_code: "EUR", region_id: "PL", quantity: 5 },
|
||||
context: { currency_code: "PLN", region_id: "PL", quantity: 5 },
|
||||
}
|
||||
)
|
||||
|
||||
expect(priceSetsResult).toEqual([
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
amount: "50",
|
||||
currency_code: "EUR",
|
||||
amount: "250",
|
||||
currency_code: "PLN",
|
||||
min_quantity: "4",
|
||||
max_quantity: "10",
|
||||
},
|
||||
@@ -489,9 +536,10 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: {
|
||||
currency_code: "EUR",
|
||||
currency_code: "PLN",
|
||||
region_id: "PL",
|
||||
customer_group_id: "test",
|
||||
customer_group_id: "test-customer-group",
|
||||
company_id: "does-not-exist",
|
||||
},
|
||||
}
|
||||
)
|
||||
@@ -499,17 +547,17 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
expect(priceSetsResult).toEqual([
|
||||
{
|
||||
id: "price-set-EUR",
|
||||
amount: "500",
|
||||
currency_code: "EUR",
|
||||
min_quantity: "1",
|
||||
max_quantity: "10",
|
||||
amount: null,
|
||||
currency_code: null,
|
||||
min_quantity: null,
|
||||
max_quantity: null,
|
||||
},
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
amount: "200",
|
||||
currency_code: "EUR",
|
||||
amount: "300",
|
||||
currency_code: "PLN",
|
||||
min_quantity: "1",
|
||||
max_quantity: "3",
|
||||
max_quantity: "4",
|
||||
},
|
||||
])
|
||||
})
|
||||
@@ -522,6 +570,7 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
currency_code: "EUR",
|
||||
region_id: "PL",
|
||||
customer_group_id: "test-customer-group",
|
||||
company_id: "medusa-company-id",
|
||||
},
|
||||
}
|
||||
)
|
||||
@@ -529,10 +578,10 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
expect(priceSetsResult).toEqual([
|
||||
{
|
||||
id: "price-set-EUR",
|
||||
amount: "500",
|
||||
currency_code: "EUR",
|
||||
min_quantity: "1",
|
||||
max_quantity: "10",
|
||||
amount: null,
|
||||
currency_code: null,
|
||||
min_quantity: null,
|
||||
max_quantity: null,
|
||||
},
|
||||
// Currency Code + Region value + customer group id
|
||||
{
|
||||
@@ -545,45 +594,15 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
])
|
||||
})
|
||||
|
||||
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",
|
||||
currency_code: "PLN",
|
||||
region_id: "PL",
|
||||
customer_group_id: "does-not-exist",
|
||||
company_id: "does-not-exist",
|
||||
},
|
||||
}
|
||||
)
|
||||
@@ -607,14 +626,15 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
])
|
||||
})
|
||||
|
||||
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 () => {
|
||||
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 default value", async () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: {
|
||||
currency_code: "EUR",
|
||||
currency_code: "PLN",
|
||||
region_id: "does-not-exist",
|
||||
customer_group_id: "does-not-exist",
|
||||
company_id: "does-not-exist",
|
||||
},
|
||||
}
|
||||
)
|
||||
@@ -622,19 +642,19 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
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,
|
||||
},
|
||||
// PLN price set is not setup for EUR currency_code so it will default to a null set
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
amount: "1000",
|
||||
currency_code: "PLN",
|
||||
min_quantity: "1",
|
||||
max_quantity: "10",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
@@ -642,7 +662,7 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: { does_not_exist: "EUR", does_not_exist_2: "Berlin" },
|
||||
context: { currency_code: "EUR", does_not_exist_2: "Berlin" },
|
||||
}
|
||||
)
|
||||
|
||||
@@ -668,25 +688,25 @@ describe("PricingModule Service - Calculate Price", () => {
|
||||
const priceSetsResult = await service.calculatePrices(
|
||||
{ id: ["price-set-EUR", "price-set-PLN"] },
|
||||
{
|
||||
context: { currency_code: "EUR", does_not_exist: "Berlin" },
|
||||
context: { currency_code: "PLN", region_id: "PL", city: "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,
|
||||
},
|
||||
{
|
||||
id: "price-set-PLN",
|
||||
amount: "300",
|
||||
currency_code: "PLN",
|
||||
min_quantity: "1",
|
||||
max_quantity: "4",
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
+3
-1
@@ -7,6 +7,7 @@ import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
|
||||
import { createPriceRules } from "../../../__fixtures__/price-rule"
|
||||
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"
|
||||
|
||||
@@ -29,9 +30,10 @@ describe("PricingModule Service - PriceRule", () => {
|
||||
|
||||
await createCurrencies(testManager)
|
||||
await createMoneyAmounts(testManager)
|
||||
await createRuleTypes(testManager)
|
||||
await createPriceSets(testManager)
|
||||
await createRuleTypes(testManager)
|
||||
await createPriceSetMoneyAmounts(testManager)
|
||||
await createPriceSetMoneyAmountRules(testManager)
|
||||
await createPriceRules(testManager)
|
||||
})
|
||||
|
||||
|
||||
+8
-7
@@ -256,7 +256,7 @@ describe("PricingModule Service - PriceSetMoneyAmountRules", () => {
|
||||
|
||||
describe("createPriceSetMoneyAmountRules", () => {
|
||||
it("should create a priceSetMoneyAmountRules successfully", async () => {
|
||||
const created = await service.createPriceSetMoneyAmountRules([
|
||||
await service.createPriceSetMoneyAmountRules([
|
||||
{
|
||||
price_set_money_amount: "price-set-money-amount-EUR",
|
||||
rule_type: "rule-type-2",
|
||||
@@ -264,19 +264,20 @@ describe("PricingModule Service - PriceSetMoneyAmountRules", () => {
|
||||
},
|
||||
])
|
||||
|
||||
const [priceSetMoneyAmountRules] =
|
||||
const priceSetMoneyAmountRules =
|
||||
await service.listPriceSetMoneyAmountRules(
|
||||
{
|
||||
id: [created[0]?.id],
|
||||
},
|
||||
{},
|
||||
{
|
||||
relations: ["price_set_money_amount", "rule_type"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(priceSetMoneyAmountRules).toEqual(
|
||||
const created =
|
||||
priceSetMoneyAmountRules[priceSetMoneyAmountRules.length - 1]
|
||||
|
||||
expect(created).toEqual(
|
||||
expect.objectContaining({
|
||||
id: created[0]?.id,
|
||||
id: expect.any(String),
|
||||
value: "New priceSetMoneyAmountRule",
|
||||
price_set_money_amount: expect.objectContaining({
|
||||
id: "price-set-money-amount-EUR",
|
||||
|
||||
+483
-37
@@ -1,45 +1,34 @@
|
||||
import { CreatePriceSetDTO, IPricingModuleService } from "@medusajs/types"
|
||||
import {
|
||||
CreatePriceSetDTO,
|
||||
CreatePriceSetRuleTypeDTO,
|
||||
IPricingModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { PriceSet } from "@models"
|
||||
|
||||
import { initialize } from "../../../../src"
|
||||
import { createCurrencies } from "../../../__fixtures__/currency"
|
||||
import {
|
||||
createMoneyAmounts,
|
||||
defaultMoneyAmountsData,
|
||||
} from "../../../__fixtures__/money-amount"
|
||||
import {
|
||||
createPriceRules,
|
||||
defaultPriceRuleData,
|
||||
} from "../../../__fixtures__/price-rule"
|
||||
import { createPriceSets } from "../../../__fixtures__/price-set"
|
||||
import {
|
||||
createPriceSetMoneyAmounts,
|
||||
defaultPriceSetMoneyAmountsData,
|
||||
} from "../../../__fixtures__/price-set-money-amount"
|
||||
import { createRuleTypes } from "../../../__fixtures__/rule-type"
|
||||
import { PriceSetRuleType, initialize } from "../../../../src"
|
||||
import { seedPriceData } from "../../../__fixtures__/seed-price-data"
|
||||
import { DB_URL, MikroOrmWrapper } from "../../../utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
async function seedData({
|
||||
moneyAmountsData = defaultMoneyAmountsData,
|
||||
priceRuleData = defaultPriceRuleData,
|
||||
priceSetMoneyAmountsData = defaultPriceSetMoneyAmountsData,
|
||||
} = {}) {
|
||||
const testManager = MikroOrmWrapper.forkManager()
|
||||
async function createPriceSetPriceRules(
|
||||
manager: SqlEntityManager,
|
||||
priceSetRulesData: CreatePriceSetRuleTypeDTO[]
|
||||
): Promise<void> {
|
||||
const priceSetRules: PriceSetRuleType[] = []
|
||||
|
||||
await createCurrencies(testManager)
|
||||
await createMoneyAmounts(testManager, moneyAmountsData)
|
||||
await createPriceSets(testManager)
|
||||
await createPriceSetMoneyAmounts(testManager, priceSetMoneyAmountsData)
|
||||
await createRuleTypes(testManager)
|
||||
await createPriceRules(testManager, priceRuleData)
|
||||
for (let priceSetRuleData of priceSetRulesData) {
|
||||
const priceRule = manager.create(PriceSetRuleType, priceSetRuleData)
|
||||
|
||||
priceSetRules.push(priceRule)
|
||||
}
|
||||
|
||||
await manager.persistAndFlush(priceSetRules)
|
||||
}
|
||||
|
||||
describe("PricingModule Service - PriceSet", () => {
|
||||
let service: IPricingModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let repositoryManager: SqlEntityManager
|
||||
let data!: PriceSet[]
|
||||
|
||||
@@ -52,9 +41,21 @@ describe("PricingModule Service - PriceSet", () => {
|
||||
schema: process.env.MEDUSA_PRICING_DB_SCHEMA,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
beforeEach(async () => await seedData())
|
||||
const testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
await seedPriceData(testManager)
|
||||
await createPriceSetPriceRules(testManager, [
|
||||
{
|
||||
price_set: "price-set-1",
|
||||
rule_type: "rule-type-1",
|
||||
},
|
||||
{
|
||||
price_set: "price-set-2",
|
||||
rule_type: "rule-type-2",
|
||||
},
|
||||
] as unknown as CreatePriceSetRuleTypeDTO[])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
@@ -287,20 +288,179 @@ describe("PricingModule Service - PriceSet", () => {
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
it("should throw an error when creating a price set with rule attributes that don't exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
await service.create([
|
||||
{
|
||||
random: "does-not-exist",
|
||||
} as any,
|
||||
rules: [{ rule_attribute: "does-not-exist" }],
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual('PriceSet with id "undefined" not found')
|
||||
expect(error.message).toEqual(
|
||||
"Rule types don't exist for: does-not-exist"
|
||||
)
|
||||
})
|
||||
|
||||
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.create([
|
||||
{
|
||||
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 money amounts with rule attribute: city"
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a price set with rule types", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
},
|
||||
])
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
rule_types: [
|
||||
expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a price set with rule types and money amounts", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
currency_code: "USD",
|
||||
rules: {
|
||||
region_id: "1",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
rule_types: [
|
||||
expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
],
|
||||
money_amounts: [
|
||||
expect.objectContaining({
|
||||
amount: "100",
|
||||
currency_code: "USD",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a price set with money amounts with and without rules", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
currency_code: "USD",
|
||||
rules: {
|
||||
region_id: "1",
|
||||
},
|
||||
},
|
||||
{
|
||||
amount: 150,
|
||||
currency_code: "USD",
|
||||
rules: {},
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
rule_types: [
|
||||
expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
],
|
||||
money_amounts: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: "100",
|
||||
currency_code: "USD",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
amount: "150",
|
||||
currency_code: "USD",
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a price set with rule types and money amountss", async () => {
|
||||
const [priceSet] = await service.create([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
currency_code: "USD",
|
||||
rules: {
|
||||
region_id: "10",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
rule_types: [
|
||||
expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
],
|
||||
money_amounts: [
|
||||
expect.objectContaining({
|
||||
amount: "100",
|
||||
currency_code: "USD",
|
||||
}),
|
||||
],
|
||||
price_rules: [
|
||||
expect.objectContaining({
|
||||
value: "10",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a priceSet successfully", async () => {
|
||||
@@ -321,4 +481,290 @@ describe("PricingModule Service - PriceSet", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("removeRules", () => {
|
||||
it("should delete prices for a price set associated to the rules that are deleted", async () => {
|
||||
const createdPriceSet = await service.create([
|
||||
{
|
||||
rules: [
|
||||
{
|
||||
rule_attribute: "region_id",
|
||||
},
|
||||
{
|
||||
rule_attribute: "currency_code",
|
||||
},
|
||||
],
|
||||
prices: [
|
||||
{
|
||||
currency_code: "EUR",
|
||||
amount: 100,
|
||||
rules: {
|
||||
region_id: "test-region",
|
||||
currency_code: "test-currency",
|
||||
},
|
||||
},
|
||||
{
|
||||
currency_code: "EUR",
|
||||
amount: 500,
|
||||
rules: {
|
||||
currency_code: "test-currency",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
await service.removeRules([
|
||||
{
|
||||
id: createdPriceSet[0].id,
|
||||
rules: ["region_id"],
|
||||
},
|
||||
])
|
||||
|
||||
let priceSet = await service.list(
|
||||
{
|
||||
id: [createdPriceSet[0].id],
|
||||
},
|
||||
{
|
||||
relations: ["rule_types", "money_amounts", "price_rules"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(
|
||||
expect.arrayContaining(
|
||||
expect.objectContaining({
|
||||
id: priceSet[0].id,
|
||||
price_rules: [
|
||||
{
|
||||
id: expect.any(String),
|
||||
rule_type: expect.objectContaining({
|
||||
rule_attribute: "currency_code",
|
||||
}),
|
||||
},
|
||||
],
|
||||
money_amounts: [
|
||||
expect.objectContaining({
|
||||
amount: "500",
|
||||
currency_code: "EUR",
|
||||
}),
|
||||
],
|
||||
rule_types: [
|
||||
expect.objectContaining({
|
||||
rule_attribute: "currency_code",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
await service.removeRules([
|
||||
{
|
||||
id: createdPriceSet[0].id,
|
||||
rules: ["currency_code"],
|
||||
},
|
||||
])
|
||||
|
||||
priceSet = await service.list(
|
||||
{
|
||||
id: [createdPriceSet[0].id],
|
||||
},
|
||||
{
|
||||
relations: ["rule_types", "money_amounts", "price_rules"],
|
||||
}
|
||||
)
|
||||
expect(priceSet).toEqual([
|
||||
{
|
||||
id: expect.any(String),
|
||||
price_rules: [],
|
||||
money_amounts: [],
|
||||
rule_types: [],
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("addPrices", () => {
|
||||
it("should add prices to existing price set", async () => {
|
||||
await service.addPrices([
|
||||
{
|
||||
priceSetId: "price-set-1",
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
currency_code: "USD",
|
||||
rules: { currency_code: "USD" },
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
const [priceSet] = await service.list(
|
||||
{ id: ["price-set-1"] },
|
||||
{ relations: ["money_amounts"] }
|
||||
)
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "price-set-1",
|
||||
money_amounts: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: "100",
|
||||
currency_code: "USD",
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should add prices to multiple existing price set", async () => {
|
||||
await service.addPrices([
|
||||
{
|
||||
priceSetId: "price-set-1",
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
currency_code: "USD",
|
||||
rules: { currency_code: "USD" },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
priceSetId: "price-set-2",
|
||||
prices: [
|
||||
{
|
||||
amount: 150,
|
||||
currency_code: "EUR",
|
||||
rules: { region_id: "region-2" },
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
const priceSets = await service.list(
|
||||
{ id: ["price-set-1", "price-set-2"] },
|
||||
{ relations: ["money_amounts"] }
|
||||
)
|
||||
|
||||
expect(priceSets).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "price-set-1",
|
||||
money_amounts: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: "100",
|
||||
currency_code: "USD",
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "price-set-2",
|
||||
money_amounts: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: "150",
|
||||
currency_code: "EUR",
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
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")
|
||||
})
|
||||
})
|
||||
|
||||
describe("addRules", () => {
|
||||
it("should add rules to existing price set", async () => {
|
||||
await service.addRules([
|
||||
{
|
||||
priceSetId: "price-set-1",
|
||||
rules: [
|
||||
{
|
||||
attribute: "region_id",
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
const [priceSet] = await service.list(
|
||||
{ id: ["price-set-1"] },
|
||||
{ relations: ["rule_types"] }
|
||||
)
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "price-set-1",
|
||||
rule_types: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
rule_attribute: "currency_code",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail to add rules to non-existent price sets", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.addRules([
|
||||
{
|
||||
priceSetId: "price-set-doesn't-exist",
|
||||
rules: [
|
||||
{
|
||||
attribute: "region_id",
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"PriceSets with ids: price-set-doesn't-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail to add rules with non-existent attributes", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.addRules([
|
||||
{
|
||||
priceSetId: "price-set-1",
|
||||
rules: [
|
||||
{
|
||||
attribute: "city",
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"Rule types don't exist for attributes: city"
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user