public void Setup()
        {
            _businessSafeSessionManager = new Mock<IBusinessSafeSessionManager>();
            _bus = new Mock<IBus>();

            _responsibilitiesService = new Mock<IResponsibilitiesService>();
            _responsibilitiesService.Setup(
                x => x.CreateResponsibilityTaskFromWizard(It.IsAny<CreateResponsibilityTasksFromWizardRequest>()));

            _viewModel = new GenerateResponsibilityTaskViewModel
            {
                SiteId = 1L,
                TaskId = 1L,
                ResponsibilityId = 1L,
                Frequency = TaskReoccurringType.Weekly,
                Owner = Guid.NewGuid(),
                StartDate = DateTime.Now.ToShortDateString(),
                EndDate = DateTime.Now.AddDays(1).ToShortDateString()
            };
        }
        public void Given_a_model_with_invalid_frequencty_When_generate_task_Then_add_error_to_modelstate()
        {
            //given
            var target = GetTarget();

            var viewModel = new GenerateResponsibilityTaskViewModel
            {
                SiteId = 1L,
                TaskId = 1L,
                ResponsibilityId = 1L,
                Frequency = TaskReoccurringType.None,
                Owner = Guid.NewGuid(),
                StartDate = DateTime.Now.ToShortDateString(),
                EndDate = DateTime.Now.AddDays(1).ToShortDateString()
            };

            //when
            target.GenerateTask(viewModel);
            //then
            Assert.That(target.ModelState.IsValid == false);

        }
        public void Given_a_model_with_enddate_before_startdate_When_generate_task_Then_add_error_to_modelstate()
        {
            //given
            var target = GetTarget();

            var viewModel = new GenerateResponsibilityTaskViewModel
            {
                SiteId = 1L,
                TaskId = 1L,
                ResponsibilityId = 1L,
                Frequency = TaskReoccurringType.Weekly,
                Owner = Guid.NewGuid(),
                StartDate = DateTime.Now.ToShortDateString(),
                EndDate = DateTime.Now.AddDays(-1).ToShortDateString(),
            };

            //when
            target.BulkGenerateTasks(new List<GenerateResponsibilityTaskViewModel> { viewModel });
            //then
            Assert.That(target.ModelState.IsValid == false);

        }
        private void ValidateGenerateResponsibilityTasks(GenerateResponsibilityTaskViewModel model)
        {
            // todo: use data annotations???
            if (model.Frequency == TaskReoccurringType.None)
            {
                ModelState.AddModelError("FrequencyId", "Please select a frequency for the selected task.");
            }

            DateTime startDate = DateTime.MinValue;
            if(model.StartDate==null || !DateTime.TryParse(model.StartDate, out startDate))
            {
                ModelState.AddModelError("StartDate", "Please select a valid start date for the selected task.");
            }

            if (startDate < DateTime.Now.Date)
            {
                ModelState.AddModelError("StartDate", "Start date can not be in the past.");
            }

            DateTime endDate = DateTime.MaxValue;
            if (model.EndDate != null && !DateTime.TryParse(model.EndDate, out endDate))
            {
                ModelState.AddModelError("EndDate", "Please select a valid end date for the selected task.");
            }

            if (model.EndDate != null && endDate < startDate)
            {
                ModelState.AddModelError("EndDate", "Please select an end date after the start date");
            }

            if (model.Owner == null || model.Owner == Guid.Empty)
            {
                ModelState.AddModelError("Assignee", "Please select an assignee for the selected task.");
            }
        }
        public JsonResult GenerateTask(GenerateResponsibilityTaskViewModel viewModel)
        {
            JsonResult result;

            ValidateGenerateResponsibilityTasks(viewModel);
            if (!ModelState.IsValid)
            {
                result = ModelStateErrorsAsJson();
            }
            else
            {
                var taskGuid = Guid.NewGuid();
                using (var session = _businessSafeSessionManager.Session)
                {
                    var request = CreateResponsibilityTasksFromWizardRequest.Create(CurrentUser.CompanyId,
                                                                                    CurrentUser.UserId, viewModel.SiteId,
                                                                                    viewModel.ResponsibilityId,
                                                                                    viewModel.TaskId,
                                                                                    viewModel.Frequency,
                                                                                    viewModel.Owner, viewModel.StartDate,
                                                                                    viewModel.EndDate,
                                                                                    taskGuid);

                    _responsibilitiesService.CreateResponsibilityTaskFromWizard(request);
                    _businessSafeSessionManager.CloseSession();
                }

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

                result = Json(new {Success = true});
            }

            return result;
        }
        public void Given_a_model_without_an_enddate_When_generate_task_Then_do_not_add_error_to_modelstate()
        {
            //given
            var target = GetTarget();

            var viewModel = new GenerateResponsibilityTaskViewModel
            {
                SiteId = 1L,
                TaskId = 1L,
                ResponsibilityId = 1L,
                Frequency = TaskReoccurringType.Weekly,
                Owner = Guid.NewGuid(),
                StartDate = DateTime.Now.ToShortDateString(),
                EndDate = null,
            };

            //when
            target.GenerateTask(viewModel);

            //then
            Assert.That(target.ModelState.IsValid);
        }