private async Task CreateHistory(Ticket ticket, string comment)
        {
            var history = new TicketHistory();
            var user    = await _context.Users.SingleOrDefaultAsync(u => u.Login == HttpContext.User.Identity.Name);

            history.DateOfChange = DateTime.Now;
            history.Action       = (Models.Action)(int) ticket.Status;
            history.UserID       = user.ID;
            history.TicketID     = ticket.ID;
            history.Comment      = comment;

            _context.Add(history);
            await _context.SaveChangesAsync();
        }
        // GET: TicketHistories/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TicketHistory ticketHistory = db.TicketHistories.Find(id);

            if (ticketHistory == null)
            {
                return(HttpNotFound());
            }
            return(View(ticketHistory));
        }
Beispiel #3
0
        public static void AnyAttachment(Ticket ticket, TicketAttachment attachment, bool add)
        {
            var newHistory = new TicketHistory
            {
                UserId       = HttpContext.Current.User.Identity.GetUserId(),
                Updated      = DateTime.Now,
                PropertyName = "Ticket Attachment",
                OldValue     = add? "": attachment.Title,
                NewValue     = add? attachment.Title : "",
            };

            ticket.TicketHistories.Add(newHistory);
            db.TicketHistories.Add(newHistory);
        }
Beispiel #4
0
        public TicketHistory CreateTicketHistory(string propertyName, string oldValue, string newValue, int ticketId)
        {
            var history = new TicketHistory
            {
                Changed      = DateTime.Now,
                TicketId     = ticketId,
                PropertyName = propertyName,
                OldValue     = oldValue,
                NewValue     = newValue,
                UserId       = HttpContext.Current.User.Identity.GetUserId()
            };

            return(history);
        }
Beispiel #5
0
        //
        //History ############################################################
        //

        //Assignment History
        public void AssignmentHistory(TicketPost oldTicket, TicketPost newTicket, DateTimeOffset ticketUpdatedTimeStamp, ApplicationUser ticketEditor, string propertyChanged)
        {
            var newHistory = new TicketHistory();

            newHistory.TicketId        = oldTicket.Id;
            newHistory.UpdatedByUserId = ticketEditor.Id;
            newHistory.PropertyChanged = propertyChanged;
            newHistory.UpdatedTime     = ticketUpdatedTimeStamp;
            newHistory.OldAndNewValues = propertyChanged + " was changed From: " +
                                         ((oldTicket.AssignedToUserID != null) ? GetDisplayName(oldTicket.AssignedToUserID) : "Unassigned")
                                         + "  To: " + GetDisplayName(newTicket.AssignedToUserID);
            db.TicketHistories.Add(newHistory);
            db.SaveChanges();
        }
Beispiel #6
0
        public void AddHistory(int ticketId, string updateProperty, string oldValue, string newValue, string userId)
        {
            ApplicationDbContext db            = new ApplicationDbContext();
            TicketHistory        ticketHistory = new TicketHistory();

            ticketHistory.TicketId = ticketId;
            ticketHistory.Property = updateProperty;
            ticketHistory.OldValue = oldValue;
            ticketHistory.NewValue = newValue;
            ticketHistory.UserId   = userId;
            ticketHistory.Changed  = DateTime.Now;
            db.TicketHistories.Add(ticketHistory);
            db.SaveChanges();
        }
        // GET: TicketHistories/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TicketHistory ticketHistory = TicketHistoryHelper.GetTicketHistory(id);

            if (ticketHistory == null)
            {
                return(HttpNotFound());
            }
            return(View(ticketHistory));
        }
Beispiel #8
0
        public void GenericHistory(TicketPost oldTicket, string oldPropertyName, string newPropertyName, DateTimeOffset ticketUpdatedTimeStamp, ApplicationUser ticketEditor, string propertyChanged)
        {
            var newHistory = new TicketHistory();

            newHistory.TicketId        = oldTicket.Id;
            newHistory.UpdatedByUserId = ticketEditor.Id;
            newHistory.PropertyChanged = propertyChanged;
            newHistory.UpdatedTime     = ticketUpdatedTimeStamp;
            newHistory.OldAndNewValues = propertyChanged + " was changed From: " +
                                         oldPropertyName
                                         + "  To: " + newPropertyName;
            db.TicketHistories.Add(newHistory);
            db.SaveChanges();
        }
Beispiel #9
0
        // GET: TicketHistories/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("Oops5", "Home", null));
            }
            TicketHistory ticketHistory = db.TicketHistories.Find(id);

            if (ticketHistory == null)
            {
                return(RedirectToAction("Oops5", "Home", null));
            }
            return(View(ticketHistory));
        }
Beispiel #10
0
        public void AddTicketHistory(Ticket oldTicket, Ticket newTicket)
        {
            //Each of these properties can trigger a history if they change
            var propList = new List <string>
            {
                "Title",
                "Description",
                "TicketTypeId",
                "TicketStatusId",
                "TicketPriorityId",
                "AssignedToUserId",
                "ProjectId"
            };

            //Write a for loop that loops through the properties of a Ticket
            foreach (var property in propList)
            {
                //Add an extra test for null property values
                if (newTicket.GetType().GetProperty(property).GetValue(newTicket) == null)
                {
                    newTicket.GetType().GetProperty(property).SetValue(newTicket, "");
                }

                if (oldTicket.GetType().GetProperty(property).GetValue(oldTicket) == null)
                {
                    oldTicket.GetType().GetProperty(property).SetValue(oldTicket, "");
                }
                //Having an issue with null property values...AssignToUserId
                var newValue = newTicket.GetType().GetProperty(property) == null ? "" : newTicket.GetType().GetProperty(property).GetValue(newTicket).ToString();
                var oldValue = oldTicket.GetType().GetProperty(property) == null ? "" : oldTicket.GetType().GetProperty(property).GetValue(oldTicket).ToString();

                if (newValue != oldValue)
                {
                    //Add TicketHistory...
                    var newTicketHistory = new TicketHistory();
                    newTicketHistory.UserId   = HttpContext.Current.User.Identity.GetUserId();
                    newTicketHistory.Changed  = DateTime.Now;
                    newTicketHistory.TicketId = newTicket.Id;

                    //Record Property name and Values
                    newTicketHistory.Property = property;
                    newTicketHistory.OldValue = oldValue;
                    newTicketHistory.NewValue = newValue;

                    db.TicketHistories.Add(newTicketHistory);
                    db.SaveChanges();
                }
            }
        }
Beispiel #11
0
        public void RecordHistoricalChanges(Ticket oldTicket, Ticket newTicket)
        {
            if (oldTicket.TicketStatusId != newTicket.TicketStatusId)
            {
                var newHistoryRecord = new TicketHistory
                {
                    Property = "TicketStatusId",
                    OldValue = oldTicket.TicketStatus.StatusName,
                    NewValue = newTicket.TicketStatus.StatusName,
                    Changed  = (DateTime)newTicket.Updated,
                    TicketId = newTicket.Id,
                    UserId   = HttpContext.Current.User.Identity.GetUserId()
                };

                db.TicketHistories.Add(newHistoryRecord);
            }

            if (oldTicket.TicketPriorityId != newTicket.TicketPriorityId)
            {
                var newHistoryRecord = new TicketHistory
                {
                    Property = "TicketStatusId",
                    OldValue = oldTicket.TicketPriority.PriorityName,
                    NewValue = newTicket.TicketPriority.PriorityName,
                    Changed  = (DateTime)newTicket.Updated,
                    TicketId = newTicket.Id,
                    UserId   = HttpContext.Current.User.Identity.GetUserId()
                };

                db.TicketHistories.Add(newHistoryRecord);
            }

            if (oldTicket.DeveloperId != newTicket.DeveloperId)
            {
                var newHistoryRecord = new TicketHistory
                {
                    Property = "DeveloperId",
                    OldValue = oldTicket.Developer == null ? "Unassigned" : oldTicket.Developer.FullName,
                    NewValue = newTicket.Developer == null ? "Unassigned" : newTicket.Developer.FullName,
                    Changed  = (DateTime)newTicket.Updated,
                    TicketId = newTicket.Id,
                    UserId   = HttpContext.Current.User.Identity.GetUserId()
                };

                db.TicketHistories.Add(newHistoryRecord);
            }

            db.SaveChanges();
        }
Beispiel #12
0
        public void MakeTicketHistories(Ticket ticket, string userId)
        {
            var history = new TicketHistory
            {
                TicketID = ticket.ID,
                Property = "Assigned User",
                OldValue = ticket.AssignedToUser.UserName,
                NewValue = "",
                Changed  = DateTime.Now,
                UserID   = userId
            };

            DbContext.TicketHistories.Add(history);
            DbContext.SaveChanges();
        }
        // GET: TicketHistories/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TicketHistory ticketHistory = db.TicketHistories.Find(id);

            if (ticketHistory == null)
            {
                return(HttpNotFound());
            }
            ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketHistory.TicketId);
            return(View(ticketHistory));
        }
Beispiel #14
0
        public static void Edit(int id, int ticketId, string property, string oldValue, string newValue, DateTime changed, string userId)
        {
            ApplicationDbContext db            = new ApplicationDbContext();
            TicketHistory        ticketHistory = GetTicketHistory(id);

            ticketHistory.TicketId        = ticketId;
            ticketHistory.Property        = property;
            ticketHistory.OldValue        = oldValue;
            ticketHistory.NewValue        = newValue;
            ticketHistory.Changed         = changed;
            ticketHistory.UserId          = userId;
            db.Entry(ticketHistory).State = EntityState.Modified;
            db.SaveChanges();
            db.Dispose();
        }
Beispiel #15
0
        public void AddTicketHistory(int ticketId, string propertyName, string oldValue, string newValue)
        {
            var newTicketHistory = new TicketHistory
            {
                Property    = propertyName,
                OldValue    = oldValue,
                NewValue    = newValue,
                ChangedDate = DateTime.Now,
                UserId      = HttpContext.Current.User.Identity.GetUserId(),
                TicketId    = ticketId
            };

            db.TicketHistories.Add(newTicketHistory);
            db.SaveChanges();
        }
Beispiel #16
0
        private void CreateHistory(Ticket newTicket, string oldValue, string newValue, string property)
        {
            var user    = newTicket.project.Users.FirstOrDefault();
            var history = new TicketHistory()
            {
                TicketId  = newTicket.Id,
                User      = user,
                ChangedOn = DateTime.Now,
                Property  = property,
                OldValue  = oldValue,
                NewValue  = newValue
            };

            db.TicketHistories.Add(history);
        }
        public ActionResult Create([Bind(Include = "Id,TicketId,Property,Changed,UserId")] TicketHistory ticketHistory)
        {
            var nHelper = new UserNotificationsHelper();

            ViewBag.Notifications = nHelper.filterNotifications(User.Identity.GetUserId());
            if (ModelState.IsValid)
            {
                db.TicketHistories.Add(ticketHistory);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketHistory.TicketId);
            return(View(ticketHistory));
        }
Beispiel #18
0
        public void AddHistoryEvent(int ticketId, string changedProperty, bool oldValue, bool newValue)
        {
            TicketHistory newHistory = new TicketHistory();
            string        userName   = db.Users.AsNoTracking().First(t => t.Id == _userId).FullName;

            newHistory.AuthorId   = _userId;
            newHistory.AuthorName = userName;
            newHistory.TicketId   = ticketId;
            newHistory.Property   = changedProperty;
            newHistory.OldValue   = oldValue.ToString();
            newHistory.NewValue   = newValue.ToString();
            newHistory.Created    = DateTimeOffset.UtcNow;
            db.TicketHistories.Add(newHistory);
            db.SaveChanges();
        }
Beispiel #19
0
        private void CreateTicketHistory(Ticket newTicket, string oldValue, string newValue, string property)
        {
            var userId  = HttpContext.Current.User.Identity.GetUserId();
            var history = new TicketHistory()
            {
                TicketId  = newTicket.Id,
                UserId    = userId,
                ChangedOn = DateTime.Now,
                Property  = property,
                OldValue  = oldValue,
                NewValue  = newValue
            };

            db.TicketHistories.Add(history);
        }
Beispiel #20
0
 public ActionResult Edit([Bind(Include = "Id,TicketId,UserId,Property,OldValue,NewValue,Changed")] TicketHistory ticketHistory)
 {
     if (ModelState.IsValid)
     {
         db.Entry(ticketHistory).State = EntityState.Modified;
         if (!roleHelper.IsUserDemo())
         {
             db.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     ViewBag.TicketId = new SelectList(db.Tickets, "Id", "OwnerUserId", ticketHistory.TicketId);
     ViewBag.UserId   = new SelectList(db.Users, "Id", "FirstName", ticketHistory.UserId);
     return(View(ticketHistory));
 }
Beispiel #21
0
        public ActionResult Edit([Bind(Include = "Id,Title,Description,ProjectId,TicketTypeId,TicketPriorityId,TicketStatusId,OwnerUserId,AssignedToUserId")] Ticket ticket)
        {
            if (ModelState.IsValid)
            {
                TicketHistory tHistory = new TicketHistory();
                //TicketHistory lastTicket = ticket.TicketHistories.LastOrDefault();
                //tHistory.OldValue = lastTicket.NewValue;
                //lastTicket.NewValue = ticket.Id;
                //tHistory.NewValue = null;

                Ticket newTicket = new Ticket();
                newTicket = ticket;
                Ticket lastTicket = new Ticket();
                lastTicket = db.Tickets.AsNoTracking().Single(t => t.Id == ticket.Id);

                // If the Assignment Changes in the Edit Create new Notification else create just history
                if (newTicket.AssignedToUserId != lastTicket.AssignedToUserId)
                {
                    TicketNotification ticketNotification = new TicketNotification();
                    ticketNotification.UserId        = ticket.AssignedToUserId;
                    ticketNotification.TicketId      = ticket.Id;
                    ticketNotification.TickedNew     = false;
                    ticketNotification.TicketChanged = true;
                    db.TicketNotifications.Add(ticketNotification);
                }

                tHistory          = addTicketHistories(newTicket, lastTicket);
                tHistory.TicketId = newTicket.Id;
                string currentUserId = User.Identity.GetUserId();
                //   tHistory.User = db.Users.FirstOrDefault(u => u.Id == currentUserId);
                tHistory.UserId = currentUserId;

                ticket.Updated = System.DateTime.Now;

                db.TicketHistories.Add(tHistory);
                db.SaveChanges();

                db.Entry(ticket).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.OwnerUserId      = new SelectList(db.Users, "Id", "FirstName", ticket.OwnerUserId);
            ViewBag.ProjectId        = new SelectList(db.Projects, "Id", "Name", ticket.ProjectId);
            ViewBag.TicketPriorityId = new SelectList(db.TicketPriorities, "Id", "Name", ticket.TicketPriorityId);
            ViewBag.TicketStatusId   = new SelectList(db.TicketStatuses, "Id", "Name", ticket.TicketStatusId);
            ViewBag.TicketTypeId     = new SelectList(db.TicketTypes, "Id", "Name", ticket.TicketTypeId);
            return(View("Edit"));
        }
        //For call from SaveChanges();
        public void CreatePropertyHistory(int ticketId, string property, string oldValue, string newValue)
        {
            int intOriginalValue = 0;
            int intCurrentValue  = 0;
            var oldValueIsNumber = int.TryParse(oldValue, out intOriginalValue);
            var newValueIsNumber = int.TryParse(newValue, out intCurrentValue);

            var ticketHistory = new TicketHistory()
            {
                TicketId = ticketId,
                Changed  = DateTime.Now,
                UserId   = HttpContext.Current.User.Identity.GetUserId()
            };

            if (property == "ProjectId")
            {
                ticketHistory.Property = "Project";
                ticketHistory.OldValue = DbContext.Projects.FirstOrDefault(p => p.Id == intOriginalValue).Name;
                ticketHistory.NewValue = DbContext.Projects.FirstOrDefault(p => p.Id == intCurrentValue).Name;
            }
            else if (property == "TicketTypeId")
            {
                ticketHistory.Property = "Type";
                ticketHistory.OldValue = DbContext.TicketTypes.FirstOrDefault(p => p.Id == intOriginalValue).Name;
                ticketHistory.NewValue = DbContext.TicketTypes.FirstOrDefault(p => p.Id == intCurrentValue).Name;
            }
            else if (property == "TicketStatusId")
            {
                ticketHistory.Property = "Status";
                ticketHistory.OldValue = DbContext.TicketStatuses.FirstOrDefault(p => p.Id == intOriginalValue).Name;
                ticketHistory.NewValue = DbContext.TicketStatuses.FirstOrDefault(p => p.Id == intCurrentValue).Name;
            }
            else if (property == "TicketPriorityId")
            {
                ticketHistory.Property = "Priority";
                ticketHistory.OldValue = DbContext.TicketPriorities.FirstOrDefault(p => p.Id == intOriginalValue).Name;
                ticketHistory.NewValue = DbContext.TicketPriorities.FirstOrDefault(p => p.Id == intCurrentValue).Name;
            }
            else
            {
                ticketHistory.OldValue = oldValue;
                ticketHistory.NewValue = newValue;
                ticketHistory.Property = property;
            }

            DbContext.TicketHistories.Add(ticketHistory);
            DbContext.SaveChanges();
        }
        public ActionResult AttachmentDeleteConfirmed(int id)
        {
            TicketAttachment attachments   = db.TicketAttachments.Find(id);
            TicketHistory    ticketHistory = new TicketHistory();


            ticketHistory.AuthorId = User.Identity.GetUserId();
            ticketHistory.Created  = DateTimeOffset.UtcNow;
            ticketHistory.TicketId = attachments.TicketId;
            ticketHistory.Property = "ATTACHMENT REMOVED";
            db.TicketHistories.Add(ticketHistory);
            db.TicketAttachments.Remove(attachments);
            db.SaveChanges();

            return(RedirectToAction("Details", new { id = attachments.TicketId }));
        }
        // GET: TicketHistories/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TicketHistory ticketHistory = TicketHistoryHelper.GetTicketHistory(id);

            if (ticketHistory == null)
            {
                return(HttpNotFound());
            }
            ViewBag.TicketId = new SelectList(TicketHelper.GetTickets(), "Id", "Title", ticketHistory.TicketId);
            ViewBag.UserId   = new SelectList(UserHelper.GetAllUsers(), "Id", "Email", ticketHistory.UserId);
            return(View(ticketHistory));
        }
Beispiel #25
0
 private void DeveloperUpdate(Ticket oldTicket, Ticket newTicket)
 {
     if (oldTicket.DeveloperId != newTicket.DeveloperId)
     {
         var history = new TicketHistory()
         {
             TicketId  = newTicket.Id,
             UserId    = HttpContext.Current.User.Identity.GetUserId(),
             Property  = "Developer",
             OldValue  = oldTicket.DeveloperId == null ? "No Developer Assigned" : oldTicket.Developer.FullName,
             NewValue  = newTicket.DeveloperId == null ? "No Developer Assigned" : newTicket.Developer.FullName,
             ChangedOn = DateTime.Now
         };
         db.TicketHistories.Add(history);
     }
 }
Beispiel #26
0
        public void AddHistory(int ticketnum, string propertyName, string oldval, string newval)
        {
            var user   = HttpContext.Current.User;
            var record = new TicketHistory
            {
                TicketId     = ticketnum,
                UserId       = user.Identity.GetUserId(),
                PropertyName = propertyName,
                OldValue     = oldval,
                NewValue     = newval,
                Changed      = DateTime.Now
            };

            db.TicketHistories.Add(record);
            db.SaveChanges();
        }
Beispiel #27
0
        public static void RecordDeveloperAssignment(Ticket origin, Ticket edit)
        {
            var ticketId = origin.Id;
            var history  = new TicketHistory
            {
                Property = "Developer Assignment",
                OldValue = OldDeveloperWord(origin),
                NewValue = edit.AssignedToUser.FullName,
                Changed  = DateTime.Now,
                UserId   = HttpContext.Current.User.Identity.GetUserId(),
                TicketId = ticketId
            };

            db.TicketHistories.Add(history);
            db.SaveChanges();
        }
Beispiel #28
0
 private void TypeUpdate(Ticket oldTicket, Ticket newTicket)
 {
     if (oldTicket.TicketTypeId != newTicket.TicketTypeId)
     {
         var history = new TicketHistory()
         {
             TicketId  = newTicket.Id,
             UserId    = HttpContext.Current.User.Identity.GetUserId(),
             Property  = "Type",
             OldValue  = oldTicket.TicketType.Name,
             NewValue  = newTicket.TicketType.Name,
             ChangedOn = DateTime.Now
         };
         db.TicketHistories.Add(history);
     }
 }
Beispiel #29
0
 public Ticket BookTicket(Trip trip, int noOfPassengers, string travelClass, out bool couponStatus)
 {
     if (trip != null)
     {
         Ticket ticket;
         if (trip.TravelClass.Contains(travelClass) && trip.Availability.Length > noOfPassengers)
         {
             ticket = new Ticket(trip, noOfPassengers, travelClass);
             TicketHistory.Add(ticket);
             couponStatus = CheckForCoupon(ticket);
             return(ticket);
         }
     }
     couponStatus = false;
     return(null);
 }
        public ActionResult Edit([Bind(Include = "Id,ProjectId,TicketTypeId,TicketStatusId,TicektPriorityId,SubmitterId,DeveloperId,Title,Description,Created,Updated,IsArchived")] Ticket ticket)
        {
            if (ModelState.IsValid)
            {
                //I want to use AsNoTracking() to get a Memento Ticket object
                var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticket.Id);

                ticket.Updated         = DateTime.Now;
                ticket.SubmitterId     = User.Identity.GetUserId();
                db.Entry(ticket).State = EntityState.Modified;
                db.SaveChanges();

                var newTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticket.Id);

                //Use the History Helper to create the appropriate records
                historyHelper.ManageHistoryRecordCreation(oldTicket, ticket);

                //Use the Notification Helper to create the appropriate records
                notificationHelper.ManageNotifications(oldTicket, newTicket);

                //Now I can compare the new Ticket, "ticket" to the old Ticket, "oldTicket"
                //for changes that need to be recorded in the History table
                if (oldTicket.Title != ticket.Title)
                {
                    var newHistoryRecord = new TicketHistory();

                    newHistoryRecord.Created  = (DateTime)ticket.Updated;
                    newHistoryRecord.UserId   = User.Identity.GetUserId();
                    newHistoryRecord.Property = "Title";
                    newHistoryRecord.OldValue = oldTicket.Title;
                    newHistoryRecord.NewValue = ticket.Title;
                    newHistoryRecord.TicketId = ticket.Id;

                    db.TicketHistories.Add(newHistoryRecord);
                    db.SaveChanges();

                    //Use the Notification Helper to create the appropriate records
                    notificationHelper.ManageNotifications(oldTicket, newTicket);
                }

                return(RedirectToAction("Index", "TicketHistories"));
            }
            ViewBag.DeveloperId = new SelectList(db.Users, "Id", "Email", ticket.DeveloperId);
            ViewBag.ProjectId   = new SelectList(db.Projects, "Id", "Name", ticket.ProjectId);
            ViewBag.SubmitterId = new SelectList(db.Users, "Id", "FirstName", ticket.SubmitterId);
            return(View(ticket));
        }
        public async Task<ActionResult> Edit([Bind(Include = "Id,ProjectId,Title,Description,Created,Updated,TicketPriorityId,TicketStatusId,TicketTypeId,AssignedToId, OwnedById")] Ticket ticket)
        {
            if (ModelState.IsValid)
            {
                var properties = new List<string>() { "Updated", "Description", "TicketTypeId","TicketStatusId" };
                var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticket.Id);

                var EditId = Guid.NewGuid().ToString();
                var UserId = User.Identity.GetUserId();


                if(User.IsInRole("Admin") || User.IsInRole("ProjectManager"))
                {
                 
                    //Change TicketStatus from New to Assigned Automagically
                    //if (ticket.TicketStatusId == (db.TicketStatus.First(ts=> ts.Name == "New").Id) && ticket.AssignedToId == null)
                    //{
                    //    ticket.TicketStatusId = db.TicketStatus.FirstOrDefault(ts=> ts.Name == "Open").Id;
                    //}

                    //Check TicketPriority
                    if (oldTicket.TicketPriorityId != ticket.TicketPriorityId)
                    {
                        var newUser = db.Users.Find(ticket.AssignedToId);

                        properties.Add("TicketPriorityId");

                        var ChangedPriority = new TicketHistory
                        {
                            TicketId = ticket.Id,
                            UserId = UserId,
                            Property = "Ticket Priority",
                            OldValue = db.TicketPriority.FirstOrDefault(p => p.Id == oldTicket.TicketPriorityId).Name,
                            NewValue = db.TicketPriority.FirstOrDefault(p => p.Id == ticket.TicketPriorityId).Name,
                            Changed = System.DateTimeOffset.Now,
                        };

                        db.TicketHistories.Add(ChangedPriority);

                        if (newUser != null)
                        {
                            var mailer = new EmailService();
                            var Notification = newUser != null ? new IdentityMessage()
                            {
                                Subject = "You have a new Notification",
                                Destination = newUser.Email,
                                Body = "The priority for your assigned ticket: " + ticket.Title + " has been changed.\n" + Environment.NewLine +
                                       " The new priority is: " + db.TicketPriority.FirstOrDefault(p => p.Id == ticket.TicketPriorityId).Name
                            } : null;

                            await mailer.SendAsync(Notification);
                        }
                    }

                    //Check if AssignedToId has changed
                    if (oldTicket.AssignedToId != ticket.AssignedToId)
                    {
                        
                        properties.Add("AssignedToId");
                        var newUser = db.Users.FirstOrDefault(u => u.Id == ticket.AssignedToId) ?? null;
                        var AssignedHistory = new TicketHistory
                        
                        {
                            TicketId = ticket.Id,
                            UserId = UserId,
                            Property = "Assigned To",
                            OldValue = (oldTicket.AssignedToId == null ? "Not Yet Assigned" : db.Users.FirstOrDefault(u => u.Id == oldTicket.AssignedToId).DisplayName),
                            NewValue = newUser != null ? newUser.DisplayName : "Unassigned",
                            Changed = System.DateTimeOffset.Now,
                        };

                        db.TicketHistories.Add(AssignedHistory);
                        
                        if (newUser != null)
                        {
                            var mailer = new EmailService();
                            var Notification = newUser != null ? new IdentityMessage()
                            {
                                Subject = "You have a new Notification",
                                Destination = newUser.Email,
                                Body = "You have been assigned the ticket:  " + ticket.Title + "!"
                            } : null;

                            
                            await mailer.SendAsync(Notification);
                        }

                    }

                    properties.AddRange(new string[] { "TicketPriorityId", "AssignedToId" });
                }


                //Check Description
                if (oldTicket.Description != ticket.Description)
                {

                    var newUser = db.Users.Find(ticket.AssignedToId);

                    var ChangedDescription = new TicketHistory
                    {
                        TicketId = ticket.Id,
                        UserId = UserId,
                        Property = "Description",
                        OldValue = oldTicket.Description,
                        NewValue = ticket.Description,
                        Changed = System.DateTimeOffset.Now,
                    };

                    db.TicketHistories.Add(ChangedDescription);

                    if (newUser != null)
                    {
                        var mailer = new EmailService();
                        var Notification = newUser != null ? new IdentityMessage()
                        {
                            Subject = "You have a new Notification",
                            Destination = newUser.Email,
                            Body = "The description for your assigned ticket: " + ticket.Title + " has been changed.  The new description is: " + ticket.Description
                        } : null;

                        
                        await mailer.SendAsync(Notification);
                    }

                }

                //Check TicketType
                if (oldTicket.TicketTypeId != ticket.TicketTypeId)
                {
                    
                    var ChangedType = new TicketHistory
                    {
                        TicketId = ticket.Id,
                        UserId = UserId,
                        Property = "Ticket Type",
                        OldValue = db.TicketType.FirstOrDefault(p => p.Id == oldTicket.TicketTypeId).Name,
                        NewValue = db.TicketType.FirstOrDefault(p => p.Id == ticket.TicketTypeId).Name,
                        Changed = System.DateTimeOffset.Now,
                    };
                    
                    db.TicketHistories.Add(ChangedType);

                }

                //Check TicketStatus
                if (oldTicket.TicketStatusId != ticket.TicketStatusId)
                {
                    var newUser = db.Users.Find(ticket.AssignedToId);
                    
                    properties.Add("TicketStatusId");
                    var ChangedStatus = new TicketHistory
                    {
                        TicketId = ticket.Id,
                        UserId = UserId,
                        Property = "Ticket Status",
                        OldValue = db.TicketStatus.FirstOrDefault(p => p.Id == oldTicket.TicketStatusId).Name,
                        NewValue = db.TicketStatus.FirstOrDefault(p => p.Id == ticket.TicketStatusId).Name,
                        Changed = System.DateTimeOffset.Now,
                    };

                    db.TicketHistories.Add(ChangedStatus);

                    if (newUser != null)
                    {
                        var mailer = new EmailService();
                        var Notification = newUser != null ? new IdentityMessage()
                        {
                            Subject = "You have a new Notification",
                            Destination = newUser.Email,
                            Body = "The priority for your assigned ticket: " + ticket.Title + " has been changed.\n" + Environment.NewLine +
                                   " The new priority is: " + db.TicketStatus.FirstOrDefault(p => p.Id == ticket.TicketStatusId).Name
                        } : null;


                        await mailer.SendAsync(Notification);
                    }

                }

                ticket.Updated = DateTimeOffset.Now;
                ticket.OwnedById = ticket.OwnedById;

                db.Update(ticket, properties.ToArray());
                await db.SaveChangesAsync();
                return RedirectToAction("Details", new { projectId = ticket.ProjectId, id = ticket.Id });
            }

            var devId = db.Roles.First(r => r.Name == "Developer").Id;

            ViewBag.AssignedToId = new SelectList(db.Users.Where(u => u.Roles.Any(r => r.RoleId == devId)), "Id", "FirstName", ticket.OwnedById);
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Title", ticket.ProjectId);
            ViewBag.TicketPriorityId = new SelectList(db.TicketPriority, "Id", "Name", ticket.TicketPriorityId);
            ViewBag.TicketStatusId = new SelectList(db.TicketStatus, "Id", "Name", ticket.TicketStatusId);
            ViewBag.TicketTypeId = new SelectList(db.TicketType, "Id", "Name", ticket.TicketTypeId);
            return View(ticket);
        }