chore(): Reorganize modules (#7210)

**What**
Move all modules to the modules directory
This commit is contained in:
Adrien de Peretti
2024-05-02 17:33:34 +02:00
committed by GitHub
parent 7a351eef09
commit 4eae25e1ef
870 changed files with 91 additions and 62 deletions

View File

@@ -0,0 +1,12 @@
export const defaultPriceListRuleData = [
{
id: "price-list-rule-1",
price_list_id: "price-list-1",
rule_type_id: "rule-type-1",
},
{
id: "price-list-rule-2",
price_list_id: "price-list-1",
rule_type_id: "rule-type-2",
},
]

View File

@@ -0,0 +1,22 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceListRule } from "@models"
import { defaultPriceListRuleData } from "./data"
export * from "./data"
export async function createPriceListRules(
manager: SqlEntityManager,
priceListRuleData: any[] = defaultPriceListRuleData
): Promise<PriceListRule[]> {
const priceListRules: PriceListRule[] = []
for (let data of priceListRuleData) {
const plr = manager.create(PriceListRule, data)
priceListRules.push(plr)
}
await manager.persistAndFlush(priceListRules)
return priceListRules
}

View File

@@ -0,0 +1,14 @@
export const defaultPriceListData = [
{
id: "price-list-1",
title: "Price List 1",
description: "test",
rules_count: 0,
},
{
id: "price-list-2",
title: "Price List 2",
description: "test",
rules_count: 0,
},
]

View File

@@ -0,0 +1,22 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceList } from "@models"
import { defaultPriceListData } from "./data"
export * from "./data"
export async function createPriceLists(
manager: SqlEntityManager,
priceListData: any[] = defaultPriceListData
): Promise<PriceList[]> {
const priceLists: PriceList[] = []
for (let data of priceListData) {
const pl = manager.create(PriceList, data)
priceLists.push(pl)
}
await manager.persistAndFlush(priceLists)
return priceLists
}

View File

@@ -0,0 +1,22 @@
import { CreatePriceRuleDTO } from "@medusajs/types"
export * from "./data"
export const defaultPriceRuleData = [
{
id: "price-rule-1",
price_set_id: "price-set-1",
rule_type_id: "rule-type-1",
value: "USD",
price_list_id: "test",
price_id: "price-set-money-amount-USD",
},
{
id: "price-rule-2",
price_set_id: "price-set-2",
rule_type_id: "rule-type-2",
value: "region_1",
price_list_id: "test",
price_id: "price-set-money-amount-EUR",
},
] as unknown as CreatePriceRuleDTO[]

View File

@@ -0,0 +1,30 @@
import { PriceRule } from "@models"
import { CreatePriceRuleDTO } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { defaultPriceRuleData } from "./data"
export * from "./data"
export async function createPriceRules(
manager: SqlEntityManager,
pricesRulesData: CreatePriceRuleDTO[] = defaultPriceRuleData
): Promise<PriceRule[]> {
const priceRules: PriceRule[] = []
for (let priceRuleData of pricesRulesData) {
const priceRuleDataClone: CreatePriceRuleDTO = { ...priceRuleData }
priceRuleDataClone.price_set_id = priceRuleDataClone.price_set_id
priceRuleDataClone.rule_type_id = priceRuleDataClone.rule_type_id
priceRuleDataClone.price_id = priceRuleDataClone.price_id
const priceRule = manager.create(PriceRule, priceRuleDataClone)
priceRules.push(priceRule)
}
await manager.persistAndFlush(priceRules)
return priceRules
}

View File

@@ -0,0 +1,13 @@
import { CreatePriceSetDTO } from "@medusajs/types"
export const defaultPriceSetsData = [
{
id: "price-set-1",
},
{
id: "price-set-2",
},
{
id: "price-set-3",
},
] as unknown as CreatePriceSetDTO[]

View File

@@ -0,0 +1,37 @@
import { CreatePriceSetDTO } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Price, PriceSet } from "@models"
import { defaultPriceSetsData } from "./data"
export * from "./data"
export async function createPriceSets(
manager: SqlEntityManager,
priceSetsData: CreatePriceSetDTO[] = defaultPriceSetsData
): Promise<PriceSet[]> {
const priceSets: PriceSet[] = []
for (let priceSetData of priceSetsData) {
const priceSetDataClone = { ...priceSetData }
const prices = priceSetDataClone.prices || []
delete priceSetDataClone.prices
let priceSet = manager.create(PriceSet, priceSetDataClone) as PriceSet
manager.persist(priceSet)
for (let priceData of prices) {
const price = manager.create(Price, {
...priceData,
price_set_id: priceSet.id,
title: "test",
})
manager.persist(price)
}
await manager.flush()
}
return priceSets
}

View File

@@ -0,0 +1,26 @@
export const defaultPricesData = [
{
id: "price-set-money-amount-USD",
currency_code: "USD",
amount: 500,
title: "price set money amount USD",
price_set_id: "price-set-1",
rules_count: 1,
},
{
id: "price-set-money-amount-EUR",
currency_code: "EUR",
amount: 400,
title: "price set money amount EUR",
price_set_id: "price-set-2",
rules_count: 1,
},
{
id: "price-set-money-amount-CAD",
currency_code: "CAD",
amount: 600,
title: "price set money amount CAD",
price_set_id: "price-set-3",
rules_count: 1,
},
]

View File

@@ -0,0 +1,21 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Price } from "@models"
import { defaultPricesData } from "./data"
export * from "./data"
export async function createPrices(
manager: SqlEntityManager,
pricesData: any[] = defaultPricesData
): Promise<Price[]> {
const prices: Price[] = []
for (let data of pricesData) {
const price = manager.create(Price, data)
prices.push(price)
}
await manager.persistAndFlush(prices)
return prices
}

View File

@@ -0,0 +1,12 @@
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",
},
]

View File

@@ -0,0 +1,22 @@
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
}

View File

@@ -0,0 +1,22 @@
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)
export async function seedPriceData(
testManager: SqlEntityManager,
{
priceSetsData = defaultPriceSetsData,
priceRuleData = defaultPriceRuleData,
pricesData = defaultPricesData,
ruleTypesData = defaultRuleTypesData,
} = {}
) {
await createPriceSets(testManager, priceSetsData)
await createPrices(testManager, pricesData)
await createRuleTypes(testManager, ruleTypesData)
await createPriceRules(testManager, priceRuleData)
}

View File

@@ -0,0 +1,336 @@
import { Modules } from "@medusajs/modules-sdk"
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { createPriceLists } from "../../../__fixtures__/price-list"
import { createPriceListRules } from "../../../__fixtures__/price-list-rules"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
jest.setTimeout(30000)
moduleIntegrationTestRunner({
moduleName: Modules.PRICING,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
describe("PriceListRule Service", () => {
let testManager: SqlEntityManager
beforeEach(async () => {
testManager = await MikroOrmWrapper.forkManager()
await createRuleTypes(testManager)
await createPriceLists(testManager)
await createPriceListRules(testManager)
})
describe("list", () => {
it("should list priceListRules", async () => {
const priceListRuleResult = await service.listPriceListRules()
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should list priceListRules by pricelist id", async () => {
const priceListRuleResult = await service.listPriceListRules({
id: ["price-list-rule-1"],
})
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
])
})
})
describe("listAndCount", () => {
it("should return pricelistrules and count", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules()
expect(count).toEqual(2)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should return pricelistrules and count when filtered", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules({
id: ["price-list-rule-1"],
})
expect(count).toEqual(1)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-1",
}),
])
})
it("should return pricelistrules and count when using skip and take", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules({}, { skip: 1, take: 1 })
expect(count).toEqual(2)
expect(priceListRuleResult).toEqual([
expect.objectContaining({
id: "price-list-rule-2",
}),
])
})
it("should return requested fields", async () => {
const [priceListRuleResult, count] =
await service.listAndCountPriceListRules(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(priceListRuleResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "price-list-rule-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-list-rule-1"
it("should return priceList for the given id", async () => {
const priceListRuleResult = await service.retrievePriceListRule(id)
expect(priceListRuleResult).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when priceListRule with id does not exist", async () => {
let error
try {
await service.retrievePriceListRule("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceListRule with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrievePriceListRule(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceListRule - id must be defined")
})
})
describe("delete", () => {
const id = "price-list-rule-1"
it("should delete the pricelists given an id successfully", async () => {
await service.deletePriceListRules([id])
const priceListResult = await service.listPriceListRules({
id: [id],
})
expect(priceListResult).toHaveLength(0)
})
})
describe("update", () => {
const id = "price-list-rule-2"
it("should update the value of the priceListRule successfully", async () => {
await service.updatePriceListRules([
{
id,
price_list_id: "price-list-2",
rule_type_id: "rule-type-2",
},
])
const priceList = await service.retrievePriceListRule(id, {
relations: ["price_list", "rule_type"],
})
expect(priceList.price_list.id).toEqual("price-list-2")
expect(priceList.rule_type.id).toEqual("rule-type-2")
})
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceListRules([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceListRule with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should create a priceListRule successfully", async () => {
const [created] = await service.createPriceListRules([
{
price_list_id: "price-list-2",
rule_type_id: "rule-type-2",
},
])
const [priceListRule] = await service.listPriceListRules(
{
id: [created.id],
},
{
relations: ["price_list", "rule_type"],
}
)
expect(priceListRule.price_list.id).toEqual("price-list-2")
expect(priceListRule.rule_type.id).toEqual("rule-type-2")
})
})
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: {
sales_channel: "sc-1",
},
})
const [priceList] = await service.listPriceLists(
{ id: ["price-list-1"] },
{
relations: [
"price_list_rules",
"price_list_rules.price_list_rule_values",
],
}
)
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" }),
],
}),
])
)
})
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: {
sales_channel: ["sc-1", "sc-2"],
},
})
const [priceList] = await service.listPriceLists(
{
id: ["price-list-1"],
},
{
relations: [
"price_list_rules",
"price_list_rules.price_list_rule_values",
],
}
)
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" }),
]),
}),
])
)
})
})
describe("removePriceListRules", () => {
it("should remove a priceListRule from a priceList", async () => {
await service.removePriceListRules({
price_list_id: "price-list-1",
rules: ["currency_code"],
})
const [priceList] = await service.listPriceLists(
{
id: ["price-list-1"],
},
{
relations: ["price_list_rules"],
}
)
expect(priceList.price_list_rules).toEqual([
expect.objectContaining({ rule_type: { id: "rule-type-2" } }),
])
})
})
})
},
})

View File

@@ -0,0 +1,309 @@
import { Modules } from "@medusajs/modules-sdk"
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { SuiteOptions, moduleIntegrationTestRunner } from "medusa-test-utils"
import { Price } from "../../../../src"
import { createPrices } from "../../../__fixtures__/price"
import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { createRuleTypes } from "../../../__fixtures__/rule-type"
jest.setTimeout(30000)
moduleIntegrationTestRunner({
moduleName: Modules.PRICING,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
describe("PricingModule Service - PriceRule", () => {
let testManager: SqlEntityManager
beforeEach(async () => {
testManager = await MikroOrmWrapper.forkManager()
await createPriceSets(testManager)
await createRuleTypes(testManager)
await createPrices(testManager)
await createPriceRules(testManager)
})
describe("list", () => {
it("should list priceRules", async () => {
const PriceRulesResult = await service.listPriceRules()
const serialized = JSON.parse(JSON.stringify(PriceRulesResult))
expect(serialized).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should list priceRules by id", async () => {
const priceRuleResult = await service.listPriceRules({
id: ["price-rule-1"],
})
expect(priceRuleResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
])
})
it("should list priceRules with relations and selects", async () => {
const priceRulesResult = await service.listPriceRules(
{
id: ["price-rule-1"],
},
{
select: ["id", "price_set.id"],
relations: ["price_set"],
}
)
const serialized = JSON.parse(JSON.stringify(priceRulesResult))
expect(serialized).toEqual([
{
id: "price-rule-1",
price_set: {
id: "price-set-1",
},
price_set_id: "price-set-1",
},
])
})
describe("listAndCount", () => {
it("should return priceRules and count", async () => {
const [priceRulesResult, count] =
await service.listAndCountPriceRules()
expect(count).toEqual(2)
expect(priceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should return priceRules and count when filtered", async () => {
const [priceRulesResult, count] =
await service.listAndCountPriceRules({
id: ["price-rule-1"],
})
expect(count).toEqual(1)
expect(priceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-1",
}),
])
})
it("should list PriceRules with relations and selects", async () => {
const [PriceRulesResult, count] =
await service.listAndCountPriceRules(
{
id: ["price-rule-1"],
},
{
select: ["id", "price_set.id"],
relations: ["price_set"],
}
)
const serialized = JSON.parse(JSON.stringify(PriceRulesResult))
expect(count).toEqual(1)
expect(serialized).toEqual([
{
id: "price-rule-1",
price_set: {
id: "price-set-1",
},
price_set_id: "price-set-1",
},
])
})
it("should return PriceRules and count when using skip and take", async () => {
const [PriceRulesResult, count] =
await service.listAndCountPriceRules({}, { skip: 1, take: 1 })
expect(count).toEqual(2)
expect(PriceRulesResult).toEqual([
expect.objectContaining({
id: "price-rule-2",
}),
])
})
it("should return requested fields", async () => {
const [PriceRulesResult, count] =
await service.listAndCountPriceRules(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(PriceRulesResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
id: "price-rule-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-rule-1"
it("should return PriceRule for the given id", async () => {
const PriceRule = await service.retrievePriceRule(id)
expect(PriceRule).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when PriceRule with id does not exist", async () => {
let error
try {
await service.retrievePriceRule("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceRule with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrievePriceRule(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceRule - id must be defined")
})
it("should return PriceRule based on config select param", async () => {
const PriceRule = await service.retrievePriceRule(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(PriceRule))
expect(serialized).toEqual({
id,
})
})
})
describe("delete", () => {
const id = "price-set-1"
it("should delete the PriceRules given an id successfully", async () => {
await service.deletePriceRules([id])
const PriceRules = await service.listPriceRules({
id: [id],
})
expect(PriceRules).toHaveLength(0)
})
})
describe("update", () => {
const id = "price-set-1"
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceRules([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'PriceRule with id "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updatePriceRules([
{
random: "does-not-exist",
} as any,
])
} catch (e) {
error = e
}
expect(error.message).toEqual('PriceRule with id "" not found')
})
it("should create a PriceRule successfully", async () => {
const price: Price = testManager.create(Price, {
currency_code: "EUR",
amount: 100,
price_set_id: "price-set-1",
title: "test",
rules_count: 0,
})
await testManager.persist(price).flush()
await service.createPriceRules({
id: "price-rule-new",
price_set_id: "price-set-1",
rule_type_id: "rule-type-1",
value: "region_1",
price_list_id: "test",
price_id: price.id,
})
const [priceRule] = await service.listPriceRules({
id: ["price-rule-new"],
})
expect(priceRule).toEqual(
expect.objectContaining({
id: "price-rule-new",
})
)
})
})
})
})
},
})

View File

@@ -0,0 +1,782 @@
import { Modules } from "@medusajs/modules-sdk"
import {
CreatePriceSetDTO,
CreatePriceSetRuleTypeDTO,
IPricingModuleService,
} from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { PriceSetRuleType } from "../../../../src"
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({
moduleName: Modules.PRICING,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
describe("PricingModule Service - PriceSet", () => {
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", () => {
it("list priceSets", async () => {
const priceSetsResult = await service.list()
expect(priceSetsResult).toEqual([
expect.objectContaining({
id: "price-set-1",
}),
expect.objectContaining({
id: "price-set-2",
}),
expect.objectContaining({
id: "price-set-3",
}),
])
})
it("list priceSets by id", async () => {
const priceSetsResult = await service.list({
id: ["price-set-1"],
})
expect(priceSetsResult).toEqual([
expect.objectContaining({
id: "price-set-1",
}),
])
})
it("list priceSets with relations and selects", async () => {
const priceSetsResult = await service.list(
{
id: ["price-set-1"],
},
{
select: ["id", "prices.id", "prices.amount"],
relations: ["prices"],
}
)
const serialized = JSON.parse(JSON.stringify(priceSetsResult))
expect(serialized).toEqual([
{
id: "price-set-1",
prices: [
expect.objectContaining({
id: "price-set-money-amount-USD",
amount: 500,
}),
],
},
])
})
})
describe("listAndCount", () => {
it("should return priceSets and count", async () => {
const [priceSetsResult, count] = await service.listAndCount()
expect(count).toEqual(3)
expect(priceSetsResult).toEqual([
expect.objectContaining({
id: "price-set-1",
}),
expect.objectContaining({
id: "price-set-2",
}),
expect.objectContaining({
id: "price-set-3",
}),
])
})
it("should return priceSets and count when filtered", async () => {
const [priceSetsResult, count] = await service.listAndCount({
id: ["price-set-1"],
})
expect(count).toEqual(1)
expect(priceSetsResult).toEqual([
expect.objectContaining({
id: "price-set-1",
}),
])
})
it("list priceSets with relations and selects", async () => {
const [priceSetsResult, count] = await service.listAndCount(
{
id: ["price-set-1"],
},
{
select: ["id", "prices.amount", "prices.id"],
relations: ["prices"],
}
)
const serialized = JSON.parse(JSON.stringify(priceSetsResult))
expect(count).toEqual(1)
expect(serialized).toEqual([
{
id: "price-set-1",
prices: [
expect.objectContaining({
id: "price-set-money-amount-USD",
amount: 500,
}),
],
},
])
})
it("should return priceSets and count when using skip and take", async () => {
const [priceSetsResult, count] = await service.listAndCount(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(3)
expect(priceSetsResult).toEqual([
expect.objectContaining({
id: "price-set-2",
}),
])
})
it("should return requested fields", async () => {
const [priceSetsResult, count] = await service.listAndCount(
{},
{
take: 1,
select: ["id"],
}
)
const serialized = JSON.parse(JSON.stringify(priceSetsResult))
expect(count).toEqual(3)
expect(serialized).toEqual([
{
id: "price-set-1",
},
])
})
})
describe("retrieve", () => {
const id = "price-set-1"
it("should return priceSet for the given id", async () => {
const priceSet = await service.retrieve(id)
expect(priceSet).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when priceSet with id does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"PriceSet with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("priceSet - id must be defined")
})
it("should return priceSet based on config select param", async () => {
const priceSet = await service.retrieve(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(priceSet))
expect(serialized).toEqual({
id,
})
})
})
describe("delete", () => {
const id = "price-set-1"
it("should delete the priceSets given an id successfully", async () => {
await service.delete([id])
const priceSets = await service.list({
id: [id],
})
expect(priceSets).toHaveLength(0)
})
})
describe("update", () => {
const id = "price-set-1"
it("should throw an error when an id does not exist", async () => {
let error = await service
.update("does-not-exist", {})
.catch((e) => e.message)
expect(error).toEqual(
"PriceSet with id: does-not-exist was not found"
)
})
it("should create, update, and delete prices to a price set", async () => {
const priceSetBefore = await service.retrieve(id, {
relations: ["prices"],
})
const updateResponse = await service.update(priceSetBefore.id, {
prices: [
{ amount: 100, currency_code: "USD" },
{ amount: 200, currency_code: "EUR" },
],
})
const priceSetAfter = await service.retrieve(id, {
relations: ["prices"],
})
expect(priceSetBefore.prices).toHaveLength(1)
expect(priceSetBefore.prices?.[0]).toEqual(
expect.objectContaining({
amount: 500,
currency_code: "USD",
})
)
expect(priceSetAfter.prices).toHaveLength(2)
expect(priceSetAfter.prices).toEqual(
expect.arrayContaining([
expect.objectContaining({
amount: 100,
currency_code: "USD",
}),
expect.objectContaining({
amount: 200,
currency_code: "EUR",
}),
])
)
expect(updateResponse.prices).toHaveLength(2)
expect(updateResponse.prices).toEqual(
expect.arrayContaining([
expect.objectContaining({
amount: 100,
currency_code: "USD",
}),
expect.objectContaining({
amount: 200,
currency_code: "EUR",
}),
])
)
})
})
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.create([
{
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
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",
}),
],
prices: [
expect.objectContaining({
amount: 100,
currency_code: "USD",
rules_count: 1,
}),
],
})
)
})
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",
},
],
},
])
expect(priceSet).toEqual(
expect.objectContaining({
rule_types: [
expect.objectContaining({
rule_attribute: "region_id",
}),
],
prices: 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 amounts", 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",
}),
],
prices: [
expect.objectContaining({
amount: 100,
currency_code: "USD",
}),
],
price_rules: [
expect.objectContaining({
value: "10",
}),
],
})
)
})
it("should create a priceSet successfully", async () => {
await service.create([
{
id: "price-set-new",
} as unknown as CreatePriceSetDTO,
])
const [priceSet] = await service.list({
id: ["price-set-new"],
})
expect(priceSet).toEqual(
expect.objectContaining({
id: "price-set-new",
})
)
})
})
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", "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.list(
{ 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([
{
priceSetId: "price-set-1",
prices: [
{
amount: 100,
currency_code: "USD",
rules: { currency_code: "USD" },
},
],
},
])
const [priceSet] = await service.list(
{ id: ["price-set-1"] },
{ relations: ["prices"] }
)
expect(priceSet).toEqual(
expect.objectContaining({
id: "price-set-1",
prices: 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: ["prices"] }
)
expect(priceSets).toEqual([
expect.objectContaining({
id: "price-set-1",
prices: expect.arrayContaining([
expect.objectContaining({
amount: 100,
currency_code: "USD",
}),
]),
}),
expect.objectContaining({
id: "price-set-2",
prices: 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"
)
})
})
})
},
})

View File

@@ -0,0 +1,242 @@
import { createRuleTypes } from "../../../__fixtures__/rule-type"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { IPricingModuleService } from "@medusajs/types"
jest.setTimeout(30000)
moduleIntegrationTestRunner({
moduleName: Modules.PRICING,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPricingModuleService>) => {
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",
})
)
})
})
})
},
})