Skip to content

Commit

Permalink
feat: add a notification request when a new chat is created
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseAlbDR committed Apr 1, 2024
1 parent 65f03da commit b228f9e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/presentation/chats/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
7 changes: 6 additions & 1 deletion src/presentation/chats/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ 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() {
const router = Router();

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);
Expand Down
15 changes: 13 additions & 2 deletions src/presentation/chats/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('-');
Expand All @@ -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,
Expand Down Expand Up @@ -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;
};

Expand Down

0 comments on commit b228f9e

Please sign in to comment.