|
@@ -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);
|
|
|
}
|