diff --git a/src/app.controller.ts b/src/app.controller.ts index 5d03701..addff03 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -1,8 +1,18 @@ -import { Controller, Get, Req, Res } from '@nestjs/common'; +import { + Controller, + Get, + Req, + Res, + Patch, + Param, + Body, + NotFoundException, + BadRequestException, + HttpCode, +} 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, Response } from 'express'; @ApiTags('Status') @@ -24,8 +34,34 @@ export class AppController { @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(); + async getHealthCheck(@Res() res: Response) { + const { status, data } = await this.appService.getHealthCheck(); return res.status(status).send(data); } + + @Patch(':applicationId') + @HttpCode(200) + async updateStatus( + @Param('userId') userId: string, + @Param('applicationId') applicationId: string, + @Body('status') status: string, + ) { + if (!status) { + throw new BadRequestException('Novo status é obrigatório'); + } + + const application = await this.appService.updateStatus( + userId, + applicationId, + status, + ); + + if (!application) { + throw new NotFoundException( + 'Candidatura não encontrada ou não pertence ao usuário', + ); + } + + return { message: 'Status atualizado com sucesso', application }; + } } diff --git a/src/app.service.ts b/src/app.service.ts index 13ff340..0d59588 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -3,68 +3,86 @@ 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'; +import { InjectRepository } from '@nestjs/typeorm'; +import { ApplicationEntity } from './database/entities/applications.entity'; +import { Repository } from 'typeorm'; @Injectable() export class AppService { constructor( private mailService: MailService, - private userRepository: UserRepository - ){} + private userRepository: UserRepository, + @InjectRepository(ApplicationEntity) + private applicationRepository: Repository, + ) {} getAppStatus(baseUrl: string) { return `
Sou JuniorProjeto Opensource para melhorar o match entre os profissionais Juniors e Empresas!
`; } - async getHealthCheck(){ + async getHealthCheck() { const databaseStatus = await this.checkDatabase(); const mailerStatus = await this.checkEmail(); const data = { databaseStatus, - mailerStatus - } + mailerStatus, + }; return { status: 201, - data + data, }; } - - private async checkDatabase(){ - try{ + private async checkDatabase() { + try { const options: PageOptionsDto = { page: 1, take: 10, orderByColumn: 'id', - order: Order.ASC + order: Order.ASC, }; const allUsers = await this.userRepository.getAllUsers(options); - if (allUsers == null || allUsers == undefined){ - return "DOWN"; + if (allUsers == null || allUsers == undefined) { + return 'DOWN'; } - return "OK"; - } - catch(error){ - return "DOWN"; + return 'OK'; + } catch (error) { + return 'DOWN'; } } - - private async checkEmail(){ - try{ + + private async checkEmail() { + try { await this.mailService.sendMail({ subject: 'HealthCheck', template: './health', context: { - arg1: "Argumento1", - arg2: "Argumento2" + arg1: 'Argumento1', + arg2: 'Argumento2', }, - email: 'carteiro@soujunior.tech' + email: 'carteiro@soujunior.tech', }); - return "OK"; + return 'OK'; + } catch (error) { + return 'DOWN'; } - catch(error){ - return "DOWN"; + } + + async updateStatus( + user_id: string, + applicationId: string, + status: string, + ): Promise { + const application = await this.applicationRepository.findOne({ + where: { id: applicationId, user_id }, + }); + + if (!application) { + return null; } + application.status = status; + await this.applicationRepository.save(application); + return application; } - }