private bool ValidateAlertJobsQueueData(AlertJobsQueueData newdata, IConfiguration configuration) { var HRToken = new JwtSecurityToken(newdata.HRToken); if (Helper.TokenValid(HRToken) == false) { return(false); } if (newdata.HRToken.Trim() == "") { return(false); } if (newdata.AlertNames.Count() < 1) { return(false); } if (newdata.AlertJobsQueue == null) { return(false); } // Check if HR is up before calling HR routines var hrResponse = Helper.GetHRServerStatus(configuration); // We expect a 400 - Bad Request, if 404 Not Found, return an error if (hrResponse.StatusCode == 404) { return(false); } return(true); }
public IActionResult Create([FromBody] AlertJobsQueueData newdata) { var data = _repository.CreateAlertJobsQueue(newdata, _configuration); return(Helper.CheckResult(data)); }
public IActionResult Create([FromBody] AlertJobsQueueData newmodel) { if (!ModelState.IsValid) { { return(NotFound()); } } if (newmodel.HRToken.Trim() == "") { { return(NotFound("Invalid Token")); } } if (newmodel.AlertNames.Count() < 1) { return(BadRequest("No Alert Names found")); } if (newmodel.AlertJobsQueue == null) { return(BadRequest("No Alert Job found")); } // Check if HR is up var hruri = _configuration.GetSection("HumanReview:uri").Value + "auth/token"; var hrResponse = Common.ServerStatusBy(hruri); // We expect a 400 - Bad Request, anything else specifically 404 Not Found, return an error if (hrResponse.StatusCode == 404) { { return(NotFound("Error: Human Review Service unavailable")); } } AlertJobsQueue alertJobQueue = newmodel.AlertJobsQueue; List <AlertNames> alertNames = newmodel.AlertNames; ReturnData retValue = null; int alertJobsQueueID = 0; var identity = alertJobQueue.CreatedBy; try { var returnObject = _context.AlertJobsQueue.Add(alertJobQueue); _context.SaveChanges(); // check id of new Batch contained in alertJobQueue alertJobsQueueID = alertJobQueue.AlertJobsQueueID; var todayUtc = DateTime.UtcNow; foreach (var alertname in alertNames) { var newAlertEntity = new AlertJobsQueueEntity(); newAlertEntity.AlertNameID = alertname.AlertNameID; newAlertEntity.AlertJobsQueueID = alertJobsQueueID; // created above newAlertEntity.CreatedBy = identity; newAlertEntity.UpdatedBy = identity; newAlertEntity.DateCreatedUTC = todayUtc; newAlertEntity.LastUpdatedUTC = todayUtc; newAlertEntity.StatusID = 26; // Newly Created, based in CollectionItem, CollectionID //newAlertEntity.WorkItemID _context.AlertJobsQueueEntity.Add(newAlertEntity); } _context.SaveChanges(); } catch (Exception e) { var loginfo = e.Message; ; } // Human Review #region Human Review Entries var AlertEntities = _context.AlertJobsQueueEntity.Where(t => t.AlertJobsQueueID == alertJobsQueueID); var Modules = _context.ApplicationModules; var QueueGuid = (from mods in Modules where mods.ModuleName == "Alerts" select mods.QueueGuid).FirstOrDefault(); try { foreach (var alertentity in AlertEntities) { var JsonData = JsonConvert.SerializeObject(alertentity); WorkItemRequest HRCreateRequest = new WorkItemRequest(); HRCreateRequest.name = "Created WorkItem for Alert Entity " + alertentity.AlertJobsQueueEntityID; HRCreateRequest.description = "Created WorkItem for Alert Entity " + alertentity.AlertJobsQueueEntityID; HRCreateRequest.queueGuid = QueueGuid; HRCreateRequest.statusDetailTypeGuid = _configuration.GetSection("HumanReview:statusDetailTypeGuid_ins").Value; HRCreateRequest.reviewTypeGuid = _configuration.GetSection("HumanReview:reviewTypeGuid_ins").Value; HRCreateRequest.formDefinitionJson = JsonData; HRCreateRequest.isActive = true; var returnGuid = Common.getWorkItemAsync(HRCreateRequest, newmodel.HRToken, _configuration); if (returnGuid.value.workItemGuid == null) { var workitemnotcreated = new { Success = false, Message = "WorkItem not created" }; return(Json(workitemnotcreated)); } alertentity.WorkItemID = new Guid(returnGuid.value.workItemGuid); } retValue = _context.SaveData(); if (retValue.Message == "Success") { return(Ok(retValue)); } // return error if workitemid was not updated for this entity } catch (Exception e) { var loginfo = e.Message; var iactdelete = Delete(alertJobsQueueID); } #endregion { return(BadRequest(retValue)); } }
public AlertJobsQueue CreateAlertJobsQueue(AlertJobsQueueData newdata, IConfiguration configuration) { bool DataValid = ValidateAlertJobsQueueData(newdata, configuration); if (DataValid == false) { return(null); } // // Editorial Routines AlertJobsQueue alertJobQueue = newdata.AlertJobsQueue; List <AlertNames> alertNames = newdata.AlertNames; int alertJobsQueueID = 0; var identity = alertJobQueue.CreatedBy; // Save the Job Queue entry var returnObject = _context.AlertJobsQueue.Add(alertJobQueue); _context.SaveChanges(); // check id of new Batch contained in alertJobQueue alertJobsQueueID = alertJobQueue.AlertJobsQueueID; var todayUtc = DateTime.UtcNow; // Save each Alert Entitiy foreach (var alertname in alertNames) { var newAlertEntity = new AlertJobsQueueEntity { AlertNameID = alertname.AlertNameID, AlertJobsQueueID = alertJobsQueueID, CreatedBy = identity, UpdatedBy = identity, DateCreatedUTC = todayUtc, LastUpdatedUTC = todayUtc, StatusID = 26 // Newly Created, based in CollectionItem, CollectionID }; _context.AlertJobsQueueEntity.Add(newAlertEntity); } _context.SaveChanges(); // // Human Review routines var AlertEntities = _context.AlertJobsQueueEntity.Where(t => t.AlertJobsQueueID == alertJobsQueueID); var Modules = _context.ApplicationModules; // Human Review Queue Guid saved in the Editorial Database var QueueGuid = (from mods in Modules where mods.ModuleName == EditorialModules.Alerts select mods.QueueGuid).FirstOrDefault(); var QueueGuidString = QueueGuid.ToString(); foreach (var alertentity in AlertEntities) { var JsonData = JsonConvert.SerializeObject(alertentity); var HRCreateRequest = Helper.BuildHRWorkItemRequest(EditorialModules.Alerts, QueueGuidString, JsonData, configuration, null, HRRequestMode.Create); var returnGuid = Helper.GetHRWorkItem((WorkItemRequest)HRCreateRequest, newdata.HRToken, configuration); if (returnGuid.Value.workItemGuid == null) { return(null); } // Save the WorkItemGuid created above to this Alert Entity entry alertentity.WorkItemID = new Guid(returnGuid.Value.workItemGuid); } _context.SaveChanges(); return(alertJobQueue); }