import { Controller, Get } from '@nestjs/common'; import { ApiTags, ApiOperation } from '@nestjs/swagger'; import { Public } from '../common/decorators/public.decorator'; import { CrmPrismaService } from '../prisma/crm-prisma.service'; import { RedisService } from '../redis/redis.service'; interface HealthResponse { status: 'ok' | 'error'; service: string; timestamp: string; version: string; services: { database: 'up' | 'down'; redis: 'up' | 'down'; }; } @ApiTags('Health') @Controller('health') export class HealthController { constructor( private readonly prisma: CrmPrismaService, private readonly redis: RedisService, ) {} @Get() @Public() @ApiOperation({ summary: 'Health-Check fuer CRM-Service' }) async check(): Promise { const [dbStatus, redisStatus] = await Promise.allSettled([ this.checkDatabase(), this.checkRedis(), ]); const allUp = dbStatus.status === 'fulfilled' && dbStatus.value && redisStatus.status === 'fulfilled' && redisStatus.value; return { status: allUp ? 'ok' : 'error', service: 'crm-service', timestamp: new Date().toISOString(), version: '0.1.0', services: { database: dbStatus.status === 'fulfilled' && dbStatus.value ? 'up' : 'down', redis: redisStatus.status === 'fulfilled' && redisStatus.value ? 'up' : 'down', }, }; } private async checkDatabase(): Promise { try { await this.prisma.$queryRaw`SELECT 1`; return true; } catch { return false; } } private async checkRedis(): Promise { try { const pong = await this.redis.ping(); return pong === 'PONG'; } catch { return false; } } }