public AddRiskAssessmentFurtherControlMeasureTaskViewModel GetViewModel()
        {
            var riskAssessmentHazard = _riskAssessmentHazardService.GetByIdAndCompanyId(_riskAssessmentHazardId, _companyId);

            var viewModel = new AddRiskAssessmentFurtherControlMeasureTaskViewModel()
                                    {
                                        HazardSummary = _riskAssessmentHazardSummaryViewModelFactory
                                            .WithCompanyId(_companyId)
                                            .WithRiskAssessmentHazardId(_riskAssessmentHazardId)
                                            .GetViewModel(),
                                        CompanyId = _companyId,
                                        RiskAssessmentHazardId = _riskAssessmentHazardId,
                                        ExistingDocuments = new ExistingDocumentsViewModel()
                                        {
                                            DocumentTypeId = (int)DocumentTypeEnum.GRADocumentType
                                        },
                                        TaskReoccurringTypes = new TaskReoccurringType().ToSelectList(),
                                        FurtherControlMeasureTaskCategory = _furtherControlMeasureTaskCategory,
                                        DoNotSendTaskCompletedNotification = riskAssessmentHazard.RiskAssessment.RiskAssessor == null ? false : riskAssessmentHazard.RiskAssessment.RiskAssessor.DoNotSendTaskCompletedNotifications,
                                        DoNotSendTaskOverdueNotification = riskAssessmentHazard.RiskAssessment.RiskAssessor == null ? false : riskAssessmentHazard.RiskAssessment.RiskAssessor.DoNotSendTaskOverdueNotifications
                                    };


            
            return viewModel;
        }
        public void Give_IsRecurring_is_false_When_CreateFurtherControlMeasureTask_Then_Set_TaskRecurringType_to_None()
        {
            //given
            var target = GetTarget();
            
            var viewModel = new AddRiskAssessmentFurtherControlMeasureTaskViewModel()
            {
                TaskAssignedToId = Guid.NewGuid(),
                RiskAssessmentHazardId = 500,
                IsRecurring = false,
                TaskReoccurringTypeId = (int)TaskReoccurringType.Monthly

            };

            var result = new MultiHazardRiskAssessmentFurtherControlMeasureTaskDto()
            {
                CreatedDate = DateTime.Now.ToShortDateString()
            };

            _furtherControlMeasureTaskService
                .Setup(x => x.AddFurtherControlMeasureTask(It.IsAny<SaveFurtherControlMeasureTaskRequest>()))
                .Returns(result);
            
            //when

            target.CreateFurtherControlMeasureTask(viewModel, new DocumentsToSaveViewModel());

            //then
            _furtherControlMeasureTaskService
                .Verify(x => x.AddFurtherControlMeasureTask(It.Is<SaveFurtherControlMeasureTaskRequest>(y => y.TaskReoccurringTypeId == (int)TaskReoccurringType.None)), Times.Once());
        }
        public void When_CreateFurtherControlMeasureTask_called_Then_correct_methods_are_called()
        {
            // Arrange
            var target = GetTarget();

            var documentsToSaveViewModel = new DocumentsToSaveViewModel();
            var viewmodel = new AddRiskAssessmentFurtherControlMeasureTaskViewModel()
                                {
                                    TaskAssignedToId = Guid.NewGuid(),
                                    RiskAssessmentHazardId = 500,
                                    
                                };

            var userId = target.CurrentUser.UserId;

            var result = new MultiHazardRiskAssessmentFurtherControlMeasureTaskDto()
                             {
                                 CreatedDate = DateTime.Now.ToShortDateString()
                             };

            _furtherControlMeasureTaskService
                .Setup(x => x.AddFurtherControlMeasureTask(It.Is<SaveFurtherControlMeasureTaskRequest>(y => y.CompanyId == viewmodel.CompanyId &&
                                                                                           y.Title == viewmodel.Title &&
                                                                                           y.Description == viewmodel.Description &&
                                                                                           y.Reference == viewmodel.Reference &&
                                                                                           y.UserId == userId)))
                .Returns(result);

            // Act
            target.CreateFurtherControlMeasureTask(viewmodel, documentsToSaveViewModel);

            // Assert
            _furtherControlMeasureTaskService.VerifyAll();
            _businessSafeSessionManager.Verify(x => x.CloseSession(), Times.Exactly(1));
        }
        public JsonResult CreateFurtherControlMeasureTask(AddRiskAssessmentFurtherControlMeasureTaskViewModel viewmodel, DocumentsToSaveViewModel documentsToSave)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { Success = false, Errors = ModelState.GetErrorMessages() });
            }

            MultiHazardRiskAssessmentFurtherControlMeasureTaskDto result;
            Guid taskGuid = Guid.NewGuid();
            using (var session = _businessSafeSessionManager.Session)
            {
                if(!viewmodel.IsRecurring)
                {
                    viewmodel.TaskReoccurringTypeId = (int)TaskReoccurringType.None;
                }

                var saveTaskRequest = SaveFurtherControlMeasureTaskRequest.Create(
                    viewmodel.Title,
                    viewmodel.Description,
                    viewmodel.Reference,
                    viewmodel.TaskCompletionDueDate,
                    viewmodel.TaskStatusId,
                    viewmodel.CompanyId,
                    0,
                    viewmodel.RiskAssessmentHazardId,
                    viewmodel.TaskAssignedToId.Value,
                    (int)viewmodel.FurtherControlMeasureTaskCategory,
                    viewmodel.TaskReoccurringTypeId,
                    viewmodel.FirstDueDate,
                    viewmodel.TaskReoccurringEndDate,
                    !viewmodel.DoNotSendTaskNotification,
                    !viewmodel.DoNotSendTaskCompletedNotification,
                    !viewmodel.DoNotSendTaskOverdueNotification,
                    !viewmodel.DoNotSendTaskDueTomorrowNotification,
                    CurrentUser.UserId,
                    documentsToSave.CreateDocumentRequests,
                    taskGuid
                    );

                result = _furtherControlMeasureTaskService.AddFurtherControlMeasureTask(saveTaskRequest);

                _businessSafeSessionManager.CloseSession();
            }

            _bus.Publish(new TaskAssigned { TaskGuid = taskGuid });

            return Json(new { Success = true, result.Id, viewmodel.RiskAssessmentHazardId, CreatedOn = result.CreatedDate });
        }
        public void Given_invalid_viewmodel_When_CreateFurtherControlMeasureTask_called_Then_correct_view_is_returned()
        {
            // Arrange
            var target = GetTarget();

            var documentsToSaveViewModel = new DocumentsToSaveViewModel();
            var viewmodel = new AddRiskAssessmentFurtherControlMeasureTaskViewModel();

            target.ModelState.AddModelError("Any", "Any");

            // Act
            var result = target.CreateFurtherControlMeasureTask(viewmodel, documentsToSaveViewModel) as JsonResult;


            // Assert
            Assert.That(result.Data.ToString(), Contains.Substring("Success = False"));
        }
        public void When_CreateFurtherControlMeasureTask_called_Then_correct_view_is_returned()
        {
            // Arrange
            var target = GetTarget();

            var documentsToSaveViewModel = new DocumentsToSaveViewModel();
            var viewmodel = new AddRiskAssessmentFurtherControlMeasureTaskViewModel()
                                {
                                    TaskAssignedToId = Guid.NewGuid(),
                                    RiskAssessmentHazardId = 500
                                };

            var dto = new MultiHazardRiskAssessmentFurtherControlMeasureTaskDto()
            {
                CreatedDate = DateTime.Now.ToShortDateString()
            };
            _furtherControlMeasureTaskService
              .Setup(x => x.AddFurtherControlMeasureTask(It.Is<SaveFurtherControlMeasureTaskRequest>(y => y.CompanyId == viewmodel.CompanyId)))
              .Returns(dto);

            // Act
            var result = target.CreateFurtherControlMeasureTask(viewmodel, documentsToSaveViewModel) as JsonResult;


            // Assert
            Assert.That(result.Data.ToString(), Contains.Substring("Success = True, Id = 0, RiskAssessmentHazardId = 500"));
        }