public ActionResult DeleteConfirmed(int id)
        {
            TicketNotify ticketNotify = db.TicketNotifies.Find(id);

            db.TicketNotifies.Remove(ticketNotify);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "Id,Body,IsRead,TicketId,UserId")] TicketNotify ticketNotify)
 {
     if (ModelState.IsValid)
     {
         db.Entry(ticketNotify).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketNotify.TicketId);
     ViewBag.UserId   = new SelectList(db.Users, "Id", "FirstName", ticketNotify.UserId);
     return(View(ticketNotify));
 }
        // GET: TicketNotify/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TicketNotify ticketNotify = db.TicketNotifies.Find(id);

            if (ticketNotify == null)
            {
                return(HttpNotFound());
            }
            return(View(ticketNotify));
        }
        // GET: TicketNotify/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TicketNotify ticketNotify = db.TicketNotifies.Find(id);

            if (ticketNotify == null)
            {
                return(HttpNotFound());
            }
            ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketNotify.TicketId);
            ViewBag.UserId   = new SelectList(db.Users, "Id", "FirstName", ticketNotify.UserId);
            return(View(ticketNotify));
        }
        private void AddUnassignmentNotification(Ticket oldTicket, Ticket newTicket)
        {
            var notification = new TicketNotify
            {
                TicketId   = newTicket.Id,
                IsRead     = false,
                UserId     = HttpContext.Current.User.Identity.GetUserId(),
                ReceiverId = oldTicket.AssignedToUserId,
                Created    = DateTime.Now,
                Body       = $"You have been unassigned from the Ticket titled '{newTicket.Title}'\n" +
                             $"with Id# '{newTicket.Id}' for Project: '{newTicket.Project.Name}'."
            };

            db.TicketNotifies.Add(notification);
            db.SaveChanges();
        }
        private void AddAssignmentNotification(Ticket oldTicket, Ticket newTicket)
        {
            var notification = new TicketNotify
            {
                TicketId   = newTicket.Id,
                IsRead     = false,
                UserId     = HttpContext.Current.User.Identity.GetUserId(),
                ReceiverId = newTicket.AssignedToUserId,
                Created    = DateTime.Now,
                Body       = $"You have been assigned to Ticket Id# '{newTicket.Id}', on Project: " +
                             $"'{newTicket.Project.Name}'.\nThe title is '{newTicket.Title}', and it was created on " +
                             $"'{newTicket.Created}'\nwith a priority of '{newTicket.TicketPriority.Name}'."
            };

            db.TicketNotifies.Add(notification);
            db.SaveChanges();
        }
        public async Task <ActionResult> Create([Bind(Include = "Id,Title,Body,ProjectId,OwnerId,PriorityId,TypeId,AssignedUserId,Type,Priority")] Tickets ticket)
        {
            if (ModelState.IsValid)
            {
                ticket.Created = DateTimeOffset.Now;
                ticket.OwnerId = User.Identity.GetUserId();

                ticket.StatusId       = db.TicketStatus.SingleOrDefault(s => s.Status == "Open").Id;
                ticket.AssignedUserId = User.Identity.GetUserId();
                ticket.OwnerId        = User.Identity.GetUserId();

                TicketHistory history = new TicketHistory();
                history.Date = ticket.Created;
                var historyBody = "Ticket created  Title: " + ticket.Title + " Body: " + ticket.Body + " Priority: " + ticket.Priority + " Type: " + ticket.Type;
                history.Body     = historyBody;
                history.Body     = historyBody.ToString();
                history.TicketId = ticket.Id;
                db.Tickets.Add(ticket);
                db.TicketHistory.Add(history);
                db.SaveChanges();


                var          user   = db.Users.Find(ticket.OwnerId);
                TicketNotify notify = new TicketNotify();
                notify.TicketId     = ticket.Id;
                notify.NotifyUserId = ticket.OwnerId;
                db.Notifications.Add(notify);
                UserRoleAssignHelper helper = new UserRoleAssignHelper();
                var manager = db.ProjectUsers.Where(p => p.ProjectId == ticket.ProjectId).ToList();
                foreach (var item in manager)
                {
                    if (helper.IsUserInRole(item.ProjectUserId, "ProjectManager"))
                    {
                        TicketNotify notify2 = new TicketNotify();
                        notify2.TicketId    = ticket.Id;
                        notify.NotifyUserId = item.ProjectUserId;
                        db.Notifications.Add(notify2);
                    }
                }
                var emails = db.Notifications.Where(t => t.TicketId == ticket.Id).Select(u => u.NotifyUser.Email).ToList();
                var apiKey = ConfigurationManager.AppSettings["SendGridAPIkey"];
                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(apiKey, from);

                    var transportWeb = new Web(credentials);

                    await transportWeb.DeliverAsync(myMessage);
                }
                return(RedirectToAction("Index"));
            }

            ViewBag.OwnerId    = new SelectList(db.Tickets, "Id", "Owner", ticket.OwnerId);
            ViewBag.ProjectId  = new SelectList(db.Projects, "Id", "Project", ticket.ProjectId);
            ViewBag.PriorityId = new SelectList(db.TicketPriorities, "Id", "Priority", ticket.Priority);
            ViewBag.TypeId     = new SelectList(db.TicketTypes, "Id", "Type", ticket.Type);
            return(View());
        }