From b228f9e01b326c68c9379c2567d16259bb9cc941 Mon Sep 17 00:00:00 2001 From: Jose Alberto Delgado Robles Date: Mon, 1 Apr 2024 17:44:38 +0200 Subject: [PATCH] feat: add a notification request when a new chat is created --- src/presentation/chats/controller.ts | 2 +- src/presentation/chats/router.ts | 7 ++++++- src/presentation/chats/service.ts | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/presentation/chats/controller.ts b/src/presentation/chats/controller.ts index 38902fc..bf65055 100644 --- a/src/presentation/chats/controller.ts +++ b/src/presentation/chats/controller.ts @@ -8,7 +8,7 @@ export class ChatController { constructor(private readonly chatService: ChatService) {} createChat = async (req: Request, res: Response) => { - const chat = await this.chatService.createChat(req.body); + const chat = await this.chatService.createChat(req.user, req.body); res.status(HttpCodes.OK).json(chat); }; diff --git a/src/presentation/chats/router.ts b/src/presentation/chats/router.ts index cbaa012..a7b1857 100644 --- a/src/presentation/chats/router.ts +++ b/src/presentation/chats/router.ts @@ -3,6 +3,7 @@ import { ChatService } from './service'; import { ChatController } from './controller'; import { JWTAdapter, envs } from '../../config'; import { AuthMiddleware } from '../middlewares'; +import { QueueService } from '../common/services'; export class ChatRoutes { static get routes() { @@ -10,7 +11,11 @@ export class ChatRoutes { const jwt = new JWTAdapter(envs.JWT_SEED); const authMiddleware = new AuthMiddleware(jwt); - const chatService = new ChatService(); + const notificationService = new QueueService( + envs.RABBITMQ_URL, + 'notification-request' + ); + const chatService = new ChatService(notificationService); const chatController = new ChatController(chatService); router.use(authMiddleware.authenticateUser); diff --git a/src/presentation/chats/service.ts b/src/presentation/chats/service.ts index 9eeddec..3ed137d 100644 --- a/src/presentation/chats/service.ts +++ b/src/presentation/chats/service.ts @@ -2,9 +2,10 @@ import { prisma } from '../../data/postgres'; import { NotFoundError } from '../../domain'; import { CreateChatDto } from '../../domain/dtos/chat/create-chat.dto'; import { PayloadUser } from '../../domain/interfaces'; +import { QueueService } from '../common/services'; export class ChatService { - constructor() {} + constructor(private readonly notificationService: QueueService) {} private getDataFromRoom(room: string) { const parts = room.split('-'); @@ -15,7 +16,7 @@ export class ChatService { return { shelterUsername, adopterUsername, animalSlug }; } - createChat = async ({ room }: CreateChatDto) => { + createChat = async (user: PayloadUser, { room }: CreateChatDto) => { const chatExist = await prisma.adoptionChat.findUnique({ where: { slug: room, @@ -57,6 +58,16 @@ export class ChatService { }, }); + this.notificationService.addMessageToQueue( + { + ...newAdoptionChat, + username: + adopterUsername === user.name ? shelterUsername : adopterUsername, + queue: 'new-chat-push-notification', + }, + 'new-chat-push-notification' + ); + return newAdoptionChat; };