Skip to content

Commit

Permalink
Endpoint de status de banco de dados e email implementados
Browse files Browse the repository at this point in the history
  • Loading branch information
kSantiagoP committed Sep 23, 2024
1 parent 9a4811e commit bba09b7
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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);
}
}
8 changes: 7 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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 {}
62 changes: 62 additions & 0 deletions src/app.service.ts
Original file line number Diff line number Diff line change
@@ -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 `<div style=text-align:center><a target="_blank" href="https://www.linkedin.com/company/soujunior/"><svg font-family="Times New Roman" font-size="16" height="299.96" viewBox="0 0 854 300" width="854.56" xmlns="http://www.w3.org/2000/svg" style="width:854.56px; height:299.96px; font-family:'Times New Roman'; font-size:16px; position:relative; z-index:1" xmlns:xlink="http://www.w3.org/1999/xlink"><style>.text {font-size: 90px;font-weight: 700;font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;}.desc {font-size: 20px;font-weight: 500;font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;}.text, .desc {animation: fadeIn 1.2s ease-in-out forwards;}@keyframes fadeIn { from {opacity: 0; } to {opacity: 1; }};</style><g transform="translate(427, 150) scale(1, 1) translate(-427, -150)"><path d="" fill="#2088f2" opacity="0.4"><animate attributeName="d" begin="0s" calcmod="spline" dur="20s" keySplines="0.2 0 0.2 1;0.2 0 0.2 1;0.2 0 0.2 1" keyTimes="0;0.333;0.667;1" repeatCount="indefinite" values="M0 0L 0 220Q 213.5 260 427 230T 854 255L 854 0 Z;M0 0L 0 245Q 213.5 260 427 240T 854 230L 854 0 Z;M0 0L 0 265Q 213.5 235 427 265T 854 230L 854 0 Z;M0 0L 0 220Q 213.5 260 427 230T 854 255L 854 0 Z" /></path><path d="" fill="#2088f2" opacity="0.4"><animate attributeName="d" begin="-10s" calcmod="spline" dur="20s" keySplines="0.2 0 0.2 1;0.2 0 0.2 1;0.2 0 0.2 1" keyTimes="0;0.333;0.667;1" repeatCount="indefinite" values="M0 0L 0 235Q 213.5 280 427 250T 854 260L 854 0 Z;M0 0L 0 250Q 213.5 220 427 220T 854 240L 854 0 Z;M0 0L 0 245Q 213.5 225 427 250T 854 265L 854 0 Z;M0 0L 0 235Q 213.5 280 427 250T 854 260L 854 0 Z" /></path></g><text alignment-baseline="middle" class="text" stroke="#none" stroke-width="1" text-anchor="middle" x="50%" y="38%" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji'; font-size:90px; font-weight:700; alignment-baseline:middle; fill:#ffffff; stroke-width:1; text-anchor:middle">Sou Junior</text><text alignment-baseline="middle" class="desc" text-anchor="middle" x="52%" y="61%" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji'; font-size:20px; font-weight:500; alignment-baseline:middle; fill:#ffffff; text-anchor:middle">Projeto Opensource para melhorar o match entre os profissionais Juniors e Empresas!</text></svg></a></div>`;
}

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: '[email protected]'
});
return "OK";
}
catch(error){
return "DOWN";
}

}

}
13 changes: 13 additions & 0 deletions src/modules/mails/templates/health.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Teste de Email</title>
</head>
<body>
<h1>Isso é um teste</h1>
<p>Isso é um teste. O primeiro argumento de contexto foi: {{arg1}}</p>
<p>O segundo argumento foi: {{arg2}}.</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';

export class HealthCheckResponse {
@ApiProperty({
example: 'DOWN',
})
databaseStatus: string;

@ApiProperty({
example: 'OK',
})
mailerStatus: string;

}

Original file line number Diff line number Diff line change
@@ -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',
}),
);
}

0 comments on commit bba09b7

Please sign in to comment.