Beispiel #1
0
        public void Setup()
        {
            _riskAssessmentId = 100;
            _viewModelFactory = new Mock<IEditPersonalRiskAssessmentSummaryViewModelFactory>();
           
            var viewModel = new EditSummaryViewModel();
            _viewModelFactory
                .Setup(x => x.GetViewModel())
                .Returns(viewModel);

            _riskAssessmentService = new Mock<IPersonalRiskAssessmentService>();
        
            _target = GetTarget();
            _currentUserId = _target.CurrentUser.UserId;

            _viewModelFactory
               .Setup(x => x.WithRiskAssessmentId(It.IsAny<long>()))
               .Returns(_viewModelFactory.Object);
            _viewModelFactory
                .Setup(x => x.WithCompanyId(It.IsAny<long>()))
                .Returns(_viewModelFactory.Object);
            _viewModelFactory
               .Setup(x => x.WithAllowableSiteIds(It.IsAny<IList<long>>()))
               .Returns(_viewModelFactory.Object);
            _viewModelFactory
                .Setup(x => x.WithCurrentUserId(_currentUserId))
                .Returns(_viewModelFactory.Object);
        }
        public ActionResult Save(EditSummaryViewModel viewModelParam)
        {
            Validate(viewModelParam);

            if (ModelState.IsValid)
            {
                try
                {
                    UpdateRiskAssessmentSummary(viewModelParam);
                    TempData["Notice"] = "Risk Assessment Summary Successfully Updated";
                    return RedirectToAction("Index", new { riskAssessmentId = viewModelParam.RiskAssessmentId, companyId = CurrentUser.CompanyId });
                }
                catch (ValidationException validationException)
                {
                    ModelState.AddValidationErrors(validationException);
                }
            }

            var viewModelReturned = _viewModelFactory
                .WithRiskAssessmentId(viewModelParam.RiskAssessmentId)
                .WithCompanyId(viewModelParam.CompanyId)
                .WithCurrentUserId(CurrentUser.UserId)
                .WithAllowableSiteIds(CurrentUser.GetSitesFilter())
                .GetViewModel();
            return View("Index", viewModelReturned);
        }
        public JsonResult SaveAndNext(EditSummaryViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UpdateRiskAssessmentSummary(model);

                    return Json(new { Success = true });
                }
                else
                {
                    return ModelStateErrorsAsJson();
                }
                
            }
            catch (ValidationException validationException)
            {
                ModelState.AddValidationErrors(validationException);
                return ModelStateErrorsAsJson();
            }
        }
        public EditSummaryViewModel GetViewModel()
        {
            var riskAssessment = _riskAssessmentService.GetWithReviews(_riskAssessmentId, _companyId, _currentUserId);

            var model = new EditSummaryViewModel()
                   {
                       RiskAssessmentId = _riskAssessmentId,
                       CompanyId = _companyId,
                       Reference = riskAssessment.Reference,
                       Title = riskAssessment.Title,
                       DateOfAssessment = riskAssessment.AssessmentDate ?? DateTime.Today,
                       RiskAssessorId = riskAssessment.RiskAssessor != null ? riskAssessment.RiskAssessor.Id : (long?) null,
                       RiskAssessor = riskAssessment.RiskAssessor != null ? riskAssessment.RiskAssessor.FormattedName : null,
                       SiteId = riskAssessment.RiskAssessmentSite != null ? riskAssessment.RiskAssessmentSite.Id : 0,
                       Site = riskAssessment.RiskAssessmentSite != null ? riskAssessment.RiskAssessmentSite.Name: string.Empty,
                       Sensitive = riskAssessment.Sensitive,
                       IsReadOnly = !IsLoggedInUserCreator(riskAssessment) && riskAssessment.Sensitive,
                       IsSensitiveReadonly = !IsLoggedInUserCreator(riskAssessment) && !IsLoggedInUserAssessor(riskAssessment) && !IsLoggedInUserReviewer(riskAssessment)
                   };
             model = AttachDropdownData(model);
            return model;
        }
 private void UpdateRiskAssessmentSummary(EditSummaryViewModel model)
 {
     _personalRiskAssessmentService.UpdateRiskAssessmentSummary(new UpdatePersonalRiskAssessmentSummaryRequest()
     {
         Id = model.RiskAssessmentId,
         CompanyId = model.CompanyId,
         Title = model.Title,
         Reference = model.Reference,
         UserId = CurrentUser.UserId,
         AssessmentDate = model.DateOfAssessment,
         RiskAssessorId = model.RiskAssessorId,
         Sensitive = model.Sensitive,
         SiteId = model.SiteId
     });
 }
 private void Validate(EditSummaryViewModel viewModel)
 {
     if (viewModel.RiskAssessorId == null)
     {
         ModelState.AddModelError("RiskAssessorId", "Please select a Risk Assessor");
     }
     if (viewModel.SiteId == null)
     {
         ModelState.AddModelError("SiteId", "Please select a Site");
     }
 }
        private EditSummaryViewModel AttachDropdownData(EditSummaryViewModel model)
        {
            var riskAssessmentAssessors = _employeeService.Search(new SearchEmployeesRequest()
            {
                CompanyId = _companyId,
                MaximumResults = 100
            });
            var assessors = riskAssessmentAssessors.OrderBy(x => x.FullName).Select(AutoCompleteViewModel.ForEmployeeNoJobTitle).AddDefaultOption();
            model.RiskAssessmentAssessors = assessors;

            var siteStructureElements = _siteService.Search(new SearchSitesRequest
            {
                CompanyId = _companyId,
                AllowedSiteIds = _allowedSiteIds
            });
            var sites = siteStructureElements.Select(x => new AutoCompleteViewModel(x.Name, x.Id.ToString())).AddDefaultOption();
            model.Sites = sites;

            return model;
        }
 public EditSummaryViewModel GetViewModel(EditSummaryViewModel model)
 {
     model = AttachDropdownData(model);
     return model;
 }