Example #1
0
        public async Task <string> SendMessage(SendMessageQuery sendMessageQuery)
        {
            _logger.LogInformation($"Make api call  get send message to user {sendMessageQuery.userPrincipalName}");
            string token = Request.Headers["Authorization"];

            return(await _userSessionService.SendMessage(token, sendMessageQuery).ConfigureAwait(false));
        }
        public async Task <IActionResult> SendMessage(DiagnoseDetailPageViewModel data)
        {
            if (ModelState.IsValid)
            {
                var viewData      = new SendMessageQuery();
                var messageStatus = new List <MessageStatus>();
                tenantGroupName = HttpContext.Session.Get <string>("SelectedTenantGroupName");
                tenant          = HttpContext.Session.Get <string>("SelectedTenantName");
                var refreshtoken = await HttpContext.GetTokenAsync("refresh_token").ConfigureAwait(false);

                accessToken = _commonService.GetAccessTokenWVD(refreshtoken); //await HttpContext.GetTokenAsync("access_token");

                if (string.IsNullOrEmpty(data.Message) && string.IsNullOrEmpty(data.Title))
                {
                    ViewBag.TitleErrorMsg   = "Subject is required";
                    ViewBag.MessageErrorMsg = "Message is required";
                }
                else if (string.IsNullOrEmpty(data.Title))
                {
                    ViewBag.TitleErrorMsg = "Title is required";
                }
                else if (string.IsNullOrEmpty(data.Message))
                {
                    ViewBag.MessageErrorMsg = "Message is required";
                }

                else
                {
                    if (data.UserSessions.Where(x => x.IsSelected == true).ToList().Count > 0)
                    {
                        foreach (var item in data.UserSessions.Where(x => x.IsSelected == true).ToList())
                        {
                            var sendMessageQuery = new SendMessageQuery()
                            {
                                tenantGroupName   = item.tenantGroupName,
                                tenantName        = item.tenantName,
                                hostPoolName      = item.hostPoolName,
                                sessionHostName   = item.sessionHostName,
                                sessionId         = item.sessionId,
                                messageTitle      = data.Title,
                                messageBody       = data.Message,
                                userPrincipalName = item.userPrincipalName,
                                adUserName        = item.adUserName
                            };
                            var Content = new StringContent(JsonConvert.SerializeObject(sendMessageQuery), Encoding.UTF8, "application/json");
                            _logger.LogInformation($"Call service to send message to {item.userPrincipalName}");
                            var response = await _userSessionService.SendMessage(accessToken, sendMessageQuery).ConfigureAwait(false);

                            if (response == HttpStatusCode.OK.ToString())
                            {
                                messageStatus.Add(new MessageStatus()
                                {
                                    Message = $"Message sent successfully to {item.adUserName}",
                                    Status  = "Success"
                                });
                            }
                            else if (response == HttpStatusCode.Forbidden.ToString() || response == HttpStatusCode.Unauthorized.ToString())
                            {
                                messageStatus.Add(new MessageStatus()
                                {
                                    Message = $"Failed to send message to {item.adUserName} . You don't have permissions to send message.",
                                    Status  = "Error"
                                });
                            }
                            else
                            {
                                messageStatus.Add(new MessageStatus()
                                {
                                    Message = $"Failed to send message to {item.adUserName}",
                                    Status  = "Error"
                                });
                            }
                        }
                        ViewBag.ErrorMsg = "";
                    }
                    else
                    {
                        ViewBag.ErrorMsg = "Please select at least one user";
                    }
                }
                return(View("ActivityHostDetails", new DiagnoseDetailPageViewModel()
                {
                    UserSessions = data.UserSessions.Where(usr => usr.IsSelected = true)
                                   .Select(usr => { usr.IsSelected = false; return usr; })
                                   .ToList(),
                    Title = data.Title,
                    Message = data.Message,
                    SendMsgStatuses = messageStatus,
                    ConnectionActivity = data.ConnectionActivity,
                    ShowConnectedUser = true,
                    ShowMessageForm = true,
                    selectedUsername = data.UserSessions.Where(x => x.IsSelected == true).ToList(),
                    VMPerformance = await GetVMPerformance(data.ConnectionActivity.SessionHostName).ConfigureAwait(false)
                }));
            }
            else
            {
                return(View("ActivityHostDetails", new DiagnoseDetailPageViewModel()
                {
                    UserSessions = data.UserSessions.Where(usr => usr.IsSelected = true)
                                   .Select(usr => { usr.IsSelected = false; return usr; })
                                   .ToList(),
                    ConnectionActivity = data.ConnectionActivity,
                    ShowConnectedUser = true,
                    ShowMessageForm = true,
                    Title = data.Title,
                    Message = data.Message,
                    selectedUsername = data.UserSessions.Where(x => x.IsSelected == true).ToList(),
                    VMPerformance = await GetVMPerformance(data.ConnectionActivity.SessionHostName).ConfigureAwait(false)
                }));
            }
        }