feat: Add currency module and remove currency models from region and pricing modules (#6536)

What:
- Creates a new currency module
- Removes currency model from the pricing module
- Removes currency model from region module
This commit is contained in:
Stevche Radevski
2024-02-29 15:09:59 +00:00
committed by GitHub
parent 06f706a51a
commit dc025302a1
102 changed files with 1502 additions and 2128 deletions
@@ -1,20 +0,0 @@
export const defaultCurrencyData = [
{
symbol: "$",
name: "US Dollar",
symbol_native: "$",
code: "USD",
},
{
symbol: "CA$",
name: "Canadian Dollar",
symbol_native: "$",
code: "CAD",
},
{
symbol: "€",
name: "Euro",
symbol_native: "€",
code: "EUR",
},
]
@@ -1,22 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Currency } from "@models"
import { defaultCurrencyData } from "./data"
export * from "./data"
export async function createCurrencies(
manager: SqlEntityManager,
currencyData: any[] = defaultCurrencyData
): Promise<Currency[]> {
const currencies: Currency[] = []
for (let curr of currencyData) {
const currency = manager.create(Currency, curr)
currencies.push(currency)
}
await manager.persistAndFlush(currencies)
return currencies
}
@@ -1,6 +1,5 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createCurrencies, defaultCurrencyData } from "./currency"
import { createMoneyAmounts, defaultMoneyAmountsData } from "./money-amount"
import { createPriceRules, defaultPriceRuleData } from "./price-rule"
import { createPriceSets, defaultPriceSetsData } from "./price-set"
@@ -21,14 +20,12 @@ export async function seedPriceData(
{
moneyAmountsData = defaultMoneyAmountsData,
priceSetsData = defaultPriceSetsData,
currencyData = defaultCurrencyData,
priceRuleData = defaultPriceRuleData,
priceSetMoneyAmountsData = defaultPriceSetMoneyAmountsData,
priceSetMoneyAmountRulesData = defaultPriceSetMoneyAmountRulesData,
ruleTypesData = defaultRuleTypesData,
} = {}
) {
await createCurrencies(testManager, currencyData)
await createMoneyAmounts(testManager, moneyAmountsData)
await createPriceSets(testManager, priceSetsData)
await createPriceSetMoneyAmounts(testManager, priceSetMoneyAmountsData)
@@ -1,278 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Currency } from "@models"
import { createCurrencies } from "../../../__fixtures__/currency"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
import { ModulesSdkTypes } from "@medusajs/types"
jest.setTimeout(30000)
describe("Currency Service", () => {
let service: ModulesSdkTypes.InternalModuleService<any>
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let data!: Currency[]
const currencyData = [
{
symbol: "$",
name: "US Dollar",
symbol_native: "$",
code: "USD",
},
{
symbol: "CA$",
name: "Canadian Dollar",
symbol_native: "$",
code: "CAD",
},
]
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("currencyService")
testManager = await MikroOrmWrapper.forkManager()
data = await createCurrencies(testManager, currencyData)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("list", () => {
it("list currencies", async () => {
const currenciesResult = await service.list()
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "CAD",
name: "Canadian Dollar",
}),
expect.objectContaining({
code: "USD",
name: "US Dollar",
}),
])
})
it("list currencies by code", async () => {
const currenciesResult = await service.list({ code: ["USD"] })
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "USD",
name: "US Dollar",
}),
])
})
})
describe("listAndCount", () => {
it("should return currencies and count", async () => {
const [currenciesResult, count] = await service.listAndCount()
expect(count).toEqual(2)
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "CAD",
name: "Canadian Dollar",
}),
expect.objectContaining({
code: "USD",
name: "US Dollar",
}),
])
})
it("should return currencies and count when filtered", async () => {
const [currenciesResult, count] = await service.listAndCount({
code: ["USD"],
})
expect(count).toEqual(1)
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "USD",
name: "US Dollar",
}),
])
})
it("should return currencies and count when using skip and take", async () => {
const [currenciesResult, count] = await service.listAndCount(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(2)
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "USD",
name: "US Dollar",
}),
])
})
it("should return requested fields", async () => {
const [currenciesResult, count] = await service.listAndCount(
{},
{
take: 1,
select: ["code"],
}
)
const serialized = JSON.parse(JSON.stringify(currenciesResult))
expect(count).toEqual(2)
expect(serialized).toEqual([
{
code: "CAD",
},
])
})
})
describe("retrieve", () => {
const code = "USD"
const name = "US Dollar"
it("should return currency for the given code", async () => {
const currency = await service.retrieve(code)
expect(currency).toEqual(
expect.objectContaining({
code,
})
)
})
it("should throw an error when currency with code does not exist", async () => {
let error
try {
await service.retrieve("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"Currency with code: does-not-exist was not found"
)
})
it("should throw an error when a code is not provided", async () => {
let error
try {
await service.retrieve(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("currency - code must be defined")
})
it("should return currency based on config select param", async () => {
const currency = await service.retrieve(code, {
select: ["code", "name"],
})
const serialized = JSON.parse(JSON.stringify(currency))
expect(serialized).toEqual({
code,
name,
})
})
})
describe("delete", () => {
const code = "USD"
it("should delete the currencies given an code successfully", async () => {
await service.delete([code])
const currencies = await service.list({
code: [code],
})
expect(currencies).toHaveLength(0)
})
})
describe("update", () => {
const code = "USD"
it("should update the name of the currency successfully", async () => {
await service.update([
{
code,
name: "United States Pounds",
},
])
const currency = await service.retrieve(code)
expect(currency.name).toEqual("United States Pounds")
})
it("should throw an error when a code does not exist", async () => {
let error
try {
await service.update([
{
code: "does-not-exist",
name: "UK",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'Currency with code "does-not-exist" not found'
)
})
})
describe("create", () => {
it("should create a currency successfully", async () => {
await service.create([
{
code: "TES",
name: "Test Dollars",
symbol: "TES1",
symbol_native: "TES2",
},
])
const [currency] = await service.list({
code: ["TES"],
})
expect(currency).toEqual(
expect.objectContaining({
code: "TES",
name: "Test Dollars",
symbol: "TES1",
symbol_native: "TES2",
})
)
})
})
})
@@ -3,7 +3,6 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Currency, MoneyAmount } from "@models"
import { MoneyAmountService } from "@services"
import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
@@ -17,7 +16,6 @@ describe("MoneyAmount Service", () => {
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let data!: MoneyAmount[]
let currencyData!: Currency[]
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
@@ -31,7 +29,6 @@ describe("MoneyAmount Service", () => {
service = container.resolve("moneyAmountService")
testManager = await MikroOrmWrapper.forkManager()
currencyData = await createCurrencies(testManager)
data = await createMoneyAmounts(testManager)
})
@@ -79,8 +76,7 @@ describe("MoneyAmount Service", () => {
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency.code", "amount"],
relations: ["currency"],
select: ["id", "min_quantity", "currency_code", "amount"],
}
)
@@ -92,9 +88,6 @@ describe("MoneyAmount Service", () => {
amount: 500,
min_quantity: "1",
currency_code: "USD",
currency: {
code: "USD",
},
},
])
})
@@ -105,8 +98,7 @@ describe("MoneyAmount Service", () => {
currency_code: ["USD"],
},
{
select: ["id", "min_quantity", "currency.code", "amount"],
relations: ["currency"],
select: ["id", "min_quantity", "currency_code", "amount"],
}
)
@@ -118,9 +110,6 @@ describe("MoneyAmount Service", () => {
min_quantity: "1",
currency_code: "USD",
amount: 500,
currency: {
code: "USD",
},
},
])
})
@@ -165,8 +154,7 @@ describe("MoneyAmount Service", () => {
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency.code", "amount"],
relations: ["currency"],
select: ["id", "min_quantity", "currency_code", "amount"],
}
)
@@ -179,9 +167,6 @@ describe("MoneyAmount Service", () => {
amount: 500,
min_quantity: "1",
currency_code: "USD",
currency: {
code: "USD",
},
},
])
})
@@ -316,7 +301,6 @@ describe("MoneyAmount Service", () => {
const moneyAmount = await service.retrieve(id)
expect(moneyAmount.currency_code).toEqual("EUR")
expect(moneyAmount.currency?.code).toEqual("EUR")
})
it("should throw an error when a id does not exist", async () => {
@@ -3,7 +3,6 @@ import { PriceSetMoneyAmount } from "@models"
import { CreatePriceRuleDTO } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceRuleService } from "@services"
import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set"
@@ -34,7 +33,6 @@ describe("PriceRule Service", () => {
service = container.resolve("priceRuleService")
await createCurrencies(testManager)
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)
@@ -3,7 +3,6 @@ import { SqlEntityManager } from "@mikro-orm/postgresql"
import { MoneyAmount, PriceSet } from "@models"
import { PriceSetService } from "@services"
import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { MikroOrmWrapper } from "../../../utils"
@@ -63,8 +62,6 @@ describe("PriceSet Service", () => {
service = container.resolve("priceSetService")
await createCurrencies(testManager)
moneyAmountsData = await createMoneyAmounts(
testManager,
moneyAmountsInputData
@@ -85,21 +85,6 @@ describe("PricingModule Service - Calculate Price", () => {
describe("calculatePrices", () => {
beforeEach(async () => {
const currencyData = [
{
symbol: "zł",
name: "Polish Zloty",
symbol_native: "zł",
code: "PLN",
},
{
symbol: "€",
name: "Euro",
symbol_native: "€",
code: "EUR",
},
]
const moneyAmountsData = [
{
id: "money-amount-PLN",
@@ -373,7 +358,6 @@ describe("PricingModule Service - Calculate Price", () => {
] as unknown as CreatePriceRuleDTO[]
await seedPriceData(MikroOrmWrapper.forkManager(), {
currencyData,
moneyAmountsData,
priceSetsData,
priceSetMoneyAmountsData,
@@ -1,277 +0,0 @@
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Currency } from "@models"
import { createCurrencies } from "../../../__fixtures__/currency"
import { MikroOrmWrapper } from "../../../utils"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { Modules } from "@medusajs/modules-sdk"
import { initModules } from "medusa-test-utils"
describe("PricingModule Service - Currency", () => {
let service: IPricingModuleService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let data!: Currency[]
let shutdownFunc: () => Promise<void>
beforeAll(async () => {
const initModulesConfig = getInitModuleConfig()
const { medusaApp, shutdown } = await initModules(initModulesConfig)
service = medusaApp.modules[Modules.PRICING]
shutdownFunc = shutdown
})
afterAll(async () => {
await shutdownFunc()
})
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = MikroOrmWrapper.forkManager()
testManager = MikroOrmWrapper.forkManager()
data = await createCurrencies(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("listCurrencies", () => {
it("list currencies", async () => {
const currenciesResult = await service.listCurrencies()
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "CAD",
name: "Canadian Dollar",
}),
expect.objectContaining({
code: "EUR",
name: "Euro",
}),
expect.objectContaining({
code: "USD",
name: "US Dollar",
}),
])
})
it("list currencies by code", async () => {
const currenciesResult = await service.listCurrencies({ code: ["USD"] })
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "USD",
name: "US Dollar",
}),
])
})
})
describe("listAndCountCurrencies", () => {
it("should return currencies and count", async () => {
const [currenciesResult, count] = await service.listAndCountCurrencies()
expect(count).toEqual(3)
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "CAD",
name: "Canadian Dollar",
}),
expect.objectContaining({
code: "EUR",
name: "Euro",
}),
expect.objectContaining({
code: "USD",
name: "US Dollar",
}),
])
})
it("should return currencies and count when filtered", async () => {
const [currenciesResult, count] = await service.listAndCountCurrencies({
code: ["USD"],
})
expect(count).toEqual(1)
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "USD",
name: "US Dollar",
}),
])
})
it("should return currencies and count when using skip and take", async () => {
const [currenciesResult, count] = await service.listAndCountCurrencies(
{},
{ skip: 1, take: 1 }
)
expect(count).toEqual(3)
expect(currenciesResult).toEqual([
expect.objectContaining({
code: "EUR",
name: "Euro",
symbol: "€",
symbol_native: "€",
}),
])
})
it("should return requested fields", async () => {
const [currenciesResult, count] = await service.listAndCountCurrencies(
{},
{
take: 1,
select: ["code"],
}
)
const serialized = JSON.parse(JSON.stringify(currenciesResult))
expect(count).toEqual(3)
expect(serialized).toEqual([
{
code: "CAD",
},
])
})
})
describe("retrieveCurrency", () => {
const code = "USD"
const name = "US Dollar"
it("should return currency for the given code", async () => {
const currency = await service.retrieveCurrency(code)
expect(currency).toEqual(
expect.objectContaining({
code,
})
)
})
it("should throw an error when currency with code does not exist", async () => {
let error
try {
await service.retrieveCurrency("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"Currency with code: does-not-exist was not found"
)
})
it("should throw an error when a code is not provided", async () => {
let error
try {
await service.retrieveCurrency(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("currency - code must be defined")
})
it("should return currency based on config select param", async () => {
const currency = await service.retrieveCurrency(code, {
select: ["code", "name"],
})
const serialized = JSON.parse(JSON.stringify(currency))
expect(serialized).toEqual({
code,
name,
})
})
})
describe("deleteCurrencies", () => {
const code = "USD"
it("should delete the currencies given an code successfully", async () => {
await service.deleteCurrencies([code])
const currencies = await service.listCurrencies({
code: [code],
})
expect(currencies).toHaveLength(0)
})
})
describe("updateCurrencies", () => {
const code = "USD"
it("should update the name of the currency successfully", async () => {
await service.updateCurrencies([
{
code,
name: "United States Pounds",
},
])
const currency = await service.retrieveCurrency(code)
expect(currency.name).toEqual("United States Pounds")
})
it("should throw an error when a code does not exist", async () => {
let error
try {
await service.updateCurrencies([
{
code: "does-not-exist",
name: "UK",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'Currency with code "does-not-exist" not found'
)
})
})
describe("createCurrencies", () => {
it("should create a currency successfully", async () => {
await service.createCurrencies([
{
code: "TES",
name: "Test Dollars",
symbol: "TES1",
symbol_native: "TES2",
},
])
const [currency] = await service.listCurrencies({
code: ["TES"],
})
expect(currency).toEqual(
expect.objectContaining({
code: "TES",
name: "Test Dollars",
symbol: "TES1",
symbol_native: "TES2",
})
)
})
})
})
@@ -1,7 +1,6 @@
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Currency, MoneyAmount } from "@models"
import { createCurrencies } from "../../../__fixtures__/currency"
import { MoneyAmount } from "@models"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { MikroOrmWrapper } from "../../../utils"
import { createPriceSetMoneyAmounts } from "../../../__fixtures__/price-set-money-amount"
@@ -20,7 +19,6 @@ describe("PricingModule Service - MoneyAmount", () => {
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
let data!: MoneyAmount[]
let currencyData!: Currency[]
let shutdownFunc: () => Promise<void>
beforeAll(async () => {
@@ -42,7 +40,6 @@ describe("PricingModule Service - MoneyAmount", () => {
repositoryManager = MikroOrmWrapper.forkManager()
testManager = MikroOrmWrapper.forkManager()
currencyData = await createCurrencies(testManager)
data = await createMoneyAmounts(testManager)
})
@@ -90,8 +87,7 @@ describe("PricingModule Service - MoneyAmount", () => {
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency.code"],
relations: ["currency"],
select: ["id", "min_quantity", "currency_code"],
}
)
@@ -103,9 +99,6 @@ describe("PricingModule Service - MoneyAmount", () => {
amount: null,
min_quantity: "1",
currency_code: "USD",
currency: {
code: "USD",
},
},
])
})
@@ -153,8 +146,7 @@ describe("PricingModule Service - MoneyAmount", () => {
id: ["money-amount-USD"],
},
{
select: ["id", "min_quantity", "currency.code", "amount"],
relations: ["currency"],
select: ["id", "min_quantity", "currency_code", "amount"],
}
)
@@ -167,9 +159,6 @@ describe("PricingModule Service - MoneyAmount", () => {
amount: 500,
min_quantity: "1",
currency_code: "USD",
currency: {
code: "USD",
},
},
])
})
@@ -392,12 +381,9 @@ describe("PricingModule Service - MoneyAmount", () => {
},
])
const moneyAmount = await service.retrieveMoneyAmount(id, {
relations: ["currency"],
})
const moneyAmount = await service.retrieveMoneyAmount(id, {})
expect(moneyAmount.currency_code).toEqual("EUR")
expect(moneyAmount.currency?.code).toEqual("EUR")
})
it("should throw an error when a id does not exist", async () => {
@@ -2,7 +2,6 @@ import { MikroOrmWrapper } from "../../../utils"
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createCurrencies } from "../../../__fixtures__/currency"
import { createPriceLists } from "../../../__fixtures__/price-list"
import { createPriceSets } from "../../../__fixtures__/price-set"
import { Modules } from "@medusajs/modules-sdk"
@@ -35,7 +34,6 @@ describe("PriceList Service", () => {
await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
await createCurrencies(testManager)
await createPriceSets(testManager)
await createPriceLists(testManager)
await service.createRuleTypes([
@@ -2,7 +2,6 @@ import { CreatePriceRuleDTO, IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PriceSetMoneyAmount } from "../../../../src"
import { createCurrencies } from "../../../__fixtures__/currency"
import { createMoneyAmounts } from "../../../__fixtures__/money-amount"
import { createPriceRules } from "../../../__fixtures__/price-rule"
import { createPriceSets } from "../../../__fixtures__/price-set"
@@ -39,7 +38,6 @@ describe("PricingModule Service - PriceRule", () => {
await MikroOrmWrapper.setupDatabase()
testManager = MikroOrmWrapper.forkManager()
await createCurrencies(testManager)
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)
@@ -1,6 +1,5 @@
import { IPricingModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
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"
@@ -38,7 +37,6 @@ describe("PricingModule Service - PriceSetMoneyAmountRules", () => {
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
await createCurrencies(testManager)
await createMoneyAmounts(testManager)
await createPriceSets(testManager)
await createRuleTypes(testManager)