Example #1
0
        /// <summary>
        /// Called when an existing WorkItem is updated
        ///
        /// </summary>
        /// <param name="wkitem"></param>
        /// <param name="token"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static WorkItemData PutHRWorkItem(WorkItemPutRequest wkitem, string token, IConfiguration configuration)
        {
            string hruri = configuration.GetSection("HumanReview:uri").Value + "workItems/";
            Uri    uri   = new Uri(hruri);
            HttpResponseMessage response;
            HttpClient          client;

            WorkItemData wrkData;

            using (client = new HttpClient())
            {
                var ReadyClient = InitializeHttpClient(client, token, true);
                //ReadyClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                var jsonText    = JsonConvert.SerializeObject(wkitem);
                var jsonContent = new StringContent(jsonText, UnicodeEncoding.UTF8, "application/json");
                ;
                response = client.PutAsync(uri, jsonContent).Result;
                response.EnsureSuccessStatusCode();
                var dataString = response.Content.ReadAsStringAsync().Result;

                wrkData = JsonConvert.DeserializeObject <WorkItemData>(dataString);

                ReadyClient.Dispose();
            }
            return(wrkData);
        }
Example #2
0
        // HR Routines
        public static WorkItemData putWorkItemForEntityAsync(WorkItemPutRequest wkitem, string token, IConfiguration configuration)
        {
            string hruri = configuration.GetSection("HumanReview:uri").Value + "workItems/";
            Uri    uri   = new Uri(hruri);
            HttpResponseMessage response;
            HttpClient          client;

            WorkItemData wrkData;

            using (client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();

                client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
                client.DefaultRequestHeaders.TryAddWithoutValidation("X-HumanReviewApi-Version", "1.0");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                var jsonText    = JsonConvert.SerializeObject(wkitem);
                var jsonContent = new StringContent(jsonText, UnicodeEncoding.UTF8, "application/json");
                ;
                response = client.PutAsync(uri, jsonContent).Result;
                response.EnsureSuccessStatusCode();
                var dataString = response.Content.ReadAsStringAsync().Result;

                wrkData = JsonConvert.DeserializeObject <WorkItemData>(dataString);
            }
            return(wrkData);
        }
Example #3
0
        /// <summary>
        ///
        /// This function returns a request structure to be sent to the Human Review for Creating and Updating WorkItems
        /// </summary>
        /// <param name="modulename"></param>
        /// <param name="QueueGuidString"></param>
        /// <param name="JsonData"></param>
        /// <param name="configuration"></param>
        /// <param name="workitemGuid"></param>
        /// <param name="hrRequestMode"></param>
        /// <returns></returns>
        public static object BuildHRWorkItemRequest(string modulename, string QueueGuidString, string JsonData,
                                                    IConfiguration configuration, string workitemGuid, HRRequestMode hrRequestMode)
        {
            string HRCreateRequestName        = "";
            string HRCreateRequestDescription = "";
            string HRStatusDetailTypeGuid     = "";
            string HRReviewTypeGuid           = "";
            bool   HREntryActive = true;
            Guid   QueueGuid     = Guid.Parse(QueueGuidString);

            if (hrRequestMode == HRRequestMode.Create)
            {
                HRCreateRequestName        = "Created Human Review WorkItem Entry for " + modulename + " Record";
                HRCreateRequestDescription = "Created Human Review WorkItem Entry for " + modulename + " Record";
                HRStatusDetailTypeGuid     = configuration.GetSection("HumanReview:statusDetailTypeGuid_ins").Value;
                HRReviewTypeGuid           = configuration.GetSection("HumanReview:reviewTypeGuid_ins").Value;

                WorkItemRequest HRCreateRequest = new WorkItemRequest
                {
                    Name                 = HRCreateRequestName,
                    Description          = HRCreateRequestDescription,
                    QueueGuid            = QueueGuid,
                    StatusDetailTypeGuid = HRStatusDetailTypeGuid,
                    ReviewTypeGuid       = HRReviewTypeGuid,
                    FormDefinitionJson   = JsonData,
                    IsActive             = HREntryActive
                };

                return(HRCreateRequest);
            }

            if (hrRequestMode == HRRequestMode.Update)
            {
                HRCreateRequestName        = "Updated Human Review WorkItem Entry for " + modulename + " Record";
                HRCreateRequestDescription = "Updated Human Review WorkItem Entry for " + modulename + " Record";
                HRStatusDetailTypeGuid     = configuration.GetSection("HumanReview:statusDetailTypeGuid_upd").Value;
                HRReviewTypeGuid           = configuration.GetSection("HumanReview:reviewTypeGuid_upd").Value;

                WorkItemPutRequest HRCreateRequest = new WorkItemPutRequest
                {
                    Name                 = HRCreateRequestName,
                    Description          = HRCreateRequestDescription,
                    QueueGuid            = QueueGuid,
                    StatusDetailTypeGuid = HRStatusDetailTypeGuid,
                    ReviewTypeGuid       = HRReviewTypeGuid,
                    FormDefinitionJson   = JsonData,
                    IsActive             = HREntryActive,
                    WorkitemGuid         = workitemGuid // Needed for Update calls
                };

                return(HRCreateRequest);
            }

            return(null);
        }
        public IActionResult UpdateEntry([FromBody] NewsDataSave objupdate)
        {
            var HRtoken             = objupdate.HRToken;
            var FeedItemQueueObject = objupdate.FeedItemQueue;
            var WorkItemGuid        = objupdate.WorkItemGuid;

            var Message = "";

            // 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")); }
            }

            if (HRtoken == null || FeedItemQueueObject == null || WorkItemGuid == null)
            {
                Message = "Invalid Request";
                return(BadRequest(Message));
            }

            var MappingEntry = _context.HRWorkItemDataMap
                               .Where(t => t.WorkItemID == WorkItemGuid && t.WorkUnitTypeID == 5)
                               .FirstOrDefault();

            /*
             * 3	Investigation
             * 4	Alerts
             * 5	News Queue
             * 6	BWQ
             */
            if (MappingEntry == null)
            {
                Message = "Work Item not found";
                return(BadRequest(Message));
            }

            var FeedItemQueueEntry = _NewsDBcontext.FeedItemQueue
                                     .Where(t => t.id == FeedItemQueueObject.id)
                                     .FirstOrDefault();

            if (FeedItemQueueEntry == null)
            {
                Message = "Item entry does not exist";
                return(BadRequest(Message));
            }

            var AppUserId = FeedItemQueueEntry.stateChangedRecipient; // the person that sent this update request

            //Check locks
            var NewsLockEntry = _NewsDBcontext.FeedItemQueueLocks
                                .Where(t => t.fkItemID == FeedItemQueueObject.id && t.LockedByRecipientID == AppUserId)
                                .FirstOrDefault();

            if (NewsLockEntry != null) // lock exists
            {
                var AppUser = _context.AppUser
                              .Where(u => u.AppUserID == AppUserId)
                              .FirstOrDefault();

                if (NewsLockEntry.LockedByRecipientID == AppUserId) // locked to someone else
                {
                    // locked to current user, remove the lock
                    _NewsDBcontext.FeedItemQueueLocks.Remove(NewsLockEntry);
                }
                else
                {
                    if (AppUser == null)
                    {
                        Message = "Record locked by " + AppUserId;
                    }
                    else
                    {
                        Message = "Record locked by " + AppUser.AppUserName;
                    }
                    return(BadRequest(Message));
                }
            }

            // Update News if everything looks ok
            _NewsDBcontext.Entry(FeedItemQueueEntry).CurrentValues.SetValues(FeedItemQueueObject);

            try
            {
                _NewsDBcontext.SaveChanges();
            }
            catch (Exception e)
            {
                var logInfo = e.Message;
                { return(BadRequest(e.Message)); }
            }

            #region Update HR Data
            try
            {
                var Modules   = _context.ApplicationModules;
                var QueueGuid = (from mods in Modules
                                 where mods.ModuleName == "News Queue"
                                 select mods.QueueGuid).FirstOrDefault();

                var JsonData = JsonConvert.SerializeObject(FeedItemQueueObject);

                WorkItemPutRequest HRPutRequest = new WorkItemPutRequest();
                HRPutRequest.isActive             = true;
                HRPutRequest.name                 = "Updating Entries for News Entry " + FeedItemQueueObject.id;
                HRPutRequest.description          = "Updating Entries for News Entry " + FeedItemQueueObject.id;
                HRPutRequest.queueGuid            = QueueGuid;
                HRPutRequest.statusDetailTypeGuid = _configuration.GetSection("HumanReview:statusDetailTypeGuid_upd").Value;
                HRPutRequest.reviewTypeGuid       = _configuration.GetSection("HumanReview:reviewTypeGuid_upd").Value;
                HRPutRequest.formDefinitionJson   = JsonData;
                HRPutRequest.workitemGuid         = WorkItemGuid.ToString();

                var returnDataFromHR = Common.putWorkItemForEntityAsync(HRPutRequest, HRtoken, _configuration);
                if (returnDataFromHR.isSuccessful != true)
                {
                    // TODO: log this error
                    { return(BadRequest("Unable to save HR data")); }
                }
            }
            catch (Exception e)
            {
                // log error
                var logInfo = e.Message;
                { return(BadRequest(e.Message)); }
            }
            #endregion

            return(Ok());
        }
Example #5
0
        public IActionResult UpdateEntry([FromBody] InvestigationDataSave objupdobj)
        {
            var objupd = objupdobj.Investigation;

            // 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")); }
            }

            var targetObject = _context.Investigation.FirstOrDefault(t => t.InvestigationID == objupd.InvestigationID);

            if (targetObject == null)
            {
                return(NotFound());
            }


            // TODO check the lock table if its still valid
            // WorkUnitTypeID == 6 is BWQ, 3 is Investigations, 4 Alerts

            var thislock = _context.RecordLocks
                           .Where(v => v.WorkUnitTypeID == 3 &&
                                  v.IDFromWorkUnitsDBTable == objupd.InvestigationID);

            foreach (var locks in thislock)
            {
                if (locks.AppUserID != Convert.ToInt32(objupd.UpdatedBy)) // record lock found for another user
                {
                    var lockedToUser = _context.AppUser.FirstOrDefault(u => u.AppUserID == Convert.ToInt32(locks.AppUserID));

                    return(BadRequest("Investigation locked to: " + lockedToUser.AppUserName));
                }

                if (locks != null) // no locks
                {
                    _context.RecordLocks.Remove(locks);
                }
            }

            var InvestigationID = targetObject.InvestigationID;
            var MMMProfileID    = targetObject.EntityName;
            var workItemGuid    = targetObject.WorkItemID.ToString();

            _context.Entry(targetObject).CurrentValues.SetValues(objupd);

            // Save Notes
            if (objupdobj.InvestigationNote != null)
            {
                _context.InvestigationNote.Add(objupdobj.InvestigationNote);
            }

            ReturnData ret;

            ret = _context.SaveData();

            if (ret.Message != "Success")
            {
                return(Json(ret));
            }

            #region Human Review
            try
            {
                var Modules   = _context.ApplicationModules;
                var QueueGuid = (from mods in Modules
                                 where mods.ModuleName == "Investigations"
                                 select mods.QueueGuid).FirstOrDefault();

                var JsonData = JsonConvert.SerializeObject(objupd); // Serialize the update to Json

                WorkItemPutRequest HRPutRequest = new WorkItemPutRequest();
                HRPutRequest.isActive             = true;
                HRPutRequest.name                 = "Updating Entries for Investigation Entry " + InvestigationID + " and Profile " + MMMProfileID;
                HRPutRequest.description          = "Updating Entries for Investigation Entry " + InvestigationID + " and Profile " + MMMProfileID;
                HRPutRequest.queueGuid            = QueueGuid;
                HRPutRequest.statusDetailTypeGuid = _configuration.GetSection("HumanReview:statusDetailTypeGuid_upd").Value;
                HRPutRequest.reviewTypeGuid       = _configuration.GetSection("HumanReview:reviewTypeGuid_upd").Value;
                HRPutRequest.formDefinitionJson   = JsonData;
                HRPutRequest.workitemGuid         = workItemGuid;

                var returnDataFromHR = Common.putWorkItemForEntityAsync(HRPutRequest, objupdobj.HRToken, _configuration);
            }
            catch (Exception e)
            {
                // log error
                var logInfo = e.Message;
            }
            #endregion

            if (ret.Message == "Success")
            {
                return(Ok());
            }

            return(NotFound(ret));
        }
        public IActionResult UpdateEntry([FromBody] BWQInstructionsData objupd)
        {
            // Check if HR is up
            var hruri      = _configuration.GetSection("HumanReview:uri").Value + "auth/token";
            var hrResponse = Common.ServerStatusBy(hruri);

            // 400 - Not Found
            // 401 - Unauthorized (unlikely)
            // 404 - Bad Request

            // We expect a 400 - Bad Request, anything else specifically 404 Not Found, return an error
            if (hrResponse.StatusCode == 404)
            {
                { return(NotFound("Human Review Service Not Found")); }
            }

            int        BWQEntityID  = 0;
            int        MMMProfileID = 0;
            string     workItemGuid = "";
            ReturnData ret;

            // Update the Editorial Data
            #region Save Editorial data
            foreach (var updateobj in objupd.instructions)
            {
                var targetObject = _context.BWQInstructions.FirstOrDefault(t => t.BWQInstructionsID == updateobj.BWQInstructionsID);
                if (targetObject == null)
                {
                    return(NotFound("BWQ Instruction not found"));
                }

                var BWQEntity = _context.BWQEntities.FirstOrDefault(u => u.BWQEntitiesID == targetObject.BWQEntitiesID);
                if (BWQEntity == null)
                {
                    return(NotFound("No BWQ Entity found for this BWQ Instruction"));
                }

                // TODO check the lock table if its still valid
                // WorkUnitTypeID == 6 is BWQ
                var thislock = _context.RecordLocks
                               .Where(v => v.WorkUnitTypeID == 6 &&
                                      v.IDFromWorkUnitsDBTable == BWQEntity.BWQEntitiesID);
                //&& v.AppUserID == Convert.ToInt32(targetObject.UpdatedBy)

                foreach (var locks in thislock)
                {
                    if (locks.AppUserID != Convert.ToInt32(updateobj.UpdatedBy)) // record lock found for another user
                    {
                        var lockedToUser = _context.AppUser.FirstOrDefault(u => u.AppUserID == Convert.ToInt32(updateobj.UpdatedBy));

                        return(BadRequest("BWQ Instruction locked to: " + lockedToUser.AppUserName));
                    }

                    if (locks != null) // no locks
                    {
                        _context.RecordLocks.Remove(locks);
                    }
                }
                // Save Instruction
                _context.Entry(targetObject).CurrentValues.SetValues(updateobj);
                BWQEntityID  = BWQEntity.BWQEntitiesID;
                MMMProfileID = BWQEntity.MMMEntityID;
                workItemGuid = BWQEntity.WorkItemID.ToString();

                if (workItemGuid == "")
                {
                    return(NotFound("No WorkItem Found for instruction " + BWQEntityID));
                }
            }

            ret = _context.SaveData(); // Save Editorial data

            if (ret.Message != "Success")
            {
                return(Json(ret));
            }

            #endregion

            #region Update HR Data
            try
            {
                var Modules   = _context.ApplicationModules;
                var QueueGuid = (from mods in Modules
                                 where mods.ModuleName == "BWQ"
                                 select mods.QueueGuid).FirstOrDefault();

                var JsonData = JsonConvert.SerializeObject(objupd.instructions);

                WorkItemPutRequest HRPutRequest = new WorkItemPutRequest();
                HRPutRequest.isActive             = true;
                HRPutRequest.name                 = "Updating Entries for BWQ Entity " + BWQEntityID + " and Profile ID " + MMMProfileID;
                HRPutRequest.description          = "Updating Entries for BWQ Entity " + BWQEntityID + " and Profile " + MMMProfileID;
                HRPutRequest.queueGuid            = QueueGuid;
                HRPutRequest.statusDetailTypeGuid = _configuration.GetSection("HumanReview:statusDetailTypeGuid_upd").Value;
                HRPutRequest.reviewTypeGuid       = _configuration.GetSection("HumanReview:reviewTypeGuid_upd").Value;
                HRPutRequest.formDefinitionJson   = JsonData;
                HRPutRequest.workitemGuid         = workItemGuid;

                var returnDataFromHR = putWorkItemForEntityAsync(HRPutRequest, objupd.HRToken);
            }
            catch (Exception e)
            {
                var LogInfo = e.Message;
            }
            #endregion


            if (ret.Message == "Success")
            {
                return(Ok());
            }

            return(NotFound(ret));
        }
        public IActionResult UpdateEntry([FromBody] AlertJobsQueueEntityDataSave updatedata)
        {
            AlertJobsQueueEntity thisentity = updatedata.AlertJobsQueueEntity;

            if (updatedata.HRToken == "")
            {
                return(BadRequest("Invalid Token"));
            }

            var targetObject = _context.AlertJobsQueueEntity
                               .FirstOrDefault(t => t.AlertJobsQueueEntityID == thisentity.AlertJobsQueueEntityID);

            if (targetObject == null)
            {
                return(NotFound());
            }

            var thisUser = _context.AppUser
                           .FirstOrDefault(t => t.AppUserID.ToString() == thisentity.UpdatedBy);

            if (thisUser == null)
            {
                return(BadRequest("Invalid User"));
            }

            // 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")); }
            }

            // WorkUnitTypeID == 6 is BWQ, 3 is Investigations, 4 Alerts
            var thislock = _context.RecordLocks
                           .Where(v => v.WorkUnitTypeID == 4 &&
                                  v.IDFromWorkUnitsDBTable == thisentity.AlertJobsQueueEntityID);

            foreach (var locks in thislock)
            {
                if (locks.AppUserID != thisUser.AppUserID) // record lock found for another user
                {
                    var lockedToUser = _context.AppUser.FirstOrDefault(u => u.AppUserID == locks.AppUserID);

                    return(BadRequest("Investigation locked to: " + lockedToUser.AppUserName));
                }

                if (locks != null) // no locks
                {
                    _context.RecordLocks.Remove(locks);
                }
            }

            _context.Entry(targetObject).CurrentValues.SetValues(thisentity); // Save Editorial data

            ReturnData ret;

            ret = _context.SaveData();

            if (ret.Message != "Success")
            {
                return(Json(ret));
            }

            #region Human Review
            try
            {
                var workItemGuid = targetObject.WorkItemID.ToString();
                var Modules      = _context.ApplicationModules;
                var QueueGuid    = (from mods in Modules
                                    where mods.ModuleName == "Alerts"
                                    select mods.QueueGuid).FirstOrDefault();

                var JsonData = JsonConvert.SerializeObject(thisentity);

                WorkItemPutRequest HRPutRequest = new WorkItemPutRequest();
                HRPutRequest.isActive             = true;
                HRPutRequest.name                 = "Updating WorkItem details for Alert Entity " + thisentity.AlertJobsQueueEntityID;
                HRPutRequest.description          = "Updating WorkItem details for Alert Entity " + thisentity.AlertJobsQueueEntityID;
                HRPutRequest.queueGuid            = QueueGuid;
                HRPutRequest.statusDetailTypeGuid = _configuration.GetSection("HumanReview:statusDetailTypeGuid_upd").Value;
                HRPutRequest.reviewTypeGuid       = _configuration.GetSection("HumanReview:reviewTypeGuid_upd").Value;
                HRPutRequest.formDefinitionJson   = JsonData;
                HRPutRequest.workitemGuid         = workItemGuid;

                var returnDataFromHR = Common.putWorkItemForEntityAsync(HRPutRequest, updatedata.HRToken, _configuration);
            }
            catch (Exception e)
            {
                // log error
                var logInfo = e.Message;
            }
            #endregion

            if (ret.Message == "Success")
            {
                return(Ok());
            }

            return(NotFound(ret));
        }