From bba09b7f83a0fd9089dcea579bb5189fe5440026 Mon Sep 17 00:00:00 2001 From: Kaique Date: Mon, 23 Sep 2024 12:33:05 -0300 Subject: [PATCH 1/3] Endpoint de status de banco de dados e email implementados --- src/app.controller.ts | 15 ++++- src/app.module.ts | 8 ++- src/app.service.ts | 62 +++++++++++++++++++ src/modules/mails/templates/health.hbs | 13 ++++ .../classes/health-check-response.swagger.ts | 15 +++++ .../app/health-check.swagger.decorator.ts | 16 +++++ 6 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/modules/mails/templates/health.hbs create mode 100644 src/shared/Swagger/decorators/app/classes/health-check-response.swagger.ts create mode 100644 src/shared/Swagger/decorators/app/health-check.swagger.decorator.ts 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 bc80037..a2b8f61 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -15,6 +15,8 @@ import { UploadModule } from './modules/upload/upload.module'; 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'; @Module({ imports: [ @@ -37,8 +39,12 @@ import { typeormConfig } from './database/data-source';import { PassportModule } UploadModule, CurriculumModule, ApplicationsModule, + TypeOrmModule.forFeature([UsersEntity]) ], 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/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', + }), + ); +} From 8a07f4f0239df3ac9bcd9be47c9eeb944b147987 Mon Sep 17 00:00:00 2001 From: Kaique <97410767+kSantiagoP@users.noreply.github.com> Date: Mon, 7 Oct 2024 22:19:50 -0300 Subject: [PATCH 2/3] Update app.module.ts --- src/app.module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.module.ts b/src/app.module.ts index 8a2ad8d..adcc223 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -42,7 +42,7 @@ import { AlertsModule } from './modules/alert/alerts.module'; UploadModule, CurriculumModule, ApplicationsModule, - TypeOrmModule.forFeature([UsersEntity]) + TypeOrmModule.forFeature([UsersEntity]), AlertsModule, ], controllers: [AppController], From ef0b90b9768ae1b94df8768a7db762b733615637 Mon Sep 17 00:00:00 2001 From: Kaique Date: Mon, 7 Oct 2024 22:27:03 -0300 Subject: [PATCH 3/3] =?UTF-8?q?corrigindo=20remo=C3=A7=C3=A3o=20incorreta?= =?UTF-8?q?=20do=20c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/mails/mail.module.ts | 1 + 1 file changed, 1 insertion(+) 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 `, },