diff --git a/src/app.controller.ts b/src/app.controller.ts index 2d5275e..5d03701 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -1,8 +1,9 @@ -import { Controller, Get, Req } from '@nestjs/common'; +import { Controller, Get, Req, Res } from '@nestjs/common'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { AppService } from './app.service'; +import { SwaggerHealthCheck } from './shared/Swagger/decorators/app/health-check.swagger.decorator'; -import { Request } from 'express'; +import { Request, Response } from 'express'; @ApiTags('Status') @Controller() @@ -17,4 +18,14 @@ export class AppController { const baseUrl = req.protocol + '://' + req.get('host'); return this.appService.getAppStatus(baseUrl); } + + @Get('/health-check') + @SwaggerHealthCheck() + @ApiOperation({ + summary: 'Retorna status dos serviços de email e banco de dados', + }) + async getHealthCheck(@Res() res: Response){ + const {status, data} = await this.appService.getHealthCheck(); + return res.status(status).send(data); + } } diff --git a/src/app.module.ts b/src/app.module.ts index 8758e41..adcc223 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -16,8 +16,11 @@ import { UserModule } from './modules/user/user.module'; import { ApplicationsModule } from './modules/applications/applications.module'; import { typeormConfig } from './database/data-source'; import { PassportModule } from '@nestjs/passport'; +import { UserRepository } from './modules/user/repository/user.repository'; +import { UsersEntity } from './database/entities/users.entity'; import { AlertsModule } from './modules/alert/alerts.module'; + @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true }), @@ -39,9 +42,13 @@ import { AlertsModule } from './modules/alert/alerts.module'; UploadModule, CurriculumModule, ApplicationsModule, + TypeOrmModule.forFeature([UsersEntity]), AlertsModule, ], controllers: [AppController], - providers: [AppService], + providers: [ + AppService, + UserRepository + ], }) export class AppModule {} diff --git a/src/app.service.ts b/src/app.service.ts index f17995f..13ff340 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -1,8 +1,70 @@ import { Injectable } from '@nestjs/common'; +import { MailService } from './modules/mails/mail.service'; +import { UserRepository } from './modules/user/repository/user.repository'; +import { PageOptionsDto } from './shared/pagination'; +import { Order } from './shared/pagination'; @Injectable() export class AppService { + constructor( + private mailService: MailService, + private userRepository: UserRepository + ){} + getAppStatus(baseUrl: string) { return `
Sou JuniorProjeto Opensource para melhorar o match entre os profissionais Juniors e Empresas!
`; } + + async getHealthCheck(){ + const databaseStatus = await this.checkDatabase(); + const mailerStatus = await this.checkEmail(); + const data = { + databaseStatus, + mailerStatus + } + return { + status: 201, + data + }; + } + + + private async checkDatabase(){ + try{ + const options: PageOptionsDto = { + page: 1, + take: 10, + orderByColumn: 'id', + order: Order.ASC + }; + const allUsers = await this.userRepository.getAllUsers(options); + if (allUsers == null || allUsers == undefined){ + return "DOWN"; + } + return "OK"; + } + catch(error){ + return "DOWN"; + } + } + + private async checkEmail(){ + try{ + await this.mailService.sendMail({ + subject: 'HealthCheck', + template: './health', + context: { + arg1: "Argumento1", + arg2: "Argumento2" + }, + email: 'carteiro@soujunior.tech' + }); + return "OK"; + } + catch(error){ + return "DOWN"; + } + + } + } diff --git a/src/modules/mails/mail.module.ts b/src/modules/mails/mail.module.ts index f63d48a..a25829d 100644 --- a/src/modules/mails/mail.module.ts +++ b/src/modules/mails/mail.module.ts @@ -22,6 +22,7 @@ import { MailService } from './mail.service'; // tls: { // rejectUnauthorized: false, // }, + }, defaults: { from: `no-reply `, }, diff --git a/src/modules/mails/templates/health.hbs b/src/modules/mails/templates/health.hbs new file mode 100644 index 0000000..4a361a6 --- /dev/null +++ b/src/modules/mails/templates/health.hbs @@ -0,0 +1,13 @@ + + + + + + Teste de Email + + +

Isso é um teste

+

Isso é um teste. O primeiro argumento de contexto foi: {{arg1}}

+

O segundo argumento foi: {{arg2}}.

+ + \ No newline at end of file diff --git a/src/shared/Swagger/decorators/app/classes/health-check-response.swagger.ts b/src/shared/Swagger/decorators/app/classes/health-check-response.swagger.ts new file mode 100644 index 0000000..40d78dc --- /dev/null +++ b/src/shared/Swagger/decorators/app/classes/health-check-response.swagger.ts @@ -0,0 +1,15 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class HealthCheckResponse { + @ApiProperty({ + example: 'DOWN', + }) + databaseStatus: string; + + @ApiProperty({ + example: 'OK', + }) + mailerStatus: string; + +} + \ No newline at end of file diff --git a/src/shared/Swagger/decorators/app/health-check.swagger.decorator.ts b/src/shared/Swagger/decorators/app/health-check.swagger.decorator.ts new file mode 100644 index 0000000..5a10a20 --- /dev/null +++ b/src/shared/Swagger/decorators/app/health-check.swagger.decorator.ts @@ -0,0 +1,16 @@ +import { applyDecorators, HttpStatus } from '@nestjs/common'; +import { ApiOperation, ApiResponse } from '@nestjs/swagger'; +import { HealthCheckResponse } from './classes/health-check-response.swagger'; + +export function SwaggerHealthCheck() { + return applyDecorators( + ApiResponse({ + status: HttpStatus.OK, + description: 'Exemplo do retorno de sucesso da rota', + type: HealthCheckResponse, + }), + ApiOperation({ + summary: 'Retorna status dos serviços de email e banco de dados', + }), + ); +}