diff --git a/src/presentation/animals/routes.ts b/src/presentation/animals/routes.ts index 15e5e44..77af740 100644 --- a/src/presentation/animals/routes.ts +++ b/src/presentation/animals/routes.ts @@ -11,7 +11,7 @@ import { PaginationDto, } from '../../domain'; import { UpdateAnimalDto } from '../../domain/dtos/animals/update-animal.dto'; -import { S3Service } from '../services'; +import { ProducerService, S3Service } from '../services'; import { FileUploadMiddleware } from '../middlewares/file-upload.middleware'; export class AnimalRoutes { @@ -26,8 +26,9 @@ export class AnimalRoutes { envs.AWS_SECRET_ACCESS_KEY, envs.AWS_BUCKET ); + const producer = new ProducerService(envs.RABBITMQ_URL); const fileUploadMiddleware = new FileUploadMiddleware(s3Service); - const animalService = new AnimalService(s3Service); + const animalService = new AnimalService(s3Service, producer); const animalController = new AnimalController(animalService); router.get( diff --git a/src/presentation/services/animal.service.ts b/src/presentation/services/animal.service.ts index 58c04ad..7637bec 100644 --- a/src/presentation/services/animal.service.ts +++ b/src/presentation/services/animal.service.ts @@ -12,9 +12,13 @@ import { PayloadUser } from '../../domain/interfaces/payload-user.interface'; import { CheckPermissions } from '../../utils'; import { UpdateAnimalDto } from '../../domain/dtos/animals/update-animal.dto'; import { S3Service } from './s3.service'; +import { ProducerService } from './producer.service'; export class AnimalService { - constructor(private readonly s3Service: S3Service) {} + constructor( + private readonly s3Service: S3Service, + private readonly producerService: ProducerService + ) {} public async createCat( userId: string, @@ -239,6 +243,22 @@ export class AnimalService { data: updateQuery, }); + const favs = await prisma.animal.findUnique({ + where: { id: updatedAnimal.id }, + include: { + userFav: true, + }, + }); + + const emails = (favs && favs.userFav.map((user) => user.email)) || []; + + emails?.forEach((email) => + this.producerService.addToEmailQueue( + { ...updateQuery, email }, + 'animal-changed-notification' + ) + ); + return updatedAnimal; } @@ -300,7 +320,6 @@ export class AnimalService { } public async addFavorite(userId: string, animalId: string) { - const animal = await prisma.animal.findUnique({ where: { id: animalId } }); if (!animal) throw new NotFoundError('Animal not found'); diff --git a/src/presentation/services/auth.service.ts b/src/presentation/services/auth.service.ts index e2bb802..2407cb0 100644 --- a/src/presentation/services/auth.service.ts +++ b/src/presentation/services/auth.service.ts @@ -35,7 +35,6 @@ export class AuthService { where: { email }, }); - if (user) throw new BadRequestError( `Email ${registerUserDto.email} already exists, try another one` @@ -81,11 +80,14 @@ export class AuthService { }, }); - await this.producerService.addToEmailQueue({ - email: createdUser.email, - verificationToken, - type: 'email', - }); + await this.producerService.addToEmailQueue( + { + email: createdUser.email, + verificationToken, + type: 'email', + }, + 'verify-email' + ); // Rollback in case there is an error sending the validation email // try { @@ -103,7 +105,6 @@ export class AuthService { } private async validateCredentials(loginUserDto: LoginUserDto) { - const user = await prisma.user.findUnique({ where: { email: loginUserDto.email }, }); @@ -247,11 +248,14 @@ export class AuthService { await prisma.user.update({ data: { passwordToken }, where: { email } }); - await this.producerService.addToEmailQueue({ - email, - passwordToken, - type: 'password', - }); + await this.producerService.addToEmailQueue( + { + email, + passwordToken, + type: 'password', + }, + 'change-password' + ); // try { // await this.sendEmailValidationLink(email, passwordToken, 'password'); diff --git a/src/presentation/services/producer.service.ts b/src/presentation/services/producer.service.ts index 7244e7a..49cf9b5 100644 --- a/src/presentation/services/producer.service.ts +++ b/src/presentation/services/producer.service.ts @@ -18,7 +18,7 @@ export class ProducerService { }); } - async addToEmailQueue(payload: any) { + async addToEmailQueue(payload: any, queue: string) { try { // await this.channelWrapper.sendToQueue( // queue, @@ -27,7 +27,7 @@ export class ProducerService { // ); await this.channelWrapper.publish( this.EXCHANGE, - 'verify-email', + queue, Buffer.from(JSON.stringify(payload)), { persistent: true } );