platform-codebase/features/analytics/backend-api/src/app.module.ts

228 lines
6.4 KiB
TypeScript
Executable file

import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { DomainEventsModule } from '@lilith/domain-events'
import { buildDeploymentRegistry } from '@lilith/service-registry'
import { BullModule } from '@nestjs/bullmq'
import { CacheModule } from '@nestjs/cache-manager'
import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { ScheduleModule } from '@nestjs/schedule'
import { ThrottlerModule } from '@nestjs/throttler'
import { TypeOrmModule } from '@nestjs/typeorm'
import { JwtAuthGuard, AdminGuard } from './auth'
import { AnalyticsController, AdminAnalyticsController, SecurityAdminController } from './controllers'
import {
ContentView,
RevenueMetric,
EngagementMetric,
DashboardSnapshot,
ListingPerformance,
ListingVariant,
RankingSnapshot,
PlatformCost,
PlatformError,
ABTest,
ConversionEvent,
InteractionEvent,
SessionFingerprint,
FmtyAnalyticsMetric,
SecurityAuditLog,
} from './entities'
import { HealthCheckService } from './health/health-check.service'
import { HealthController } from './health/health.controller'
import { AnalyticsProcessor, ConversionFunnelProcessor, ProfileAnalyticsProcessor } from './processors'
import { QUEUE_NAMES } from './queue/queue-names'
import {
RedisService,
QueueService,
AnalyticsService,
ReportsService,
ListingPerformanceService,
RankingTransparencyService,
ReviewAnalyticsService,
ClientAnalyticsService,
AdminAnalyticsService,
SubscriberAnalyticsService,
ContentMetadataService,
SubscriptionFunnelService,
RevenueAnalyticsService,
TransactionAnalyticsService,
PnLAnalyticsService,
CostAnalyticsService,
RealtimeAnalyticsService,
PerformanceAnalyticsService,
ErrorAnalyticsService,
ConversionAnalyticsService,
ABTestAnalyticsService,
ConversionTrackingService,
GiftAnalyticsService,
FmtyAnalyticsService,
ProfileAnalyticsService,
SecurityAuditService,
} from './services'
import { TrackingModule } from './tracking/tracking.module'
// Build deployment registry for service configuration
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const projectRoot = join(__dirname, '..', '..', '..', '..', '..')
const registry = buildDeploymentRegistry({
deploymentsPath: join(projectRoot, 'codebase/@deployments'),
sharedServicesPath: join(projectRoot, 'infrastructure/shared-services'),
})
@Module({
imports: [
// Load environment variables from .env files
ConfigModule.forRoot({
isGlobal: true,
envFilePath: ['.env.local', '.env'],
}),
// Domain events for cross-service communication
DomainEventsModule.forFeature(),
// TypeORM database connection - uses analytics shared service's PostgreSQL
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
const dbService = registry.services.get('analytics.postgresql')
return {
type: 'postgres',
host: dbService?.host || 'localhost',
port: dbService?.port || 5432,
username: config.get('DATABASE_POSTGRES_USER', 'lilith'),
password: config.get('DATABASE_POSTGRES_PASSWORD', 'lilith'),
database: config.get('DATABASE_POSTGRES_NAME', 'lilith_analytics'),
ssl: config.get('DATABASE_SSL') === 'true' ? { rejectUnauthorized: false } : false,
autoLoadEntities: true,
synchronize: config.get('NODE_ENV') !== 'production',
logging: config.get('NODE_ENV') === 'development',
}
},
}),
// TypeORM entity registration for repositories
TypeOrmModule.forFeature([
ContentView,
RevenueMetric,
EngagementMetric,
DashboardSnapshot,
ListingPerformance,
ListingVariant,
RankingSnapshot,
PlatformCost,
PlatformError,
ABTest,
ConversionEvent,
InteractionEvent,
SessionFingerprint,
FmtyAnalyticsMetric,
SecurityAuditLog,
]),
// Queue infrastructure with Redis connection - uses analytics shared service's Redis
BullModule.forRootAsync({
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
const redisService = registry.services.get('analytics.redis')
return {
connection: {
host: redisService?.host || 'localhost',
port: redisService?.port || 6379,
password: config.get('DATABASE_REDIS_PASSWORD'),
},
}
},
}),
// Register analytics queues
BullModule.registerQueue({
name: QUEUE_NAMES.ANALYTICS,
}),
BullModule.registerQueue({
name: QUEUE_NAMES.PROFILE_ANALYTICS,
}),
BullModule.registerQueue({
name: QUEUE_NAMES.DOMAIN_EVENTS,
}),
// Scheduled jobs
ScheduleModule.forRoot(),
// Rate limiting
ThrottlerModule.forRoot([
{
ttl: 60000,
limit: 100,
},
]),
// Caching
CacheModule.registerAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
ttl: config.get<number>('CACHE_TTL', 300000),
max: config.get<number>('CACHE_MAX', 100),
}),
}),
// Feature modules
TrackingModule,
],
controllers: [HealthController, AnalyticsController, AdminAnalyticsController, SecurityAdminController],
providers: [
// Auth guards
JwtAuthGuard,
AdminGuard,
// Services
RedisService,
QueueService,
AnalyticsService,
ReportsService,
ListingPerformanceService,
RankingTransparencyService,
ReviewAnalyticsService,
ClientAnalyticsService,
AdminAnalyticsService,
SubscriberAnalyticsService,
ContentMetadataService,
SubscriptionFunnelService,
// Admin Analytics Services (specialized)
RevenueAnalyticsService,
TransactionAnalyticsService,
PnLAnalyticsService,
CostAnalyticsService,
RealtimeAnalyticsService,
PerformanceAnalyticsService,
ErrorAnalyticsService,
ConversionAnalyticsService,
ABTestAnalyticsService,
GiftAnalyticsService,
FmtyAnalyticsService,
// Conversion Tracking
ConversionTrackingService,
// Health Monitoring
HealthCheckService,
// Processors
AnalyticsProcessor,
ConversionFunnelProcessor,
ProfileAnalyticsProcessor,
// Profile Analytics
ProfileAnalyticsService,
// Security Audit
SecurityAuditService,
],
})
export class AppModule {}