Exemplo n.º 1
0
        public Team GetById(int id, bool includeMembers = true)
        {
            IQueryable <DataSets.Team> query = _context.Teams
                                               .Include(k => k.TeamsProjects)
                                               .Include(k => k.AddedByUser)
                                               .Include(k => k.Supervisor)
                                               .Include(k => k.AssignedByUser);

            if (includeMembers)
            {
                query = query.Include(k => k.Members);
            }

            DataSets.Team dbTeam = query.FirstOrDefault(k => k.ID == id);

            if (dbTeam == null)
            {
                return(null);
            }

            Team pendingRecord = _mapper.Map <Team>(dbTeam);

            pendingRecord.MembersCount = dbTeam.Members?.Count;
            pendingRecord.HasProjects  = dbTeam.TeamsProjects?.Count > 0;

            return(pendingRecord);
        }
Exemplo n.º 2
0
        /// <summary>
        /// no commit is made to the db
        /// </summary>
        private async Task AddRemoveTeamsUsersFromContext(DataSets.Team dbTeam, List <string> userIds, string byUserId)
        {
            string notifyMessage = "your team has been changed, you are required to login again";

            int teamId = dbTeam.ID;

            //var dbTeam = _context.Teams.First(k => k.ID == teamId);

            //if (dbTeam == null)
            //{
            //    throw new ClientException("team not found");
            //}

            // get existing users under the team
            var existingUsersUnderTeam = _context.Users.Where(k => k.TeamId == teamId);

            // set null for users that are not in the list and where removed

            IQueryable <ApplicationUser> usersRemovedFromTeam = existingUsersUnderTeam.Where(k => !userIds.Contains(k.Id));

            foreach (ApplicationUser user in usersRemovedFromTeam)
            {
                user.TeamId           = null;
                user.SecurityStamp    = Guid.NewGuid().ToString("D");
                user.NotificationFlag = true;

                await observerHub.Clients.User(user.Id).SendAsync("SessionEnd", notifyMessage);

                await notificationMethods.Send(byUserId, user.Id, $"You have been removed from team \"{dbTeam.Name}\"", NotificationType.Important);
            }


            // remove all items in the model that are already in db
            userIds.RemoveAll(k => existingUsersUnderTeam.Any(u => u.Id == k));

            // update/change the rest's team id
            if (userIds.Count > 0)
            {
                await _context.Users.Where(k => userIds.Contains(k.Id)).ForEachAsync(k =>
                {
                    k.TeamId           = teamId;
                    k.NotificationFlag = true;
                    k.SecurityStamp    = Guid.NewGuid().ToString("D");
                });

                foreach (string userId in userIds)
                {
                    await observerHub.Clients.User(userId).SendAsync("SessionEnd", notifyMessage);

                    await notificationMethods.Send(byUserId, userId, $"You are assigned to team \"{dbTeam.Name}\"", NotificationType.Important);
                }
            }


            _context.SaveChanges();
        }
Exemplo n.º 3
0
        //public Superviser GetTeamSupervisorLogs(TeamSaveModel model)
        //{

        //}

        public async Task <Team> Save(TeamSaveModel model)
        {
            string supervisorId  = model.supervisorId;
            string assignedById  = model.GetAssignedByUserId();
            string addedByUserId = model.GetAddedByUserId();

            if (supervisorId == null || assignedById == null)
            {
                throw new Exception("supervisor and assigned by user are required");
            }

            if (model.userIds == null || model.userIds.Count == 0)
            {
                throw new ClientException("at least one team member is required");
            }

            if (model.id.HasValue)
            {
                // check if name already exist
                bool nameExist = _context.Teams.Any(k => k.ID != model.id.Value && k.Name == model.name);

                if (nameExist)
                {
                    throw new ClientException($"team exist under name {model.name}");
                }

                // # save team #

                // get team

                var dbTeam = _context.Teams.FirstOrDefault(k => k.ID == model.id.Value);

                if (dbTeam == null)
                {
                    throw new ClientException("record not found");
                }

                if (dbTeam.SupervisorId != supervisorId)
                {
                    // supervisor has changed
                    string removedSupervisorId = dbTeam.SupervisorId;
                    string addedSupervisorId   = supervisorId;

                    // end session and notify removed supervisor
                    await observerHub.Clients.User(removedSupervisorId).SendAsync("SessionEnd", $"You have been removed from supervising a team");

                    await notificationMethods.Send(assignedById, removedSupervisorId, $"You have been removed from supervising team \"{dbTeam.Name}\"", NotificationType.Important);

                    var dbRemovedSupervisor = _context.Users.First(k => k.Id == removedSupervisorId);

                    dbRemovedSupervisor.NotificationFlag = true;
                    dbRemovedSupervisor.SecurityStamp    = Guid.NewGuid().ToString("D");

                    // end session and notify new supervisor
                    await observerHub.Clients.User(addedSupervisorId).SendAsync("SessionEnd", "You have been assigned as a supervisor to a team");

                    await notificationMethods.Send(assignedById, addedSupervisorId, $"You have been assigned as a supervisor to team \"{dbTeam.Name}\"", NotificationType.Important);

                    var dbNewSupervisor = _context.Users.First(k => k.Id == addedSupervisorId);

                    dbNewSupervisor.NotificationFlag = true;
                    dbNewSupervisor.SecurityStamp    = Guid.NewGuid().ToString("D");

                    // move current to logs
                    _context.SupervisorLogs.Add(new DataSets.SupervisorLog()
                    {
                        TeamId           = dbTeam.ID,
                        UserId           = dbTeam.SupervisorId,
                        DateAssigned     = dbTeam.DateAssigned,
                        AssignedByUserId = dbTeam.AssignedByUserId,
                    });

                    // set new values
                    dbTeam.SupervisorId     = supervisorId;
                    dbTeam.DateAssigned     = DateTime.Now;
                    dbTeam.AssignedByUserId = assignedById;
                }

                // set model values
                dbTeam.Name = model.name;

                // add/remove team members
                await AddRemoveTeamsUsersFromContext(dbTeam, model.userIds, assignedById);


                // save changes
                _context.SaveChanges();

                // return the record
                return(GetById(dbTeam.ID));
            }
            else
            {
                // # new team #

                // check if name already exist
                bool nameExist = _context.Teams.Any(k => k.Name == model.name);

                // validation
                if (nameExist)
                {
                    throw new ClientException($"team exist under name {model.name}");
                }

                if (addedByUserId == null)
                {
                    throw new Exception("added user claim was not provided");
                }

                DataSets.Team dbTeam = new DataSets.Team()
                {
                    Name             = model.name,
                    AddedByUserId    = addedByUserId,
                    AssignedByUserId = assignedById,
                    DateAssigned     = DateTime.Now,
                    DateAdded        = DateTime.Now,
                    SupervisorId     = supervisorId,
                };

                // end session and notify new supervisor
                await observerHub.Clients.User(supervisorId).SendAsync("SessionEnd", "You have been assigned as a supervisor to a team");

                await notificationMethods.Send(addedByUserId, supervisorId, $"You have been assigned as a supervisor to team \"{dbTeam.Name}\"", NotificationType.Important);


                var dbSupervisor = _context.Users.First(k => k.Id == supervisorId);

                dbSupervisor.NotificationFlag = true;
                dbSupervisor.SecurityStamp    = Guid.NewGuid().ToString("D");

                // add the team
                _context.Teams.Add(dbTeam);

                // save changes
                _context.SaveChanges();

                // set team members
                await AddRemoveTeamsUsersFromContext(dbTeam, model.userIds, addedByUserId);

                // save changes on teamsteams
                _context.SaveChanges();

                return(GetById(dbTeam.ID));
            }
        }