public void Given_valid_model_state_When_NewFurtherControlMeasureTask_Request_Then_should_call_correct_methods()
        {
            // Given
            var controller = GetTarget();
            var viewModel = new AddEditFireRiskAssessmentFurtherControlMeasureTaskViewModel()
                                {
                                    CompanyId = 123L,
                                    RiskAssessmentId = 789L,
                                    Title = "TItle",
                                    Description = "A description",
                                    Reference = "My reference",
                                    SignificantFindingId = 888L,
                                    TaskStatusId = 3
                                };
            var documentsToSave = new DocumentsToSaveViewModel();

            var result = new FireRiskAssessmentFurtherControlMeasureTaskDto();
            _furtherControlMeasureTaskService
                .Setup(x => x.AddFurtherControlMeasureTask(It.Is<SaveFurtherControlMeasureTaskRequest>(
                    y => y.CompanyId == viewModel.CompanyId &&
                         y.RiskAssessmentId == viewModel.RiskAssessmentId &&
                         y.Title == viewModel.Title &&
                         y.Description == viewModel.Description &&
                         y.Reference == viewModel.Reference &&
                         y.SignificantFindingId == viewModel.SignificantFindingId &&
                         y.TaskStatus == (TaskStatus)viewModel.TaskStatusId
                    )))
                .Returns(result);

            // When
            controller.NewFurtherControlMeasureTask(viewModel, documentsToSave);

            // Then
            _furtherControlMeasureTaskService.VerifyAll();
        }
        public AddEditFireRiskAssessmentFurtherControlMeasureTaskViewModel GetViewModel()
        {
            var furtherActionTaskDto = _furtherControlMeasureTaskService.GetByIdAndCompanyId(_furtherControlMeasureTaskId, _companyId);

            var viewModel = new AddEditFireRiskAssessmentFurtherControlMeasureTaskViewModel()
            {
                FurtherControlMeasureTaskId = furtherActionTaskDto.Id,
                RiskAssessmentTitle = furtherActionTaskDto.RiskAssessment.Title,
                Title = furtherActionTaskDto.Title,
                Reference = furtherActionTaskDto.Reference,
                Description = furtherActionTaskDto.Description,
                TaskStatusId = furtherActionTaskDto.TaskStatusId,
                TaskStatus = furtherActionTaskDto.TaskStatusString,
                CompanyId = _companyId,
                TaskAssignedToId = furtherActionTaskDto.TaskAssignedTo.Id,
                TaskAssignedTo = furtherActionTaskDto.TaskAssignedTo.FullName,
                TaskCompletionDueDate = furtherActionTaskDto.TaskCompletionDueDate,
                CompletedComments = furtherActionTaskDto.TaskCompletedComments,
                TaskReoccurringTypeId = (int)furtherActionTaskDto.TaskReoccurringType,
                TaskReoccurringType = furtherActionTaskDto.TaskReoccurringType,
                TaskReoccurringEndDate = furtherActionTaskDto.TaskReoccurringEndDate,
                TaskReoccurringTypes = new TaskReoccurringType().ToSelectList(),
                DoNotSendTaskNotification = !furtherActionTaskDto.SendTaskNotification,
                DoNotSendTaskCompletedNotification = !furtherActionTaskDto.SendTaskCompletedNotification,
                DoNotSendTaskOverdueNotification = !furtherActionTaskDto.SendTaskOverdueNotification,
                
                ExistingDocuments = _existingDocumentsViewModelFactory
                    .WithCanDeleteDocuments(_canDeleteDocuments)
                    .WithDefaultDocumentType(furtherActionTaskDto.DefaultDocumentType)
                    .GetViewModel(furtherActionTaskDto.Documents),
                IsRecurring = furtherActionTaskDto.IsReoccurring
            };

            return viewModel;
        }
        public void Given_invalid_model_state_When_NewFurtherControlMeasureTask_Request_Then_returns_correct_result()
        {
            // Given
            var controller = GetTarget();
            var viewModel = new AddEditFireRiskAssessmentFurtherControlMeasureTaskViewModel();
            var documentsToSave = new DocumentsToSaveViewModel();

            controller.ModelState.AddModelError("error", "some problem");

            // When
            var result = controller.NewFurtherControlMeasureTask(viewModel, documentsToSave) as JsonResult;

            // Then
            Assert.That(result, Is.InstanceOf<JsonResult>());
            Assert.That(result.Data.ToString(), Is.EqualTo("{ Success = False, Errors = System.String[] }"));
        }
        public JsonResult NewFurtherControlMeasureTask(AddEditFireRiskAssessmentFurtherControlMeasureTaskViewModel viewModel, DocumentsToSaveViewModel documentsToSave)
        {
            if (!ModelState.IsValid)
                return Json(new { Success = false, Errors = ModelState.GetErrorMessages() });

            if(!viewModel.IsRecurring)
            {
                viewModel.TaskReoccurringTypeId = (int)TaskReoccurringType.None;
            }
            
            var taskGuid = Guid.NewGuid();
            SaveFurtherControlMeasureTaskRequest request = SaveFurtherControlMeasureTaskRequest.Create(
                                                                                                        viewModel.Title,
                                                                                                        viewModel.Description,
                                                                                                        viewModel.Reference,
                                                                                                        viewModel.TaskCompletionDueDate,
                                                                                                        viewModel.TaskStatusId,
                                                                                                        viewModel.CompanyId,
                                                                                                        viewModel.RiskAssessmentId,
                                                                                                        viewModel.SignificantFindingId,
                                                                                                        viewModel.TaskAssignedToId.GetValueOrDefault(),
                                                                                                        viewModel.TaskReoccurringTypeId,
                                                                                                        viewModel.FirstDueDate, 
                                                                                                        viewModel.TaskReoccurringEndDate, 
                                                                                                        CurrentUser.UserId,
                                                                                                        documentsToSave.CreateDocumentRequests,
                                                                                                        documentsToSave.DeleteDocumentRequests,
                                                                                                        !viewModel.DoNotSendTaskNotification,
                                                                                                        !viewModel.DoNotSendTaskCompletedNotification,
                                                                                                        !viewModel.DoNotSendTaskOverdueNotification,
                                                                                                        !viewModel.DoNotSendTaskDueTomorrowNotification,
                                                                                                        taskGuid);

            FireRiskAssessmentFurtherControlMeasureTaskDto result;
            using (var session = _businessSafeSessionManager.Session)
            {
                result = _furtherControlMeasureTaskService.AddFurtherControlMeasureTask(request);
                _businessSafeSessionManager.CloseSession();
            }


            _eventPublisher.PublishTaskAssigned(taskGuid);
            return Json(new { Success = true, result.Id, CreatedOn = result.CreatedDate });
        }
        public AddEditFireRiskAssessmentFurtherControlMeasureTaskViewModel GetViewModel()
        {
            var fireRiskAssessment = _fireRiskAssessmentService.GetRiskAssessment(_riskAssessmentId, _companyId);

            var viewModel = new AddEditFireRiskAssessmentFurtherControlMeasureTaskViewModel()
                                {
                                    CompanyId = _companyId,
                                    RiskAssessmentId = _riskAssessmentId,
                                    SignificantFindingId = _significantFindingId,
                                    ExistingDocuments = new ExistingDocumentsViewModel()
                                                            {
                                                                DocumentTypeId = (int)DocumentTypeEnum.FRADocumentType
                                                            },
                                    
                                    TaskReoccurringTypes = new TaskReoccurringType().ToSelectList(),
                                    DoNotSendTaskCompletedNotification = fireRiskAssessment.RiskAssessor == null ? false : fireRiskAssessment.RiskAssessor.DoNotSendTaskCompletedNotifications,
                                    DoNotSendTaskOverdueNotification = fireRiskAssessment.RiskAssessor == null ? false : fireRiskAssessment.RiskAssessor.DoNotSendTaskOverdueNotifications,
                                    Description = GetSignificantFindingAdditionalInfo(fireRiskAssessment,_significantFindingId)
                                    
                                };
            return viewModel;
        }
        public void Given_valid_model_state_When_NewFurtherControlMeasureTask_Request_Then_should_return_correct_result()
        {
            // Given
            var controller = GetTarget();
            var viewModel = new AddEditFireRiskAssessmentFurtherControlMeasureTaskViewModel()
            {
                CompanyId = 123L,
                RiskAssessmentId = 789L,
                Title = "TItle",
                Description = "A description",
                Reference = "My reference",
                SignificantFindingId = 888L,
                TaskStatusId = 3
            };
            var documentsToSave = new DocumentsToSaveViewModel();

            _furtherControlMeasureTaskService
                .Setup(x => x.AddFurtherControlMeasureTask(It.IsAny<SaveFurtherControlMeasureTaskRequest>()))
                .Returns(new FireRiskAssessmentFurtherControlMeasureTaskDto());

            // When
            var result = controller.NewFurtherControlMeasureTask(viewModel, documentsToSave) as JsonResult;

            // Then
            Assert.That(result.Data.ToString(), Is.EqualTo("{ Success = True, Id = 0, CreatedOn =  }"));
        }
        public void Given_is_recurring_is_false_When_NewFurtherControlMeasureTask_Request_Then_set_recurringtype_to_none()
        {
            // Given
            var controller = GetTarget();
            var viewModel = new AddEditFireRiskAssessmentFurtherControlMeasureTaskViewModel()
            {
                CompanyId = 123L,
                RiskAssessmentId = 789L,
                Title = "TItle",
                Description = "A description",
                Reference = "My reference",
                SignificantFindingId = 888L,
                TaskStatusId = 3,
                IsRecurring = false,
                TaskReoccurringTypeId = (int)TaskReoccurringType.Annually
            };
            var documentsToSave = new DocumentsToSaveViewModel();

            _furtherControlMeasureTaskService
                .Setup(x => x.AddFurtherControlMeasureTask(It.IsAny<SaveFurtherControlMeasureTaskRequest>()))
                .Returns(new FireRiskAssessmentFurtherControlMeasureTaskDto());

            // When
            var result = controller.NewFurtherControlMeasureTask(viewModel, documentsToSave);

            // Then
            _furtherControlMeasureTaskService
                .Verify(x => x.AddFurtherControlMeasureTask(It.Is<SaveFurtherControlMeasureTaskRequest>(y => y.TaskReoccurringTypeId == (int)TaskReoccurringType.None)));
        }