fix issues
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import type { Provider } from '@nestjs/common';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { InMemoryAdminPersistenceModule } from '../../persistence/inmemory/InMemoryAdminPersistenceModule';
|
||||
import { AdminService } from './AdminService';
|
||||
@@ -7,6 +8,7 @@ import { DashboardStatsPresenter } from './presenters/DashboardStatsPresenter';
|
||||
import { AuthModule } from '../auth/AuthModule';
|
||||
import { ListUsersUseCase } from '@core/admin/application/use-cases/ListUsersUseCase';
|
||||
import { GetDashboardStatsUseCase } from './use-cases/GetDashboardStatsUseCase';
|
||||
import { InitializationLogger } from '../../shared/logging/InitializationLogger';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ListUsersResult } from '@core/admin/application/use-cases/ListUsersUseCase';
|
||||
import type { DashboardStatsResult } from './use-cases/GetDashboardStatsUseCase';
|
||||
@@ -16,38 +18,80 @@ export const ADMIN_USER_REPOSITORY_TOKEN = 'IAdminUserRepository';
|
||||
export const LIST_USERS_OUTPUT_PORT_TOKEN = 'ListUsersOutputPort';
|
||||
export const DASHBOARD_STATS_OUTPUT_PORT_TOKEN = 'DashboardStatsOutputPort';
|
||||
|
||||
const initLogger = InitializationLogger.getInstance();
|
||||
|
||||
const adminProviders: Provider[] = [
|
||||
AdminService,
|
||||
ListUsersPresenter,
|
||||
DashboardStatsPresenter,
|
||||
{
|
||||
provide: LIST_USERS_OUTPUT_PORT_TOKEN,
|
||||
useExisting: ListUsersPresenter,
|
||||
},
|
||||
{
|
||||
provide: DASHBOARD_STATS_OUTPUT_PORT_TOKEN,
|
||||
useExisting: DashboardStatsPresenter,
|
||||
},
|
||||
{
|
||||
provide: ListUsersUseCase,
|
||||
useFactory: (
|
||||
repository: IAdminUserRepository,
|
||||
output: UseCaseOutputPort<ListUsersResult>,
|
||||
) => new ListUsersUseCase(repository, output),
|
||||
inject: [ADMIN_USER_REPOSITORY_TOKEN, LIST_USERS_OUTPUT_PORT_TOKEN],
|
||||
},
|
||||
{
|
||||
provide: GetDashboardStatsUseCase,
|
||||
useFactory: (
|
||||
repository: IAdminUserRepository,
|
||||
output: UseCaseOutputPort<DashboardStatsResult>,
|
||||
) => new GetDashboardStatsUseCase(repository, output),
|
||||
inject: [ADMIN_USER_REPOSITORY_TOKEN, DASHBOARD_STATS_OUTPUT_PORT_TOKEN],
|
||||
},
|
||||
];
|
||||
|
||||
// Diagnostics: Nest will crash with "metatype is not a constructor" if any provider resolves
|
||||
// to a non-constructable value (e.g. undefined import, object, etc.).
|
||||
for (const provider of adminProviders) {
|
||||
// Class providers are functions at runtime.
|
||||
if (typeof provider === 'function') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Custom providers should be objects with a `provide` token.
|
||||
if (!provider || typeof provider !== 'object' || !('provide' in provider)) {
|
||||
initLogger.error(
|
||||
`[AdminModule] Invalid provider entry (expected class or provider object): ${String(provider)}`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
const token = (provider as { provide: unknown }).provide;
|
||||
const tokenLabel = typeof token === 'function' ? token.name : String(token);
|
||||
|
||||
if ('useClass' in provider) {
|
||||
const useClass = (provider as { useClass?: unknown }).useClass;
|
||||
if (typeof useClass !== 'function') {
|
||||
initLogger.error(
|
||||
`[AdminModule] Provider "${tokenLabel}" has non-constructable useClass: ${String(useClass)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ('useExisting' in provider) {
|
||||
const useExisting = (provider as { useExisting?: unknown }).useExisting;
|
||||
if (typeof useExisting !== 'function' && typeof useExisting !== 'string' && typeof useExisting !== 'symbol') {
|
||||
initLogger.warn(
|
||||
`[AdminModule] Provider "${tokenLabel}" has suspicious useExisting: ${String(useExisting)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [InMemoryAdminPersistenceModule, AuthModule],
|
||||
controllers: [AdminController],
|
||||
providers: [
|
||||
AdminService,
|
||||
ListUsersPresenter,
|
||||
DashboardStatsPresenter,
|
||||
{
|
||||
provide: LIST_USERS_OUTPUT_PORT_TOKEN,
|
||||
useExisting: ListUsersPresenter,
|
||||
},
|
||||
{
|
||||
provide: DASHBOARD_STATS_OUTPUT_PORT_TOKEN,
|
||||
useExisting: DashboardStatsPresenter,
|
||||
},
|
||||
{
|
||||
provide: ListUsersUseCase,
|
||||
useFactory: (
|
||||
repository: IAdminUserRepository,
|
||||
output: UseCaseOutputPort<ListUsersResult>,
|
||||
) => new ListUsersUseCase(repository, output),
|
||||
inject: [ADMIN_USER_REPOSITORY_TOKEN, LIST_USERS_OUTPUT_PORT_TOKEN],
|
||||
},
|
||||
{
|
||||
provide: GetDashboardStatsUseCase,
|
||||
useFactory: (
|
||||
repository: IAdminUserRepository,
|
||||
output: UseCaseOutputPort<DashboardStatsResult>,
|
||||
) => new GetDashboardStatsUseCase(repository, output),
|
||||
inject: [ADMIN_USER_REPOSITORY_TOKEN, DASHBOARD_STATS_OUTPUT_PORT_TOKEN],
|
||||
},
|
||||
],
|
||||
providers: [...adminProviders],
|
||||
exports: [AdminService],
|
||||
})
|
||||
export class AdminModule {}
|
||||
export class AdminModule {}
|
||||
|
||||
Reference in New Issue
Block a user