예제 #1
0
        public ActionResult Create([Bind(Include = "Id,Title,Description,Created,ProjectId,TicketTypeId,TicketPriorityId,TicketStatusId,OwnerId,AssignedId")] Ticket ticket, string Title, string Status, string Type, string Priority, string Project)
        {
            if (ModelState.IsValid)
            {
                var queryp = db.Priorities.AsQueryable();
                var querys = db.Status.AsQueryable();
                var querypr = db.Projects.AsQueryable();
                var queryty = db.Types.AsQueryable();
                if(!string.IsNullOrWhiteSpace(Priority))
                {
                    var queryt = queryp.Where(x => x.Priority == Priority).Select(y => y.Id).Single();
                    ticket.TicketPriorityId = queryt;
                }
                if(!string.IsNullOrWhiteSpace(Status))
                {
                    var queryt = querys.Where(x => x.Status == Status).Select(y => y.Id).Single();
                    ticket.TicketStatusId = queryt;
                }
                if(!string.IsNullOrWhiteSpace(Project))
                {
                    var queryt = querypr.Where(x => x.Project == Project).Select(y => y.Id).Single();
                    ticket.ProjectId = queryt;
                }
                if(!string.IsNullOrWhiteSpace(Type))
                {
                    var queryt = queryty.Where(x => x.Type == Type).Select(y => y.Id).Single();
                    ticket.TicketTypeId = queryt;
                }
                ticket.Title = Title;
                ticket.Created = System.DateTimeOffset.Now;
                ticket.OwnerId = User.Identity.GetUserId();

                TicketNotify note = new TicketNotify();
                note.TicketId = ticket.Id;
                note.NotifyUserId = ticket.OwnerId;
                db.Notifications.Add(note);

                UserRoleHelper helper = new UserRoleHelper();
                var manager = db.ProjectUsers.Where(x => x.ProjectId == ticket.ProjectId).ToList();
                foreach(var item in manager)
                {
                    if(helper.IsUserInRole(item.ProjectUserId, "ProjectManager"))
                    {
                        TicketNotify note2 = new TicketNotify();
                        note2.TicketId = ticket.Id;
                        note2.NotifyUserId = item.ProjectUserId;
                        db.Notifications.Add(note2);
                    }
                }

                var emails = db.Notifications.Where(x => x.TicketId == ticket.Id).Select(y => y.NotifyUser.Email).ToList();
                var username = ConfigurationManager.AppSettings["SendGridUserName"];
                var password = ConfigurationManager.AppSettings["SendGridPassword"];
                var from = ConfigurationManager.AppSettings["ContactEmail"];

                foreach (var email in emails)
                {
                    SendGridMessage myMessage = new SendGridMessage();
                    myMessage.AddTo(email);
                    myMessage.From = new MailAddress(from);
                    myMessage.Subject = "Notification for Ticket #" + ticket.Id;
                    myMessage.Html = "Ticket #" + ticket.Id + " has been created";
                    var credentials = new NetworkCredential(username, password);

                    var transportWeb = new Web(credentials);

                    transportWeb.DeliverAsync(myMessage);
                }

                db.Tickets.Add(ticket);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(ticket);
        }
예제 #2
0
        public ActionResult Edit([Bind(Include = "Id,Title,Description,Created,ProjectId,TicketTypeId,TicketPriorityId,TicketStatusId,OwnerId,AssignedId")] Ticket ticket, string Assigned, int ProjectIn)
        {
            bool sendemail = false;
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrWhiteSpace(Assigned))
                {
                    var user = db.Users.Where(x => x.UserName == Assigned).Single();
                    ticket.AssignedId = user.Id;
                }

                ticket.ProjectId = ProjectIn;
                var dbin = db.Tickets.Single(x => x.Id == ticket.Id);
                db.Entry(dbin).CurrentValues.SetValues(ticket);

                var cnames = db.Entry(dbin).CurrentValues.PropertyNames;
                foreach (var curr in cnames)
                {
                    var oldvalue = "";
                    var newvalue = "";
                    if (db.Entry(dbin).Property(curr).OriginalValue != null)
                    {
                        oldvalue = db.Entry(dbin).Property(curr).OriginalValue.ToString();
                    }
                    if (db.Entry(dbin).Property(curr).CurrentValue != null)
                    {
                        newvalue = db.Entry(dbin).Property(curr).CurrentValue.ToString();
                    }
                    if (oldvalue != newvalue)
                    {
                        TicketHistory hist = new TicketHistory();
                        hist.TicketFieldName = curr;
                        hist.OldValue = oldvalue;
                        hist.NewValue = newvalue;
                        hist.HistoryUserId = User.Identity.GetUserId();
                        hist.TicketId = ticket.Id;
                        hist.Changed = System.DateTimeOffset.Now;
                        db.Histories.Add(hist);
                        if(curr == "AssignedId" || curr == "TicketPriorityId" || curr == "TicketStatusId")
                        {
                            sendemail = true;
                        }
                    }
                }
                if (db.Entry(dbin).Property("AssignedId").CurrentValue != null)
                {
                    if(!db.Entry(dbin).Property("AssignedId").OriginalValue.ToString().Equals(db.Entry(dbin).Property("AssignedId").CurrentValue.ToString()))
                    {
                        TicketNotify note = new TicketNotify();
                        note.TicketId = ticket.Id;
                        note.NotifyUserId = ticket.AssignedId;
                        db.Notifications.Add(note);
                        db.SaveChanges();
                    }
                }
                if(sendemail)
                {
                    var emails = db.Notifications.Where(x => x.TicketId == ticket.Id).ToList();
                    var username = ConfigurationManager.AppSettings["SendGridUserName"];
                    var password = ConfigurationManager.AppSettings["SendGridPassword"];
                    var from = ConfigurationManager.AppSettings["ContactEmail"];

                    foreach (var email in emails)
                    {
                        SendGridMessage myMessage = new SendGridMessage();
                        myMessage.AddTo(email.NotifyUser.Email);
                        myMessage.From = new MailAddress(from);
                        myMessage.Subject = "Notification for Ticket #" + ticket.Id;
                        myMessage.Html = "Ticket #" + ticket.Id + " has been updated";
                        if (!string.IsNullOrWhiteSpace(ticket.AssignedId))
                        {
                            if (ticket.AssignedId == email.NotifyUserId)
                            {
                                myMessage.Html = "Ticket #" + ticket.Id + " has been assigned to you or updated";
                            }
                        }
                        var credentials = new NetworkCredential(username, password);

                        var transportWeb = new Web(credentials);

                        transportWeb.DeliverAsync(myMessage);
                    }
                }
                db.Entry(dbin).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.AssignedId = new SelectList(db.Users, "Id", "FirstName", ticket.AssignedId);
            ViewBag.OwnerId = new SelectList(db.Users, "Id", "FirstName", ticket.OwnerId);
            ViewBag.TicketPriorityId = new SelectList(db.Priorities, "Id", "Priority", ticket.TicketPriorityId);
            ViewBag.TicketStatusId = new SelectList(db.Status, "Id", "Status", ticket.TicketStatusId);
            ViewBag.TicketTypeId = new SelectList(db.Types, "Id", "Type", ticket.TicketTypeId);
            return View(ticket);
        }