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

190 lines
4.8 KiB
TypeScript

import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { TypeOrmModule } from '@nestjs/typeorm'
import { ScheduleModule } from '@nestjs/schedule'
import { ThrottlerModule } from '@nestjs/throttler'
import { CacheModule } from '@nestjs/cache-manager'
import { BullModule } from '@nestjs/bullmq'
import { QUEUE_NAMES } from './queue/queue-names'
import { HealthController } from './health/health.controller'
import { AnalyticsController, AdminAnalyticsController } from './controllers'
// Entities
import {
ContentView,
RevenueMetric,
EngagementMetric,
DashboardSnapshot,
ListingPerformance,
ListingVariant,
RankingSnapshot,
PlatformCost,
PlatformError,
ABTest,
ConversionEvent,
InteractionEvent,
SessionFingerprint,
} from './entities'
// Services
import {
RedisService,
QueueService,
AnalyticsService,
ReportsService,
ListingPerformanceService,
RankingTransparencyService,
ReviewAnalyticsService,
ClientAnalyticsService,
AdminAnalyticsService,
SubscriberAnalyticsService,
ContentMetadataService,
SubscriptionFunnelService,
RevenueAnalyticsService,
TransactionAnalyticsService,
PnLAnalyticsService,
CostAnalyticsService,
RealtimeAnalyticsService,
PerformanceAnalyticsService,
ErrorAnalyticsService,
ConversionAnalyticsService,
ABTestAnalyticsService,
} from './services'
// Processors
import { AnalyticsProcessor } from './processors'
// Auth
import { JwtAuthGuard, AdminGuard } from './auth'
@Module({
imports: [
// Load environment variables from .env files
ConfigModule.forRoot({
isGlobal: true,
envFilePath: ['.env.local', '.env'],
}),
// TypeORM database connection
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
type: 'postgres',
host: config.get('DATABASE_HOST', 'localhost'),
port: config.get<number>('DATABASE_PORT', 5432),
username: config.get('DATABASE_USER', 'postgres'),
password: config.get('DATABASE_PASSWORD', 'postgres'),
database: config.get('DATABASE_NAME', 'lilith_analytics'),
ssl: config.get('DATABASE_SSL') === 'true' ? { rejectUnauthorized: false } : false,
entities: [
ContentView,
RevenueMetric,
EngagementMetric,
DashboardSnapshot,
ListingPerformance,
ListingVariant,
RankingSnapshot,
PlatformCost,
PlatformError,
ABTest,
ConversionEvent,
InteractionEvent,
SessionFingerprint,
],
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,
]),
// Queue infrastructure with Redis connection
BullModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
connection: {
host: config.get('REDIS_HOST', 'localhost'),
port: config.get<number>('REDIS_PORT', 6379),
password: config.get('REDIS_PASSWORD'),
},
}),
}),
// Register analytics queue
BullModule.registerQueue({
name: QUEUE_NAMES.ANALYTICS,
}),
// 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),
}),
}),
],
controllers: [HealthController, AnalyticsController, AdminAnalyticsController],
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,
// Processors
AnalyticsProcessor,
],
})
export class AppModule {}