feat: add unread message count API

- Added `GET /messages/unread-count` endpoint to retrieve the count of unread messages for a user.
- Implemented `getUnreadCount` method in `MessagesService` and `MessagesRepository`.
- Updated frontend service to support fetching unread message count via API.
This commit is contained in:
Mathis HERRIOT
2026-01-29 15:47:43 +01:00
parent f852835c59
commit 0972ed951f
4 changed files with 45 additions and 0 deletions

View File

@@ -22,6 +22,11 @@ export class MessagesController {
return this.messagesService.getConversations(req.user.sub); return this.messagesService.getConversations(req.user.sub);
} }
@Get("unread-count")
getUnreadCount(@Req() req: AuthenticatedRequest) {
return this.messagesService.getUnreadCount(req.user.sub);
}
@Get("conversations/with/:userId") @Get("conversations/with/:userId")
getConversationWithUser( getConversationWithUser(
@Req() req: AuthenticatedRequest, @Req() req: AuthenticatedRequest,

View File

@@ -42,6 +42,10 @@ export class MessagesService {
return this.messagesRepository.findAllConversations(userId); return this.messagesRepository.findAllConversations(userId);
} }
async getUnreadCount(userId: string) {
return this.messagesRepository.countUnreadMessages(userId);
}
async getConversationWithUser(userId: string, targetUserId: string) { async getConversationWithUser(userId: string, targetUserId: string) {
return this.messagesRepository.findConversationBetweenUsers( return this.messagesRepository.findConversationBetweenUsers(
userId, userId,

View File

@@ -133,4 +133,35 @@ export class MessagesRepository {
.from(conversationParticipants) .from(conversationParticipants)
.where(eq(conversationParticipants.conversationId, conversationId)); .where(eq(conversationParticipants.conversationId, conversationId));
} }
async markAsRead(conversationId: string, userId: string) {
await this.databaseService.db
.update(messages)
.set({ readAt: new Date() })
.where(
and(
eq(messages.conversationId, conversationId),
sql`${messages.senderId} != ${userId}`,
sql`${messages.readAt} IS NULL`,
),
);
}
async countUnreadMessages(userId: string) {
const result = await this.databaseService.db
.select({ count: sql<number>`count(*)` })
.from(messages)
.innerJoin(
conversationParticipants,
eq(messages.conversationId, conversationParticipants.conversationId),
)
.where(
and(
eq(conversationParticipants.userId, userId),
sql`${messages.senderId} != ${userId}`,
sql`${messages.readAt} IS NULL`,
),
);
return Number(result[0]?.count || 0);
}
} }

View File

@@ -29,6 +29,11 @@ export const MessageService = {
return data; return data;
}, },
async getUnreadCount(): Promise<number> {
const { data } = await api.get<number>("/messages/unread-count");
return data;
},
async getMessages(conversationId: string): Promise<Message[]> { async getMessages(conversationId: string): Promise<Message[]> {
const { data } = await api.get<Message[]>( const { data } = await api.get<Message[]>(
`/messages/conversations/${conversationId}`, `/messages/conversations/${conversationId}`,