Skip to content

Commit

Permalink
feat: notification system when a animal is updated to favorites done
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseAlbDR committed Feb 16, 2024
1 parent 91f5c7e commit c49643f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/presentation/animals/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
23 changes: 21 additions & 2 deletions src/presentation/services/animal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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');
Expand Down
28 changes: 16 additions & 12 deletions src/presentation/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class AuthService {
where: { email },
});


if (user)
throw new BadRequestError(
`Email ${registerUserDto.email} already exists, try another one`
Expand Down Expand Up @@ -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 {
Expand All @@ -103,7 +105,6 @@ export class AuthService {
}

private async validateCredentials(loginUserDto: LoginUserDto) {

const user = await prisma.user.findUnique({
where: { email: loginUserDto.email },
});
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions src/presentation/services/producer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ProducerService {
});
}

async addToEmailQueue(payload: any) {
async addToEmailQueue(payload: any, queue: string) {
try {
// await this.channelWrapper.sendToQueue(
// queue,
Expand All @@ -27,7 +27,7 @@ export class ProducerService {
// );
await this.channelWrapper.publish(
this.EXCHANGE,
'verify-email',
queue,
Buffer.from(JSON.stringify(payload)),
{ persistent: true }
);
Expand Down

0 comments on commit c49643f

Please sign in to comment.