public override int SaveChanges() { var modifiedEntities = ChangeTracker.Entries().Where(p => p.State == EntityState.Modified).ToList(); bool wasModifyed = false; Ticket ticket = new Ticket(); foreach (var change in modifiedEntities) { var entityName = change.Entity.GetType().Name; var primaryKey = Convert.ToInt32(GetPrimaryKeyValue(change).ToString()); ticket = Tickets.FirstOrDefault(t => t.Id == primaryKey); foreach (var prop in change.OriginalValues.PropertyNames) { if (Enum.IsDefined(typeof(PropertyNames), prop)) { var originalValue = "not assigned"; var currentValue = "not assigned"; if (change.OriginalValues[prop] != null) { originalValue = change.OriginalValues[prop].ToString(); } if (change.CurrentValues[prop] != null) { currentValue = change.CurrentValues[prop].ToString(); } if (originalValue != currentValue) { historyHelper = new HistoryHelper(Create()); historyHelper.CreatePropertyHistory(primaryKey, prop, originalValue, currentValue); wasModifyed = true; } } } notificationHelper = new NotificationHelper(Create()); if (wasModifyed) { var message = notificationHelper.CreateModificationNotification(ticket.Title); notificationHelper.SendNotification(ticket, message, false); } } return(base.SaveChanges()); }
public ActionResult Index() { var myAssignedTicket = new Tickets(); var user = db.Users.Find(User.Identity.GetUserId()); var ticket = db.Tickets.Where(u => u.OwnerUserId == user.Id); var assignedTickets = db.Tickets.Where(t => myAssignedTicket.AssignedToUserId == user.Id); if (User.IsInRole("Admin")) { ticket = db.Tickets; } else if (User.IsInRole("Project Manager") || User.IsInRole("Developer")) { var projects = user.Projects.ToList(); ticket = projects.SelectMany(p => p.Tickets).AsQueryable(); } else if (User.IsInRole("Submitter")) { ticket = db.Tickets.Where(t => t.OwnerUserId == user.Id); } return View(ticket.ToList()); }
public ActionResult Edit(Tickets tickets) { if (ModelState.IsValid) { var userId = User.Identity.GetUserId(); var changed = System.DateTimeOffset.Now; var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == tickets.Id); var AssignedUser = db.Users.Find(tickets.AssignedToUserId); EmailService es = new EmailService(); if (oldTicket.Title != tickets.Title) { var history1 = new TicketHistory(); history1.TicketsId = tickets.Id; history1.Property = "Title"; history1.OldValue = oldTicket.Title; history1.NewValue = tickets.Title; history1.Changed = changed; history1.UserId = userId; db.TicketHistories.Add(history1); } if (oldTicket.Description != tickets.Description) { var history2 = new TicketHistory(); history2.TicketsId = tickets.Id; history2.Property = "Description"; history2.OldValue = oldTicket.Description; history2.NewValue = tickets.Description; history2.Changed = changed; history2.UserId = userId; db.TicketHistories.Add(history2); } if (oldTicket.TicketPriorityId != tickets.TicketPriorityId) { var history3 = new TicketHistory(); history3.TicketsId = tickets.Id; history3.Property = "Priority"; history3.OldValue = oldTicket.TicketPriority.Name; history3.NewValue = db.TicketPriorities.Find(tickets.TicketPriorityId).Name; history3.Changed = changed; history3.UserId = userId; db.TicketHistories.Add(history3); IdentityMessage im3 = new IdentityMessage { Subject = "Ticket Priority Change", Body = tickets.Title + " had a priority change.", Destination = AssignedUser.Email }; es.SendAsync(im3); var notification3 = new TicketNotification(); notification3.TicketsId = tickets.Id; notification3.TimeStamp = changed; notification3.UserId = userId; notification3.Message = "Subject: " + im3.Subject + "Message: " + im3.Body; notification3.Message = im3.Subject; db.TicketNotification.Add(notification3); } if (oldTicket.TicketStatusId != tickets.TicketStatusId) { var history4 = new TicketHistory(); history4.TicketsId = tickets.Id; history4.Property = "Status"; history4.OldValue = oldTicket.TicketStatus.Name; history4.NewValue = db.TicketStatus.Find(tickets.TicketStatusId).Name; history4.Changed = changed; history4.UserId = userId; db.TicketHistories.Add(history4); IdentityMessage im4 = new IdentityMessage { Subject = "Ticket Status Change", Body = tickets.Title + " has had a status change.", Destination = AssignedUser.Email }; es.SendAsync(im4); var notification4 = new TicketNotification(); notification4.TicketsId = tickets.Id; notification4.TimeStamp = changed; notification4.UserId = userId; notification4.Message = "Subject: " + im4.Subject + "Message: " + im4.Body; notification4.Message = im4.Subject; db.TicketNotification.Add(notification4); } if (oldTicket.TicketTypeId != tickets.TicketTypeId) { var history5 = new TicketHistory(); history5.TicketsId = tickets.Id; history5.Property = "Type of Ticket"; history5.OldValue = oldTicket.TicketType.Name; history5.NewValue = db.TicketType.Find(tickets.TicketTypeId).Name; history5.Changed = changed; history5.UserId = userId; db.TicketHistories.Add(history5); } if (oldTicket.AssignedToUserId != tickets.AssignedToUserId) { var history6 = new TicketHistory(); history6.TicketsId = tickets.Id; history6.Property = "Assigned To"; history6.OldValue = oldTicket.AssignedToUser == null ? "" : oldTicket.AssignedToUser.FirstName; history6.NewValue = db.Users.Find(tickets.AssignedToUserId).FirstName; history6.Changed = changed; history6.UserId = userId; db.TicketHistories.Add(history6); IdentityMessage im6 = new IdentityMessage { Subject = "Ticket Assignment", Body = tickets.Title + " has been assigned to you.", Destination = AssignedUser.Email }; es.SendAsync(im6); var notification6 = new TicketNotification(); notification6.TicketsId = tickets.Id; notification6.TimeStamp = changed; notification6.UserId = userId; notification6.Message = "Subject: " + im6.Subject + "Message : " + im6.Body; db.TicketNotification.Add(notification6); } tickets.Updated = DateTimeOffset.Now; db.Tickets.Attach(tickets); db.Entry(tickets).Property(p => p.AssignedToUserId).IsModified = true; db.Entry(tickets).Property(p => p.Description).IsModified = true; db.Entry(tickets).Property(p => p.TicketStatusId).IsModified = true; db.Entry(tickets).Property(p => p.TicketTypeId).IsModified = true; db.Entry(tickets).Property(p => p.TicketPriorityId).IsModified = true; if(tickets.TicketStatusId == 1) { tickets.AssignedToUserId = null; } db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", tickets.ProjectId); ViewBag.TicketPriorityId = new SelectList(db.TicketPriorities, "Id", "Name", tickets.TicketPriorityId); ViewBag.TicketStatusId = new SelectList(db.TicketStatus, "Id", "Name", tickets.TicketStatusId); ViewBag.TicketTypeId = new SelectList(db.TicketType, "Id", "Name", tickets.TicketTypeId); ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "FirstName"); return RedirectToAction("Index"); }
public void CreateHistory(Tickets oldTicket, Tickets newTicket, string userId, string userName) { if (oldTicket.TicketTypeId != newTicket.TicketTypeId) { TicketHistory history = new TicketHistory(); history.TicketId = newTicket.Id; history.UserId = userId; history.UserName = userName; history.ChangedDate = System.DateTimeOffset.Now; history.OldValue = oldTicket.TicketType.TicketName; history.NewValue = getTicketName(newTicket.TicketTypeId); history.Property = "Ticket Type"; db.TicketHistory.Add(history); // Notification string notiReci = getAssignedUserEmail(newTicket.AssignedToUserId); string notiMessage = "Hello " + notiReci + ". Ticket Type has been changed for the following ticket <U>" + newTicket.Id + "</U>"; InitializeNoti(newTicket.Id, userId, notiReci, notiMessage); // SignalR var srUser = db.Users.Find(newTicket.AssignedToUserId); sr.SendNotifications(srUser.UserName, "Ticket Type Changed For Ticket Id : " + newTicket.Id + "."); } if (oldTicket.TicketStatusId != newTicket.TicketStatusId) { TicketHistory history1 = new TicketHistory(); history1.TicketId = newTicket.Id; history1.UserId = userId; history1.UserName = userName; history1.ChangedDate = System.DateTimeOffset.Now; history1.OldValue = oldTicket.TicketStatus.StatusName; history1.NewValue = getStatusName(newTicket.TicketStatusId); history1.Property = "Ticket Status"; db.TicketHistory.Add(history1); // Notification String notiReci = getAssignedUserEmail(newTicket.AssignedToUserId); string notiMessage = "Hello " + notiReci + ". Ticket Status has been changed for the following ticket <U>" + newTicket.Id + "</U>"; InitializeNoti(newTicket.Id, userId, notiReci, notiMessage); // SignalR var srUser = db.Users.Find(newTicket.AssignedToUserId); sr.SendNotifications(srUser.UserName, "Ticket Status Changed For Ticket Id : " + newTicket.Id + "."); } if (oldTicket.TicketPriorityId != newTicket.TicketPriorityId) { TicketHistory history2 = new TicketHistory(); history2.TicketId = newTicket.Id; history2.UserId = userId; history2.UserName = userName; history2.ChangedDate = System.DateTimeOffset.Now; history2.OldValue = oldTicket.TicketPriority.PriorityName; history2.NewValue = getPriorityName(newTicket.TicketPriorityId); history2.Property = "Ticket Priority"; db.TicketHistory.Add(history2); // Notification string notiReci = getAssignedUserEmail(newTicket.AssignedToUserId); string notiMessage = "Hello " + notiReci + ". Ticket Priority has been changed for the following ticket <U>" + newTicket.Id + "</U>"; InitializeNoti(newTicket.Id, userId, notiReci, notiMessage); // SignalR var srUser = db.Users.Find(newTicket.AssignedToUserId); sr.SendNotifications(srUser.UserName, "Ticket Priority Changed For Ticket Id : " + newTicket.Id + "."); } if (oldTicket.AssignedToUserId != newTicket.AssignedToUserId) { //string empt = ""; TicketHistory history3 = new TicketHistory(); history3.TicketId = newTicket.Id; history3.UserId = userId; history3.UserName = userName; history3.ChangedDate = System.DateTimeOffset.Now; if(oldTicket.AssignedToUserId == null) { history3.OldValue = "Unassigned"; } else { history3.OldValue = oldTicket.AssignedToUser.FirstName; } history3.NewValue = getAssignedUser(newTicket.AssignedToUserId); history3.Property = "Assigned To"; db.TicketHistory.Add(history3); //Notification string notiReci = getAssignedUserEmail(newTicket.AssignedToUserId); string notiMessage = "Hello " + notiReci + ". You have been assigned the following ticket <U>" + newTicket.Id + "</U>"; InitializeNoti(newTicket.Id,userId,notiReci,notiMessage); // SignalR var srUser = db.Users.Find(newTicket.AssignedToUserId); sr.SendNotifications(srUser.UserName, "You Have Been Assigned A New Ticket : " + newTicket.Id + "."); } db.SaveChanges(); }