public async Task <EnsureManagerHasTeamResponse> Execute(EnsureManagerHasTeamRequest request)
        {
            var team = await _teamRepository.GetByManagerIdAsync(request.ManagerId);

            if (team == null)
            {
                var manager = await _employeeRepository.GetByIdAsync(request.ManagerId);

                if (manager == null)
                {
                    throw new ManagerNotFoundException(request.ManagerId);
                }

                team = new Domain.Entity.LearningCalendar.Team
                {
                    Manager   = manager,
                    Employees = null
                };

                await _teamRepository.UpdateAsync(team);
            }

            return(new EnsureManagerHasTeamResponse
            {
                TeamId = team.Id,
                EmployeeIds = team.Employees?.Select(employee => employee.Id).ToList() ?? new List <Guid>(),
                ManagerId = team.Manager.Id
            });
        }
Пример #2
0
        public async Task Execute(ReassignEmployeeOperationRequest request)
        {
            if (request.EmployeeId == request.ManagerId)
            {
                throw new ApplicationException("An employee can't manage himself.");
            }

            bool isAuthorizedToReassign = await _authorizationContext.IsAuthorizedForEmployee(request.EmployeeId) &&
                                          await _authorizationContext.IsAuthorizedForEmployee(request.ManagerId);

            if (!isAuthorizedToReassign)
            {
                throw new ApplicationException($"Not authorized to reassign employee {request.EmployeeId}");
            }

            var employeeToAssign = await _employeeRepository.GetByIdAsync(request.EmployeeId);

            var myTeamTree = await _authorizationContext.GetTeamTree();

            var reassignedEmployeeTeamTree = myTeamTree
                                             .FindAnyOrDefault(
                team => team.Employees.Select(employee => employee.ManagedTeam),
                team => team.Manager.Id == employeeToAssign.Id);

            var subordinates = reassignedEmployeeTeamTree?.GetAllEmployees() ?? new List <Domain.Entity.LearningCalendar.Employee>();

            bool isNewManagerASubordinate = subordinates.Any(subordinate => subordinate.Id == request.ManagerId);

            if (isNewManagerASubordinate)
            {
                throw new ApplicationException("Can't reassign an employee to his subordinate");
            }

            var ensureNewManagerHasTeamRequest = new EnsureManagerHasTeamRequest
            {
                ManagerId = request.ManagerId
            };
            await _ensureManagerHasTeamOperation.Execute(ensureNewManagerHasTeamRequest);

            var newManager = await _employeeRepository.GetByIdAsync(request.ManagerId);

            employeeToAssign.Team = newManager.ManagedTeam;

            await _employeeRepository.UpdateAsync(employeeToAssign);
        }