Browse Source

ChatUser: Default Constractor

zinab_elgendy 1 month ago
parent
commit
bdbb78e902
2 changed files with 42 additions and 32 deletions
  1. 26 18
      MTWorkHR.Application/Chat/ChatHub.cs
  2. 16 14
      MTWorkHR.Application/Dtos/User/ChatUserDto.cs

+ 26 - 18
MTWorkHR.Application/Chat/ChatHub.cs

@@ -47,25 +47,33 @@ namespace MTWorkHR.API.Chat
       
         public async Task<List<ChatUserDto>> GetAllCompanyEmployees()
         {
-            var employees = await _userManager.GetUsersInRoleAsync("Employee");
-            var CompanyId = GetAuthenticatedCompanyId();
-            var userId = Context.User.Identities.FirstOrDefault().FindFirst("uid")?.Value;
-            var res = employees.Where(e => e.CompanyId == CompanyId && e.Id != userId).ToList();
-
-            var response = MapperObject.Mapper.Map<List<ChatUserDto>>(res);
+            List<ChatUserDto> response = null;
+            try
+            {
+                var employees = await _userManager.GetUsersInRoleAsync("Employee");
+                var CompanyId = GetAuthenticatedCompanyId();
+                var userId = Context.User.Identities.FirstOrDefault().FindFirst("uid")?.Value;
+                var res = employees.Where(e => e.CompanyId == CompanyId && e.Id != userId).ToList();
+            
+                response = MapperObject.Mapper.Map<List<ChatUserDto>>(res);
 
-            var allConnections = await _unitOfWork.HubConnection.GetAllAsync();
-            var onlineUsers = allConnections.Item1
-                .Select(c => new { c.UserId, c.SignalrId }).ToList();
-            foreach(var emp in res)
+                var allConnections = await _unitOfWork.HubConnection.GetAllAsync();
+                var onlineUsers = allConnections.Item1
+                    .Select(c => new { c.UserId, c.SignalrId }).ToList();
+                foreach (var emp in res)
+                {
+                    var online = onlineUsers.FirstOrDefault(u => u.UserId == emp.Id);
+                    var profileImg = "";
+                    var lastMessage = await GetLastMessage(emp.Id);
+                    var lastMsg = lastMessage.Item1?.Content;
+                    var lastMsgDate = lastMessage.Item1?.CreateDate;
+                    var unseenCount = lastMessage.Item2;
+                    var chatUser = new ChatUserDto(emp.Id, emp.FirstName + " " + emp.LastName, online?.SignalrId, emp.Email, online != null ? true : false, profileImg, unseenCount, lastMsg, lastMsgDate);
+                }
+            }catch(Exception e)
             {
-                var online = onlineUsers.FirstOrDefault(u=>  u.UserId == emp.Id);
-                var profileImg = "";
-                var lastMessage = await GetLastMessage(emp.Id);
-                var lastMsg = lastMessage.Item1?.Content;
-                var lastMsgDate = lastMessage.Item1?.CreateDate;
-                var unseenCount = lastMessage.Item2;
-                var chatUser = new ChatUserDto(emp.Id, emp.FirstName + " " + emp.LastName, online?.SignalrId, emp.Email, online != null ? true : false, profileImg, unseenCount,lastMsg,lastMsgDate);
+                var m = e.Message;
+                Console.WriteLine(e.Message);
             }
 
             return response;
@@ -132,7 +140,7 @@ namespace MTWorkHR.API.Chat
             var userId = Context.User.Identities.FirstOrDefault().FindFirst("uid")?.Value;
             var allmessages = await _unitOfWork.ChatMessage.GetAllWithChildrenAsync(userId, contactId);
             // Ensure the query is fully materialized before passing it to SignalR
-            var lastOne = allmessages.Item1.LastOrDefault();
+            var lastOne = allmessages.Item1.OrderByDescending(m=> m.Id).FirstOrDefault();
             var unseen = allmessages.Item1.Count(m=> m.IsSeen == false);
             return new Tuple<ChatMessage, int>( lastOne?? new ChatMessage(), unseen);
         }

+ 16 - 14
MTWorkHR.Application/Dtos/User/ChatUserDto.cs

@@ -1,4 +1,5 @@
-using System;
+using Newtonsoft.Json.Serialization;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -8,24 +9,25 @@ namespace MTWorkHR.Application.Models
 {
     public class ChatUserDto
     {
-        public string UserId { get; set; }
-        public string UserName { get; set; }
-        public string ConnectionId { get; set; } //signalrId
-        public string FirstName { get; set; }
-        public string LastName { get; set; }
-        public string FavoriteName { get; set; }
-        public string PhoneNumber { get; set; }
-        public string Email { get; set; }
-        public bool IsOnline { get; set; }
+        public string? UserId { get; set; }
+        public string? UserName { get; set; }
+        public string? ConnectionId { get; set; } //signalrId
+        public string? FirstName { get; set; }
+        public string? LastName { get; set; }
+        public string? FavoriteName { get; set; }
+        public string? PhoneNumber { get; set; }
+        public string? Email { get; set; }
+        public bool? IsOnline { get; set; }
         public string? ProfileImage { get; set; }
         public string? LastMessage { get; set; }
         public DateTime? LastMessageDate { get; set; }
         public int? UnseenCount { get; set; }
-        public ChatUserDto(string someId, string someName, string someConnId)
+        public ChatUserDto() { }
+        public ChatUserDto(string userId, string userName, string ConnId)
         {
-            UserId = someId;
-            UserName = someName;
-            ConnectionId = someConnId;
+            UserId = userId;
+            UserName = userName;
+            ConnectionId = ConnId;
         }
 
         public ChatUserDto(string userId, string userName, string ConnId, string email, bool isOnline, string profileImg, int? unseenCount,string lastMessage, DateTime? lastMessageDate)