@@ -162,6 +162,11 @@ export class ChatService {
162162 } ) ;
163163
164164 const savedMessage = await this . messageRepository . save ( message ) ;
165+
166+ // Update the chat's updatedAt timestamp to reflect the latest message
167+ chat . updatedAt = new Date ( ) ;
168+ await this . chatRepository . save ( chat ) ;
169+
165170 console . log ( "Sent event" , `chat:${ chatId } ` ) ;
166171 this . eventEmitter . emit ( `chat:${ chatId } ` , [ savedMessage ] ) ;
167172
@@ -280,33 +285,58 @@ export class ChatService {
280285 page : number ;
281286 totalPages : number ;
282287 } > {
283- // First, get the chat IDs that the user is part of
284- const [ chatIds , total ] = await this . chatRepository
288+ // Get chats ordered by the most recent message timestamp
289+ const queryBuilder = this . chatRepository
285290 . createQueryBuilder ( "chat" )
286- . select ( [ "chat.id " , "chat.updatedAt" ] )
291+ . leftJoin ( "chat.messages " , "message" )
287292 . innerJoin ( "chat.participants" , "participants" )
288293 . where ( "participants.id = :userId" , { userId } )
289- . orderBy ( "chat.updatedAt" , "DESC" )
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
301+
302+ // Get total count for pagination
303+ const total = await queryBuilder . getCount ( ) ;
304+
305+ // Apply pagination
306+ const chats = await queryBuilder
290307 . skip ( ( page - 1 ) * limit )
291308 . take ( limit )
292- . getManyAndCount ( ) ;
309+ . getMany ( ) ;
293310
294- // Then, load the full chat data with all relations
295- const chats = await this . chatRepository . find ( {
296- where : { id : In ( chatIds . map ( ( chat ) => chat . id ) ) } ,
311+ // Load full chat data with all relations for the paginated results
312+ const chatsWithRelations = await this . chatRepository . find ( {
313+ where : { id : In ( chats . map ( ( chat ) => chat . id ) ) } ,
297314 relations : [
298315 "participants" ,
299316 "messages" ,
300317 "messages.sender" ,
301318 "messages.readStatuses" ,
302319 "messages.readStatuses.user" ,
303320 ] ,
304- order : { updatedAt : "DESC" } ,
321+ } ) ;
322+
323+ // Sort the chats by latest message timestamp (since we loaded relations, we need to sort again)
324+ const sortedChats = chatsWithRelations . sort ( ( a , b ) => {
325+ const aLatestMessage = a . messages [ a . messages . length - 1 ] ;
326+ const bLatestMessage = b . messages [ b . messages . length - 1 ] ;
327+
328+ if ( ! aLatestMessage && ! bLatestMessage ) {
329+ return b . createdAt . getTime ( ) - a . createdAt . getTime ( ) ;
330+ }
331+ if ( ! aLatestMessage ) return 1 ;
332+ if ( ! bLatestMessage ) return - 1 ;
333+
334+ return bLatestMessage . createdAt . getTime ( ) - aLatestMessage . createdAt . getTime ( ) ;
305335 } ) ;
306336
307337 // For each chat, get the latest message and its read status
308338 const chatsWithLatestMessage = await Promise . all (
309- chats . map ( async ( chat ) => {
339+ sortedChats . map ( async ( chat ) => {
310340 const latestMessage = chat . messages [ chat . messages . length - 1 ] ;
311341 if ( ! latestMessage ) {
312342 return { ...chat , latestMessage : undefined } ;
0 commit comments