public void Given_valid_When_CreateResponsibilitiesFromWizard_Then_template_is_mapped()
        {
            // Given
            Responsibility passedResponsibility = null;
            _responsibilityRepository.Setup(x =>
                x.Save(It.IsAny<Responsibility>()))
                .Callback<Responsibility>(y => passedResponsibility = y);

            var requestedOwnerEmployeeId = _employees.First().Id;
            var requestedTemplateId = _statutoryResponsibilityTemplates.First().Id;
            var target = GetTarget();

            _employeeRepository
                .Setup(x => x.GetByIds(It.IsAny<IList<Guid>>()))
                .Returns(_employees.Where(x => x.Id == requestedOwnerEmployeeId));

            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = _sites.Select(x => x.Id).ToArray(),
                ResponsibilityFromTemplateDetails = new List<ResponsibilityFromTemplateDetail> { new ResponsibilityFromTemplateDetail() { ResponsibilityTemplateId = requestedTemplateId, ResponsiblePersonEmployeeId = requestedOwnerEmployeeId } },
                CompanyId = _companyId
            };

            // When
            target.CreateResponsibilitiesFromWizard(request);

            // Then
            _responsibilityRepository.Verify(x =>
                x.Save(It.Is<Responsibility>(y => y.StatutoryResponsibilityTemplateCreatedFrom == _statutoryResponsibilityTemplates.First())),
                Times.Exactly(request.SiteIds.Length * request.ResponsibilityFromTemplateDetails.Count));
        }
        public void Given_requested_Employees_are_not_found_When_CreateResponsibilitiesFromWizard_Then_throw_exception()
        {
            // Given
            var tempResponsibilityTemplateDetails = new List<ResponsibilityFromTemplateDetail>
                                                    {
                                                        new ResponsibilityFromTemplateDetail()
                                                        {
                                                            ResponsibilityTemplateId =
                                                                _statutoryResponsibilityTemplates.ElementAt(0).Id,
                                                            ResponsiblePersonEmployeeId = Guid.NewGuid(),
                                                            FrequencyId = TaskReoccurringType.Annually
                                                        }
                                                    };

            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = _sites.Select(x => x.Id).ToArray(),
                ResponsibilityFromTemplateDetails = tempResponsibilityTemplateDetails,
                CompanyId = _companyId
            };

            // When
            var e = Assert.Throws<EmployeeRequestedForStatutoryResponsibilityNotValidException>(() => target.CreateResponsibilitiesFromWizard(request));
        }
        public void Given_requested_site_and_but_not_template_combination_has_already_been_created_for_company_When_CreateResponsibilitiesFromWizard_Then_only_save_combination_that_doesnt_yet_exist()
        {
            // Given
            var firstRequestedSiteId = _sites.First().Id;
            var requestedOwnerEmployeeId = _employees.First().Id;
            var firstRequestedTemplateId = _requestedResponsibilityFromTemplateDetails.First().ResponsibilityTemplateId;
            var secondRequestedTemplateId = _requestedResponsibilityFromTemplateDetails.ElementAt(1).ResponsibilityTemplateId;

            _responsibilityRepository
                .Setup(x => x.GetStatutoryByCompanyId(It.IsAny<long>()))
                .Returns(new List<Responsibility>
                         {
                             new Responsibility()
                             {
                                 Site = new Site() { Id = firstRequestedSiteId },
                                 StatutoryResponsibilityTemplateCreatedFrom = _statutoryResponsibilityTemplates.First(),
                                 CompanyId = _companyId
                             }
                         });

            _employeeRepository
                .Setup(x => x.GetByIds(It.IsAny<IList<Guid>>()))
                .Returns(_employees.Where(x => x.Id == requestedOwnerEmployeeId));

            _siteRepository
                .Setup(x => x.GetByIds(It.IsAny<long[]>()))
                .Returns(_sites.Where(x => x.Id == firstRequestedSiteId));

            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = new[] { firstRequestedSiteId },
                ResponsibilityFromTemplateDetails = new List<ResponsibilityFromTemplateDetail>
                                                    {
                                                        new ResponsibilityFromTemplateDetail()
                                                        {
                                                            FrequencyId = TaskReoccurringType.Weekly,
                                                            ResponsibilityTemplateId = firstRequestedTemplateId,
                                                            ResponsiblePersonEmployeeId = requestedOwnerEmployeeId
                                                        },
                                                        new ResponsibilityFromTemplateDetail()
                                                        {
                                                            FrequencyId = TaskReoccurringType.Weekly,
                                                            ResponsibilityTemplateId = secondRequestedTemplateId,
                                                            ResponsiblePersonEmployeeId = requestedOwnerEmployeeId
                                                        }
                                                    },
                CompanyId = _companyId
            };

            // When
            target.CreateResponsibilitiesFromWizard(request);

            // Then
            _responsibilityRepository.Verify(x =>
                x.Save(It.IsAny<Responsibility>()),
                Times.Once());
        }
        public void Given_requested_sites_are_not_found_When_CreateResponsibilitiesFromWizard_Then_throw_exception()
        {
            // Given

            var requestedSiteIds = _sites.Select(x => x.Id).ToArray().Concat(new long[] { 1245L }).ToArray();

            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = requestedSiteIds,
                ResponsibilityFromTemplateDetails = _requestedResponsibilityFromTemplateDetails,
                CompanyId = _companyId
            };

            // When
            var e = Assert.Throws<SiteRequestedForStatutoryResponsibilityNotValidException>(() => target.CreateResponsibilitiesFromWizard(request));
        }
        public void Given_requested_employees_are_not_in_company_When_CreateResponsibilitiesFromWizard_Then_throw_exception()
        {
            // Given
            const long anotherCompanyId = 354634234L;
            var otherCompaniesEmployees = new List<Employee>
                     {
                             new Employee() {Id = Guid.NewGuid(), CompanyId = anotherCompanyId},
                             new Employee() {Id = Guid.NewGuid(), CompanyId = anotherCompanyId},
                             new Employee() {Id = Guid.NewGuid(), CompanyId = anotherCompanyId}
                     };
            _employeeRepository
                .Setup(x => x.GetByIds(It.IsAny<List<Guid>>()))
                .Returns(otherCompaniesEmployees);

            var requestedResponsibilityFromTemplateDetails = new List<ResponsibilityFromTemplateDetail>
            {
                new ResponsibilityFromTemplateDetail() { ResponsibilityTemplateId = _statutoryResponsibilityTemplates.ElementAt(0).Id, ResponsiblePersonEmployeeId = otherCompaniesEmployees.ElementAt(0).Id, FrequencyId = TaskReoccurringType.Annually },
                new ResponsibilityFromTemplateDetail() { ResponsibilityTemplateId = _statutoryResponsibilityTemplates.ElementAt(1).Id, ResponsiblePersonEmployeeId = otherCompaniesEmployees.ElementAt(1).Id, FrequencyId = TaskReoccurringType.ThreeYearly },
                new ResponsibilityFromTemplateDetail() { ResponsibilityTemplateId = _statutoryResponsibilityTemplates.ElementAt(2).Id, ResponsiblePersonEmployeeId = otherCompaniesEmployees.ElementAt(2).Id, FrequencyId = TaskReoccurringType.TwentyFourMonthly },
                new ResponsibilityFromTemplateDetail() { ResponsibilityTemplateId = _statutoryResponsibilityTemplates.ElementAt(3).Id, ResponsiblePersonEmployeeId = otherCompaniesEmployees.ElementAt(2).Id, FrequencyId = TaskReoccurringType.FiveYearly },
            };

            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = _sites.Select(x => x.Id).ToArray(),
                ResponsibilityFromTemplateDetails = requestedResponsibilityFromTemplateDetails,
                CompanyId = _companyId,
            };

            // When
            var e = Assert.Throws<EmployeeRequestedForStatutoryResponsibilityNotValidException>(() => target.CreateResponsibilitiesFromWizard(request));
        }
        public void Given_request_When_CreateResponsibilitiesFromWizard_Then_User_For_Auditing_mapped_to_each_new_Responsibility()
        {
            // Given
            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = _sites.Select(x => x.Id).ToArray(),
                ResponsibilityFromTemplateDetails = _requestedResponsibilityFromTemplateDetails,
                CompanyId = _companyId,
            };

            // When
            target.CreateResponsibilitiesFromWizard(request);

            _responsibilityRepository.Verify(x =>
                     x.Save(It.Is<Responsibility>(y => y.CreatedBy == _testUser)),
                     Times.Exactly((request.SiteIds.Length * request.ResponsibilityFromTemplateDetails.Count)));
        }
        public void Given_requested_sites_are_not_in_company_When_CreateResponsibilitiesFromWizard_Then_throw_exception()
        {
            // Given
            const long anotherCompanyId = 354634234L;
           var otherCompaniesSites = new List<Site>
                     {
                         new Site { Id = 123L, ClientId = anotherCompanyId },
                         new Site { Id = 456L, ClientId = anotherCompanyId },
                         new Site { Id = 789L, ClientId = anotherCompanyId }
                     };
            _siteRepository
                .Setup(x => x.GetByIds(It.IsAny<long[]>()))
                .Returns(otherCompaniesSites);

            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = _sites.Select(x => x.Id).ToArray(),
                ResponsibilityFromTemplateDetails = _requestedResponsibilityFromTemplateDetails,
                CompanyId = _companyId,
            };

            // When
            var e = Assert.Throws<SiteRequestedForStatutoryResponsibilityNotValidException>(() => target.CreateResponsibilitiesFromWizard(request));
        }
        public void Given_request_When_CreateResponsibilitiesFromWizard_Then_owner_mapped_to_each_new_Responsibility()
        {
            // Given
            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = _sites.Select(x => x.Id).ToArray(),
                ResponsibilityFromTemplateDetails = _requestedResponsibilityFromTemplateDetails,
                CompanyId = _companyId,

            };

            // When
            target.CreateResponsibilitiesFromWizard(request);

            // Then
            foreach (var employeeId in _requestedResponsibilityFromTemplateDetails.Select(x => x.ResponsiblePersonEmployeeId).Distinct())
            {
                _responsibilityRepository.Verify(x =>
                    x.Save(It.Is<Responsibility>(y => y.Owner.Id == employeeId)),
                    Times.Exactly(_sites.Count * _requestedResponsibilityFromTemplateDetails.Count(x => x.ResponsiblePersonEmployeeId == employeeId)));
            };
        }
        public void Given_request_When_CreateResponsibilitiesFromWizard_Then_frequency_mapped_to_each_new_Responsibility()
        {
            // Given
            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = _sites.Select(x => x.Id).ToArray(),
                ResponsibilityFromTemplateDetails = _requestedResponsibilityFromTemplateDetails,
                CompanyId = _companyId,

            };

            // When
            target.CreateResponsibilitiesFromWizard(request);

            // Then
            foreach (var templateDetails in request.ResponsibilityFromTemplateDetails)
            {
                _responsibilityRepository.Verify(x =>
                    x.Save(It.Is<Responsibility>(y => y.InitialTaskReoccurringType == templateDetails.FrequencyId)),
                    Times.Exactly(_sites.Count));
            };
        }
        public void Given_request_When_CreateResponsibilitiesFromWizard_Then_all_required_sites_retrieved()
        {
            // Given
            IList<long> passedSiteIds = null;
            _siteRepository
                .Setup(x => x.GetByIds(It.IsAny<IList<long>>()))
                .Callback<IList<long>>(y => passedSiteIds = y)
                .Returns(_sites);
            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = _sites.Select(x => x.Id).ToArray(),
                ResponsibilityFromTemplateDetails = _requestedResponsibilityFromTemplateDetails,
                CompanyId = _companyId
            };

            // When
            target.CreateResponsibilitiesFromWizard(request);

            // Then
            foreach (var siteId in request.SiteIds)
            {
                Assert.That(passedSiteIds.Contains(siteId));
            };
        }
        public void Given_request_When_CreateResponsibilitiesFromWizard_Then_required_reason_mapped_to_each_new_Responsibility()
        {
            // Given
            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = _sites.Select(x => x.Id).ToArray(),
                ResponsibilityFromTemplateDetails = _requestedResponsibilityFromTemplateDetails,
                CompanyId = _companyId
            };

            // When
            target.CreateResponsibilitiesFromWizard(request);

            // Then
            foreach (var template in _statutoryResponsibilityTemplates)
            {
                _responsibilityRepository.Verify(x =>
                    x.Save(It.Is<Responsibility>(y => y.ResponsibilityReason == template.ResponsibilityReason)),
                    Times.Exactly(_sites.Count));
            };
        }
        public void Given_request_with_sites_and_responsibilities_When_CreateResponsibilitiesFromWizard_Then_call_save_a_number_of_times_equal_to_cartesian_product(int totalSites, int totalResponsibilities)
        {
            // Given
            var requestedSiteId = _sites.First().Id;
            var requestedOwnerEmployeeId = _employees.First().Id;

            _employeeRepository
                .Setup(x => x.GetByIds(It.IsAny<IList<Guid>>()))
                .Returns(_employees.Where(x => x.Id == requestedOwnerEmployeeId));

            _siteRepository
                .Setup(x => x.GetByIds(It.IsAny<long[]>()))
                .Returns(_sites.Where(x => x.Id == requestedSiteId));

            var target = GetTarget();
            var request = new CreateResponsibilityFromWizardRequest()
            {
                SiteIds = new long[totalSites],
                ResponsibilityFromTemplateDetails = new List<ResponsibilityFromTemplateDetail>(),
                CompanyId = _companyId
            };
            for (var i = 0; i < totalSites; i++)
            {
                request.SiteIds[i] = requestedSiteId;
            }
            for (var i = 0; i < totalResponsibilities; i++)
            {
                request.ResponsibilityFromTemplateDetails.Add(new ResponsibilityFromTemplateDetail() { ResponsibilityTemplateId = 456L, ResponsiblePersonEmployeeId = requestedOwnerEmployeeId });
            }

            // When
            target.CreateResponsibilitiesFromWizard(request);
            
            // Then
            _responsibilityRepository.Verify(x => x.Save(It.IsAny<Responsibility>()), Times.Exactly(totalSites * totalResponsibilities));

        }
 private IEnumerable<Employee> GetRequestedOwnerEmployeeTemplates(CreateResponsibilityFromWizardRequest request)
 {
     var ids = request.ResponsibilityFromTemplateDetails.Select(req => req.ResponsiblePersonEmployeeId).ToList();
     return _employeeRepository.GetByIds(ids);
 }
 private IEnumerable<StatutoryResponsibilityTemplate> GetRequestedResponsibilityTemplates(CreateResponsibilityFromWizardRequest request)
 {
     var templateIds = request.ResponsibilityFromTemplateDetails.Select(req => req.ResponsibilityTemplateId).ToList();
     return _statutoryResponsibilityTemplateRepository.GetByIds(templateIds);
 }
        public void CreateResponsibilitiesFromWizard(CreateResponsibilityFromWizardRequest request)
        {
            _log.Add(request);
            var templates = GetRequestedResponsibilityTemplates(request);
            var sites = _siteRepository.GetByIds(request.SiteIds);
            if (sites.Count() != request.SiteIds.Distinct().Count() || sites.Any(site => site.ClientId != request.CompanyId))
            {
                throw new SiteRequestedForStatutoryResponsibilityNotValidException();
            }

            var employees = GetRequestedOwnerEmployeeTemplates(request);
            if (employees.Count() != request.ResponsibilityFromTemplateDetails.Select(x => x.ResponsiblePersonEmployeeId).Distinct().Count() || employees.Any(employee => employee.CompanyId != request.CompanyId))
            {
                throw new EmployeeRequestedForStatutoryResponsibilityNotValidException();
            }

            var responsibilities = _responsibilityRepository.GetStatutoryByCompanyId(request.CompanyId);

            var creatingUser = _userForAuditingRepository.GetByIdAndCompanyId(request.UserId, request.CompanyId);

            foreach (var resp in request.ResponsibilityFromTemplateDetails)
            {
                foreach (var siteId in request.SiteIds)
                {
                    var currTemplate = templates.First(x => x.Id == resp.ResponsibilityTemplateId);

                    if (!responsibilities.Any(x => x.Site != null && x.Site.Id == siteId && x.StatutoryResponsibilityTemplateCreatedFrom == currTemplate))
                    {
                        _responsibilityRepository.Save(
                            Responsibility.Create(
                                request.CompanyId,
                                currTemplate.ResponsibilityCategory,
                                currTemplate.Title,
                                currTemplate.Description,
                                sites.Single(x => x.Id == siteId),
                                currTemplate.ResponsibilityReason,
                                employees.Single(x => x.Id == resp.ResponsiblePersonEmployeeId),
                                resp.FrequencyId,
                                currTemplate,
                                creatingUser));
                    }
                }
            }
        }
        public JsonResult GenerateResponsibilities(CreateResponsibilityFromSiteAndResponsibilityTemplateModel model)
        {
            ValidateGenerateResponsibilities(model);
            if (!ModelState.IsValid)
            {
                return ModelStateErrorsAsJson();
            }

            var request = new CreateResponsibilityFromWizardRequest
                                {
                                    CompanyId = CurrentUser.CompanyId,
                                    SiteIds = model.SiteIds,
                                    UserId = CurrentUser.UserId,
                                    ResponsibilityFromTemplateDetails = model.Responsibilities
                                        .Select(x => new ResponsibilityFromTemplateDetail()
                                                    {
                                                        ResponsibilityTemplateId = x.ResponsibilityTemplateId,
                                                        FrequencyId = x.FrequencyId,
                                                        ResponsiblePersonEmployeeId = x.ResponsiblePersonEmployeeId.Value
                                                    })
                                        .ToList()
                                };

            _responsibilitiesService.CreateResponsibilitiesFromWizard(request);
            return Json(new { Success = true });
        }