public ActionResult AddTicketUsers(TicketUserViewModel ticketUser)
        {
            var applicationUserId        = User.Identity.GetUserId();
            var developerRole            = DbContext.Roles.FirstOrDefault(r => r.Name == "Developer").Id;
            var unassignedDeveloperUsers = DbContext.Users;
            var user = DbContext.Users.Where(u => u.Roles.Any(p => p.RoleId == developerRole))
                       .FirstOrDefault(p => p.Id == ticketUser.UserID);

            var ticket = DbContext.Tickets.FirstOrDefault(
                p => p.ID == ticketUser.ID);

            if (user == null)
            {
                return(RedirectToAction(nameof(ManageProjectUsersController.EditProjectUsers)));
            }

            var historyWriter = new CustomHelpers();

            historyWriter.MakeTicketHistories(ticket, user, applicationUserId);
            var message = new IdentityMessage
            {
                Destination = $"{user.Email}",
                Subject     = $"You've been assigned to a new a new ticket: {ticket.Title}",
                Body        = $"new ticket--- {ticket.Title}: {ticket.Description}"
            };
            var emailService = new EmailService();

            emailService.SendAsync(message);

            ticket.AssignedToUserID = user.Id;

            var ticketNotification = new TicketNotification
            {
                UserID   = user.Id,
                TicketID = ticket.ID
            };

            DbContext.TicketNotifications.Add(ticketNotification);

            DbContext.SaveChanges();
            return(RedirectToAction("EditTicketUsers", "ManageTicketUsers", new { id = ticketUser.ID }));
        }
        public ActionResult RemoveTicketUsers(TicketUserViewModel ticketUser)
        {
            var applicationUserId = User.Identity.GetUserId();
            var user = DbContext.Users.FirstOrDefault(
                p => p.Id == ticketUser.UserID);

            var ticket = DbContext.Tickets.FirstOrDefault(
                p => p.ID == ticketUser.ID);

            if (user == null)
            {
                return(RedirectToAction(nameof(ManageProjectUsersController.EditProjectUsers)));
            }

            var historyWriter = new CustomHelpers();

            historyWriter.MakeTicketHistories(ticket, applicationUserId);

            ticket.AssignedToUserID = null;

            DbContext.SaveChanges();
            return(RedirectToAction("EditTicketUsers", "ManageTicketUsers", new { id = ticketUser.ID }));
        }
Ejemplo n.º 3
0
        private ActionResult Save(int?id, EditTicketViewModel formData)
        {
            var project = DbContext.Projects
                          .FirstOrDefault(p => p.ID == formData.ProjectID);

            if (!ModelState.IsValid)
            {
                //return View(formData.ProjectID);
                return(RedirectToAction("FullProjectBySlug", "Project", new { slug = project.Slug }));
            }

            var userId = User.Identity.GetUserId();

            var ticketType = DbContext.TicketTypes.FirstOrDefault(t => t.Name == formData.Type);

            var ticketPriority = DbContext.TicketPriorities.FirstOrDefault(t => t.Name == formData.Priority);

            Ticket ticket;

            if (!id.HasValue)
            {
                ticket = new Ticket
                {
                    DateCreated = DateTime.Now,
                    OwnerUserID = userId,
                    Slug        = ProjectController.Slugify(formData.Title),
                    ProjectID   = formData.ProjectID
                };
                DbContext.Tickets.Add(ticket);
            }
            else
            {
                ticket = DbContext.Tickets.FirstOrDefault(
                    p => p.ID == id);

                if ((User.IsInRole("Admin") || User.IsInRole("Project Manager")) ||
                    (User.IsInRole("Developer") && userId == ticket.AssignedToUserID) ||
                    (User.IsInRole("Submitter") && userId == ticket.OwnerUserID))
                {
                    // continue
                }
                else
                {
                    return(RedirectToAction("FullProjectBySlug", "Project", new { slug = project.Slug }));
                }

                if (ticket == null)
                {
                    return(RedirectToAction(nameof(ProjectController.Index)));
                }

                ticket.DateUpdated = DateTime.Now;

                var historyWriter = new CustomHelpers();
                historyWriter.MakeTicketHistories(ticket, formData, userId);

                var notifyUsers = DbContext.TicketNotifications.Where(u => u.TicketID == ticket.ID).ToList();
                foreach (var notifyuser in notifyUsers)
                {
                    var    user  = DbContext.Users.FirstOrDefault(u => u.Id == notifyuser.UserID);
                    string alert = $"there has been a change to a ticket you are watching: {ticket.Title}";
                    if (user == ticket.AssignedToUser)
                    {
                        alert = $"there has been a change to a ticket assign to you: {ticket.Title}";
                    }
                    var message = new IdentityMessage
                    {
                        Destination = $"{user.Email}",
                        Subject     = $"{alert}",
                        Body        = $"{alert}"
                    };
                    var emailService = new EmailService();
                    emailService.SendAsync(message);
                }
            }

            ticket.Title       = formData.Title;
            ticket.Description = formData.Description;
            ticket.TypeID      = ticketType.ID;
            ticket.PriorityID  = ticketPriority.ID;
            ticket.StatusID    = DbContext.TicketStatuses.FirstOrDefault(t => t.Name == formData.Status).ID;

            DbContext.SaveChanges();

            return(RedirectToAction("FullProjectBySlug", "Project", new { slug = project.Slug }));
        }