import { Module, Global } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { JwtModule } from '@nestjs/jwt'; import { HttpModule } from '@nestjs/axios'; import { APP_FILTER, APP_INTERCEPTOR, APP_GUARD } from '@nestjs/core'; // Config import { appConfig, databaseConfig, jwtConfig, redisConfig, kafkaConfig, smsConfig, walletConfig } from '@/config'; // Controllers import { UserAccountController } from '@/api/controllers/user-account.controller'; import { HealthController } from '@/api/controllers/health.controller'; import { ReferralsController } from '@/api/controllers/referrals.controller'; // Application Services import { UserApplicationService } from '@/application/services/user-application.service'; import { TokenService } from '@/application/services/token.service'; // Domain Services import { AccountSequenceGeneratorService, UserValidatorService, WalletGeneratorService, } from '@/domain/services'; import { USER_ACCOUNT_REPOSITORY } from '@/domain/repositories/user-account.repository.interface'; import { MPC_KEY_SHARE_REPOSITORY } from '@/domain/repositories/mpc-key-share.repository.interface'; // Infrastructure import { PrismaService } from '@/infrastructure/persistence/prisma/prisma.service'; import { UserAccountRepositoryImpl } from '@/infrastructure/persistence/repositories/user-account.repository.impl'; import { MpcKeyShareRepositoryImpl } from '@/infrastructure/persistence/repositories/mpc-key-share.repository.impl'; import { RedisService } from '@/infrastructure/redis/redis.service'; import { EventPublisherService } from '@/infrastructure/kafka/event-publisher.service'; import { SmsService } from '@/infrastructure/external/sms/sms.service'; import { MpcClientService, MpcWalletService } from '@/infrastructure/external/mpc'; import { BackupClientService, MpcShareStorageService } from '@/infrastructure/external/backup'; import { WalletGeneratorServiceImpl } from '@/infrastructure/external/blockchain/wallet-generator.service.impl'; // Shared import { GlobalExceptionFilter, TransformInterceptor } from '@/shared/filters/global-exception.filter'; import { JwtAuthGuard } from '@/shared/guards/jwt-auth.guard'; // ============ Infrastructure Module ============ @Global() @Module({ imports: [ HttpModule.register({ timeout: 300000, maxRedirects: 5, }), ], providers: [ PrismaService, RedisService, EventPublisherService, SmsService, MpcClientService, MpcWalletService, BackupClientService, MpcShareStorageService, // WalletGeneratorService 抽象类由 WalletGeneratorServiceImpl 实现 WalletGeneratorServiceImpl, { provide: WalletGeneratorService, useExisting: WalletGeneratorServiceImpl }, { provide: MPC_KEY_SHARE_REPOSITORY, useClass: MpcKeyShareRepositoryImpl }, ], exports: [ PrismaService, RedisService, EventPublisherService, SmsService, MpcClientService, MpcWalletService, BackupClientService, MpcShareStorageService, WalletGeneratorService, MPC_KEY_SHARE_REPOSITORY, ], }) export class InfrastructureModule {} // ============ Domain Module ============ @Module({ imports: [InfrastructureModule], providers: [ { provide: USER_ACCOUNT_REPOSITORY, useClass: UserAccountRepositoryImpl }, AccountSequenceGeneratorService, UserValidatorService, // WalletGeneratorService 由 InfrastructureModule 提供 ], exports: [ USER_ACCOUNT_REPOSITORY, AccountSequenceGeneratorService, UserValidatorService, // WalletGeneratorService 由 InfrastructureModule 导出 ], }) export class DomainModule {} // ============ Application Module ============ @Module({ imports: [DomainModule, InfrastructureModule], providers: [UserApplicationService, TokenService], exports: [UserApplicationService, TokenService], }) export class ApplicationModule {} // ============ API Module ============ @Module({ imports: [ApplicationModule], controllers: [HealthController, UserAccountController, ReferralsController], }) export class ApiModule {} // ============ App Module ============ @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, load: [appConfig, databaseConfig, jwtConfig, redisConfig, kafkaConfig, smsConfig, walletConfig], }), JwtModule.registerAsync({ global: true, inject: [ConfigService], useFactory: (configService: ConfigService) => ({ secret: configService.get('JWT_SECRET'), signOptions: { expiresIn: configService.get('JWT_ACCESS_EXPIRES_IN', '2h') }, }), }), InfrastructureModule, DomainModule, ApplicationModule, ApiModule, ], providers: [ { provide: APP_FILTER, useClass: GlobalExceptionFilter }, { provide: APP_INTERCEPTOR, useClass: TransformInterceptor }, { provide: APP_GUARD, useClass: JwtAuthGuard }, ], }) export class AppModule {}