feat(core-flows,types,pricing,medusa): Products API can create prices with rules (#7796)
* chore: Products API can create prices with rules * chore: fix tests * chore: cleanup * chore: address comments
This commit is contained in:
+8
-105
@@ -1,16 +1,16 @@
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import {
|
||||
MockEventBusService,
|
||||
moduleIntegrationTestRunner,
|
||||
} from "medusa-test-utils"
|
||||
import { createPriceLists } from "../../../__fixtures__/price-list"
|
||||
import { createPriceSets } from "../../../__fixtures__/price-set"
|
||||
import {
|
||||
CommonEvents,
|
||||
composeMessage,
|
||||
Modules,
|
||||
PricingEvents,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
MockEventBusService,
|
||||
moduleIntegrationTestRunner,
|
||||
} from "medusa-test-utils"
|
||||
import { createPriceLists } from "../../../__fixtures__/price-list"
|
||||
import { createPriceSets } from "../../../__fixtures__/price-set"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -745,41 +745,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail to add a price with non-existing rule-types in the price-set to a priceList", async () => {
|
||||
await service.createRuleTypes([
|
||||
{
|
||||
name: "twitter_handle",
|
||||
rule_attribute: "twitter_handle",
|
||||
},
|
||||
])
|
||||
|
||||
let error
|
||||
try {
|
||||
await service.addPriceListPrices([
|
||||
{
|
||||
price_list_id: "price-list-1",
|
||||
prices: [
|
||||
{
|
||||
amount: 123,
|
||||
currency_code: "EUR",
|
||||
price_set_id: "price-set-1",
|
||||
rules: {
|
||||
twitter_handle: "owjuhl",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
} catch (err) {
|
||||
error = err
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"" +
|
||||
`Invalid rule type configuration: Price set rules doesn't exist for rule_attribute "twitter_handle" in price set price-set-1`
|
||||
)
|
||||
})
|
||||
|
||||
it("should add a price with rules to a priceList successfully", async () => {
|
||||
await service.createRuleTypes([
|
||||
{
|
||||
@@ -788,15 +753,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
},
|
||||
])
|
||||
|
||||
const r = await service.addRules([
|
||||
{
|
||||
priceSetId: "price-set-1",
|
||||
rules: [{ attribute: "region_id" }],
|
||||
},
|
||||
])
|
||||
|
||||
jest.clearAllMocks()
|
||||
|
||||
await service.addPriceListPrices([
|
||||
{
|
||||
price_list_id: "price-list-1",
|
||||
@@ -886,14 +842,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
|
||||
describe("updatePriceListPrices", () => {
|
||||
it("should update a price to a priceList successfully", async () => {
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{
|
||||
rules: [
|
||||
{ rule_attribute: "region_id" },
|
||||
{ rule_attribute: "customer_group_id" },
|
||||
],
|
||||
},
|
||||
])
|
||||
const [priceSet] = await service.createPriceSets([{}])
|
||||
|
||||
await service.addPriceListPrices([
|
||||
{
|
||||
@@ -907,7 +856,7 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
rules: {
|
||||
region_id: "test",
|
||||
},
|
||||
} as any,
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
@@ -981,52 +930,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail to add a price with non-existing rule-types in the price-set to a priceList", async () => {
|
||||
await service.createRuleTypes([
|
||||
{ name: "twitter_handle", rule_attribute: "twitter_handle" },
|
||||
{ name: "region_id", rule_attribute: "region_id" },
|
||||
])
|
||||
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{ rules: [{ rule_attribute: "region_id" }] },
|
||||
])
|
||||
|
||||
await service.addPriceListPrices([
|
||||
{
|
||||
price_list_id: "price-list-1",
|
||||
prices: [
|
||||
{
|
||||
id: "test-price-id",
|
||||
amount: 123,
|
||||
currency_code: "EUR",
|
||||
price_set_id: priceSet.id,
|
||||
rules: { region_id: "test" },
|
||||
} as any,
|
||||
],
|
||||
},
|
||||
])
|
||||
|
||||
const error = await service
|
||||
.updatePriceListPrices([
|
||||
{
|
||||
price_list_id: "price-list-1",
|
||||
prices: [
|
||||
{
|
||||
id: "test-price-id",
|
||||
amount: 123,
|
||||
price_set_id: priceSet.id,
|
||||
rules: { twitter_handle: "owjuhl" },
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual(
|
||||
`Invalid rule type configuration: Price set rules doesn't exist for rule_attribute "twitter_handle" in price set ${priceSet.id}`
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("removePrices", () => {
|
||||
|
||||
+8
-245
@@ -3,6 +3,12 @@ import {
|
||||
CreatePriceSetRuleTypeDTO,
|
||||
IPricingModuleService,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
CommonEvents,
|
||||
composeMessage,
|
||||
Modules,
|
||||
PricingEvents,
|
||||
} from "@medusajs/utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
MockEventBusService,
|
||||
@@ -10,12 +16,6 @@ import {
|
||||
} from "medusa-test-utils"
|
||||
import { PriceSetRuleType } from "../../../../src/models"
|
||||
import { seedPriceData } from "../../../__fixtures__/seed-price-data"
|
||||
import {
|
||||
CommonEvents,
|
||||
composeMessage,
|
||||
Modules,
|
||||
PricingEvents,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@@ -346,24 +346,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should throw an error when creating a price set with rule attributes that don't exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "does-not-exist" }],
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -386,32 +368,13 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
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.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
},
|
||||
])
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
rule_types: [
|
||||
expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
],
|
||||
})
|
||||
"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([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
@@ -426,11 +389,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
rule_types: [
|
||||
expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
],
|
||||
prices: [
|
||||
expect.objectContaining({
|
||||
amount: 100,
|
||||
@@ -477,10 +435,9 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a price set with money amounts with and without rules", async () => {
|
||||
it("should create a price set with prices", async () => {
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices: [
|
||||
{
|
||||
amount: 100,
|
||||
@@ -499,11 +456,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
|
||||
expect(priceSet).toEqual(
|
||||
expect.objectContaining({
|
||||
rule_types: [
|
||||
expect.objectContaining({
|
||||
rule_attribute: "region_id",
|
||||
}),
|
||||
],
|
||||
prices: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
amount: 100,
|
||||
@@ -518,44 +470,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a price set with rule types and money amounts", async () => {
|
||||
const [priceSet] = await service.createPriceSets([
|
||||
{
|
||||
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",
|
||||
}),
|
||||
],
|
||||
prices: [
|
||||
expect.objectContaining({
|
||||
amount: 100,
|
||||
currency_code: "USD",
|
||||
}),
|
||||
],
|
||||
price_rules: [
|
||||
expect.objectContaining({
|
||||
value: "10",
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should create a priceSet successfully", async () => {
|
||||
await service.createPriceSets([
|
||||
{
|
||||
@@ -575,92 +489,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
})
|
||||
})
|
||||
|
||||
describe("removeRules", () => {
|
||||
it("should delete prices for a price set associated to the rules that are deleted", async () => {
|
||||
const createdPriceSet = await service.createPriceSets([
|
||||
{
|
||||
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.listPriceSets(
|
||||
{ id: [createdPriceSet[0].id] },
|
||||
{ relations: ["rule_types", "prices", "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",
|
||||
}),
|
||||
},
|
||||
],
|
||||
prices: [
|
||||
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.listPriceSets(
|
||||
{ id: [createdPriceSet[0].id] },
|
||||
{ relations: ["rule_types", "prices", "price_rules"] }
|
||||
)
|
||||
expect(priceSet).toEqual([
|
||||
{
|
||||
id: expect.any(String),
|
||||
price_rules: [],
|
||||
prices: [],
|
||||
rule_types: [],
|
||||
created_at: expect.any(Date),
|
||||
updated_at: expect.any(Date),
|
||||
deleted_at: null,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("addPrices", () => {
|
||||
it("should add prices to existing price set", async () => {
|
||||
await service.addPrices([
|
||||
@@ -784,71 +612,6 @@ moduleIntegrationTestRunner<IPricingModuleService>({
|
||||
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.listPriceSets(
|
||||
{ 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