public async Task <ScriptResult> Post([FromBody] ScriptResult result) { _dataService.AddOrUpdateScriptResult(result); if (result.HadErrors && result.SavedScriptId.HasValue) { var savedScript = await _dataService.GetSavedScript(result.SavedScriptId.Value); if (savedScript.GenerateAlertOnError) { await _dataService.AddAlert(result.DeviceID, result.OrganizationID, $"Alert triggered while running script {savedScript.Name}.", string.Join("\n", result.ErrorOutput)); } if (savedScript.SendEmailOnError) { var device = _dataService.GetDevice(result.DeviceID); await _emailSender.SendEmailAsync(savedScript.SendErrorEmailTo, "Script Run Alert", $"An alert was triggered while running script {savedScript.Name} on device {device.DeviceName}. <br /><br />" + $"Error Output: <br /><br /> " + $"{string.Join("<br /><br />", result.ErrorOutput)}"); } } if (result.ScriptRunId.HasValue) { await _dataService.AddScriptResultToScriptRun(result.ID, result.ScriptRunId.Value); } return(result); }
public async Task AddAlert() { await _dataService.AddAlert(_testData.Device1.ID, _testData.OrganizationID, "Test Message"); var alerts = _dataService.GetAlerts(_testData.Admin1.Id); Assert.AreEqual("Test Message", alerts.First().Message); }
public async Task <IActionResult> Create(AlertOptions alertOptions) { Request.Headers.TryGetValue("OrganizationID", out var orgID); _dataService.WriteEvent("Alert created. Alert Options: " + JsonSerializer.Serialize(alertOptions), orgID); if (alertOptions.ShouldAlert) { try { await _dataService.AddAlert(alertOptions.AlertDeviceID, orgID, alertOptions.AlertMessage); } catch (Exception ex) { _dataService.WriteEvent(ex, orgID); } } if (alertOptions.ShouldEmail) { try { await _emailSender.SendEmailAsync(alertOptions.EmailTo, alertOptions.EmailSubject, alertOptions.EmailBody, orgID); } catch (Exception ex) { _dataService.WriteEvent(ex, orgID); } } if (alertOptions.ShouldSendApiRequest) { try { using var httpClient = _httpClientFactory.CreateClient(); using var request = new HttpRequestMessage( new HttpMethod(alertOptions.ApiRequestMethod), alertOptions.ApiRequestUrl); request.Content = new StringContent(alertOptions.ApiRequestBody); request.Content.Headers.ContentType.MediaType = "application/json"; foreach (var header in alertOptions.ApiRequestHeaders) { request.Headers.TryAddWithoutValidation(header.Key, header.Value); } using var response = await httpClient.SendAsync(request); _dataService.WriteEvent($"Alert API Response Status: {response.StatusCode}.", orgID); } catch (Exception ex) { _dataService.WriteEvent(ex, orgID); } } return(Ok()); }