public IHttpActionResult PutCounterMeasure(CounterMeasureEditRequest counterMeasureRequest)
        {
            CheckModelState();

            //if user is kpi owner not an admin, check if he is the kpi owner of the given
            //scorecard else return unauthorized
            if (IsUserKPIOwnerOrTeamMemberNotAdmin())
            {
                bool isUserKPIOwnerOfScorecard = userManager.
                                                 IsUserKPIOwnerOfScorecard(Username, counterMeasureRequest.ScorecardId.Value);
                bool isUserTeamMemberOfScorecard = userManager.
                                                   IsUserTeamMemberOfScorecard(Username, counterMeasureRequest.ScorecardId.Value);
                if (!isUserKPIOwnerOfScorecard && !isUserTeamMemberOfScorecard)
                {
                    return(Unauthorized());
                }
            }

            counterMeasureManager.EditCounterMeasure(counterMeasureRequest, Username);
            return(Ok());
        }
示例#2
0
        /// <summary>
        /// Method to update existing counter measure
        /// </summary>
        /// <param name="counterMeasureRequest">counter measure to be updated</param>
        /// <param name="userName">logged in user name</param>
        public void EditCounterMeasure(CounterMeasureEditRequest counterMeasureRequest,
                                       string userName)
        {
            //get counter measure entity based on counter measure id
            var existingCounterMeasure = counterMeasureRepository.Get
                                             (counterMeasureRequest.CounterMeasureId.Value);

            if (existingCounterMeasure != null)
            {
                if (existingCounterMeasure.CounterMeasureStatusId ==
                    Constants.CounterMeasureStatusConfirmed)
                {
                    throw new NDMSBusinessException
                              (Constants.ConfirmedCounterMeasureEditErrorMessage);
                }

                if (existingCounterMeasure.DueDate != counterMeasureRequest.DueDate)
                {
                    ValidateCounterMeasureDueDate(counterMeasureRequest.DueDate);
                }

                // Get logged in user id
                int loggedInUserId = userRepository.GetAll().FirstOrDefault(
                    x => x.AccountName == userName).Id;

                // If the assigned user is not added to NDMS users table, add it here
                var assignedUserIds = userRepository.AddADUsersToNDMS(new List <string>()
                {
                    counterMeasureRequest.AssignedTo
                });

                //update changes to existing counter measure entity
                existingCounterMeasure.CounterMeasurePriorityId = counterMeasureRequest.CounterMeasurePriorityId != null ?
                                                                  counterMeasureRequest.CounterMeasurePriorityId.Value :
                                                                  (int?)null;
                existingCounterMeasure.Action                 = counterMeasureRequest.Action;
                existingCounterMeasure.AssignedTo             = assignedUserIds.First();
                existingCounterMeasure.DueDate                = counterMeasureRequest.DueDate;
                existingCounterMeasure.LastModifiedBy         = loggedInUserId;
                existingCounterMeasure.LastModifiedOn         = TimeZoneUtility.GetCurrentTimestamp();
                existingCounterMeasure.CounterMeasureStatusId = counterMeasureRequest
                                                                .CounterMeasureStatusId.Value;
                if (!string.IsNullOrEmpty(counterMeasureRequest.Comment))
                {
                    // Add new comment if there are no comments
                    if (existingCounterMeasure.CounterMeasureComments == null)
                    {
                        existingCounterMeasure.CounterMeasureComments =
                            new List <CounterMeasureComment>()
                        {
                            CounterMeasureConverters.ConvertCommentToCounterMeasureComment
                                (counterMeasureRequest.Comment, loggedInUserId)
                        };
                    }
                    else
                    {
                        //else add new comment.
                        existingCounterMeasure.CounterMeasureComments.Add(
                            CounterMeasureConverters.ConvertCommentToCounterMeasureComment
                                (counterMeasureRequest.Comment, loggedInUserId)
                            );
                    }
                }
                counterMeasureRepository.Save();
            }
        }