public void Given_Previous_ResponsibilityId_Not_Found_When_Copy_Responsibility_Then_Return_Not_Found_Exception()
        {
            // Given
            _target = GetTarget();

            _responsibilityRepo.Setup(x => x.GetByIdAndCompanyId(It.IsAny<long>(), It.IsAny<long>())).Throws(new ResponsibilityNotFoundException(_originalResponsibilityId, _companyId));

            var request = new CopyResponsibilityRequest()
            {
                OriginalResponsibilityId = 1,
                Title = "Title"
            };

            // When
            Assert.Throws<ResponsibilityNotFoundException>(() => _target.CopyResponsibility(request));

            // Then
        }
        public JsonResult CopyResponsibility(CopyResponsibilityViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return ModelStateErrorsAsJson();
            }

            var request = new CopyResponsibilityRequest()
            {
                CompanyId = CurrentUser.CompanyId,
                UserId = CurrentUser.UserId,
                OriginalResponsibilityId = viewModel.ResponsibilityId,
                Title = viewModel.Title
            };

            var result = _responsibilitiesService.CopyResponsibility(request);

            return Json(new { Success = true, Id = result });
        }
        public void Given_ResponsibilityId_Found_When_Copy_Responsibility_Then_Check_New_Responsibility_Created_With_Originals_Parameters()
        {
            // Given
            _target = GetTarget();
            
            _responsibilityRepo.Setup(x => x.GetByIdAndCompanyId(It.IsAny<long>(), It.IsAny<long>()))
                .Returns(_originalResponibility);

            var request = new CopyResponsibilityRequest()
            {
                CompanyId = _companyId,
                OriginalResponsibilityId = _originalResponsibilityId,
                Title = "new Title"
            };

            Responsibility newResponsibility = null;

            _responsibilityRepo.Setup(x => x.Save(It.IsAny<Responsibility>()))
                .Callback<Responsibility>(r => newResponsibility = r);

            // When
            _target.CopyResponsibility(request);
            
            // Then
            Assert.True(newResponsibility.CompanyId.Equals(_originalResponibility.CompanyId));
            Assert.True(newResponsibility.ResponsibilityCategory.Equals(_originalResponibility.ResponsibilityCategory));
            Assert.False(newResponsibility.Title.Equals(_originalResponibility.Title));
            Assert.True(newResponsibility.Description.Equals(_originalResponibility.Description));
            Assert.IsNull(newResponsibility.Site);
            Assert.IsNull(newResponsibility.Owner);
            Assert.True(newResponsibility.InitialTaskReoccurringType.Equals(_originalResponibility.InitialTaskReoccurringType));
            Assert.True(newResponsibility.ResponsibilityTasks.Count.Equals(_originalResponibility.ResponsibilityTasks.Count));
            Assert.False(newResponsibility.CreatedOn.Equals(_originalResponibility.CreatedOn));
            Assert.True(newResponsibility.StatutoryResponsibilityTemplateCreatedFrom.Equals(_originalResponibility.StatutoryResponsibilityTemplateCreatedFrom));
            Assert.False(newResponsibility.Id.Equals(_originalResponibility.Id));
        }
        public void Given_ResponsibilityId_Found_For_Responsibility_With_Tasks_When_Copy_Responsibility_Then_Check_New_Responsibility_Created_With_Originals_Tasks()
        {
            // Given
            _target = GetTarget();

            _responsibilityRepo.Setup(x => x.GetByIdAndCompanyId(It.IsAny<long>(), It.IsAny<long>()))
               .Returns(_originalResponibility);

            var request = new CopyResponsibilityRequest()
            {
                CompanyId = _companyId,
                OriginalResponsibilityId = _originalResponsibilityId,
                Title = "new Title"
            };

            Responsibility newResponsibility = null;

            _responsibilityRepo.Setup(x => x.Save(It.IsAny<Responsibility>()))
                .Callback<Responsibility>(r => newResponsibility = r);

            // When
            _target.CopyResponsibility(request);

            Assert.True(newResponsibility.ResponsibilityTasks.Count.Equals(_originalResponibility.ResponsibilityTasks.Count));

        }
        public long CopyResponsibility(CopyResponsibilityRequest request)
        {
            _log.Add(request);

            try
            {
                var responsibility = _responsibilityRepository.GetByIdAndCompanyId(request.OriginalResponsibilityId , request.CompanyId);
                if (responsibility == null)
                {
                    throw (new ResponsibilityNotFoundException(request.OriginalResponsibilityId, request.CompanyId));
                }

                var creatingUser = _userForAuditingRepository.GetByIdAndCompanyId(request.UserId, request.CompanyId);
                var newResponsibility = responsibility.CopyWithoutSiteAndOwner(request.Title, creatingUser);
                _responsibilityRepository.Save(newResponsibility);
                return newResponsibility.Id;
            }
            catch (Exception ex)
            {
                _log.Add(ex);
                throw;
            }
        }