diff --git a/backend/src/messages/messages.controller.ts b/backend/src/messages/messages.controller.ts index 7e946e3..bbe4831 100644 --- a/backend/src/messages/messages.controller.ts +++ b/backend/src/messages/messages.controller.ts @@ -22,6 +22,17 @@ export class MessagesController { return this.messagesService.getConversations(req.user.sub); } + @Get("conversations/with/:userId") + getConversationWithUser( + @Req() req: AuthenticatedRequest, + @Param("userId") targetUserId: string, + ) { + return this.messagesService.getConversationWithUser( + req.user.sub, + targetUserId, + ); + } + @Get("conversations/:id") getMessages( @Req() req: AuthenticatedRequest, diff --git a/backend/src/messages/messages.service.ts b/backend/src/messages/messages.service.ts index 8019fa2..0e89ca7 100644 --- a/backend/src/messages/messages.service.ts +++ b/backend/src/messages/messages.service.ts @@ -42,6 +42,13 @@ export class MessagesService { return this.messagesRepository.findAllConversations(userId); } + async getConversationWithUser(userId: string, targetUserId: string) { + return this.messagesRepository.findConversationBetweenUsers( + userId, + targetUserId, + ); + } + async getMessages(userId: string, conversationId: string) { const isParticipant = await this.messagesRepository.isParticipant( conversationId, @@ -51,6 +58,20 @@ export class MessagesService { throw new ForbiddenException("You are not part of this conversation"); } + // Marquer comme lus + await this.messagesRepository.markAsRead(conversationId, userId); + return this.messagesRepository.findMessagesByConversationId(conversationId); } + + async markAsRead(userId: string, conversationId: string) { + const isParticipant = await this.messagesRepository.isParticipant( + conversationId, + userId, + ); + if (!isParticipant) { + throw new ForbiddenException("You are not part of this conversation"); + } + return this.messagesRepository.markAsRead(conversationId, userId); + } } diff --git a/frontend/src/services/message.service.ts b/frontend/src/services/message.service.ts index 4be77d2..168b282 100644 --- a/frontend/src/services/message.service.ts +++ b/frontend/src/services/message.service.ts @@ -36,6 +36,13 @@ export const MessageService = { return data; }, + async getConversationWith(userId: string): Promise { + const { data } = await api.get( + `/messages/conversations/with/${userId}`, + ); + return data; + }, + async sendMessage(recipientId: string, text: string): Promise { const { data } = await api.post("/messages", { recipientId,