public ActionResult EditTicket(EditTicketViewModel formData) { if (!ModelState.IsValid) { // Redirect back to the form with the inputed from data, since something is missing or incorrect. EditTicketViewModel viewModelTicket = TicketHelper.TicketDataByRole(formData, User, RoleManager, UserManager, DbContext); return(View(viewModelTicket)); } if (formData is null) { return(RedirectToDashError()); } var ticket = DbContext.Tickets.FirstOrDefault(tckt => tckt.Id == formData.Id); if (ticket is null) { return(RedirectToDashError()); } // Apply changes if (ProjectHelper.IsAdminOrManager(User)) { ticket.Title = formData.Title; ticket.Description = formData.Description; ticket.DateUpdated = DateTime.Now; ticket.ProjectId = formData.ProjectId; ticket.TicketStatusId = (int)formData.TicketStatusId; ticket.TicketPriorityId = formData.TicketPriorityId; ticket.TicketTypeId = formData.TicketTypeId; ticket.AssignedToId = formData.AssignedToId; } else if (ProjectHelper.IsDevOrSubmitter(User)) { ticket.Title = formData.Title; ticket.Description = formData.Description; ticket.DateUpdated = DateTime.Now; ticket.ProjectId = formData.ProjectId; ticket.TicketPriorityId = formData.TicketPriorityId; ticket.TicketTypeId = formData.TicketTypeId; } // Generate history var user = UserManager.FindById(User.Identity.GetUserId()); var ticketHistory = TicketHelper.GenerateTicketHistory(DbContext, ticket, user); if (ticketHistory != null) { ticket.TicketHistories.Add(ticketHistory); // Notify users (Only if there's a change...) TicketHelper.SendNotificationsToUsers(ticket, "Ticket changes"); // If a new dev has been assigned to this ticket, // >Remove the old dev from the mailing list, // >Add the new dev, // >Then notify the user they've been assigned to this ticket. if (TicketHelper.IsNewDevAssigned(ticketHistory)) { var history = ticketHistory.TicketHistoryDetails.FirstOrDefault(p => p.Property == "Dev Assigned"); var devUserOld = UserManager.Users.FirstOrDefault(p => p.DisplayName == history.OldValue); var devUserNew = UserManager.Users.FirstOrDefault(p => p.DisplayName == history.NewValue); TicketHelper.ManageDevNotificationAssignment(devUserOld, devUserNew, ticket); TicketHelper.NotifyUserByEmail(devUserNew, ticket.Title); } } // Save to db DbContext.SaveChanges(); // Return to details page of the ticket. return(RedirectToAction("DetailsTicket", "Ticket", new { ticketId = ticket.Id })); }