public async Task <ActionResult> GetAlertById(string id) { try { var start = DateTime.Now; var token = string.Empty; if (Request.Headers.ContainsKey("Authorization")) { token = Request.Headers["Authorization"].ToString()?.Split(" ")?[1]; } _graphService = _graphServiceProvider.GetService(token); var startAll = DateTime.Now; var alert = await _graphService.GetAlertDetailsAsync(id); Debug.WriteLine($"Executing time GetAlertDetailsFromGraph: {DateTime.Now - startAll}"); if (alert == null) { return(NotFound()); } var sdkQueryBuilder = new StringBuilder(); var restQueryBuilder = new StringBuilder(); sdkQueryBuilder.Append($"await graphClient.Security.Alerts[\"{id}\"].Request().GetAsync()"); restQueryBuilder.Append($"<a href=\"https://developer.microsoft.com/en-us/graph/graph-explorer?request=security/alerts/{id}&method=GET&version={_graphService.GraphUrlVersion}&GraphUrl=https://graph.microsoft.com\" target=\"_blank\">https://graph.microsoft.com/{_graphService.GraphUrlVersion}/security/alerts/{id}/</a>"); var alertModel = alert.ToAlertDetailsViewModel(); await AddAdditionalInformationAboutAlert(alert, alertModel); //Only for demo if (alertModel.HistoryStates == null || alertModel.HistoryStates?.Count() == 0) { alertModel.HistoryStates = _demoExample.GetAlertHistoryStates(alert, alertModel.AssignedTo, alertModel?.UserAccountDevices?.FirstOrDefault()); } ResultQueriesViewModel resultQueriesViewModel = new ResultQueriesViewModel(sdkQueryBuilder.ToString(), restQueryBuilder.ToString()); AlertDetailsResponse response = new AlertDetailsResponse(alertModel, resultQueriesViewModel); Debug.WriteLine($"Executing time AlertController GetAlertsById: {DateTime.Now - start}"); return(Ok(response)); } catch (Exception exception) { return(BadRequest(exception.Message)); } }
public async Task <ActionResult> UpdateAlert([FromBody] AlertUpdateRequest updateAlertModel, string id) { try { var start = DateTime.Now; var token = string.Empty; if (Request.Headers.ContainsKey("Authorization")) { token = Request.Headers["Authorization"].ToString()?.Split(" ")?[1]; } _graphService = _graphServiceProvider.GetService(token); var sdkQueryBuilder = new StringBuilder(); var restQueryBuilder = new StringBuilder(); sdkQueryBuilder.Append($"await graphClient.Security.Alerts[\"{id}\"].Request().UpdateAsync(updatedAlert)"); restQueryBuilder.Append($"PATCH <a>https://graph.microsoft.com/{_graphService.GraphUrlVersion}/security/alerts/{id}</a>"); var email = $"\"{await _graphService.GetMyEmailAddressAsync()}\""; if (!string.IsNullOrEmpty(updateAlertModel.AssignedTo)) { email = $" \"assignedTo\" = \"{updateAlertModel.AssignedTo}\" "; } if (!Enum.TryParse <AlertStatus>(updateAlertModel.Status, true, out var status)) { throw new ArgumentOutOfRangeException(nameof(updateAlertModel.Status)); } if (!Enum.TryParse <AlertFeedback>(updateAlertModel.Feedback, true, out var feedback)) { throw new ArgumentOutOfRangeException(nameof(updateAlertModel.Feedback)); } UserAccountDevice userUpn = _memoryCacheHelper.GetUserAccountDevice(updateAlertModel.UserUpn); if (userUpn == null) { userUpn = await _graphService.GetUserDetailsAsync(updateAlertModel.UserUpn, populatePicture : true, populateManager : true, populateDevices : true); _memoryCacheHelper.SetUserAccountDevice(updateAlertModel.UserUpn, userUpn); } UserAccountDevice assignedTo = _memoryCacheHelper.GetUserAccountDevice(updateAlertModel.AssignedTo); if (assignedTo == null) { assignedTo = await _graphService.GetUserDetailsAsync(updateAlertModel.AssignedTo, populatePicture : true, populateManager : true, populateDevices : true); _memoryCacheHelper.SetUserAccountDevice(updateAlertModel.AssignedTo, assignedTo); } _demoExample.AddAlertHistoryState(id, new AlertHistoryState() { Status = status, Feedback = feedback, Comments = new List <string>() { updateAlertModel.Comments.Last() }, AssignedTo = assignedTo, UpdatedDateTime = DateTimeOffset.UtcNow, User = userUpn }); restQueryBuilder.Append($" Request Body: {{ \"status\" = \"{updateAlertModel?.Status}\", {email} alert.Feedback = {updateAlertModel?.Feedback}; alert.Comments = {updateAlertModel?.Comments} "); var resultQueriesViewModel = new ResultQueriesViewModel(sdkQueryBuilder.ToString(), restQueryBuilder.ToString()); var alert = await _graphService.GetAlertDetailsAsync(id); if (alert == null) { return(NotFound()); } await _graphService.UpdateAlertAsync(alert, updateAlertModel); alert = await _graphService.GetAlertDetailsAsync(id); var alertModel = alert.ToAlertDetailsViewModel(); await AddAdditionalInformationAboutAlert(alert, alertModel); //Only for demo if (alertModel.HistoryStates == null || alertModel.HistoryStates?.Count() == 0) { alertModel.HistoryStates = _demoExample.GetAlertHistoryStates(alert, alertModel.AssignedTo, alertModel?.UserAccountDevices?.FirstOrDefault()); } AlertDetailsResponse response = new AlertDetailsResponse(alertModel, resultQueriesViewModel); Debug.WriteLine($"Executing time AlertController UpdateAlert: {DateTime.Now - start}"); return(Ok(response)); } catch (Exception exception) { return(BadRequest(exception.Message)); } }