/// <summary>
        /// GET: /Request/{id}
        ///
        /// Retrieves an SR that matches the ID provided in the URI
        /// </summary>
        /// <returns></returns>
        public Request Get(int id)
        {
            var userId = Authenticate().Id;

            var request = _serviceRequestController.GetServiceRequest(userId, id);

            //Need to determine what we want
            if (_userManager.UserHasPermission(userId, ApiAccess.FullApiAccess))
            {
                return(new Request(request));
            }
            else if (_userManager.UserHasPermission(userId, ApiAccess.OnlyUsersRequests))
            {
                if (request.RequestedByUserId == userId || request.ApproverUserId == userId)
                {
                    return(new Request(request));
                }
            }
            throw new PermissionException($"User is not able to access Service Request with ID {id}", userId, ApiAccess.OnlyUsersRequests);
        }
Exemple #2
0
        public ActionResult SaveFormSelection(ServiceRequestFormReturnModel form, int submit)
        {
            /* STEP ONE - Get the Service Package and SR */
            ServiceRequestModel model = new ServiceRequestModel             //used to hold all the data until redirecting
            {
                CurrentIndex     = submit,
                ServiceRequestId = form.Id,
                Mode             = ServiceRequestMode.Selection
            };

            try
            {
                model.ServiceRequest = _serviceRequestController.GetServiceRequest(UserId, form.Id); //SR
                model.SelectedAction = model.ServiceRequest.Action;
                if (model.SelectedAction == ServiceRequestAction.New)                                //Package
                {
                    model.NewPackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId,
                                                                       ServiceRequestAction.New); //package
                    model.SelectedAction = ServiceRequestAction.New;                              //new package
                    if (model.NewPackage == null)
                    {
                        model.NewPackage     = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId);
                        model.SelectedAction = ServiceRequestAction.New;
                    }
                }
                else if (model.SelectedAction == ServiceRequestAction.Change)
                {
                    model.ChangePackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId, ServiceRequestAction.Change);
                }
                else if (model.ServiceRequest.Action == ServiceRequestAction.Remove)
                {
                    model.RemovePackage  = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId, ServiceRequestAction.Remove);
                    model.SelectedAction = ServiceRequestAction.Remove;                     //remove package
                    if (model.NewPackage == null && model.RemovePackage == null)
                    {
                        model.NewPackage = ServicePackageHelper.GetPackage(UserId, _portfolioService, model.ServiceRequest.ServiceOptionId);
                    }
                }
            }
            catch (Exception exception)
            {
                TempData["MessageType"] = WebMessageType.Failure;
                TempData["Message"]     = $"Failed to retrieve service request information, error: {exception.Message}";
                model.CurrentIndex      = -1;            //either the SR does not exist or the option does not exist
                return(View("ServiceRequest", model));
            }

            /* STEP TWO - figure out what category or service to work with */
            //tag identified
            IServicePackageTag currentTag = null;

            if (model.ServiceRequest.Action == ServiceRequestAction.New)
            {
                if (model.NewPackage != null)
                {
                    currentTag = model.GetPackageTags(ServiceRequestAction.New).ToArray()[form.CurrentIndex];
                }
            }
            else if (model.ServiceRequest.Action == ServiceRequestAction.Change)
            {
                if (model.ChangePackage != null)
                {
                    currentTag = model.GetPackageTags(ServiceRequestAction.Change).ToArray()[form.CurrentIndex];
                }
            }
            else if (model.ServiceRequest.Action == ServiceRequestAction.Remove)
            {
                if (model.RemovePackage != null)
                {
                    currentTag = model.GetPackageTags(ServiceRequestAction.Remove).ToArray()[form.CurrentIndex];
                }
            }

            /* STEP THREE - add/remove options in the SR */
            {
                ICollection <IServiceRequestOptionDto> formOptions;
                if (form.Options == null)
                {
                    formOptions = new List <IServiceRequestOptionDto>();
                }                 //need to avoid a null pointer exception here
                else
                {
                    formOptions = form.GetServiceRequestOptions().ToList();
                    //get metadata for form options
                    foreach (var option in formOptions)
                    {
                        option.ServiceOptionName = _portfolioService.GetServiceOption(UserId, option.ServiceOptionId).Name;
                        option.Code =
                            _portfolioService.GetServiceOptionCategory(UserId,
                                                                       _portfolioService.GetServiceOption(UserId, option.ServiceOptionId).ServiceOptionCategoryId).Code;
                    }
                }
                if (model.ServiceRequest.ServiceRequestOptions == null)
                {
                    model.ServiceRequest.ServiceRequestOptions = new List <IServiceRequestOptionDto>();
                }


                List <IServiceOptionCategoryDto> currentCategories = new List <IServiceOptionCategoryDto>();

                if (currentTag is IServiceOptionCategoryTagDto)                 //deal with a category
                {
                    currentCategories.Add(_portfolioService.GetServiceOptionCategory(UserId, ((IServiceOptionCategoryTagDto)model.GetPackageTags(model.SelectedAction).ToArray()[form.CurrentIndex]).ServiceOptionCategoryId));
                }
                else if (currentTag is IServiceTagDto)
                {
                    currentCategories.AddRange(_portfolioService.GetService(((IServiceTagDto)model.GetPackageTags(model.SelectedAction).ToArray()[form.CurrentIndex]).ServiceId).ServiceOptionCategories);
                }

                try
                {
                    foreach (var category in currentCategories)
                    {
                        foreach (var option in category.ServiceOptions)                         /* sighhhhh */
                        {
                            var formDto = (from o in formOptions where o.ServiceOptionId == option.Id select o).FirstOrDefault();
                            var srDto   =
                                (from o in model.ServiceRequest.ServiceRequestOptions where o.ServiceOptionId == option.Id select o)
                                .FirstOrDefault();

                            if (formDto != null && srDto == null)                             //add condition
                            {
                                _serviceRequestOptionController.ModifyServiceRequestOption(UserId,
                                                                                           (from o in formOptions where o.ServiceOptionId == option.Id select o).First(), EntityModification.Create);
                            }
                            else if (formDto == null && srDto != null)                             //remove condition
                            {
                                _serviceRequestOptionController.ModifyServiceRequestOption(UserId,
                                                                                           (from o in model.ServiceRequest.ServiceRequestOptions where o.ServiceOptionId == option.Id select o).First(),
                                                                                           EntityModification.Delete);
                            }
                            else if (formDto != null /* && srDto != null */)                             //update condition
                            {
                                _serviceRequestOptionController.ModifyServiceRequestOption(UserId, srDto, EntityModification.Delete);
                                _serviceRequestOptionController.ModifyServiceRequestOption(UserId, formDto, EntityModification.Create);
                            }                             /* done \*/
                        }
                    }
                    model.ServiceRequest = _serviceRequestController.GetServiceRequest(UserId, form.Id);
                    // refresh the data in the model now
                }
                catch (Exception exception)                 //what, a problem?
                {
                    TempData["MessageType"] = WebMessageType.Failure;
                    TempData["Message"]     = $"Failed to retrieve service request information, error: {exception.Message}";
                    model.CurrentIndex      = -1;                //either the SR does not exist or the option does not exist
                    return(View("ServiceRequest", model));
                }
            }
            /* STEP FOUR - add/remove user input data */
            //update and add
            if (form.UserInput != null)
            {
                List <ServiceRequestUserInputDto> userDataList = (from u in form.UserInput
                                                                  where u.Value != null
                                                                  select new ServiceRequestUserInputDto
                {
                    Id = u.Id,
                    Name = u.Name,
                    UserInputType = u.UserInputType,
                    ServiceRequestId = form.Id,
                    InputId = u.InputId,
                    Value = u.Value
                }).ToList();
                foreach (var userData in userDataList)
                {
                    try
                    {
                        _serviceRequestUserInputController.ModifyServiceRequestUserInput(UserId, userData, userData.Id > 0 ? EntityModification.Update : EntityModification.Create);
                    }
                    catch (Exception exception)
                    {
                        TempData["MessageType"] = WebMessageType.Failure;
                        TempData["Message"]     = $"Failed to save user input data, error: {exception.Message}";
                        return(View("ServiceRequest", model));
                    }
                }
            }
            ICollection <UserInputTag> userInputs;

            userInputs = form.UserInput == null ? new List <UserInputTag>() : (from u in form.UserInput select new UserInputTag {
                UserInput = u, Required = false
            }).ToList();

            var options = from o in model.ServiceRequest.ServiceRequestOptions select new ServiceOptionDto {
                Id = o.ServiceOptionId
            };
            var requiredInputs = _portfolioService.GetInputsForServiceOptions(UserId, options);

            //clean up - figure out what can be removed
            foreach (var userData in userInputs)
            {
                switch (userData.UserInput.UserInputType)
                {
                case UserInputType.ScriptedSelection:
                    foreach (var input in (from s in requiredInputs.UserInputs where s is IScriptedSelectionInputDto select s))
                    {
                        if (userData.UserInput.UserInputType == UserInputType.ScriptedSelection & userData.UserInput.InputId == input.Id)
                        {
                            userData.Required = true;
                        }
                    }
                    break;

                case UserInputType.Selection:
                    foreach (var input in (from s in requiredInputs.UserInputs where s is ISelectionInputDto select s))
                    {
                        if (userData.UserInput.UserInputType == UserInputType.Selection & userData.UserInput.InputId == input.Id)
                        {
                            userData.Required = true;
                        }
                    }
                    break;

                default:
                    foreach (var input in (from s in requiredInputs.UserInputs where s is ITextInputDto select s))
                    {
                        if (userData.UserInput.UserInputType == UserInputType.Text & userData.UserInput.InputId == input.Id)
                        {
                            userData.Required = true;
                        }
                    }
                    break;
                }
            }

            //do the removals
            foreach (var userData in userInputs)
            {
                if (!userData.Required && userData.UserInput.Id > 0)                 //avoid the new
                {
                    _serviceRequestUserInputController.ModifyServiceRequestUserInput(UserId, new ServiceRequestUserInputDto {
                        Id = userData.UserInput.Id
                    }, EntityModification.Delete);
                }
            }
            //you've made it this far, all saving is complete (if saves were done)
            TempData["MessageType"] = WebMessageType.Success;
            TempData["Message"]     = "Service Request saved successfully";
            /* STEP FIVE - navigation */

            model.CurrentIndex = submit;
            if (submit >= 99999)                //submission
            {
                return(RedirectToAction("ShowServiceRequest", "ServiceRequestApproval", new { id = form.Id }));
            }
            else if (submit >= 88888)
            {
                return(RedirectToAction("Index", "ServiceRequestApproval"));
            }

            model.Mode = ServiceRequestMode.Selection;
            return(RedirectToAction("Form", new { id = model.ServiceRequestId, index = model.CurrentIndex, mode = model.Mode }));
        }
        /// <summary>
        /// Data to show a state change of SR
        /// </summary>
        /// <param name="ps"></param>
        /// <param name="userId"></param>
        /// <param name="sr"></param>
        /// <param name="serviceRequestId"></param>
        /// <returns></returns>
        public static ServiceRequestStateChangeModel CreateStateChangeModel(IPortfolioService ps, int userId, IServiceRequestController sr,
                                                                            int serviceRequestId)
        {
            var model = new ServiceRequestStateChangeModel
            {
                ServiceRequestModel = new ServiceRequestModel(),
                ConfirmNextState    = false
            };

            model.ServiceRequestModel.ServiceRequest = sr.GetServiceRequest(userId, serviceRequestId);

            List <DisplayListOption> displayList = new List <DisplayListOption>();

            foreach (var serviceRequestOption in model.ServiceRequestModel.ServiceRequest.ServiceRequestOptions)                //add the option name
            {
                var listOption = new DisplayListOption
                {
                    ServiceRequestOption = serviceRequestOption,
                    UserInputs           = new List <DisplayListUserInput>(),
                    ServiceOption        = ps.GetServiceOption(userId, serviceRequestOption.ServiceOptionId)
                };

                var userInputs = ps.GetInputsForServiceOptions(userId,
                                                               new IServiceOptionDto[1] {
                    new ServiceOptionDto {
                        Id = serviceRequestOption.ServiceOptionId
                    }
                });                                                                                                                      //get user inputs
                if (userInputs != null)
                {
                    foreach (var userData in model.ServiceRequestModel.ServiceRequest.ServiceRequestUserInputs)
                    {
                        if (userData.UserInputType == UserInputType.Text)
                        {
                            foreach (var userInput in userInputs.TextInputs)
                            {
                                if (userInput.Id == userData.InputId)
                                {
                                    var displayUserInput = new DisplayListUserInput {
                                        DisplayName = userInput.DisplayName
                                    };
                                    displayUserInput.ServiceRequestUserInput = userData;
                                    listOption.UserInputs.Add(displayUserInput);
                                }
                            }
                        }
                        else if (userData.UserInputType == UserInputType.Selection)
                        {
                            foreach (var userInput in userInputs.SelectionInputs)
                            {
                                if (userInput.Id == userData.InputId)
                                {
                                    var displayUserInput = new DisplayListUserInput {
                                        DisplayName = userInput.DisplayName
                                    };
                                    displayUserInput.ServiceRequestUserInput = userData;
                                    listOption.UserInputs.Add(displayUserInput);
                                }
                            }
                        }
                        else if (userData.UserInputType == UserInputType.ScriptedSelection)
                        {
                            foreach (var userInput in userInputs.ScriptedSelectionInputs)
                            {
                                if (userInput.Id == userData.InputId)
                                {
                                    var displayUserInput = new DisplayListUserInput {
                                        DisplayName = userInput.DisplayName
                                    };
                                    displayUserInput.ServiceRequestUserInput = userData;
                                    listOption.UserInputs.Add(displayUserInput);
                                }
                            }
                        }
                    }
                    displayList.Add(listOption);
                }
            }
            model.DisplayList = displayList;
            return(model);
        }