Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 33 additions & 253 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^6.1.3",
"@nestjs/terminus": "^10.1.0",
"@nestjs/typeorm": "^10.0.0",
"@stellar/stellar-sdk": "^12.0.0",
"axios": "^1.6.0",
"class-transformer": "^0.5.1",
Expand All @@ -47,7 +46,6 @@
"prom-client": "^15.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0",
"typeorm": "^0.3.17",
"uuid": "^9.0.0",
"zod": "^4.4.3"
},
Expand Down
18 changes: 2 additions & 16 deletions src/__tests__/app.smoke.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmHealthIndicator } from '@nestjs/terminus';
import request from 'supertest';
import { AppModule } from '../app.module';
import { CorsOriginsCacheService } from '../middleware/cors-origins-cache.service';
import { DrizzleHealthIndicator } from '../monitoring/drizzle-health.indicator';
import { PaymentDetectorService } from '../payments/payment-detector.service';
import { RedisService } from '../redis/redis.service';

jest.mock('@nestjs/typeorm', () => {
const actual = jest.requireActual('@nestjs/typeorm');

return {
...actual,
TypeOrmModule: {
...actual.TypeOrmModule,
forRoot: jest.fn(() => ({
module: class MockTypeOrmModule {},
})),
},
};
});

describe('App smoke test', () => {
let app: INestApplication;

Expand All @@ -37,7 +23,7 @@ describe('App smoke test', () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(TypeOrmHealthIndicator)
.overrideProvider(DrizzleHealthIndicator)
.useValue({
pingCheck: jest.fn().mockResolvedValue({
database: {
Expand Down
7 changes: 0 additions & 7 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import { AuthModule } from './auth/auth.module';
import { MerchantsModule } from './merchants/merchants.module';
Expand All @@ -19,12 +18,6 @@ import { SecurityHeadersMiddleware } from './middleware/security-headers.middlew
imports: [
ConfigModule,
ScheduleModule.forRoot(),
TypeOrmModule.forRoot({
type: 'postgres',
url: process.env.DATABASE_URL,
autoLoadEntities: true,
synchronize: process.env.NODE_ENV !== 'production',
}),
RedisModule,
RateLimitModule,
AuditModule,
Expand Down
45 changes: 45 additions & 0 deletions src/monitoring/drizzle-health.indicator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { HealthCheckError } from '@nestjs/terminus';
import { DrizzleHealthIndicator } from './drizzle-health.indicator';
import { db } from '../db';

jest.mock('../db', () => ({
db: {
execute: jest.fn(),
},
}));

describe('DrizzleHealthIndicator', () => {
const execute = db.execute as jest.Mock;

beforeEach(() => {
execute.mockReset();
});

it('returns an up status when the Drizzle database ping succeeds', async () => {
execute.mockResolvedValueOnce([{ one: 1 }]);

await expect(new DrizzleHealthIndicator().pingCheck('database')).resolves.toEqual({
database: {
status: 'up',
},
});
expect(execute).toHaveBeenCalledTimes(1);
});

it('throws a health check error with a down status when the Drizzle database ping fails', async () => {
execute.mockRejectedValueOnce(new Error('database unavailable'));

const result = new DrizzleHealthIndicator().pingCheck('database');

await expect(result).rejects.toBeInstanceOf(HealthCheckError);
await expect(result).rejects.toMatchObject({
causes: {
database: {
status: 'down',
message: 'database unavailable',
},
},
});
expect(execute).toHaveBeenCalledTimes(1);
});
});
22 changes: 22 additions & 0 deletions src/monitoring/drizzle-health.indicator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from '@nestjs/common';
import { HealthCheckError, HealthIndicator, type HealthIndicatorResult } from '@nestjs/terminus';
import { sql } from 'drizzle-orm';
import { db } from '../db';

@Injectable()
export class DrizzleHealthIndicator extends HealthIndicator {
async pingCheck(key: string): Promise<HealthIndicatorResult> {
try {
await db.execute(sql`SELECT 1`);

return this.getStatus(key, true);
} catch (error) {
const message = error instanceof Error ? error.message : 'Database health check failed';

throw new HealthCheckError(
`${key} is not available`,
this.getStatus(key, false, { message }),
);
}
}
}
5 changes: 3 additions & 2 deletions src/monitoring/health.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService, TypeOrmHealthIndicator } from '@nestjs/terminus';
import { HealthCheck, HealthCheckService } from '@nestjs/terminus';
import { DrizzleHealthIndicator } from './drizzle-health.indicator';

@Controller('health')
export class HealthController {
constructor(
private health: HealthCheckService,
private db: TypeOrmHealthIndicator,
private db: DrizzleHealthIndicator,
) {}

@Get()
Expand Down
3 changes: 2 additions & 1 deletion src/monitoring/monitoring.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { HealthController } from './health.controller';
import { DrizzleHealthIndicator } from './drizzle-health.indicator';
import { MetricsController } from './metrics.controller';
import { MetricsService } from './metrics.service';

@Module({
imports: [TerminusModule],
controllers: [HealthController, MetricsController],
providers: [MetricsService],
providers: [MetricsService, DrizzleHealthIndicator],
exports: [MetricsService],
})
export class MonitoringModule {}