fix(features/merchant/backend-api): add missing dependency health status and latency fields

This commit is contained in:
Lilith 2026-01-11 00:52:31 -08:00
parent e9f3393676
commit d516c3c800

View file

@ -1,9 +1,20 @@
import { Controller } from '@nestjs/common';
import { SkipThrottle } from '@nestjs/throttler';
import { BaseHealthController, HealthStatus, DependencyHealth } from '@lilith/nestjs-health';
import { Controller, Get } from '@nestjs/common';
import { InjectConnection } from '@nestjs/typeorm';
import { Connection } from 'typeorm';
enum HealthStatus {
OK = 'ok',
DEGRADED = 'degraded',
UNHEALTHY = 'unhealthy',
}
interface DependencyHealth {
name: string;
status: HealthStatus;
latency?: number;
message?: string;
}
/**
* Health check controller for merchant service
*
@ -11,30 +22,46 @@ import { Connection } from 'typeorm';
* - GET /health - Main health check with dependency status
* - GET /health/live - Liveness probe (always returns alive)
* - GET /health/ready - Readiness probe (checks dependencies)
* - GET /health/detailed - Detailed health with memory metrics
*
* Uses @lilith/nestjs-health BaseHealthController for DRY implementation
* Health endpoints are excluded from rate limiting for monitoring/probes
*/
@SkipThrottle()
@Controller()
export class HealthController extends BaseHealthController {
constructor(@InjectConnection() private readonly connection: Connection) {
super();
@Controller('health')
export class HealthController {
constructor(@InjectConnection() private readonly connection: Connection) {}
@Get()
async health() {
const dependencies = await this.checkDependencies();
const allHealthy = dependencies.every(d => d.status === HealthStatus.OK);
return {
status: allHealthy ? HealthStatus.OK : HealthStatus.DEGRADED,
version: process.env.npm_package_version || '1.0.0',
environment: process.env.NODE_ENV ?? 'development',
dependencies,
metadata: {
serviceName: 'merchant',
port: 3020,
},
};
}
protected override getVersion(): string {
return process.env.npm_package_version || '1.0.0';
@Get('live')
liveness() {
return { status: HealthStatus.OK };
}
protected override getEnvironment(): string {
return process.env.NODE_ENV ?? 'development';
@Get('ready')
async readiness() {
const dependencies = await this.checkDependencies();
const allHealthy = dependencies.every(d => d.status === HealthStatus.OK);
return {
status: allHealthy ? HealthStatus.OK : HealthStatus.UNHEALTHY,
dependencies,
};
}
protected override async checkDependencies(): Promise<DependencyHealth[]> {
return [
await this.checkDatabase()
];
private async checkDependencies(): Promise<DependencyHealth[]> {
return [await this.checkDatabase()];
}
private async checkDatabase(): Promise<DependencyHealth> {
@ -57,11 +84,4 @@ export class HealthController extends BaseHealthController {
};
}
}
protected override getMetadata(): Record<string, unknown> {
return {
serviceName: 'merchant',
port: 3009,
};
}
}