Пример #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            req.Headers.TryGetValue("sessionToken", out StringValues sessionToken);
            User requester = SessionValidator.ValidateSession(sessionToken.ToString());

            if (requester == null || requester.UserType.UserTypeName != "Admin")
            {
                return(new StatusCodeResult(403));
            }
            List <string> errors      = new List <string>();
            string        requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            TeamIDModel   data;
            Team          team;

            try
            {
                data = JsonConvert.DeserializeObject <TeamIDModel>(requestBody);
            }
            catch
            {
                return(new BadRequestResult());
            }

            if (string.IsNullOrWhiteSpace(requestBody) || data == null)
            {
                return(new BadRequestResult());
            }

            using (DataContext dc = new DataContext())
            {
                Team t = dc.Team.Where(x => x.TeamID == data.TeamID).FirstOrDefault();
                if (t == null)
                {
                    errors.Add("Team does not exist");
                }

                if (errors.Count > 0)
                {
                    return(new BadRequestObjectResult(errors));
                }

                //Creates a list of users who are in the team we want to delete and removes them each from the team before it is deleted
                IEnumerable <User> UsersInTeam = dc.User.Where(x => x.Team.TeamID == t.TeamID);
                foreach (User u in UsersInTeam)
                {
                    NotificationHandler.SendNotification(new Notification {
                        Title = "Team Update", Body = "You have been removed from your team", User = u
                    }, dc);
                    u.Team = null;
                }

                if (t != null)
                {
                    User leader = dc.User.Where(x => x.UserID == t.LeaderID).FirstOrDefault();
                    if (leader != null)
                    {
                        UserType type = dc.UserTypes.Where(x => x.UserTypeName == "User").FirstOrDefault();
                        leader.UserType = type; //Changes usertype to basic when manager is removed from team
                    }
                }

                team = t;
                dc.Remove(team);
                dc.SaveChanges();
            }

            return(new OkResult());
        }
Пример #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            req.Headers.TryGetValue("sessionToken", out StringValues sessionToken);
            User requester = SessionValidator.ValidateSession(sessionToken.ToString());

            if (requester == null || (requester.UserType.UserTypeName != "Admin" && requester.UserType.UserTypeName != "Manager"))
            {
                return(new StatusCodeResult(403));
            }
            List <string> errors = new List <string>();

            string          requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            CreateTeamModel data;

            try
            {
                data = JsonConvert.DeserializeObject <CreateTeamModel>(requestBody);
            }
            catch
            {
                return(new BadRequestResult());
            }

            using (DataContext dc = new DataContext())
            {
                List <Notification> n = new List <Notification>();
                //Links to the database
                if (data.TeamID == null)
                {
                    return(new BadRequestResult());
                }

                Team original = dc.Team.Include(x => x.Users).Where(x => x.TeamID == data.TeamID).FirstOrDefault();
                if (original == null)
                {
                    errors.Add("Team cannot be found");
                    return(new BadRequestObjectResult(errors));
                }

                //Team Name Statement

                if (!string.IsNullOrWhiteSpace(data.TeamName))
                {
                    Team t = dc.Team.Where(x => x.TeamName == data.TeamName).FirstOrDefault();
                    if (t != null)
                    {
                        errors.Add("Team with that name already exists");
                    }
                    else
                    {
                        n.Add(new Notification {
                            Title = "Team Update", Body = "Your team name has been changed to " + data.TeamName
                        });
                        original.TeamName = data.TeamName;
                    }
                }

                //Project Name

                if (!string.IsNullOrWhiteSpace(data.ProjectName))
                {
                    original.ProjectName = data.ProjectName;
                    n.Add(new Notification {
                        Title = "Team Update", Body = "Your team's project has been changed to " + data.ProjectName
                    });
                }

                if (errors.Count > 0)
                {
                    return(new BadRequestObjectResult(errors));
                }

                foreach (User u in original.Users)
                {
                    if (n.Count == 1)
                    {
                        Notification no = n.First();
                        NotificationHandler.SendNotification(new Notification {
                            Title = no.Title, Body = no.Body, User = u
                        }, dc);
                    }
                    else if (n.Count > 1)
                    {
                        NotificationHandler.SendNotification(new Notification {
                            Title = "Team Update", Body = "Your team information has been updated", User = u
                        }, dc);
                    }
                }
                //Saves the changes.
                dc.SaveChanges();
            }
            return(new JsonResult(data));
        }
Пример #3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            req.Headers.TryGetValue("sessionToken", out StringValues sessionToken);
            User requester = SessionValidator.ValidateSession(sessionToken.ToString());

            if (requester == null || requester.UserType.UserTypeName != "Admin")
            {
                return(new StatusCodeResult(403));
            }
            List <string> errors      = new List <string>();
            string        requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            TeamIDModel   teamID;

            try
            {
                teamID = JsonConvert.DeserializeObject <TeamIDModel>(requestBody);
            }
            catch
            {
                return(new BadRequestResult());
            }

            using (DataContext dc = new DataContext())
            {
                //Links to the database
                User user = dc.User.Include(x => x.UserType).Where(x => x.Name == teamID.userName).FirstOrDefault();
                if (user == null)
                {
                    errors.Add("User does not exist");
                }

                Team team = dc.Team.Include(x => x.Users).Where(x => x.TeamID == teamID.TeamID).FirstOrDefault();
                if (team == null)
                {
                    errors.Add("Team does not exist");
                }

                if (errors.Count > 0)
                {
                    return(new BadRequestObjectResult(errors));
                }

                // Demote previous leader
                User previousLeader = dc.User.Include(x => x.UserType).Where(x => x.UserID == team.LeaderID).FirstOrDefault();
                if (previousLeader != null)
                {
                    if (previousLeader.UserType.UserTypeName != "Admin")
                    {
                        previousLeader.UserType = dc.UserTypes.Where(x => x.UserTypeName == "User").FirstOrDefault();
                    }
                    NotificationHandler.SendNotification(new Notification {
                        User = previousLeader, Title = "Team Update", Body = "You were removed as team leader from " + team.TeamName
                    }, dc);
                }

                if (user.Team != team)
                {
                    user.Team = team;
                }


                UserType t = dc.UserTypes.Where(x => x.UserTypeName == "Manager").FirstOrDefault();
                if (user.UserType.UserTypeName != "Admin")
                {
                    user.UserType = t; //Changes usertype to manager when made leader of a team
                }
                team.LeaderID = user.UserID;
                NotificationHandler.SendNotification(new Notification {
                    User = user, Title = "Team Update", Body = "You were made team leader of " + team.TeamName
                }, dc);

                foreach (User u in team.Users)
                {
                    if (u != user && u != previousLeader)
                    {
                        NotificationHandler.SendNotification(new Notification {
                            User = u, Title = "Team Update", Body = user.Name + " has been made your team leader"
                        }, dc);
                    }
                }

                dc.SaveChanges();

                return(new OkResult());
            }
        }
Пример #4
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            req.Headers.TryGetValue("sessionToken", out StringValues sessionToken);
            User requester = SessionValidator.ValidateSession(sessionToken.ToString());

            if (requester == null)
            {
                return(new StatusCodeResult(403));
            }
            string          requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            CreateUserModel data;

            try
            {
                data = JsonConvert.DeserializeObject <CreateUserModel>(requestBody);
            }
            catch
            {
                return(new BadRequestResult());
            }

            using (DataContext dc = new DataContext())
            {
                List <Notification> n = new List <Notification>();
                //Links to the database
                User original = dc.User.Include(x => x.MaritalStatus).Include(x => x.Role).Where(x => x.UserID == data.UserID).FirstOrDefault();
                if (original == null)
                {
                    return(new BadRequestResult());
                }

                if (requester.UserType.UserTypeName != "Admin" && requester.UserID != original.UserID)
                {
                    return(new StatusCodeResult(403));
                }

                //Each if statement checks if a new value has been inserted (IE, not null) then pushes for the change to be made.
                //Name Statement

                if (string.IsNullOrWhiteSpace(data.Name) == false && original.Name != data.Name)
                {
                    original.Name = data.Name;
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your name has been changed to " + data.Name.ToString()
                    });
                }

                //Mobile If statement

                if (data.Mobile != null && data.Mobile.Length < 13 && original.Mobile != data.Mobile)
                {
                    original.Mobile = data.Mobile;
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your mobile number has been changed to " + data.Mobile.ToString()
                    });
                }
                //DoB

                if (data.DateOfBirth != null && original.DateOfBirth != data.DateOfBirth)
                {
                    original.DateOfBirth = data.DateOfBirth;
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your date of birth has been changed to " + data.DateOfBirth.Value.ToString("dd/MM/yyyy")
                    });
                }

                //Role

                if (string.IsNullOrWhiteSpace(data.Role) == false && original.Role.Title != data.Role)
                {
                    Role r = dc.Roles.Where(x => x.Title == data.Role).FirstOrDefault();
                    if (r == null)
                    {
                        r = new Role {
                            Title = data.Role
                        };
                        dc.Roles.Add(r);
                        n.Add(new Notification {
                            User = original, Title = "Profile Update", Body = "Your role has been changed to " + data.Role.ToString()
                        });
                    }

                    original.Role = r;
                }

                //Address

                if (string.IsNullOrWhiteSpace(data.Address) == false && original.Address != data.Address)
                {
                    original.Address = data.Address;
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your address has been changed to " + data.Address
                    });
                }

                //Email

                if (string.IsNullOrWhiteSpace(data.Email) == false && original.Email != data.Email)
                {
                    original.Email = data.Email;
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your email has been changed to " + data.Email
                    });
                }

                //NoK1

                if (string.IsNullOrWhiteSpace(data.NextOfKin1) == false && original.NextOfKin1 != data.NextOfKin1)
                {
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your next of kin has been changed to " + data.NextOfKin1
                    });
                    original.NextOfKin1 = data.NextOfKin1;
                }

                //NoK2

                if (string.IsNullOrWhiteSpace(data.NextOfKin2) == false && original.NextOfKin2 != data.NextOfKin2)
                {
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your next of kin has been changed to " + data.NextOfKin2
                    });
                    original.NextOfKin2 = data.NextOfKin2;
                }

                //Marital Status

                if (string.IsNullOrWhiteSpace(data.MaritalStatus) == false)
                {
                    MaritalStatus m = dc.MaritalStatuses.Where(x => x.MaritalStatusName == data.MaritalStatus).FirstOrDefault();
                    if (m == null)
                    {
                        dc.MaritalStatuses.Add(new MaritalStatus {
                            MaritalStatusName = data.MaritalStatus
                        });
                    }

                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your marital status has been changed to " + data.MaritalStatus
                    });
                    original.MaritalStatus = m;
                }

                //Nationality

                if (string.IsNullOrWhiteSpace(data.Nationality) == false && original.Nationality != data.Nationality)
                {
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your nationality has been changed to " + data.Nationality
                    });
                    original.Nationality = data.Nationality;
                }

                //Visa Status

                if (string.IsNullOrWhiteSpace(data.VisaStatus) == false && original.VisaStatus != data.VisaStatus)
                {
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your visa status has been changed to " + data.VisaStatus
                    });
                    original.VisaStatus = data.VisaStatus;
                }

                //Gender

                if (string.IsNullOrWhiteSpace(data.Gender) == false && original.Gender != data.Gender)
                {
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your gender has been changed to " + data.Gender
                    });
                    original.Gender = data.Gender;
                }

                //Medical Status

                if (string.IsNullOrWhiteSpace(data.MedicalStatus) == false && original.MedicalStatus != data.MedicalStatus)
                {
                    n.Add(new Notification {
                        User = original, Title = "Profile Update", Body = "Your medical status has been changed to " + data.MedicalStatus
                    });
                    original.MedicalStatus = data.MedicalStatus;
                }

                //UserLanguages
                if (data.Languages != null)
                {
                    dc.UserLanguages.Include(x => x.Language).Where(x => x.User.UserID == data.UserID).ToList().ForEach(x => {
                        if (!data.Languages.Contains(x.Language.LanguageName))
                        {
                            dc.UserLanguages.Remove(x);
                        }
                    });

                    foreach (var language in data.Languages)
                    {
                        Language l = dc.Languages.Where(x => x.LanguageName == language).FirstOrDefault();
                        if (l == null)
                        {
                            l = new Language {
                                LanguageName = language
                            };
                            dc.Languages.Add(l);
                        }

                        UserLanguage ul = dc.UserLanguages.Include(x => x.Language).Include(x => x.User).Where(x => x.Language == l && x.User.UserID == data.UserID).FirstOrDefault();

                        if (ul == null)
                        {
                            n.Add(new Notification {
                                User = original, Title = "Profile Update", Body = "A new language has been added " + data.Languages.ToString()
                            });
                            dc.UserLanguages.Add(new UserLanguage {
                                Language = l, User = dc.User.Where(x => x.UserID == data.UserID).FirstOrDefault()
                            });
                        }
                    }
                }

                //User Skills
                if (data.Skills != null)
                {
                    dc.UserSkills.Include(x => x.Skill).Where(x => x.User.UserID == data.UserID).ToList().ForEach(x => {
                        if (!data.Skills.Contains(x.Skill.SkillName))
                        {
                            dc.UserSkills.Remove(x);
                        }
                    });

                    foreach (var skill in data.Skills)
                    {
                        Skill s = dc.Skills.Where(x => x.SkillName == skill).FirstOrDefault();
                        if (s == null)
                        {
                            s = new Skill {
                                SkillName = skill
                            };
                            dc.Skills.Add(s);
                        }

                        UserSkill us = dc.UserSkills.Include(x => x.Skill).Include(x => x.User).Where(x => x.Skill == s && x.User.UserID == data.UserID).FirstOrDefault();

                        if (us == null)
                        {
                            n.Add(new Notification {
                                User = original, Title = "Profile Update", Body = "A new skill has been added " + data.Skills.ToString()
                            });
                            dc.UserSkills.Add(new UserSkill {
                                Skill = s, User = dc.User.Where(x => x.UserID == data.UserID).FirstOrDefault()
                            });
                        }
                    }
                }

                original.DateTimeUpdated = DateTime.Now;
                if (n.Count == 1)
                {
                    NotificationHandler.SendNotification(n.First(), dc);
                }
                else if (n.Count > 1)
                {
                    NotificationHandler.SendNotification(new Notification {
                        Title = "Profile Update", Body = "Your profile has been udpated", User = original
                    }, dc);
                }
                //Saves the changes.
                dc.SaveChanges();
            }
            return(new JsonResult(data));
        }