diff --git a/apps/scv-gateway/src/status/dto/service-status.dto.ts b/apps/scv-gateway/src/status/dto/service-status.dto.ts index 6f17f01..3596665 100644 --- a/apps/scv-gateway/src/status/dto/service-status.dto.ts +++ b/apps/scv-gateway/src/status/dto/service-status.dto.ts @@ -4,6 +4,18 @@ export class ServiceStatusDto { @ApiProperty() applicationVersion: string; + @ApiProperty() + databaseConnection: 'OK' | 'DOWN'; + @ApiProperty() lastMigration?: string; + + @ApiProperty() + queueConnection: 'OK' | 'DOWN'; + + @ApiProperty() + middlewareConnection: 'OK' | 'DOWN'; + + @ApiProperty() + middlewareVersion?: string; } diff --git a/apps/scv-gateway/src/status/status.module.ts b/apps/scv-gateway/src/status/status.module.ts index c0e6a62..2792cf1 100644 --- a/apps/scv-gateway/src/status/status.module.ts +++ b/apps/scv-gateway/src/status/status.module.ts @@ -1,8 +1,39 @@ import { Module } from '@nestjs/common'; import { StatusService } from './status.service'; import { StatusController } from './status.controller'; +import { HttpModule } from '@nestjs/axios'; +import { ConfigModule, ConfigService } from '@nestjs/config'; +import { ClientsModule, Transport } from '@nestjs/microservices'; +import { MqConfig } from '../config/config.type'; @Module({ + imports: [ + HttpModule, + ConfigModule, + ClientsModule.registerAsync([ + { + name: 'MQ_SERVICE', + imports: [ConfigModule], + useFactory: (configService: ConfigService) => ({ + transport: Transport.RMQ, + options: { + urls: [ + `amqp://${configService.get('mq').username}:${ + configService.get('mq').password + }@${configService.get('mq').host}:${ + configService.get('mq').port + }`, + ], + queue: configService.get('mq').verificationQueue, + queueOptions: { + durable: true, + }, + }, + }), + inject: [ConfigService], + }, + ]), + ], controllers: [StatusController], providers: [StatusService], }) diff --git a/apps/scv-gateway/src/status/status.service.ts b/apps/scv-gateway/src/status/status.service.ts index 9f5b865..b14326b 100644 --- a/apps/scv-gateway/src/status/status.service.ts +++ b/apps/scv-gateway/src/status/status.service.ts @@ -1,26 +1,74 @@ -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; import { ServiceStatusDto } from './dto/service-status.dto'; import { DataSource } from 'typeorm'; +import { HttpService } from '@nestjs/axios'; +import { ConfigService } from '@nestjs/config'; +import { firstValueFrom } from 'rxjs'; +import { ClientProxy } from '@nestjs/microservices'; @Injectable() export class StatusService { - constructor(private dataSource: DataSource) {} + constructor( + private dataSource: DataSource, + private readonly httpService: HttpService, + private readonly configService: ConfigService, + @Inject('MQ_SERVICE') + private readonly mqClient: ClientProxy, + ) {} async getStatus(): Promise { - let lastMigration: string; + const [ + [databaseConnection, lastMigration], + queueConnection, + [middlewareConnection, middlewareVersion], + ] = await Promise.all([ + this.getDatabaseConnection(), + this.getQueueConnection(), + this.getMiddlewareConnection(), + ]); + const serviceStatusDto: ServiceStatusDto = { + applicationVersion: APPLICATION_VERSION, + lastMigration, + databaseConnection: databaseConnection ? 'OK' : 'DOWN', + queueConnection: queueConnection ? 'OK' : 'DOWN', + middlewareConnection: middlewareConnection ? 'OK' : 'DOWN', + middlewareVersion, + }; + + return serviceStatusDto; + } + + private async getDatabaseConnection(): Promise<[boolean, string]> { try { const lastMigrations = await this.dataSource.query( 'SELECT name FROM migrations ORDER BY id DESC LIMIT 1', ); - lastMigration = lastMigrations[0]?.name; - } catch (e) { - lastMigration = 'unable to check'; + return [true, lastMigrations[0]?.name]; + } catch (error) { + return [false, 'UNABLE TO CHECK']; } - const serviceStatusDto: ServiceStatusDto = { - applicationVersion: APPLICATION_VERSION, - lastMigration: lastMigration, - }; + } - return serviceStatusDto; + private async getQueueConnection(): Promise { + try { + await this.mqClient.connect(); + await this.mqClient.close(); + return true; + } catch (error) { + return false; + } + } + + private async getMiddlewareConnection(): Promise<[boolean, string]> { + try { + const response = await firstValueFrom( + this.httpService.get( + `${this.configService.get('app').aeMdwUrl}/v2/status`, + ), + ); + return [true, response.data.mdw_version]; + } catch (error) { + return [false, 'UNABLE TO CHECK']; + } } }