2 Commity f8256ddd16 ... bdbb78e902

Autor SHA1 Wiadomość Data
  zinab_elgendy bdbb78e902 ChatUser: Default Constractor 2 tygodni temu
  zinab_elgendy 44a211b485 Mapper for UserId 2 tygodni temu

+ 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)

+ 2 - 1
MTWorkHR.Application/Mapper/MappingProfile.cs

@@ -54,7 +54,8 @@ namespace MTWorkHR.Application.Mapper
             CreateMap<UserBasicInfoDto, ApplicationUser>().ReverseMap();
             CreateMap<UserBasicInfoDto, UserDto>().ReverseMap();
 
-            CreateMap<ApplicationUser, ChatUserDto>().ReverseMap();
+            CreateMap<ApplicationUser, ChatUserDto>()
+            .ForMember(s => s.UserId, o => o.MapFrom(s => s.Id )).ReverseMap();
             
             CreateMap<AttachmentDto, UserAttachment>().ReverseMap();
             CreateMap<UserAddress, UserAddressDto>()