fix(utils): Fix inferrence of public method name and service registration name based on configuration (#8237)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { MedusaService } from "../medusa-service"
|
import { MedusaService } from "../medusa-service"
|
||||||
|
import { model } from "../../dml"
|
||||||
|
|
||||||
const baseRepoMock = {
|
const baseRepoMock = {
|
||||||
serialize: jest.fn().mockImplementation((item) => item),
|
serialize: jest.fn().mockImplementation((item) => item),
|
||||||
@@ -52,7 +53,7 @@ describe("Abstract Module Service Factory", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("Main Model Methods", () => {
|
describe("Main Model Methods", () => {
|
||||||
let instance: medusaService
|
let instance: InstanceType<typeof medusaService>
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks()
|
jest.clearAllMocks()
|
||||||
@@ -172,4 +173,109 @@ describe("Abstract Module Service Factory", () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("Custom configuration using DML", () => {
|
||||||
|
const containerMock = {
|
||||||
|
baseRepository: baseRepoMock,
|
||||||
|
mainModelMockRepository: baseRepoMock,
|
||||||
|
otherModelMock1Repository: baseRepoMock,
|
||||||
|
otherModelMock2Repository: baseRepoMock,
|
||||||
|
houseService: {
|
||||||
|
retrieve: jest.fn().mockResolvedValue({ id: "1", name: "Item" }),
|
||||||
|
list: jest.fn().mockResolvedValue([{ id: "1", name: "Item" }]),
|
||||||
|
delete: jest.fn().mockResolvedValue(undefined),
|
||||||
|
softDelete: jest.fn().mockResolvedValue([[], {}]),
|
||||||
|
restore: jest.fn().mockResolvedValue([[], {}]),
|
||||||
|
},
|
||||||
|
carService: {
|
||||||
|
retrieve: jest.fn().mockResolvedValue({ id: "1", name: "Item" }),
|
||||||
|
list: jest.fn().mockResolvedValue([{ id: "1", name: "Item" }]),
|
||||||
|
delete: jest.fn().mockResolvedValue(undefined),
|
||||||
|
softDelete: jest.fn().mockResolvedValue([[], {}]),
|
||||||
|
restore: jest.fn().mockResolvedValue([[], {}]),
|
||||||
|
},
|
||||||
|
userService: {
|
||||||
|
retrieve: jest.fn().mockResolvedValue({ id: "1", name: "Item" }),
|
||||||
|
list: jest.fn().mockResolvedValue([{ id: "1", name: "Item" }]),
|
||||||
|
delete: jest.fn().mockResolvedValue(undefined),
|
||||||
|
softDelete: jest.fn().mockResolvedValue([[], {}]),
|
||||||
|
restore: jest.fn().mockResolvedValue([[], {}]),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const MockModel = model.define("user", {
|
||||||
|
id: model.id().primaryKey(),
|
||||||
|
})
|
||||||
|
const MockModel2 = model.define("car", {
|
||||||
|
id: model.id().primaryKey(),
|
||||||
|
})
|
||||||
|
const MockModel3 = model.define("house", {
|
||||||
|
id: model.id().primaryKey(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const medusaService = MedusaService({
|
||||||
|
MockModel,
|
||||||
|
MockModel2,
|
||||||
|
MockModel3,
|
||||||
|
})
|
||||||
|
|
||||||
|
let instance: InstanceType<typeof medusaService>
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks()
|
||||||
|
instance = new medusaService(containerMock)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should have the correct methods name while resolving the correct underlying service representation of target models", async () => {
|
||||||
|
const prototype = Object.getPrototypeOf(instance)
|
||||||
|
|
||||||
|
expect(prototype).toHaveProperty("retrieveMockModel")
|
||||||
|
expect(prototype).toHaveProperty("listMockModels")
|
||||||
|
expect(prototype).toHaveProperty("listAndCountMockModels")
|
||||||
|
expect(prototype).toHaveProperty("createMockModels")
|
||||||
|
expect(prototype).toHaveProperty("updateMockModels")
|
||||||
|
expect(prototype).toHaveProperty("deleteMockModels")
|
||||||
|
expect(prototype).toHaveProperty("softDeleteMockModels")
|
||||||
|
|
||||||
|
expect(prototype).toHaveProperty("retrieveMockModel2")
|
||||||
|
expect(prototype).toHaveProperty("listMockModel2s")
|
||||||
|
expect(prototype).toHaveProperty("listAndCountMockModel2s")
|
||||||
|
expect(prototype).toHaveProperty("createMockModel2s")
|
||||||
|
expect(prototype).toHaveProperty("updateMockModel2s")
|
||||||
|
expect(prototype).toHaveProperty("deleteMockModel2s")
|
||||||
|
expect(prototype).toHaveProperty("softDeleteMockModel2s")
|
||||||
|
|
||||||
|
expect(prototype).toHaveProperty("retrieveMockModel3")
|
||||||
|
expect(prototype).toHaveProperty("listMockModel3s")
|
||||||
|
expect(prototype).toHaveProperty("listAndCountMockModel3s")
|
||||||
|
expect(prototype).toHaveProperty("createMockModel3s")
|
||||||
|
expect(prototype).toHaveProperty("updateMockModel3s")
|
||||||
|
expect(prototype).toHaveProperty("deleteMockModel3s")
|
||||||
|
expect(prototype).toHaveProperty("softDeleteMockModel3s")
|
||||||
|
|
||||||
|
let result = await instance.retrieveMockModel("1")
|
||||||
|
expect(result).toEqual({ id: "1", name: "Item" })
|
||||||
|
expect(containerMock.userService.retrieve).toHaveBeenCalledWith(
|
||||||
|
"1",
|
||||||
|
undefined,
|
||||||
|
defaultContext
|
||||||
|
)
|
||||||
|
|
||||||
|
result = await instance.retrieveMockModel2("1")
|
||||||
|
expect(result).toEqual({ id: "1", name: "Item" })
|
||||||
|
expect(containerMock.carService.retrieve).toHaveBeenCalledWith(
|
||||||
|
"1",
|
||||||
|
undefined,
|
||||||
|
defaultContext
|
||||||
|
)
|
||||||
|
|
||||||
|
result = await instance.retrieveMockModel3("1")
|
||||||
|
expect(result).toEqual({ id: "1", name: "Item" })
|
||||||
|
expect(containerMock.houseService.retrieve).toHaveBeenCalledWith(
|
||||||
|
"1",
|
||||||
|
undefined,
|
||||||
|
defaultContext
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import {
|
|||||||
ModelsConfigTemplate,
|
ModelsConfigTemplate,
|
||||||
} from "./types/medusa-service"
|
} from "./types/medusa-service"
|
||||||
import { buildModelsNameToLinkableKeysMap } from "./joiner-config-builder"
|
import { buildModelsNameToLinkableKeysMap } from "./joiner-config-builder"
|
||||||
|
import { DmlEntity } from "../dml"
|
||||||
|
|
||||||
const readMethods = ["retrieve", "list", "listAndCount"] as BaseMethods[]
|
const readMethods = ["retrieve", "list", "listAndCount"] as BaseMethods[]
|
||||||
const writeMethods = [
|
const writeMethods = [
|
||||||
@@ -46,7 +47,7 @@ const methods: BaseMethods[] = [...readMethods, ...writeMethods]
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
function buildMethodNamesFromModel(
|
function buildMethodNamesFromModel(
|
||||||
modelName: string,
|
defaultMethodName: string,
|
||||||
model: ModelEntries[keyof ModelEntries]
|
model: ModelEntries[keyof ModelEntries]
|
||||||
): Record<string, string> {
|
): Record<string, string> {
|
||||||
return methods.reduce((acc, method) => {
|
return methods.reduce((acc, method) => {
|
||||||
@@ -54,10 +55,14 @@ function buildMethodNamesFromModel(
|
|||||||
|
|
||||||
if (method === "retrieve") {
|
if (method === "retrieve") {
|
||||||
normalizedModelName =
|
normalizedModelName =
|
||||||
"singular" in model && model.singular ? model.singular : modelName
|
"singular" in model && model.singular
|
||||||
|
? model.singular
|
||||||
|
: defaultMethodName
|
||||||
} else {
|
} else {
|
||||||
normalizedModelName =
|
normalizedModelName =
|
||||||
"plural" in model && model.plural ? model.plural : pluralize(modelName)
|
"plural" in model && model.plural
|
||||||
|
? model.plural
|
||||||
|
: pluralize(defaultMethodName)
|
||||||
}
|
}
|
||||||
|
|
||||||
const methodName = `${method}${upperCaseFirst(normalizedModelName)}`
|
const methodName = `${method}${upperCaseFirst(normalizedModelName)}`
|
||||||
@@ -406,11 +411,11 @@ export function MedusaService<
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const modelsMethods: [
|
const modelsMethods: [
|
||||||
string,
|
string, // model name
|
||||||
TModels[keyof TModels],
|
TModels[keyof TModels], // configuration (dml, conf, entity)
|
||||||
Record<string, string>
|
Record<string, string> // method names
|
||||||
][] = Object.entries(models as {}).map(([name, config]) => [
|
][] = Object.entries(models as {}).map(([name, config]) => [
|
||||||
name,
|
DmlEntity.isDmlEntity(config) ? config.name : name,
|
||||||
config as TModels[keyof TModels],
|
config as TModels[keyof TModels],
|
||||||
buildMethodNamesFromModel(name, config as TModels[keyof TModels]),
|
buildMethodNamesFromModel(name, config as TModels[keyof TModels]),
|
||||||
])
|
])
|
||||||
|
|||||||
Reference in New Issue
Block a user