Пример #1
0
        public List <string> ListProjectManagers(int?projectId)
        {
            var projectManagers         = new List <string>();
            var project                 = db.Projects.Find(projectId);
            var projectUsers            = project.Users.ToList();
            UserRoleAssignHelper helper = new UserRoleAssignHelper(db);

            foreach (var user in projectUsers)
            {
                if (helper.IsUserInRole(user.Id, "ProjectManager"))
                {
                    projectManagers.Add(user.Email);
                }
            }
            return(projectManagers);
        }
        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());
        }