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:
Riqwan Thamir
2024-06-21 15:30:36 +02:00
committed by GitHub
parent ed104d5aac
commit 8ac74c1357
11 changed files with 131 additions and 741 deletions
@@ -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", () => {
@@ -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"
)
})
})
})
},
})
@@ -43,16 +43,15 @@ import {
PriceListRuleValue,
PriceRule,
PriceSet,
PriceSetRuleType,
RuleType,
} from "@models"
import { PriceListService, RuleTypeService } from "@services"
import { ServiceTypes } from "@types"
import { eventBuilders, validatePriceListDates } from "@utils"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import { PriceListIdPrefix } from "../models/price-list"
import { PriceSetIdPrefix } from "../models/price-set"
import { ServiceTypes } from "@types"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
@@ -74,7 +73,6 @@ const generateMethodForModels = {
PriceListRuleValue,
PriceRule,
Price,
PriceSetRuleType,
RuleType,
}
@@ -102,7 +100,6 @@ export default class PricingModuleService
protected readonly ruleTypeService_: RuleTypeService
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<PriceSet>
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceRule>
protected readonly priceSetRuleTypeService_: ModulesSdkTypes.IMedusaInternalService<PriceSetRuleType>
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<Price>
protected readonly priceListService_: PriceListService
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceListRule>
@@ -115,7 +112,6 @@ export default class PricingModuleService
ruleTypeService,
priceSetService,
priceRuleService,
priceSetRuleTypeService,
priceService,
priceListService,
priceListRuleService,
@@ -132,7 +128,6 @@ export default class PricingModuleService
this.priceSetService_ = priceSetService
this.ruleTypeService_ = ruleTypeService
this.priceRuleService_ = priceRuleService
this.priceSetRuleTypeService_ = priceSetRuleTypeService
this.priceService_ = priceService
this.priceListService_ = priceListService
this.priceListRuleService_ = priceListRuleService
@@ -497,9 +492,7 @@ export default class PricingModuleService
const { entities: upsertedPrices } =
await this.priceService_.upsertWithReplace(
prices,
{
relations: ["price_rules"],
},
{ relations: ["price_rules"] },
sharedContext
)
@@ -527,37 +520,6 @@ export default class PricingModuleService
return priceSets
}
async addRules(
data: PricingTypes.AddRulesDTO,
sharedContext?: Context
): Promise<PricingTypes.PriceSetDTO>
async addRules(
data: PricingTypes.AddRulesDTO[],
sharedContext?: Context
): Promise<PricingTypes.PriceSetDTO[]>
@InjectManager("baseRepository_")
async addRules(
data: PricingTypes.AddRulesDTO | PricingTypes.AddRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO> {
const inputs = Array.isArray(data) ? data : [data]
const priceSets = await this.addRules_(inputs, sharedContext)
const dbPriceSets = await this.listPriceSets(
{ id: priceSets.map(({ id }) => id) },
{ relations: ["rule_types"] }
)
const orderedPriceSets = priceSets.map((priceSet) => {
return dbPriceSets.find((p) => p.id === priceSet.id)!
})
return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0]
}
async addPrices(
data: AddPricesDTO,
sharedContext?: Context
@@ -591,50 +553,6 @@ export default class PricingModuleService
return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0]
}
@InjectTransactionManager("baseRepository_")
async removeRules(
data: PricingTypes.RemovePriceSetRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<void> {
const priceSets = await this.priceSetService_.list(
{ id: data.map((d) => d.id) },
{},
sharedContext
)
const priceSetIds = priceSets.map((ps) => ps.id)
const ruleTypes = await this.ruleTypeService_.list(
{
rule_attribute: data.map((d) => d.rules || []).flat(),
},
{ take: null },
sharedContext
)
const ruleTypeIds = ruleTypes.map((rt) => rt.id)
const priceSetRuleTypes = await this.priceSetRuleTypeService_.list(
{ price_set_id: priceSetIds, rule_type_id: ruleTypeIds },
{ take: null },
sharedContext
)
const priceRules = await this.priceRuleService_.list(
{ price_set_id: priceSetIds, rule_type_id: ruleTypeIds },
{ select: ["price"], take: null },
sharedContext
)
await this.priceSetRuleTypeService_.delete(
priceSetRuleTypes.map((psrt) => psrt.id),
sharedContext
)
await this.priceService_.delete(
priceRules.map((pr) => pr.price.id),
sharedContext
)
}
@InjectManager("baseRepository_")
@EmitEvents()
// @ts-ignore
@@ -723,7 +641,12 @@ export default class PricingModuleService
const input = Array.isArray(data) ? data : [data]
const ruleAttributes = deduplicate(
data.map((d) => d.rules?.map((r) => r.rule_attribute) ?? []).flat()
data
.map(
(d) =>
d.prices?.map((ma) => Object.keys(ma?.rules ?? {})).flat() ?? []
)
.flat()
)
const ruleTypes = await this.ruleTypeService_.list(
@@ -744,56 +667,27 @@ export default class PricingModuleService
if (invalidRuleAttributes.length > 0) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Rule types don't exist for: ${invalidRuleAttributes.join(", ")}`
)
}
const invalidMoneyAmountRule = data
.map(
(d) => d.prices?.map((ma) => Object.keys(ma?.rules ?? {})).flat() ?? []
)
.flat()
.filter((r) => !ruleTypeMap.has(r))
if (invalidMoneyAmountRule.length > 0) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Rule types don't exist for money amounts with rule attribute: ${invalidMoneyAmountRule.join(
`Rule types don't exist for prices with rule attribute: ${invalidRuleAttributes.join(
", "
)}`
)
}
const ruleSetRuleTypeToCreateMap: Map<string, PriceSetRuleType> = new Map()
const toCreate = input.map((inputData) => {
const id = generateEntityId(
(inputData as unknown as PriceSet).id,
PriceSetIdPrefix
)
const { prices, rules = [], ...rest } = inputData
const { prices, ...rest } = inputData
let pricesData: CreatePricesDTO[] = []
rules.forEach((rule) => {
const priceSetRuleType = {
rule_type_id: ruleTypeMap.get(rule.rule_attribute).id,
price_set_id: id,
} as PriceSetRuleType
ruleSetRuleTypeToCreateMap.set(
JSON.stringify(priceSetRuleType),
priceSetRuleType
)
})
if (inputData.prices) {
pricesData = inputData.prices.map((price) => {
let { rules: priceRules = {}, ...rest } = price
const cleanRules = priceRules ? removeNullish(priceRules) : {}
const numberOfRules = Object.keys(cleanRules).length
const rulesDataMap = new Map()
Object.entries(priceRules).map(([attribute, value]) => {
@@ -803,16 +697,6 @@ export default class PricingModuleService
value,
}
rulesDataMap.set(JSON.stringify(rule), rule)
const priceSetRuleType = {
rule_type_id: ruleTypeMap.get(attribute).id,
price_set_id: id,
} as PriceSetRuleType
ruleSetRuleTypeToCreateMap.set(
JSON.stringify(priceSetRuleType),
priceSetRuleType
)
})
return {
@@ -880,99 +764,9 @@ export default class PricingModuleService
sharedContext,
})
if (ruleSetRuleTypeToCreateMap.size) {
await this.priceSetRuleTypeService_.create(
Array.from(ruleSetRuleTypeToCreateMap.values()),
sharedContext
)
}
return createdPriceSets
}
@InjectTransactionManager("baseRepository_")
protected async addRules_(
inputs: PricingTypes.AddRulesDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<PriceSet[]> {
const priceSets = await this.priceSetService_.list(
{ id: inputs.map((d) => d.priceSetId) },
{ relations: ["rule_types"] },
sharedContext
)
const priceSetRuleTypeMap: Map<string, Map<string, RuleTypeDTO>> = new Map(
priceSets.map((priceSet) => [
priceSet.id,
new Map([...priceSet.rule_types].map((rt) => [rt.rule_attribute, rt])),
])
)
const priceSetMap = new Map(priceSets.map((p) => [p.id, p]))
const invalidPriceSetInputs = inputs.filter(
(d) => !priceSetMap.has(d.priceSetId)
)
if (invalidPriceSetInputs.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`PriceSets with ids: ${invalidPriceSetInputs
.map((d) => d.priceSetId)
.join(", ")} was not found`
)
}
const ruleTypes = await this.ruleTypeService_.list(
{
rule_attribute: inputs
.map((data) => data.rules.map((r) => r.attribute))
.flat(),
},
{ take: null },
sharedContext
)
const ruleTypeMap: Map<string, RuleTypeDTO> = new Map(
ruleTypes.map((rt) => [rt.rule_attribute, rt])
)
const invalidRuleAttributeInputs = inputs
.map((d) => d.rules.map((r) => r.attribute))
.flat()
.filter((r) => !ruleTypeMap.has(r))
if (invalidRuleAttributeInputs.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Rule types don't exist for attributes: ${[
...new Set(invalidRuleAttributeInputs),
].join(", ")}`
)
}
const priceSetRuleTypesCreate: PricingTypes.CreatePriceSetRuleTypeDTO[] = []
inputs.forEach((data) => {
for (const rule of data.rules) {
if (priceSetRuleTypeMap.get(data.priceSetId)!.has(rule.attribute)) {
continue
}
priceSetRuleTypesCreate.push({
rule_type_id: ruleTypeMap.get(rule.attribute)!.id,
price_set_id: priceSetMap.get(data.priceSetId)!.id,
})
}
})
await this.priceSetRuleTypeService_.create(
priceSetRuleTypesCreate,
sharedContext
)
return priceSets
}
@InjectTransactionManager("baseRepository_")
protected async addPrices_(
input: AddPricesDTO[],
@@ -1365,9 +1159,6 @@ export default class PricingModuleService
const ruleTypeAttributes: string[] = []
const priceListIds: string[] = []
const priceIds: string[] = []
const priceSetIds = data
.map((d) => d.prices.map((price) => price.price_set_id))
.flat()
for (const priceListData of data) {
priceListIds.push(priceListData.price_list_id)
@@ -1398,53 +1189,6 @@ export default class PricingModuleService
ruleTypes.map((rt) => [rt.rule_attribute, rt])
)
const priceSets = await this.listPriceSets(
{ id: priceSetIds },
{ relations: ["rule_types"] },
sharedContext
)
const priceSetRuleTypeMap: Map<string, Set<string>> = priceSets.reduce(
(acc, curr) => {
const priceSetRuleAttributeSet: Set<string> =
acc.get(curr.id) || new Set()
for (const rt of curr.rule_types ?? []) {
priceSetRuleAttributeSet.add(rt.rule_attribute)
}
acc.set(curr.id, priceSetRuleAttributeSet)
return acc
},
new Map()
)
const ruleTypeErrors: string[] = []
for (const priceListData of data) {
for (const price of priceListData.prices) {
for (const ruleAttribute of Object.keys(price.rules ?? {})) {
if (
!priceSetRuleTypeMap.get(price.price_set_id)?.has(ruleAttribute)
) {
ruleTypeErrors.push(
`rule_attribute "${ruleAttribute}" in price set ${price.price_set_id}`
)
}
}
}
}
if (ruleTypeErrors.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Invalid rule type configuration: Price set rules doesn't exist for ${ruleTypeErrors.join(
", "
)}`
)
}
const priceLists = await this.listPriceLists(
{ id: priceListIds },
{ take: null },
@@ -1532,52 +1276,6 @@ export default class PricingModuleService
sharedContext
)
const priceSets = await this.listPriceSets(
{ id: priceSetIds },
{ relations: ["rule_types"] },
sharedContext
)
const priceSetRuleTypeMap: Map<string, Set<string>> = priceSets.reduce(
(acc, curr) => {
const priceSetRuleAttributeSet: Set<string> =
acc.get(curr.id) || new Set()
for (const rt of curr.rule_types ?? []) {
priceSetRuleAttributeSet.add(rt.rule_attribute)
}
acc.set(curr.id, priceSetRuleAttributeSet)
return acc
},
new Map()
)
const ruleTypeErrors: string[] = []
for (const priceListData of data) {
for (const price of priceListData.prices) {
for (const rule_attribute of Object.keys(price.rules ?? {})) {
if (
!priceSetRuleTypeMap.get(price.price_set_id)?.has(rule_attribute)
) {
ruleTypeErrors.push(
`rule_attribute "${rule_attribute}" in price set ${price.price_set_id}`
)
}
}
}
}
if (ruleTypeErrors.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Invalid rule type configuration: Price set rules doesn't exist for ${ruleTypeErrors.join(
", "
)}`
)
}
const ruleTypeMap: Map<string, RuleTypeDTO> = new Map(
ruleTypes.map((rt) => [rt.rule_attribute, rt])
)