Esempio n. 1
0
        public void MarkEmployeeAsDeleted(MarkEmployeeAsDeletedRequest request)
        {
            bool hasEmployeeGotOutstandingTasks = _taskService.HasEmployeeGotOutstandingTasks(request.EmployeeId, request.CompanyId);
            if (hasEmployeeGotOutstandingTasks)
            {
                throw new TryingToDeleteEmployeeWithOutstandingTasksException(request.EmployeeId);
            }

            var employee = _employeeRepository.GetByIdAndCompanyId(request.EmployeeId, request.CompanyId);
            var user = _userForAuditingRepository.GetByIdAndCompanyId(request.UserId, request.CompanyId);
            employee.MarkForDelete(user);
            _employeeRepository.SaveOrUpdate(employee);

            if (employee.User == null) return;
            var deleteUserInPeninsulaOnlineCommand = new Peninsula.Online.Messages.Commands.DeleteUser() {ActioningUserId = request.UserId, UserId = employee.User.Id};
            _bus.Send(deleteUserInPeninsulaOnlineCommand);
        }
        public void Given_valid_request_When_mark_employee_as_deleted_Then_should_send_command_to_PeninsulaOnline_to_delete_user()
        {

            // Given
            var request = new MarkEmployeeAsDeletedRequest
            {
                CompanyId = 1,
                EmployeeId = Guid.NewGuid(),
                UserId = Guid.NewGuid()
            };


            var target = CreateEmployeeService();
            var employee = new Employee();
            employee.User = new User() {Id = Guid.NewGuid()};

            _employeeRepository
                .Setup(x => x.GetByIdAndCompanyId(request.EmployeeId, request.CompanyId))
                .Returns(() => employee);


            _userRepository.Setup(x => x.GetByIdAndCompanyId(request.UserId, request.CompanyId)).Returns(new UserForAuditing { Id = request.UserId });

            _taskService
                .Setup(x => x.HasEmployeeGotOutstandingTasks(request.EmployeeId, request.CompanyId))
                .Returns(false);

            DeleteUser deleteUserCommand = null;
            _bus.Setup(x=>x.Send(It.IsAny<Object[]>()))
                .Callback<Object[]>(x => deleteUserCommand = (DeleteUser) x[0]);
                

            // When
            target.MarkEmployeeAsDeleted(request);

            var message = new Peninsula.Online.Messages.Commands.DeleteUser() {ActioningUserId = request.UserId, UserId = employee.User.Id};

            // Then
            _bus.Verify(x => x.Send(It.IsAny<DeleteUser>()),Times.Once());
            Assert.AreEqual(request.UserId,deleteUserCommand.ActioningUserId);
            Assert.AreEqual(employee.User.Id, deleteUserCommand.UserId);

        }