Exemplo n.º 1
0
        public async Task <Unit> Handle(AssignJobPositionCommand request, CancellationToken cancellationToken)
        {
            var user = _userAccessor.GetUser();

            var userId = Guid.Parse(user.FindFirst(ClaimTypes.NameIdentifier).Value);

            var entity = await _context.DepartmentTeams.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException("Not exist an entity with this id", nameof(DepartmentTeam));
            }

            // Update many to many JobDepartmentTeams
            var currentJobs = await _context.JobDepartmentTeams.Where(x => x.DepartmentTeamId == request.Id)
                              .Select(x => x.JobPositionId).ToListAsync(cancellationToken);

            var addJobs    = request.JobPositionIds.Except(currentJobs).ToList();
            var removeJobs = currentJobs.Except(request.JobPositionIds).ToList();

            if (addJobs.Count != 0)
            {
                foreach (var item in addJobs)
                {
                    var teamJob = new JobDepartmentTeam {
                        DepartmentTeamId = request.Id, JobPositionId = item
                    };
                    _context.JobDepartmentTeams.Add(teamJob);
                }
            }

            if (removeJobs.Count != 0)
            {
                foreach (var item in removeJobs)
                {
                    var teamJob = _context.JobDepartmentTeams
                                  .First(x => x.DepartmentTeamId == request.Id && x.JobPositionId == item);

                    if (teamJob != null)
                    {
                        _context.JobDepartmentTeams.Remove(teamJob);
                    }
                }
            }



            _context.DepartmentTeams.Update(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemplo n.º 2
0
        public async Task <Unit> Handle(CreateJobPositionCommand request, CancellationToken cancellationToken)
        {
            var user   = _userAccessor.GetUser();
            var userId = Guid.Parse(user.FindFirst(ClaimTypes.NameIdentifier).Value);
            var entity = new JobPosition
            {
                Description  = request.Description,
                HourlySalary = request.HourlySalary,
                Abbreviation = request.Abbreviation
            };

            entity.CreateEnd(Guid.NewGuid(), request.Name, userId);

            if (request.Active)
            {
                entity.Activate();
            }
            else
            {
                entity.Deactivate();
            }

            if (request.DepartmentTeamId != null)
            {
                var jobTeam = new JobDepartmentTeam
                {
                    DepartmentTeamId = request.DepartmentTeamId.Value,
                    JobPositionId    = entity.Id
                };

                _context.JobDepartmentTeams.Add(jobTeam);
            }

            _context.JobPositions.Add(entity);

            await _mediator.Publish(new JobPositionCreated()
            {
                PrimaryEntityId   = entity.Id,
                PrimaryEntityName = entity.Name,
                UserName          = user.Identity.Name
            }, cancellationToken);


            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemplo n.º 3
0
        public async Task <Unit> Handle(UpdateDepartmentTeamCommand request, CancellationToken cancellationToken)
        {
            var user   = _userAccessor.GetUser();
            var userId = Guid.Parse(user.FindFirst(ClaimTypes.NameIdentifier).Value);

            var departmentTeam = _context.DepartmentTeams.Find(request.Id);

            departmentTeam.Name                 = request.Name;
            departmentTeam.Abbreviation         = request.Abbreviation;
            departmentTeam.Description          = request.Description;
            departmentTeam.DepartmentId         = request.DepartmentId;
            departmentTeam.DepartmentTeamLeadId = request.DepartmentTeamLeadId;

            departmentTeam.Department = await _context.Departments.FindAsync(departmentTeam.DepartmentId);

            if (request.Active)
            {
                departmentTeam.Activate();
            }
            else
            {
                departmentTeam.Deactivate();
            }

            _context.DepartmentTeams.Update(departmentTeam);

            List <Guid?> usersToAdd, usersToRemove;

            // Update members
            if (request.MembersIds != null)
            {
                var existingUsers = _context.UserDepartmentTeams.Where(x => x.DeparmentTeamId == request.Id).ToList();
                usersToAdd    = request.MembersIds.Except(existingUsers.Select(x => (Guid?)x.UserId)).ToList();
                usersToRemove = existingUsers.Select(x => (Guid?)x.UserId).Except(request.MembersIds).ToList();

                // Remove Users from team
                if (usersToRemove.Count != 0)
                {
                    foreach (var item in usersToRemove)
                    {
                        var removeFromTeam = _context.UserDepartmentTeams.First(d =>
                                                                                d.DeparmentTeamId == request.Id && d.UserId == item.Value);
                        if (removeFromTeam != null)
                        {
                            _context.UserDepartmentTeams.Remove(removeFromTeam);
                        }
                    }
                }

                // Add Users in team
                if (usersToAdd.Count != 0)
                {
                    foreach (var item in usersToAdd)
                    {
                        var addInTeam = new UserDepartmentTeam()
                        {
                            DeparmentTeamId = departmentTeam.Id,
                            UserId          = item.Value
                        };
                        _context.UserDepartmentTeams.Add(addInTeam);
                    }
                }
            }



            if (request.JobPositionIds != null)
            {
                // Remove jobs
                var oldJobsList = _context.JobDepartmentTeams.Where(x => x.DepartmentTeamId == request.Id).ToList();

                _context.JobDepartmentTeams.RemoveRange(oldJobsList);


                // Insert users

                foreach (var item in request.JobPositionIds)
                {
                    var jobs = new JobDepartmentTeam
                    {
                        DepartmentTeamId = departmentTeam.Id,
                        JobPositionId    = item.Value
                    };
                    _context.JobDepartmentTeams.Add(jobs);
                }
            }


            await _context.SaveChangesAsync(cancellationToken);

            // Notifications
            // -------------

            var members = _context.UserDepartmentTeams.Where(x => x.DeparmentTeamId == request.Id)
                          .Select(x => x.User.Email).ToList();

            var notification = new DepartmentTeamUpdated()
            {
                PrimaryEntityName = departmentTeam.Name,
                PrimaryEntityId   = departmentTeam.Id,
                GroupEntityName   = departmentTeam.Department.Name,
                GroupEntityId     = departmentTeam.Department.Id,
                UserName          = user.Identity.Name
            };

            if (members.Any())
            {
                notification.Recipients = members;
            }
            await _mediator.Publish(notification, cancellationToken);

            return(Unit.Value);
        }
Exemplo n.º 4
0
        public async Task <Unit> Handle(CreateDepartmentTeamCommand request, CancellationToken cancellationToken)
        {
            var user = _userAccessor.GetUser();

            var userId = Guid.Parse(user.FindFirst(ClaimTypes.NameIdentifier).Value);

            var departmentTeam = new DepartmentTeam
            {
                Name                 = request.Name,
                Abbreviation         = request.Abbreviation,
                DepartmentId         = request.DepartmentId,
                DepartmentTeamLeadId = request.DepartmentTeamLeadId,
                Description          = request.Description
            };

            departmentTeam.CreateEnd(Guid.NewGuid(), request.Name, userId);


            if (departmentTeam.DepartmentId != null)
            {
                departmentTeam.Department = await _context.Departments.FindAsync(departmentTeam.DepartmentId);
            }

            if (request.Active)
            {
                departmentTeam.Activate();
            }
            else
            {
                departmentTeam.Deactivate();
            }

            await _context.DepartmentTeams.AddAsync(departmentTeam, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            if (request.JobPositions != null)
            {
                var jobsList = new List <JobDepartmentTeam>();

                foreach (var item in request.JobPositions)
                {
                    var jobs = new JobDepartmentTeam
                    {
                        DepartmentTeamId = departmentTeam.Id,
                        JobPositionId    = item.Value
                    };
                    jobsList.Add(jobs);
                }
                await _context.JobDepartmentTeams.AddRangeAsync(jobsList, cancellationToken);
            }

            var userList = new List <UserDepartmentTeam>();

            if (request.Members != null)
            {
                foreach (var item in request.Members)
                {
                    if (item == null)
                    {
                        continue;
                    }
                    var members = new UserDepartmentTeam
                    {
                        DeparmentTeamId = departmentTeam.Id,
                        UserId          = item.Value
                    };
                    userList.Add(members);
                }
            }

            if (request.DepartmentTeamLeadId != null)
            {
                var checkLead = userList.FirstOrDefault(x => x.UserId == request.DepartmentTeamLeadId);

                if (checkLead == null)
                {
                    var memberTeam = new UserDepartmentTeam
                    {
                        UserId          = request.DepartmentTeamLeadId.Value,
                        DeparmentTeamId = departmentTeam.Id
                    };

                    userList.Add(memberTeam);
                }
            }

            _context.UserDepartmentTeams.AddRange(userList);
            await _context.SaveChangesAsync(cancellationToken);

            // Notifications
            // --------------

            var notification = new DepartmentTeamCreated()
            {
                PrimaryEntityName = departmentTeam.Name,
                PrimaryEntityId   = departmentTeam.Id,
                GroupEntityName   = departmentTeam.Department != null ? departmentTeam.Department.Name : "No Department",
                GroupEntityId     = departmentTeam.DepartmentId ?? Guid.NewGuid(),
                UserName          = user.Identity.Name
            };

            if (userList.Any())
            {
                notification.Recipients = userList.Select(x => x.User.Email).ToList();
            }
            await _mediator.Publish(notification, cancellationToken);

            return(Unit.Value);
        }