public CompleteRiskAssessmentReviewRequestBuilder WithCompleteReview_completeReviewViewModel(CompleteReviewViewModel completeReviewViewModel)
 {
     _completeReviewViewModel = completeReviewViewModel;
     return this;
 }
        public JsonResult Complete(CompleteReviewViewModel viewModel)
        {

            if (!viewModel.Archive && !viewModel.NextReviewDate.HasValue)
                ModelState.AddModelError("", "Either a valid Next Review Date must be entered or Archive must be checked.");

            if (!ModelState.IsValid)
                return Json(new { Success = false, Errors = ModelState.GetErrorMessages() });

            var riskAssessmentReview = _riskAssessmentReviewService.GetByIdAndCompanyId(
                                                                viewModel.RiskAssessmentReviewId,
                                                                CurrentUser.CompanyId
                                                                );

            var riskAssessment = riskAssessmentReview.RiskAssessment;


            var documentResult = CreateReviewAuditDocument(viewModel.RiskAssessmentType, riskAssessment);

            var completeRiskAssessmentReviewRequest = CreateCompleteRiskAssessmentReviewRequest(
                                                                                viewModel,
                                                                                riskAssessment,
                                                                                documentResult.NewFileName,
                                                                                documentResult.DocumentLibraryId,
                                                                                documentResult.DocumentType);


            if (viewModel.RiskAssessmentType == RiskAssessmentType.FRA)
            {
                _fireRiskAssessmentService.CompleteFireRiskAssessementReview(completeRiskAssessmentReviewRequest);
            }
            else
            {
                _riskAssessmentReviewService.CompleteRiskAssessementReview(completeRiskAssessmentReviewRequest);
            }

            if (riskAssessmentReview.RiskAssessment.RiskAssessor != null
                && riskAssessmentReview.RiskAssessment.RiskAssessor.Employee.MainContactDetails != null 
                && !string.IsNullOrEmpty(riskAssessmentReview.RiskAssessment.RiskAssessor.Employee.MainContactDetails.Email))
            {
                var task = riskAssessmentReview.RiskAssessmentReviewTask;
                _bus.Send(new SendReviewCompletedEmail
                              {
                                  TaskReference = task.RiskAssessment.Reference,
                                  Title = task.RiskAssessment.Title,
                                  Description = task.Description,
                                  RiskAssessorName = riskAssessmentReview.RiskAssessment.RiskAssessor.Employee.FullName,
                                  RiskAssessorEmail =
                                      riskAssessmentReview.RiskAssessment.RiskAssessor.Employee.MainContactDetails !=
                                      null
                                          ? riskAssessmentReview.RiskAssessment.RiskAssessor.Employee.MainContactDetails
                                                .Email
                                          : null,
                                  TaskAssignedTo = task.TaskAssignedTo != null ? task.TaskAssignedTo.FullName : string.Empty
                              });
            }

            return Json(new { Success = true });
        }
        private CompleteRiskAssessmentReviewRequest CreateCompleteRiskAssessmentReviewRequest(CompleteReviewViewModel viewModel,
            RiskAssessmentDto riskAssessment, string newFileName, long documentLibraryId, DocumentTypeEnum documentTypeEnum)
        {
            var completeRiskAssessmentReviewRequest = new CompleteRiskAssessmentReviewRequestBuilder()
                .WithClientId(CurrentUser.CompanyId)
                .WithUserId(CurrentUser.UserId)
                .WithCompleteReview_completeReviewViewModel(viewModel);

            if (documentLibraryId != default(long))
            {
                var requestBuilder = new CreateDocumentRequestBuilder()
                    .WithCompanyId(CurrentUser.CompanyId)
                    .WithDocumentOriginTypeId(DocumentOriginType.TaskCompleted)
                    .WithDocumentType(documentTypeEnum)
                    .WithFilename(newFileName)
                    .WithDocumentLibraryId(documentLibraryId);

                if (riskAssessment.RiskAssessmentSite != null)
                    requestBuilder.WithSiteId(riskAssessment.RiskAssessmentSite.Id);

                var createDocumentRequest = requestBuilder.Build();

                completeRiskAssessmentReviewRequest.WithCreateDocumentRequest(createDocumentRequest);
            }

            return completeRiskAssessmentReviewRequest.Build();
        }
        public void Given_that_complete_is_called_When_required_fields_are_supplied_Then_json_success_returned()
        {
            //Given
            var target = GetTarget();
            var validModel = new CompleteReviewViewModel()
            {
                CompanyId = 123,
                RiskAssessmentReviewId = 789,
                IsComplete = true,
                CompletedComments = "complete",
                NextReviewDate = DateTime.Now.AddYears(1),
                RiskAssessmentType = RiskAssessmentType.GRA
            };
            
            //When
            var result = target.Complete(validModel);
            dynamic data = result.Data;

            //Then
            Assert.That(result, Is.InstanceOf<JsonResult>());
            Assert.That(data.Success, Is.EqualTo(true));
        }
        public void Given_a_valid_CompleteReviewViewModel_When_completing_a_review_Then_the_correct_command_is_created_and_sent()
        {
            //Given
            _raReviewDto.RiskAssessment.RiskAssessor = new RiskAssessorDto() { Id = 243L, Employee = new EmployeeDto { FullName = "ReviewAssignedTo full name", MainContactDetails = new EmployeeContactDetailDto { Email = "*****@*****.**" } } };
            _raReviewDto.ReviewAssignedTo = new EmployeeDto() { MainContactDetails = new EmployeeContactDetailDto { Email = "*****@*****.**" }, FullName = "ReviewAssignedTo full name" };
            _raReviewDto.RiskAssessmentReviewTask = new RiskAssessmentReviewTaskDto()
                                                        {
                                                            TaskAssignedTo = new EmployeeDto {MainContactDetails = new EmployeeContactDetailDto{ Email = "*****@*****.**"}, FullName = "TaskAssignedToDto name"}
                                                            ,
                                                            RiskAssessment = _raReviewDto.RiskAssessment
                                                        };


            var validModel = new CompleteReviewViewModel()
                                 {
                                     CompanyId = 123,
                                     RiskAssessmentReviewId = 789,
                                     IsComplete = true,
                                     CompletedComments = "complete",
                                     NextReviewDate = DateTime.Now.AddYears(1),
                                     RiskAssessmentType = RiskAssessmentType.GRA,
                                 };



            var param = new object[0];
            SendReviewCompletedEmail emailCommand = null;
            _bus.Setup(x => x.Send(It.IsAny<SendReviewCompletedEmail>()))
                .Callback<object[]>(x => emailCommand = (SendReviewCompletedEmail) x[0]);

            var target = GetTarget();

            //When
            var result = target.Complete(validModel);

            //Then
            _bus.Verify(x => x.Send(It.IsAny<SendReviewCompletedEmail>()), Times.Once());

            Assert.AreEqual(_raReviewDto.RiskAssessment.Title, emailCommand.Title);
            Assert.AreEqual(_raReviewDto.RiskAssessment.Reference, emailCommand.TaskReference);
            Assert.AreEqual(_raReviewDto.RiskAssessmentReviewTask.Description, emailCommand.Description);
            Assert.AreEqual(_raReviewDto.RiskAssessment.RiskAssessor.Employee.FullName, emailCommand.RiskAssessorName);
            Assert.AreEqual(_raReviewDto.RiskAssessmentReviewTask.TaskAssignedTo.FullName, emailCommand.TaskAssignedTo);

        }
        public void When_complete_is_called_Then_check_sql_feature_is_available_then_call_GetSqlReport_documentLibraryUploader_Upload(RiskAssessmentType raType, string configKeyName, bool featureAvailable, SqlReportHelper.ReportType reportType)
        {
            // Given
            var model = new CompleteReviewViewModel()
            {
                CompanyId = TestControllerHelpers.CompanyIdAssigned,
                RiskAssessmentId = 123,
                RiskAssessmentReviewId = 456,
                IsComplete = true,
                CompletedComments = "complete",
                NextReviewDate = DateTime.Today,
                RiskAssessmentType = raType
            };

            var target = GetTarget();
            target.IsSqlReportAvailable = raType == RiskAssessmentType.GRA;

            // When
            target.Complete(model);

            //Then
            var timesToCall = featureAvailable ? Times.Once() : Times.Never();
            
            _reviewAuditService.Verify(x => x.CreateReviewAuditDocument(raType, It.IsAny<RiskAssessmentDto>()), timesToCall);
        }
        public void Given_map_file_from_sql_report_service_Then_CreateCompleteRiskAssessmentReviewRequest_Then_values_are_valid(long riskAssessmentId, long reviewId)
        {
            // Given
            var completeReviewViewModel = new CompleteReviewViewModel()
                                              {
                                                  CompanyId = TestControllerHelpers.CompanyIdAssigned, 
                                                  RiskAssessmentId = riskAssessmentId, 
                                                  RiskAssessmentReviewId = reviewId, 
                                                  IsComplete = true, 
                                                  CompletedComments = "complete", 
                                                  NextReviewDate = DateTime.Today, 
                                                  RiskAssessmentType = RiskAssessmentType.GRA,
                                              };

            var target = GetTarget();

            //When
            target.Complete(completeReviewViewModel);

            
            //Then
            _riskAssessmentReviewService
                .Verify(x => x.CompleteRiskAssessementReview(It.Is<CompleteRiskAssessmentReviewRequest>(y => y.RiskAssessmentId == completeReviewViewModel.RiskAssessmentId && 
                                                                                                             y.ClientId == completeReviewViewModel.CompanyId && 
                                                                                                             y.CompletedComments == completeReviewViewModel.CompletedComments && 
                                                                                                             y.IsComplete == completeReviewViewModel.IsComplete &&
                                                                                                             y.RiskAssessmentReviewId == completeReviewViewModel.RiskAssessmentReviewId &&
                                                                                                             y.NextReviewDate == completeReviewViewModel.NextReviewDate)));
        }
        public void Given_that_complete_is_called_When_required_fields_are_supplied_Then_current_user_is_added_to_request_model()
        {
            //Given
            var target = GetTarget();
            var validModel = new CompleteReviewViewModel()
            {
                CompanyId = 123,
                RiskAssessmentId = 456,
                RiskAssessmentReviewId = 789,
                IsComplete = true,
                CompletedComments = "complete",
                NextReviewDate = DateTime.Today,
                RiskAssessmentType = RiskAssessmentType.GRA
            };
            var modelPassedToService = new CompleteRiskAssessmentReviewRequest();
            _riskAssessmentReviewService
                .Setup(x => x.CompleteRiskAssessementReview(It.IsAny<CompleteRiskAssessmentReviewRequest>()))
                .Callback<CompleteRiskAssessmentReviewRequest>(y => modelPassedToService = y);



            //When
            target.Complete(validModel);

            //Then
            _riskAssessmentReviewService.VerifyAll();
            Assert.That(modelPassedToService.ReviewingUserId.ToString(), Is.Not.EqualTo("00000000-0000-0000-0000-000000000000"));
        }