zinab_elgendy 1 miesiąc temu
rodzic
commit
eb140b5d5b

+ 15 - 1
MTWorkHR.Application/Chat/ChatHub.cs

@@ -60,7 +60,10 @@ namespace MTWorkHR.API.Chat
             {
                 var online = onlineUsers.FirstOrDefault(u=>  u.UserId == emp.Id);
                 var profileImg = "";
-                var chatUser = new ChatUserDto(emp.Id, emp.FirstName + " " + emp.LastName, online?.SignalrId, emp.Email, online != null ? true : false, profileImg);
+                var lastMsg = "";
+                var lastMsgDate = "";
+                var unseenCount = 0;
+                var chatUser = new ChatUserDto(emp.Id, emp.FirstName + " " + emp.LastName, online?.SignalrId, emp.Email, online != null ? true : false, profileImg, unseenCount,lastMsg,lastMsgDate);
             }
 
             return response;
@@ -120,6 +123,17 @@ namespace MTWorkHR.API.Chat
 
             await Clients.Caller.SendAsync("PreviousMessages", messagesList);
         }
+
+
+        public async Task<ChatMessage> GetLastMessage()
+        {
+            var userId = Context.User.Identities.FirstOrDefault().FindFirst("uid")?.Value;
+            var allmessages = await _unitOfWork.ChatMessage.GetAllUserMessagesAsync(userId);
+            // Ensure the query is fully materialized before passing it to SignalR
+            var lastOne = allmessages.Item1.LastOrDefault();
+
+            return lastOne?? new ChatMessage();
+        }
         //----------------------------------------------------------------------
         //----------------------------------------------------------------------
         //----------------------------------------------------------------------

+ 8 - 2
MTWorkHR.Application/Dtos/User/ChatUserDto.cs

@@ -18,6 +18,9 @@ namespace MTWorkHR.Application.Models
         public string Email { get; set; }
         public bool IsOnline { get; set; }
         public string? ProfileImage { get; set; }
+        public string? LastMessage { get; set; }
+        public string? LastMessageDate { get; set; }
+        public int? UnseenCount { get; set; }
         public ChatUserDto(string someId, string someName, string someConnId)
         {
             UserId = someId;
@@ -25,14 +28,17 @@ namespace MTWorkHR.Application.Models
             ConnectionId = someConnId;
         }
 
-        public ChatUserDto(string someId, string userName, string ConnId, string email, bool isOnline, string profileImg)
+        public ChatUserDto(string userId, string userName, string ConnId, string email, bool isOnline, string profileImg, int? unseenCount,string lastMessage, string lastMessageDate)
         {
-            UserId = someId;
+            UserId = userId;
             UserName = userName;
             ConnectionId = ConnId;
             Email = email;
             IsOnline = isOnline;
             ProfileImage = profileImg;
+            UnseenCount = unseenCount;
+            LastMessageDate = lastMessageDate;
+            LastMessage = lastMessage;
         }
     }
 }

+ 1 - 0
MTWorkHR.Core/IRepositories/Chat/IChatMessageRepository.cs

@@ -12,5 +12,6 @@ namespace MTWorkHR.Core.IRepositories
     {
         Task<ChatMessage> GetByIdWithAllChildren(long id);
         Task<Tuple<IQueryable<ChatMessage>, int>> GetAllWithChildrenAsync(string userId, string contactId);
+        Task<Tuple<IQueryable<ChatMessage>, int>> GetAllUserMessagesAsync(string userId);
     }
 }

+ 8 - 0
MTWorkHR.Infrastructure/Repositories/Chat/ChatMessageRepository.cs

@@ -29,5 +29,13 @@ namespace MTWorkHR.Infrastructure.Repositories
 
             return new Tuple<IQueryable<ChatMessage>, int>(query, total);
         }
+
+        public async Task<Tuple<IQueryable<ChatMessage>, int>> GetAllUserMessagesAsync(string userId)
+        {
+            var query = dbSet.Include(x => x.ChatAttachments).Where(m => (m.SenderId == userId) || (m.ReceiverId == userId )).AsQueryable();
+            var total = await query.CountAsync();
+
+            return new Tuple<IQueryable<ChatMessage>, int>(query, total);
+        }
     }
 }