Chore/rm main entity concept (#7709)
**What** Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate. This pr also includes various fixes in different modules Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
2895ccfba8
commit
48963f55ef
@@ -1,19 +1,19 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ICurrencyModuleService } from "@medusajs/types"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleIntegrationTestRunner<ICurrencyModuleService>({
|
||||
moduleName: Modules.CURRENCY,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<ICurrencyModuleService>) => {
|
||||
testSuite: ({ service }) => {
|
||||
describe("Currency Module Service", () => {
|
||||
describe("list", () => {
|
||||
it("list currencies", async () => {
|
||||
const currenciesResult = await service.list({}, { take: null })
|
||||
const currenciesResult = await service.listCurrencies(
|
||||
{},
|
||||
{ take: null }
|
||||
)
|
||||
expect(currenciesResult).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
@@ -31,7 +31,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("list currencies by code", async () => {
|
||||
const currenciesResult = await service.list(
|
||||
const currenciesResult = await service.listCurrencies(
|
||||
{ code: ["usd"] },
|
||||
{ take: null }
|
||||
)
|
||||
@@ -45,7 +45,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("list currencies by code regardless of case-sensitivity", async () => {
|
||||
const currenciesResult = await service.list(
|
||||
const currenciesResult = await service.listCurrencies(
|
||||
{ code: ["Usd"] },
|
||||
{ take: null }
|
||||
)
|
||||
@@ -59,12 +59,10 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
describe("listAndCountCurrenciesCurrencies", () => {
|
||||
it("should return currencies and count", async () => {
|
||||
const [currenciesResult, count] = await service.listAndCount(
|
||||
{},
|
||||
{ take: null }
|
||||
)
|
||||
const [currenciesResult, count] =
|
||||
await service.listAndCountCurrencies({}, { take: null })
|
||||
|
||||
expect(count).toEqual(120)
|
||||
expect(currenciesResult).toEqual(
|
||||
@@ -82,12 +80,13 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return currencies and count when filtered", async () => {
|
||||
const [currenciesResult, count] = await service.listAndCount(
|
||||
{
|
||||
code: ["usd"],
|
||||
},
|
||||
{ take: null }
|
||||
)
|
||||
const [currenciesResult, count] =
|
||||
await service.listAndCountCurrencies(
|
||||
{
|
||||
code: ["usd"],
|
||||
},
|
||||
{ take: null }
|
||||
)
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(currenciesResult).toEqual([
|
||||
@@ -99,10 +98,8 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return currencies and count when using skip and take", async () => {
|
||||
const [currenciesResult, count] = await service.listAndCount(
|
||||
{},
|
||||
{ skip: 5, take: 1 }
|
||||
)
|
||||
const [currenciesResult, count] =
|
||||
await service.listAndCountCurrencies({}, { skip: 5, take: 1 })
|
||||
|
||||
expect(count).toEqual(120)
|
||||
expect(currenciesResult).toEqual([
|
||||
@@ -114,13 +111,14 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return requested fields", async () => {
|
||||
const [currenciesResult, count] = await service.listAndCount(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["code", "rounding"],
|
||||
}
|
||||
)
|
||||
const [currenciesResult, count] =
|
||||
await service.listAndCountCurrencies(
|
||||
{},
|
||||
{
|
||||
take: 1,
|
||||
select: ["code", "rounding"],
|
||||
}
|
||||
)
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(currenciesResult))
|
||||
|
||||
@@ -140,7 +138,7 @@ moduleIntegrationTestRunner({
|
||||
const name = "US Dollar"
|
||||
|
||||
it("should return currency for the given code", async () => {
|
||||
const currency = await service.retrieve(code)
|
||||
const currency = await service.retrieveCurrency(code)
|
||||
|
||||
expect(currency).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -150,7 +148,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return currency for the given code in a case-insensitive manner", async () => {
|
||||
const currency = await service.retrieve(code.toUpperCase())
|
||||
const currency = await service.retrieveCurrency(code.toUpperCase())
|
||||
|
||||
expect(currency).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -163,7 +161,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
await service.retrieveCurrency("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
@@ -177,7 +175,7 @@ moduleIntegrationTestRunner({
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
await service.retrieveCurrency(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
@@ -186,7 +184,7 @@ moduleIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should return currency based on config select param", async () => {
|
||||
const currency = await service.retrieve(code, {
|
||||
const currency = await service.retrieveCurrency(code, {
|
||||
select: ["code", "name"],
|
||||
})
|
||||
|
||||
|
||||
@@ -8,10 +8,7 @@
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-currency-seed": "dist/scripts/bin/run-seed.js"
|
||||
"node": ">=20"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { CurrencyModuleService } from "@services"
|
||||
import initialDataLoader from "./loaders/initial-data"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
|
||||
export * from "./types"
|
||||
export * from "./models"
|
||||
export * from "./services"
|
||||
const service = CurrencyModuleService
|
||||
const loaders = [initialDataLoader]
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -1,33 +1,11 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import Currency from "./models/currency"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
export const LinkableKeys: Record<string, string> = {
|
||||
code: Currency.name,
|
||||
currency_code: Currency.name,
|
||||
default_currency_code: Currency.name,
|
||||
}
|
||||
export const joinerConfig = defineJoinerConfig(Modules.CURRENCY)
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.CURRENCY,
|
||||
primaryKeys: ["code"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
name: ["currency", "currencies"],
|
||||
args: { entity: Currency.name },
|
||||
},
|
||||
],
|
||||
} as ModuleJoinerConfig
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { CurrencyModuleService } from "@services"
|
||||
import initialDataLoader from "./loaders/initial-data"
|
||||
|
||||
const service = CurrencyModuleService
|
||||
const loaders = [initialDataLoader]
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import * as Models from "@models"
|
||||
import { EOL } from "os"
|
||||
|
||||
const args = process.argv
|
||||
const path = args.pop() as string
|
||||
|
||||
export default (async () => {
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-currency-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
const run = ModulesSdkUtils.buildSeedScript({
|
||||
moduleName: Modules.CURRENCY,
|
||||
models: Models,
|
||||
pathToMigrations: __dirname + "/../../migrations",
|
||||
seedHandler: async ({ manager, data }) => {
|
||||
// TODO: Add seed logic
|
||||
},
|
||||
})
|
||||
await run({ path })
|
||||
})()
|
||||
@@ -10,29 +10,24 @@ import {
|
||||
ModuleJoinerConfig,
|
||||
ModulesSdkTypes,
|
||||
} from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
|
||||
import { Currency } from "@models"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
|
||||
const generateMethodForModels = {}
|
||||
import { MedusaService } from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
currencyService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
export default class CurrencyModuleService<TEntity extends Currency = Currency>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
CurrencyTypes.CurrencyDTO,
|
||||
{
|
||||
Currency: { dto: CurrencyTypes.CurrencyDTO }
|
||||
}
|
||||
>(Currency, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
export default class CurrencyModuleService
|
||||
extends MedusaService<{
|
||||
Currency: { dto: CurrencyTypes.CurrencyDTO }
|
||||
}>({ Currency }, entityNameToLinkableKeysMap)
|
||||
implements ICurrencyModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly currencyService_: ModulesSdkTypes.IMedusaInternalService<TEntity>
|
||||
protected readonly currencyService_: ModulesSdkTypes.IMedusaInternalService<Currency>
|
||||
|
||||
constructor(
|
||||
{ baseRepository, currencyService }: InjectedDependencies,
|
||||
@@ -48,36 +43,39 @@ export default class CurrencyModuleService<TEntity extends Currency = Currency>
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
retrieve(
|
||||
// @ts-expect-error
|
||||
async retrieveCurrency(
|
||||
code: string,
|
||||
config?: FindConfig<CurrencyTypes.CurrencyDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<CurrencyTypes.CurrencyDTO> {
|
||||
return this.currencyService_.retrieve(
|
||||
code?.toLowerCase(),
|
||||
return await super.retrieveCurrency(
|
||||
CurrencyModuleService.normalizeFilters({ code: [code] })!.code![0],
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
list(
|
||||
// @ts-expect-error
|
||||
async listCurrencies(
|
||||
filters?: FilterableCurrencyProps,
|
||||
config?: FindConfig<CurrencyTypes.CurrencyDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<CurrencyTypes.CurrencyDTO[]> {
|
||||
return this.currencyService_.list(
|
||||
return await super.listCurrencies(
|
||||
CurrencyModuleService.normalizeFilters(filters),
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
listAndCount(
|
||||
// @ts-expect-error
|
||||
async listAndCountCurrencies(
|
||||
filters?: FilterableCurrencyProps,
|
||||
config?: FindConfig<CurrencyTypes.CurrencyDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<[CurrencyTypes.CurrencyDTO[], number]> {
|
||||
return this.currencyService_.listAndCount(
|
||||
return await super.listAndCountCurrencies(
|
||||
CurrencyModuleService.normalizeFilters(filters),
|
||||
config,
|
||||
sharedContext
|
||||
|
||||
Reference in New Issue
Block a user