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:
@@ -1,24 +0,0 @@
|
||||
import { Currency } from "@models"
|
||||
import { asValue } from "awilix"
|
||||
|
||||
;(Currency as any).meta = {
|
||||
/**
|
||||
* Need to mock the Currency model as well to expose the primary keys when it is different than `id`
|
||||
*/
|
||||
primaryKeys: ["code"],
|
||||
}
|
||||
|
||||
export const nonExistingCurrencyCode = "non-existing-code"
|
||||
export const currencyRepositoryMock = {
|
||||
currencyRepository: asValue({
|
||||
find: jest.fn().mockImplementation(async ({ where: { code } }) => {
|
||||
if (code === nonExistingCurrencyCode) {
|
||||
return []
|
||||
}
|
||||
|
||||
return [{}]
|
||||
}),
|
||||
findAndCount: jest.fn().mockResolvedValue([[], 0]),
|
||||
getFreshManager: jest.fn().mockResolvedValue({}),
|
||||
}),
|
||||
}
|
||||
@@ -1,228 +0,0 @@
|
||||
import {
|
||||
currencyRepositoryMock,
|
||||
nonExistingCurrencyCode,
|
||||
} from "../__fixtures__/currency"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { asValue } from "awilix"
|
||||
import ContainerLoader from "../../loaders/container"
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
|
||||
const code = "existing-currency"
|
||||
|
||||
describe("Currency service", function () {
|
||||
let container: MedusaContainer
|
||||
|
||||
beforeEach(async function () {
|
||||
jest.clearAllMocks()
|
||||
|
||||
container = createMedusaContainer()
|
||||
container.register("manager", asValue({}))
|
||||
|
||||
await ContainerLoader({ container })
|
||||
|
||||
container.register(currencyRepositoryMock)
|
||||
})
|
||||
|
||||
it("should retrieve a currency", async function () {
|
||||
const currencyService = container.resolve("currencyService")
|
||||
const currencyRepository = container.resolve("currencyRepository")
|
||||
|
||||
await currencyService.retrieve(code)
|
||||
|
||||
expect(currencyRepository.find).toHaveBeenCalledWith(
|
||||
{
|
||||
where: {
|
||||
code,
|
||||
},
|
||||
options: {
|
||||
fields: undefined,
|
||||
limit: 15,
|
||||
offset: 0,
|
||||
populate: [],
|
||||
},
|
||||
},
|
||||
expect.any(Object)
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail to retrieve a currency", async function () {
|
||||
const currencyService = container.resolve("currencyService")
|
||||
const currencyRepository = container.resolve("currencyRepository")
|
||||
|
||||
const err = await currencyService
|
||||
.retrieve(nonExistingCurrencyCode)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(currencyRepository.find).toHaveBeenCalledWith(
|
||||
{
|
||||
where: {
|
||||
code: nonExistingCurrencyCode,
|
||||
},
|
||||
options: {
|
||||
fields: undefined,
|
||||
limit: 15,
|
||||
offset: 0,
|
||||
populate: [],
|
||||
withDeleted: undefined,
|
||||
},
|
||||
},
|
||||
expect.any(Object)
|
||||
)
|
||||
|
||||
expect(err.message).toBe(
|
||||
`Currency with code: ${nonExistingCurrencyCode} was not found`
|
||||
)
|
||||
})
|
||||
|
||||
it("should list currencys", async function () {
|
||||
const currencyService = container.resolve("currencyService")
|
||||
const currencyRepository = container.resolve("currencyRepository")
|
||||
|
||||
const filters = {}
|
||||
const config = {
|
||||
relations: [],
|
||||
}
|
||||
|
||||
await currencyService.list(filters, config)
|
||||
|
||||
expect(currencyRepository.find).toHaveBeenCalledWith(
|
||||
{
|
||||
where: {},
|
||||
options: {
|
||||
fields: undefined,
|
||||
limit: 15,
|
||||
offset: 0,
|
||||
orderBy: {
|
||||
code: "ASC",
|
||||
},
|
||||
populate: [],
|
||||
withDeleted: undefined,
|
||||
},
|
||||
},
|
||||
expect.any(Object)
|
||||
)
|
||||
})
|
||||
|
||||
it("should list currencys with filters", async function () {
|
||||
const currencyService = container.resolve("currencyService")
|
||||
const currencyRepository = container.resolve("currencyRepository")
|
||||
|
||||
const filters = {
|
||||
tags: {
|
||||
value: {
|
||||
$in: ["test"],
|
||||
},
|
||||
},
|
||||
}
|
||||
const config = {
|
||||
relations: [],
|
||||
}
|
||||
|
||||
await currencyService.list(filters, config)
|
||||
|
||||
expect(currencyRepository.find).toHaveBeenCalledWith(
|
||||
{
|
||||
where: {
|
||||
tags: {
|
||||
value: {
|
||||
$in: ["test"],
|
||||
},
|
||||
},
|
||||
},
|
||||
options: {
|
||||
fields: undefined,
|
||||
limit: 15,
|
||||
offset: 0,
|
||||
orderBy: {
|
||||
code: "ASC",
|
||||
},
|
||||
populate: [],
|
||||
withDeleted: undefined,
|
||||
},
|
||||
},
|
||||
expect.any(Object)
|
||||
)
|
||||
})
|
||||
|
||||
it("should list currencys with filters and relations", async function () {
|
||||
const currencyService = container.resolve("currencyService")
|
||||
const currencyRepository = container.resolve("currencyRepository")
|
||||
|
||||
const filters = {
|
||||
tags: {
|
||||
value: {
|
||||
$in: ["test"],
|
||||
},
|
||||
},
|
||||
}
|
||||
const config = {
|
||||
relations: ["tags"],
|
||||
}
|
||||
|
||||
await currencyService.list(filters, config)
|
||||
|
||||
expect(currencyRepository.find).toHaveBeenCalledWith(
|
||||
{
|
||||
where: {
|
||||
tags: {
|
||||
value: {
|
||||
$in: ["test"],
|
||||
},
|
||||
},
|
||||
},
|
||||
options: {
|
||||
fields: undefined,
|
||||
limit: 15,
|
||||
offset: 0,
|
||||
orderBy: {
|
||||
code: "ASC",
|
||||
},
|
||||
withDeleted: undefined,
|
||||
populate: ["tags"],
|
||||
},
|
||||
},
|
||||
expect.any(Object)
|
||||
)
|
||||
})
|
||||
|
||||
it("should list and count the currencies with filters and relations", async function () {
|
||||
const currencyService = container.resolve("currencyService")
|
||||
const currencyRepository = container.resolve("currencyRepository")
|
||||
|
||||
const filters = {
|
||||
tags: {
|
||||
value: {
|
||||
$in: ["test"],
|
||||
},
|
||||
},
|
||||
}
|
||||
const config = {
|
||||
relations: ["tags"],
|
||||
}
|
||||
|
||||
await currencyService.listAndCount(filters, config)
|
||||
|
||||
expect(currencyRepository.findAndCount).toHaveBeenCalledWith(
|
||||
{
|
||||
where: {
|
||||
tags: {
|
||||
value: {
|
||||
$in: ["test"],
|
||||
},
|
||||
},
|
||||
},
|
||||
options: {
|
||||
fields: undefined,
|
||||
limit: 15,
|
||||
offset: 0,
|
||||
orderBy: {
|
||||
code: "ASC",
|
||||
},
|
||||
withDeleted: undefined,
|
||||
populate: ["tags"],
|
||||
},
|
||||
},
|
||||
expect.any(Object)
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,6 @@
|
||||
describe("Noop test", () => {
|
||||
it("noop check", async () => {
|
||||
expect(true).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -28,7 +28,6 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import {
|
||||
Currency,
|
||||
MoneyAmount,
|
||||
PriceList,
|
||||
PriceListRule,
|
||||
@@ -55,7 +54,6 @@ import { ServiceTypes } from "@types"
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
pricingRepository: PricingRepositoryService
|
||||
currencyService: ModulesSdkTypes.InternalModuleService<any>
|
||||
moneyAmountService: ModulesSdkTypes.InternalModuleService<any>
|
||||
priceSetService: ModulesSdkTypes.InternalModuleService<any>
|
||||
priceSetMoneyAmountRulesService: ModulesSdkTypes.InternalModuleService<any>
|
||||
@@ -69,7 +67,6 @@ type InjectedDependencies = {
|
||||
}
|
||||
|
||||
const generateMethodForModels = [
|
||||
Currency,
|
||||
MoneyAmount,
|
||||
PriceList,
|
||||
PriceListRule,
|
||||
@@ -84,7 +81,6 @@ const generateMethodForModels = [
|
||||
export default class PricingModuleService<
|
||||
TPriceSet extends PriceSet = PriceSet,
|
||||
TMoneyAmount extends MoneyAmount = MoneyAmount,
|
||||
TCurrency extends Currency = Currency,
|
||||
TRuleType extends RuleType = RuleType,
|
||||
TPriceSetMoneyAmountRules extends PriceSetMoneyAmountRules = PriceSetMoneyAmountRules,
|
||||
TPriceRule extends PriceRule = PriceRule,
|
||||
@@ -98,7 +94,6 @@ export default class PricingModuleService<
|
||||
InjectedDependencies,
|
||||
PricingTypes.PriceSetDTO,
|
||||
{
|
||||
Currency: { dto: PricingTypes.CurrencyDTO }
|
||||
MoneyAmount: { dto: PricingTypes.MoneyAmountDTO }
|
||||
PriceSetMoneyAmount: { dto: PricingTypes.PriceSetMoneyAmountDTO }
|
||||
PriceSetMoneyAmountRules: {
|
||||
@@ -114,7 +109,6 @@ export default class PricingModuleService<
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly pricingRepository_: PricingRepositoryService
|
||||
protected readonly currencyService_: ModulesSdkTypes.InternalModuleService<TCurrency>
|
||||
protected readonly moneyAmountService_: ModulesSdkTypes.InternalModuleService<TMoneyAmount>
|
||||
protected readonly ruleTypeService_: RuleTypeService<TRuleType>
|
||||
protected readonly priceSetService_: ModulesSdkTypes.InternalModuleService<TPriceSet>
|
||||
@@ -131,7 +125,6 @@ export default class PricingModuleService<
|
||||
baseRepository,
|
||||
pricingRepository,
|
||||
moneyAmountService,
|
||||
currencyService,
|
||||
ruleTypeService,
|
||||
priceSetService,
|
||||
priceSetMoneyAmountRulesService,
|
||||
@@ -149,7 +142,6 @@ export default class PricingModuleService<
|
||||
|
||||
this.baseRepository_ = baseRepository
|
||||
this.pricingRepository_ = pricingRepository
|
||||
this.currencyService_ = currencyService
|
||||
this.moneyAmountService_ = moneyAmountService
|
||||
this.ruleTypeService_ = ruleTypeService
|
||||
this.priceSetService_ = priceSetService
|
||||
@@ -749,36 +741,6 @@ export default class PricingModuleService<
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createCurrencies(
|
||||
data: PricingTypes.CreateCurrencyDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const currencies = await this.currencyService_.create(data, sharedContext)
|
||||
|
||||
return await this.baseRepository_.serialize<PricingTypes.CurrencyDTO[]>(
|
||||
currencies,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async updateCurrencies(
|
||||
data: PricingTypes.UpdateCurrencyDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const currencies = await this.currencyService_.update(data, sharedContext)
|
||||
|
||||
return await this.baseRepository_.serialize<PricingTypes.CurrencyDTO[]>(
|
||||
currencies,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@InjectTransactionManager("baseRepository_")
|
||||
async createRuleTypes(
|
||||
data: PricingTypes.CreateRuleTypeDTO[],
|
||||
|
||||
Reference in New Issue
Block a user