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:
Adrien de Peretti
2024-06-19 15:02:16 +02:00
committed by GitHub
parent 2895ccfba8
commit 48963f55ef
533 changed files with 6469 additions and 9769 deletions
@@ -32,5 +32,5 @@ export async function createAuthIdentities(
},
]
): Promise<AuthIdentity[]> {
return await service.create(userData)
return await service.createAuthIdentities(userData)
}
@@ -5,12 +5,9 @@ import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
moduleIntegrationTestRunner({
moduleIntegrationTestRunner<IAuthModuleService>({
moduleName: Modules.AUTH,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IAuthModuleService>) => {
testSuite: ({ service }) => {
describe("AuthModuleService - AuthIdentity", () => {
beforeEach(async () => {
await createAuthIdentities(service)
@@ -18,7 +15,7 @@ moduleIntegrationTestRunner({
describe("listAuthIdentities", () => {
it("should list authIdentities", async () => {
const authIdentities = await service.list(
const authIdentities = await service.listAuthIdentities(
{},
{ relations: ["provider_identities"] }
)
@@ -43,7 +40,7 @@ moduleIntegrationTestRunner({
})
it("should list authIdentities by id", async () => {
const authIdentities = await service.list({
const authIdentities = await service.listAuthIdentities({
id: ["test-id"],
})
@@ -55,7 +52,7 @@ moduleIntegrationTestRunner({
})
it("should list authIdentities by provider", async () => {
const authIdentities = await service.list({
const authIdentities = await service.listAuthIdentities({
provider_identities: {
provider: "manual",
},
@@ -74,10 +71,11 @@ moduleIntegrationTestRunner({
describe("listAndCountAuthIdentities", () => {
it("should list and count authIdentities", async () => {
const [authIdentities, count] = await service.listAndCount(
{},
{ relations: ["provider_identities"] }
)
const [authIdentities, count] =
await service.listAndCountAuthIdentities(
{},
{ relations: ["provider_identities"] }
)
expect(count).toEqual(3)
expect(authIdentities).toEqual([
@@ -100,9 +98,10 @@ moduleIntegrationTestRunner({
})
it("should listAndCount authIdentities by provider_id", async () => {
const [authIdentities, count] = await service.listAndCount({
provider_identities: { provider: "manual" },
})
const [authIdentities, count] =
await service.listAndCountAuthIdentities({
provider_identities: { provider: "manual" },
})
expect(count).toEqual(2)
expect(authIdentities).toEqual([
@@ -120,7 +119,7 @@ moduleIntegrationTestRunner({
const id = "test-id"
it("should return an authIdentity for the given id", async () => {
const authIdentity = await service.retrieve(id)
const authIdentity = await service.retrieveAuthIdentity(id)
expect(authIdentity).toEqual(
expect.objectContaining({
@@ -133,7 +132,7 @@ moduleIntegrationTestRunner({
let error
try {
await service.retrieve("does-not-exist")
await service.retrieveAuthIdentity("does-not-exist")
} catch (e) {
error = e
}
@@ -144,7 +143,7 @@ moduleIntegrationTestRunner({
})
it("should not return an authIdentity with password hash", async () => {
const authIdentity = await service.retrieve("test-id-1")
const authIdentity = await service.retrieveAuthIdentity("test-id-1")
expect(authIdentity).toEqual(
expect.objectContaining({
@@ -162,7 +161,7 @@ moduleIntegrationTestRunner({
let error
try {
await service.retrieve(undefined as unknown as string)
await service.retrieveAuthIdentity(undefined as unknown as string)
} catch (e) {
error = e
}
@@ -171,7 +170,7 @@ moduleIntegrationTestRunner({
})
it("should return authIdentity based on config select param", async () => {
const authIdentity = await service.retrieve(id, {
const authIdentity = await service.retrieveAuthIdentity(id, {
select: ["id"],
})
@@ -185,9 +184,9 @@ moduleIntegrationTestRunner({
const id = "test-id"
it("should delete the authIdentities given an id successfully", async () => {
await service.delete([id])
await service.deleteAuthIdentities([id])
const authIdentities = await service.list({
const authIdentities = await service.listAuthIdentities({
id: [id],
})
@@ -202,7 +201,7 @@ moduleIntegrationTestRunner({
let error
try {
await service.update([
await service.updateAuthIdentites([
{
id: "does-not-exist",
},
@@ -217,14 +216,14 @@ moduleIntegrationTestRunner({
})
it("should update authIdentity", async () => {
await service.update([
await service.updateAuthIdentites([
{
id,
app_metadata: { email: "test@email.com" },
},
])
const [authIdentity] = await service.list({ id: [id] })
const [authIdentity] = await service.listAuthIdentities({ id: [id] })
expect(authIdentity).toEqual(
expect.objectContaining({
app_metadata: { email: "test@email.com" },
@@ -235,7 +234,7 @@ moduleIntegrationTestRunner({
describe("createAuthIdentity", () => {
it("should create a authIdentity successfully", async () => {
await service.create([
await service.createAuthIdentities([
{
id: "test",
provider_identities: [
@@ -247,9 +246,10 @@ moduleIntegrationTestRunner({
},
])
const [authIdentity, count] = await service.listAndCount({
id: ["test"],
})
const [authIdentity, count] =
await service.listAndCountAuthIdentities({
id: ["test"],
})
expect(count).toEqual(1)
expect(authIdentity[0]).toEqual(
@@ -27,7 +27,7 @@ moduleIntegrationTestRunner({
testSuite: ({ service }: SuiteOptions<IAuthModuleService>) =>
describe("Auth Module Service", () => {
beforeEach(async () => {
await service.create({
await service.createAuthIdentities({
provider_identities: [
{
entity_id: "test@admin.com",
@@ -103,7 +103,7 @@ moduleIntegrationTestRunner({
},
})
const dbAuthIdentity = await service.retrieve(
const dbAuthIdentity = await service.retrieveAuthIdentity(
result.authIdentity?.id!,
{ relations: ["provider_identities"] }
)
+1 -4
View File
@@ -8,10 +8,7 @@
"dist"
],
"engines": {
"node": ">=16"
},
"bin": {
"medusa-auth-seed": "dist/scripts/bin/run-seed.js"
"node": ">=20"
},
"repository": {
"type": "git",
+7 -1
View File
@@ -1,3 +1,9 @@
import { moduleDefinition } from "./module-definition"
import { ModuleExports } from "@medusajs/types"
import { AuthModuleService } from "@services"
import loadProviders from "./loaders/providers"
const moduleDefinition: ModuleExports = {
service: AuthModuleService,
loaders: [loadProviders] as any,
}
export default moduleDefinition
+9 -26
View File
@@ -1,31 +1,14 @@
import { AuthIdentity } from "@models"
import { MapToConfig } from "@medusajs/utils"
import { ModuleJoinerConfig } from "@medusajs/types"
import {
buildEntitiesNameToLinkableKeysMap,
defineJoinerConfig,
MapToConfig,
} from "@medusajs/utils"
import { Modules } from "@medusajs/modules-sdk"
export const LinkableKeys = {
auth_identity_id: AuthIdentity.name,
}
const entityLinkableKeysMap: MapToConfig = {}
Object.entries(LinkableKeys).forEach(([key, value]) => {
entityLinkableKeysMap[value] ??= []
entityLinkableKeysMap[value].push({
mapTo: key,
valueFrom: key.split("_").pop()!,
})
export const joinerConfig = defineJoinerConfig(Modules.AUTH, {
entityQueryingConfig: [AuthIdentity],
})
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
export const joinerConfig: ModuleJoinerConfig = {
serviceName: Modules.AUTH,
primaryKeys: ["id"],
linkableKeys: LinkableKeys,
alias: {
name: ["auth_identity", "auth_identities"],
args: {
entity: AuthIdentity.name,
},
},
}
export const entityNameToLinkableKeysMap: MapToConfig =
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
@@ -1,11 +0,0 @@
import { ModuleExports } from "@medusajs/types"
import { AuthModuleService } from "@services"
import loadProviders from "./loaders/providers"
const service = AuthModuleService
const loaders = [loadProviders] as any
export const moduleDefinition: ModuleExports = {
service,
loaders,
}
@@ -1 +0,0 @@
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
@@ -18,7 +18,7 @@ import {
InjectManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
MedusaService,
} from "@medusajs/utils"
import AuthProviderService from "./auth-provider"
@@ -28,25 +28,16 @@ type InjectedDependencies = {
providerIdentityService: ModulesSdkTypes.IMedusaInternalService<any>
authProviderService: AuthProviderService
}
const generateMethodForModels = { AuthIdentity, ProviderIdentity }
export default class AuthModuleService<
TAuthIdentity extends AuthIdentity = AuthIdentity,
TProviderIdentity extends ProviderIdentity = ProviderIdentity
>
extends ModulesSdkUtils.MedusaService<
AuthTypes.AuthIdentityDTO,
{
AuthIdentity: { dto: AuthTypes.AuthIdentityDTO }
ProviderIdentity: { dto: AuthTypes.ProviderIdentityDTO }
}
>(AuthIdentity, generateMethodForModels, entityNameToLinkableKeysMap)
export default class AuthModuleService
extends MedusaService<{
AuthIdentity: { dto: AuthTypes.AuthIdentityDTO }
ProviderIdentity: { dto: AuthTypes.ProviderIdentityDTO }
}>({ AuthIdentity, ProviderIdentity }, entityNameToLinkableKeysMap)
implements AuthTypes.IAuthModuleService
{
protected baseRepository_: DAL.RepositoryService
protected authIdentityService_: ModulesSdkTypes.IMedusaInternalService<TAuthIdentity>
protected providerIdentityService_: ModulesSdkTypes.IMedusaInternalService<TProviderIdentity>
protected authIdentityService_: ModulesSdkTypes.IMedusaInternalService<AuthIdentity>
protected providerIdentityService_: ModulesSdkTypes.IMedusaInternalService<ProviderIdentity>
protected readonly authProviderService_: AuthProviderService
constructor(
@@ -71,18 +62,19 @@ export default class AuthModuleService<
return joinerConfig
}
create(
// @ts-expect-error
createAuthIdentities(
data: AuthTypes.CreateAuthIdentityDTO[],
sharedContext?: Context
): Promise<AuthTypes.AuthIdentityDTO[]>
create(
createAuthIdentities(
data: AuthTypes.CreateAuthIdentityDTO,
sharedContext?: Context
): Promise<AuthTypes.AuthIdentityDTO>
@InjectManager("baseRepository_")
async create(
async createAuthIdentities(
data: AuthTypes.CreateAuthIdentityDTO[] | AuthTypes.CreateAuthIdentityDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<AuthTypes.AuthIdentityDTO | AuthTypes.AuthIdentityDTO[]> {
@@ -99,19 +91,19 @@ export default class AuthModuleService<
)
}
update(
// TODO: Update to follow convention
updateAuthIdentites(
data: AuthTypes.UpdateAuthIdentityDTO[],
sharedContext?: Context
): Promise<AuthTypes.AuthIdentityDTO[]>
update(
updateAuthIdentites(
data: AuthTypes.UpdateAuthIdentityDTO,
sharedContext?: Context
): Promise<AuthTypes.AuthIdentityDTO>
// TODO: should be pluralized, see convention about the methods naming or the abstract module service interface definition @engineering
@InjectManager("baseRepository_")
async update(
async updateAuthIdentites(
data: AuthTypes.UpdateAuthIdentityDTO | AuthTypes.UpdateAuthIdentityDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<AuthTypes.AuthIdentityDTO | AuthTypes.AuthIdentityDTO[]> {