@@ -285,19 +285,12 @@ export class ChatService {
285285 page : number ;
286286 totalPages : number ;
287287 } > {
288- // Get chats ordered by the most recent message timestamp
288+ // First, get all chats for the user with basic info
289289 const queryBuilder = this . chatRepository
290290 . createQueryBuilder ( "chat" )
291- . leftJoin ( "chat.messages" , "message" )
292291 . innerJoin ( "chat.participants" , "participants" )
293292 . where ( "participants.id = :userId" , { userId } )
294- . groupBy ( "chat.id" )
295- . addGroupBy ( "chat.name" )
296- . addGroupBy ( "chat.ename" )
297- . addGroupBy ( "chat.createdAt" )
298- . addGroupBy ( "chat.updatedAt" )
299- . orderBy ( "MAX(message.createdAt)" , "DESC" )
300- . addOrderBy ( "chat.createdAt" , "DESC" ) ; // Fallback for chats without messages
293+ . orderBy ( "chat.updatedAt" , "DESC" ) ;
301294
302295 // Get total count for pagination
303296 const total = await queryBuilder . getCount ( ) ;
@@ -320,7 +313,7 @@ export class ChatService {
320313 ] ,
321314 } ) ;
322315
323- // Sort the chats by latest message timestamp (since we loaded relations, we need to sort again )
316+ // Sort the chats by latest message timestamp (most recent first )
324317 const sortedChats = chatsWithRelations . sort ( ( a , b ) => {
325318 const aLatestMessage = a . messages [ a . messages . length - 1 ] ;
326319 const bLatestMessage = b . messages [ b . messages . length - 1 ] ;
0 commit comments