public ActionResult Edit([Bind(Include = "Id,ProjectId,TicketTypeId,TicketStatusId,TicketPriorityId,OwnerUserId,AssignedToUserId,Title,Description,Created,Updated")] Ticket ticket) { if (ModelState.IsValid) { //Go out to the database and get a copy of the ticket before it is changed var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticket.Id); db.Entry(ticket).State = EntityState.Modified; ticket.Updated = DateTime.Now; db.SaveChanges(); //Call the NotificationHelper to determine if a Notification needs to be created NotificationHelper.CreateAssignmentNotification(oldTicket, ticket); NotificationHelper.CreateChangeNotification(oldTicket, ticket); // NotificationHelper.ManageNotifications(oldTicket, ticket); //HistoryHelper HistoryHelper.CreateHistoryRecord(oldTicket, ticket); return(RedirectToAction("Index")); } ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "FirstName", ticket.AssignedToUserId); 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(ticket)); }
public async Task <ActionResult> AssignTicket(Ticket model) { var ticket = db.Tickets.Find(model.Id); ticket.AssignedToUserId = model.AssignedToUserId; var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticket.Id); db.SaveChanges(); NotificationHelper.CreateAssignmentNotification(oldTicket, ticket); HistoryHelper.CreateHistoryRecord(oldTicket, ticket); var callbackUrl = Url.Action("Details", "Tickets", new { id = ticket.Id }, protocol: Request.Url.Scheme); try { EmailService ems = new EmailService(); IdentityMessage msg = new IdentityMessage(); ApplicationUser user = db.Users.Find(model.AssignedToUserId); msg.Body = "You have been assigned to a new Ticket." + Environment.NewLine + "please click the following link to view details" + "<a href=\"" + callbackUrl + "\">NEW TICKET</a>"; msg.Destination = user.Email; msg.Subject = "Invite to Household"; await ems.SendAsync(msg); } catch (Exception) { await Task.FromResult(0); } return(RedirectToAction("Index")); }
public async Task <ActionResult> DeleteConfirmed(int id) { // Now add the TicketHistory for the TicketComment Deletion TicketComment ticketComment = db.TicketComments.Find(id); HistoryHelper ticketHistory = new HistoryHelper(User.Identity.GetUserId()); ticketHistory.AddHistoryEvent(ticketComment.TicketId, "Comment Deleted", null, null); int ticketId = ticketComment.TicketId; var project = db.Projects.First(p => p.Tickets.Any(t => t.Id == ticketComment.TicketId)); if (User.IsInRole("Admin") || (User.IsInRole("ProjectManager") && project.Users.Any(u => u.Id == User.Identity.GetUserId())) || (User.IsInRole("Developer") && ticketComment.AuthorId == User.Identity.GetUserId()) || (User.IsInRole("Submitter") && ticketComment.AuthorId == User.Identity.GetUserId())) { db.TicketComments.Remove(ticketComment); db.SaveChanges(); // Now send Notification, if ticket.AssignedUserId != Current User Ticket ticket = db.Tickets.Find(ticketId); if (ticket.AssignedUserId != null && ticket.AssignedUserId != User.Identity.GetUserId()) { string assignedUserEmailAddress = db.Users.Find(ticket.AssignedUserId).Email; bool commentAdded = false; NotificationHelper notification = new NotificationHelper(User.Identity.GetUserId()); await notification.AddCommentNotification(ticket.Id, ticketComment.Body, commentAdded, assignedUserEmailAddress); } return(RedirectToAction("Details", "Tickets", new { id = ticketId })); } return(RedirectToAction("Details", "Tickets", new { id = ticketId })); }
public TicketController() { DbContext = new ApplicationDbContext(); bugTrackerHelper = new BugTrackerHelper(DbContext); notificationHelper = new NotificationHelper(DbContext); historyHelper = new HistoryHelper(DbContext); }
public ActionResult ArchiveP(int ticketId, string userId) { var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticketId); var ticket = db.Tickets.Find(ticketId); ticket.TicketStatusId = db.TicketStatuses.FirstOrDefault(t => t.Name == "Archived").Id; ticket.ArchivedById = userId; ticket.Updated = DateTime.Now; db.SaveChanges(); HistoryHelper.RecordHistory(oldTicket, ticket); db.SaveChanges(); return(RedirectToAction("Details", "Tickets", new { id = ticketId })); }
public async Task <ActionResult> DeleteConfirmed(int id) { TicketAttachment ticketAttachment = db.TicketAttachments.Find(id); Ticket ticket = db.Tickets.First(t => t.Id == ticketAttachment.TicketId); Project project = db.Projects.Find(ticket.ProjectId); if ((User.IsInRole("Admin") || (User.IsInRole("ProjectManager") && project.Users.Any(u => u.Id == User.Identity.GetUserId())) || (User.IsInRole("Developer") && ticket.AssignedUserId == User.Identity.GetUserId()) || (User.IsInRole("Submitter") && ticket.OwnerUserId == User.Identity.GetUserId())) && (ticket.IsArchived == false)) { // Now delete from database db.TicketAttachments.Remove(ticketAttachment); db.SaveChanges(); // Now delete the file from the repository var absPath = Server.MapPath(ticketAttachment.FileUrl); //att.SaveAs(Path.Combine(absPath, att.FileName)); try { System.IO.File.Delete(@absPath); } catch (System.IO.IOException e) { Console.WriteLine(e.Message); Console.ReadLine(); return(RedirectToAction("Details", "Tickets", new { id = ticket.Id })); } // Now add the TicketHistory for the TicketAttachment Deletion HistoryHelper ticketHistory = new HistoryHelper(User.Identity.GetUserId()); ticketHistory.AddHistoryEvent(ticketAttachment.TicketId, "Attachment Deleted", ticketAttachment.FileUrl, null); // Now send notification, if assignedUser != Current User if (ticket.AssignedUserId != null && ticket.AssignedUserId != User.Identity.GetUserId()) { List <string> attachments = new List <string>(); attachments.Add(ticketAttachment.FileName); string assignedUserEmailAddress = db.Users.Find(ticket.AssignedUserId).Email; bool attAdded = false; NotificationHelper notification = new NotificationHelper(User.Identity.GetUserId()); await notification.AddAttachmentNotification(ticket.Id, attachments, attAdded, assignedUserEmailAddress); } return(RedirectToAction("Details", "Tickets", new { id = ticket.Id })); } return(RedirectToAction("Details", "Tickets", new { id = ticket.Id })); }
public ActionResult Edit([Bind(Include = "Id,TicketTypeId,TicketStatusId,TicketPriorityId,AssignedToUserId,Title,Description, ProjectId")] Ticket ticket) { var status = db.TicketStatuses.FirstOrDefault(t => t.Name == "Assigned").Id; var status2 = db.TicketStatuses.FirstOrDefault(t => t.Name == "Unassigned").Id; if (ModelState.IsValid) { var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticket.Id); db.Tickets.Attach(ticket); db.Entry(ticket).Property(x => x.TicketStatusId).IsModified = true; db.Entry(ticket).Property(x => x.TicketTypeId).IsModified = true; db.Entry(ticket).Property(x => x.TicketPriorityId).IsModified = true; db.Entry(ticket).Property(x => x.Title).IsModified = true; db.Entry(ticket).Property(x => x.Description).IsModified = true; db.Entry(ticket).Property(x => x.ProjectId).IsModified = true; if (linkHelper.UserCanAssignTicket(ticket)) { db.Entry(ticket).Property(x => x.AssignedToUserId).IsModified = true; ticket.TicketStatusId = status; if (String.IsNullOrEmpty(ticket.AssignedToUserId)) { ticket.TicketStatusId = status2; } //ticket.OwnerUserId = User.Identity.GetUserId(); db.SaveChanges(); } ticket.Updated = DateTime.Now; db.SaveChanges(); NotificationHelper.ManageNotifications(oldTicket, ticket); HistoryHelper.RecordHistory(oldTicket, ticket); return(RedirectToAction("Details", "Tickets", new { id = ticket.Id })); } ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "FirstName", ticket.AssignedToUserId); 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(ticket)); }
public async Task <ActionResult> Create([Bind(Include = "Id,Body")] TicketComment ticketComment, int ticketId) { TicketComment comment = db.TicketComments.Find(ticketComment.Id); Ticket ticket = db.Tickets.Find(ticketId); Project project = db.Projects.Find(ticket.ProjectId); if ((User.IsInRole("Admin") || (User.IsInRole("ProjectManager") && project.Users.Any(u => u.Id == User.Identity.GetUserId())) || (User.IsInRole("Developer") && ticket.AssignedUserId == User.Identity.GetUserId()) || (User.IsInRole("Submitter") && ticket.OwnerUserId == User.Identity.GetUserId())) && (ticket.IsArchived == false)) { if (ModelState.IsValid) { // Now add the TicketHistory for the TicketComment Creation HistoryHelper ticketHistory = new HistoryHelper(User.Identity.GetUserId()); ticketHistory.AddHistoryEvent(ticket.Id, "Comment Created", null, ticketComment.Body); ticketComment.Created = DateTimeOffset.UtcNow; ticketComment.TicketId = ticketId; ticketComment.AuthorId = User.Identity.GetUserId(); db.TicketComments.Add(ticketComment); db.SaveChanges(); // Now send Notification, if ticket.AssignedUserId != Current User if (ticket.AssignedUserId != null && ticket.AssignedUserId != User.Identity.GetUserId()) { string assignedUserEmailAddress = db.Users.Find(ticket.AssignedUserId).Email; bool commentAdded = true; NotificationHelper notification = new NotificationHelper(User.Identity.GetUserId()); await notification.AddCommentNotification(ticket.Id, ticketComment.Body, commentAdded, assignedUserEmailAddress); } return(RedirectToAction("Details", "Tickets", new { id = ticketId })); } ViewBag.ticketId = ticketId; return(View(ticketComment)); //ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketComment.TicketId); } return(RedirectToAction("Details", "Tickets", new { id = ticketId })); }
public async Task <ActionResult> Edit([Bind(Include = "Id,Body,Created,TicketId,AuthorId")] TicketComment ticketComment) { Ticket ticket = db.Tickets.Find(ticketComment.TicketId); Project project = db.Projects.First(p => p.Id == ticket.ProjectId); if ((User.IsInRole("Admin") || (User.IsInRole("ProjectManager") && project.Users.Any(u => u.Id == User.Identity.GetUserId())) || (User.IsInRole("Developer") && ticketComment.AuthorId == User.Identity.GetUserId()) || (User.IsInRole("Submitter") && ticketComment.AuthorId == User.Identity.GetUserId())) && (ticket.IsArchived == false)) { if (ModelState.IsValid) { // Now add the TicketHistory for the TicketComment Edit TicketComment oldComment = db.TicketComments.AsNoTracking().First(t => t.Id == ticketComment.Id); HistoryHelper ticketHistory = new HistoryHelper(User.Identity.GetUserId()); ticketHistory.AddHistoryEvent(ticketComment.TicketId, "Comment Edited", oldComment.Body, ticketComment.Body); // Now send Notification, if ticket.AssignedUserId != Current User if (ticket.AssignedUserId != null && ticket.AssignedUserId != User.Identity.GetUserId()) { string assignedUserEmailAddress = db.Users.Find(ticket.AssignedUserId).Email; NotificationHelper notification = new NotificationHelper(User.Identity.GetUserId()); await notification.AddCommentEditedNotification(ticket.Id, oldComment.Body, ticketComment.Body, assignedUserEmailAddress); } db.Entry(ticketComment).State = EntityState.Modified; ticketComment.Updated = DateTime.Now; db.SaveChanges(); return(RedirectToAction("Details", "Tickets", new { id = ticket.Id })); } //ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketComment.TicketId); return(View(ticketComment)); } return(RedirectToAction("Details", "Tickets", new { id = ticket.Id })); }
public ActionResult Edit([Bind(Include = "Id,ProjectId,TicketTypeId,TicketStatusId,TicketPriorityId,OwnerUserId,AssignedToUserId,Title,Description,Created, Updated")] Ticket ticket) { if (ModelState.IsValid) { var oldTicket = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == ticket.Id); db.Tickets.Attach(ticket); db.Entry(ticket).Property(x => x.TicketStatusId).IsModified = true; db.Entry(ticket).Property(x => x.TicketTypeId).IsModified = true; db.Entry(ticket).Property(x => x.TicketPriorityId).IsModified = true; db.Entry(ticket).Property(x => x.Title).IsModified = true; db.Entry(ticket).Property(x => x.Description).IsModified = true; if (ticket.AssignedToUserId != null) { db.Entry(ticket).Property(x => x.AssignedToUserId).IsModified = true; } ticket.Updated = DateTime.Now; db.SaveChanges(); NotificationHelper.CreateAssignmentNotification(oldTicket, ticket); NotificationHelper.ManageNotifications(oldTicket, ticket); //HistoryHelper var historyHelper = new HistoryHelper(); historyHelper.RecordHistory(oldTicket, ticket); return(RedirectToAction("MyIndex")); } //new edit ends ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "FirstName", ticket.AssignedToUserId); 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.TicketStatus, "Id", "StatusName", ticket.TicketStatusId); ViewBag.TicketTypeId = new SelectList(db.TicketTypes, "Id", "Name", ticket.TicketTypeId); return(View(ticket)); }
public async Task <ActionResult> Assign([Bind(Include = "Id,AssignedToUserId,Created,OwnerUserId,Title,Description,ProjectId,TicketTypeId,TicketStatusId,TicketPrioritiesId")] Tickets tickets) { HistoryHelper historyHelper = new HistoryHelper(); StringBuilder updateMessage = new StringBuilder(); var oldTicketInfo = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == tickets.Id); if (oldTicketInfo.AssignedToUserId != tickets.AssignedToUserId && oldTicketInfo.AssignedToUserId != null) { var oldAssignedUser = db.Users.Find(oldTicketInfo.AssignedToUserId).FirstName + " " + db.Users.Find(tickets.AssignedToUserId).LastName; var newAssignedUser2 = db.Users.Find(tickets.AssignedToUserId).FirstName + " " + db.Users.Find(tickets.AssignedToUserId).LastName; historyHelper.AddHistory(tickets.Id, "Assigned User", oldAssignedUser, newAssignedUser2, User.Identity.GetUserId()); updateMessage.AppendFormat("Assigned User: {0}, ", newAssignedUser2); } if (oldTicketInfo.AssignedToUserId == null && oldTicketInfo.AssignedToUserId != tickets.AssignedToUserId) { var oldAssignedUser = "******"; var newAssignedUser3 = db.Users.Find(tickets.AssignedToUserId).FirstName + " " + db.Users.Find(tickets.AssignedToUserId).LastName; historyHelper.AddHistory(tickets.Id, "Assigned User", oldAssignedUser, newAssignedUser3, User.Identity.GetUserId()); updateMessage.AppendFormat("Assigned User: {0}, ", newAssignedUser3); } //Send Notification var developer = db.Users.Find(tickets.AssignedToUserId); if (developer != null && developer.Email != null) { var svc = new EmailService(); var msg = new IdentityMessage(); msg.Destination = developer.Email; msg.Subject = "Bug Tracker Update: " + tickets.Title + " Assigned"; msg.Body = ("The following Ticket ID: " + tickets.Id + " - " + tickets.Title + ": " + updateMessage); await svc.SendAsync(msg); } TicketHistories ticketHistory = new TicketHistories(); var old = TempData["oldVal"]; var owner = TempData["owner"]; var newAssignedUser = db.Users.Find(tickets.AssignedToUserId).FirstName; if (ModelState.IsValid) { ticketHistory.TicketId = tickets.Id; ticketHistory.Property = "Ticket Assigned"; ticketHistory.OldValue = old.ToString(); ticketHistory.NewValue = newAssignedUser; ticketHistory.UserId = tickets.OwnerUserId; ticketHistory.ChangeDate = tickets.Created; ticketHistory.Changed = true; db.TicketHistories.Add(ticketHistory); //db.SaveChanges(); tickets.Updated = DateTime.Now; tickets.OwnerUserId = owner.ToString(); tickets.TicketStatusId = 2; db.Entry(tickets).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "FirstName", tickets.AssignedToUserId); ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "FirstName", tickets.OwnerUserId); ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", tickets.ProjectId); ViewBag.TicketPrioritiesId = new SelectList(db.TicketPriorities, "Id", "Name", tickets.TicketPrioritiesId); ViewBag.TicketStatusId = new SelectList(db.TicketStatuses, "Id", "Name", tickets.TicketStatusId); ViewBag.TicketTypeId = new SelectList(db.TicketTypes, "Id", "Name", tickets.TicketTypeId); return(View(tickets)); }
public async Task <ActionResult> Edit([Bind(Include = "Id,Title,Description,Created,Updated,ProjectId,TicketTypeId,TicketPrioritiesId,TicketStatusId,OwnerUserID, AssignedToUserId")] Tickets tickets) { if (ModelState.IsValid) { //TicketHistories ticketHistory = new TicketHistories(); HistoryHelper historyHelper = new HistoryHelper(); StringBuilder updateMessage = new StringBuilder(); //Old vs new data var oldTicketInfo = db.Tickets.AsNoTracking().FirstOrDefault(t => t.Id == tickets.Id); if (oldTicketInfo.Title != tickets.Title) { historyHelper.AddHistory(tickets.Id, "Title", oldTicketInfo.Title, tickets.Title, User.Identity.GetUserId()); updateMessage.AppendFormat("Ticket Title: {0}, ", tickets.Title); } if (oldTicketInfo.Description != tickets.Description) { historyHelper.AddHistory(tickets.Id, "Description", oldTicketInfo.Description, tickets.Description, User.Identity.GetUserId()); updateMessage.AppendFormat("Description: {0}, ", tickets.Description); } if (oldTicketInfo.TicketTypeId != tickets.TicketTypeId) { var oldTicketType = db.TicketTypes.Find(oldTicketInfo.TicketTypeId).Name; var newTicketType = db.TicketTypes.Find(tickets.TicketTypeId).Name; historyHelper.AddHistory(tickets.Id, "Ticket Type", oldTicketType, newTicketType, User.Identity.GetUserId()); updateMessage.AppendFormat("Ticket Type: {0}, ", newTicketType); } if (oldTicketInfo.ProjectId != tickets.ProjectId) { var oldProject = db.Projects.Find(oldTicketInfo.ProjectId).Name; var newProject = db.Projects.Find(tickets.ProjectId).Name; historyHelper.AddHistory(tickets.Id, "Project Id", oldProject, newProject, User.Identity.GetUserId()); updateMessage.AppendFormat("Project: {0}, ", newProject); } if (oldTicketInfo.TicketPrioritiesId != tickets.TicketPrioritiesId) { var oldPriority = db.TicketPriorities.Find(oldTicketInfo.TicketPrioritiesId).Name; var newPriority = db.TicketPriorities.Find(tickets.TicketPrioritiesId).Name; historyHelper.AddHistory(tickets.Id, "Priority", oldPriority, newPriority, User.Identity.GetUserId()); updateMessage.AppendFormat("Ticket Priority: {0}, ", newPriority); } if (oldTicketInfo.TicketStatusId != tickets.TicketStatusId) { var oldStatus = db.TicketStatuses.Find(oldTicketInfo.TicketStatusId).Name; var newStatus = db.TicketStatuses.Find(tickets.TicketStatusId).Name; historyHelper.AddHistory(tickets.Id, "Ticket Status", oldStatus, newStatus, User.Identity.GetUserId()); updateMessage.AppendFormat("Ticket Status: {0}, ", newStatus); } if (oldTicketInfo.AssignedToUserId != tickets.AssignedToUserId && oldTicketInfo.AssignedToUserId != null) { var oldAssignedUser = db.Users.Find(oldTicketInfo.AssignedToUserId).FirstName + " " + db.Users.Find(tickets.AssignedToUserId).LastName; var newAssignedUser = db.Users.Find(tickets.AssignedToUserId).FirstName + " " + db.Users.Find(tickets.AssignedToUserId).LastName; historyHelper.AddHistory(tickets.Id, "Assigned User", oldAssignedUser, newAssignedUser, User.Identity.GetUserId()); updateMessage.AppendFormat("Assigned User: {0}, ", newAssignedUser); } if (oldTicketInfo.AssignedToUserId == null && oldTicketInfo.AssignedToUserId != tickets.AssignedToUserId) { var oldAssignedUser = "******"; var newAssignedUser = db.Users.Find(tickets.AssignedToUserId).FirstName + " " + db.Users.Find(tickets.AssignedToUserId).LastName; historyHelper.AddHistory(tickets.Id, "Assigned User", oldAssignedUser, newAssignedUser, User.Identity.GetUserId()); updateMessage.AppendFormat("Assigned User: {0}, ", newAssignedUser); } tickets.Updated = DateTime.Now; if (tickets.TicketStatusId == 1) { tickets.AssignedToUserId = null; } db.Entry(tickets).State = EntityState.Modified; db.SaveChanges(); //Send Notification var developer = db.Users.Find(tickets.AssignedToUserId); if (developer != null && developer.Email != null) { var svc = new EmailService(); var msg = new IdentityMessage(); msg.Destination = developer.Email; msg.Subject = "Bug Tracker Update: " + tickets.Title; msg.Body = ("The following changes have been made to Ticket ID: " + tickets.Id + " - " + tickets.Title + ": " + updateMessage); await svc.SendAsync(msg); } { return(RedirectToAction("Index")); } } ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "FirstName", tickets.AssignedToUserId); ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "FirstName", tickets.OwnerUserId); ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", tickets.ProjectId); ViewBag.TicketPriorityId = new SelectList(db.TicketPriorities, "Id", "Name", tickets.TicketPrioritiesId); ViewBag.TicketStatusId = new SelectList(db.TicketStatuses, "Id", "Name", tickets.TicketStatusId); ViewBag.TicketTypeId = new SelectList(db.TicketTypes, "Id", "Name", tickets.TicketTypeId); return(View(tickets)); }
public async Task <ActionResult> Create([Bind(Include = "Id,Description,FileUrl,Created,TicketId,AuthorId")] TicketAttachment ticketAttachment, int ticketId, ICollection <HttpPostedFileBase> files) { if (!db.Tickets.Any(t => t.Id == ticketId)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Ticket ticket = db.Tickets.First(t => t.Id == ticketId); Project project = db.Projects.First(p => p.Tickets.Any(x => x.Id == ticketId)); if ((User.IsInRole("Admin") || (User.IsInRole("ProjectManager") && project.Users.Any(u => u.Id == User.Identity.GetUserId())) || (User.IsInRole("Developer") && ticket.AssignedUserId == User.Identity.GetUserId()) || (User.IsInRole("Submitter") && ticket.OwnerUserId == User.Identity.GetUserId())) && (ticket.IsArchived == false)) { if (ModelState.IsValid) { TicketAttachment attachment = new TicketAttachment(); List <string> attachments = new List <string>(); foreach (var att in files) { if (att != null && att.ContentLength > 0) { // Process File Info and Save File var ext = Path.GetExtension(att.FileName).ToLower(); // Gets image's extension and then sets it to lower case var filePath = "Content/Media/Ticket Attachments/" + ticketId; var absPath = Server.MapPath("~/" + filePath); string newFileName = att.FileName; var num = 0; while (System.IO.File.Exists(Path.Combine(absPath, newFileName))) { //Sets "filename" back to the default value newFileName = Path.GetFileNameWithoutExtension(att.FileName); //Add's parentheses after the name with a number ex. filename(4) newFileName = string.Format(newFileName + "(" + ++num + ")" + ext); //Makes sure pPic gets updated with the new filename so it could check //attach = fileName + Path.GetExtension(doc.FileName); } attachments.Add(newFileName); att.SaveAs(Path.Combine(absPath, newFileName)); // Update attachment info and add to database attachment.FileUrl = "/" + filePath + "/" + newFileName; attachment.FileName = newFileName; attachment.AuthorId = User.Identity.GetUserId(); attachment.Created = DateTime.UtcNow; attachment.TicketId = ticketId; attachment.IconUrl = ""; switch (ext) { case ".png": attachment.IconUrl = "~/Content/Images/Icons/512px/png.png"; break; case ".jpg": attachment.IconUrl = "~/Content/Images/Icons/512px/jpg.png"; break; case ".jpeg": attachment.IconUrl = "~/Content/Images/Icons/512px/jpeg.png"; break; case ".gif": attachment.IconUrl = "~/Content/Images/Icons/512px/gif.png"; break; case ".bmp": attachment.IconUrl = "~/Content/Images/Icons/512px/bmp.png"; break; case ".jfif": attachment.IconUrl = "~/Content/Images/Icons/512px/jfif.png"; break; case ".tiff": attachment.IconUrl = "~/Content/Images/Icons/512px/tiff.png"; break; case ".doc": attachment.IconUrl = "~/Content/Images/Icons/512px/doc.png"; break; case ".docx": attachment.IconUrl = "~/Content/Images/Icons/512px/doc.png"; break; case ".xls": attachment.IconUrl = "~/Content/Images/Icons/512px/xls.png"; break; case ".xlsx": attachment.IconUrl = "~/Content/Images/Icons/512px/xls.png"; break; case ".pdf": attachment.IconUrl = "~/Content/Images/Icons/512px/pdf.png"; break; case ".txt": attachment.IconUrl = "~/Content/Images/Icons/512px/txt.png"; break; case ".htm": attachment.IconUrl = "~/Content/Images/Icons/512px/html.png"; break; case ".html": attachment.IconUrl = "~/Content/Images/Icons/512px/html.png"; break; case ".ppt": attachment.IconUrl = "~/Content/Images/Icons/512px/ppt.png"; break; case ".pptx": attachment.IconUrl = "~/Content/Images/Icons/512px/ppt.png"; break; case ".zip": attachment.IconUrl = "~/Content/Images/Icons/512px/zip.png"; break; case ".rar": attachment.IconUrl = "~/Content/Images/Icons/512px/rar.png"; break; case ".csv": attachment.IconUrl = "~/Content/Images/Icons/512px/csv.png"; break; case ".rtf": attachment.IconUrl = "~/Content/Images/Icons/512px/rtf.png"; break; case ".xml": attachment.IconUrl = "~/Content/Images/Icons/512px/xml.png"; break; case ".mp3": attachment.IconUrl = "~/Content/Images/Icons/512px/mp3.png"; break; case ".mp4": attachment.IconUrl = "~/Content/Images/Icons/512px/mp4.png"; break; case ".wav": attachment.IconUrl = "~/Content/Images/Icons/512px/wav.png"; break; case ".avi": attachment.IconUrl = "~/Content/Images/Icons/512px/avi.png"; break; case ".wmv": attachment.IconUrl = "~/Content/Images/Icons/512px/_blank.png"; break; case ".mpg": attachment.IconUrl = "~/Content/Images/Icons/512px/mpg.png"; break; case ".flv": attachment.IconUrl = "~/Content/Images/Icons/512px/flv.png"; break; case ".mov": attachment.IconUrl = "~/Content/Images/Icons/512px/mov.png"; break; case ".css": attachment.IconUrl = "~/Content/Images/Icons/512px/css.png"; break; case ".less": attachment.IconUrl = "~/Content/Images/Icons/512px/less.png"; break; case ".sass": attachment.IconUrl = "~/Content/Images/Icons/512px/sass.png"; break; case ".dat": attachment.IconUrl = "~/Content/Images/Icons/512px/dat.png"; break; case ".cpp": attachment.IconUrl = "~/Content/Images/Icons/512px/cpp.png"; break; case ".js": attachment.IconUrl = "~/Content/Images/Icons/512px/js.png"; break; case ".py": attachment.IconUrl = "~/Content/Images/Icons/512px/py.png"; break; case ".rb": attachment.IconUrl = "~/Content/Images/Icons/512px/rb.png"; break; case ".php": attachment.IconUrl = "~/Content/Images/Icons/512px/php.png"; break; case ".dwg": attachment.IconUrl = "~/Content/Images/Icons/512px/dwg.png"; break; default: attachment.IconUrl = "~/Content/Images/Icons/512px/_blank.png"; break; } db.TicketAttachments.Add(attachment); db.SaveChanges(); // Now add the TicketHistory for the TicketAttachment Creation HistoryHelper ticketHistory = new HistoryHelper(User.Identity.GetUserId()); ticketHistory.AddHistoryEvent(ticketAttachment.TicketId, "Attachment Added", null, attachment.FileUrl); } } // Now, send notification, if Ticket.AssignedUserId == Current User if (ticket.AssignedUserId != null && User.Identity.GetUserId() != ticket.AssignedUserId) { ApplicationUser assignedUser = db.Users.Find(ticket.AssignedUserId); string assignedUserEmailAddress = assignedUser.Email; bool attAdded = true; NotificationHelper notification = new NotificationHelper(User.Identity.GetUserId()); await notification.AddAttachmentNotification(ticket.Id, attachments, attAdded, assignedUserEmailAddress); } return(RedirectToAction("Details", "Tickets", new { id = ticketId })); } //ViewBag.AuthorId = new SelectList(db.Users, "Id", "FirstName", ticketAttachment.AuthorId); //ViewBag.TicketId = new SelectList(db.Tickets, "Id", "Title", ticketAttachment.TicketId); return(View(ticketAttachment)); } return(RedirectToAction("Details", "Tickets", new { id = ticketId })); }
public async Task <ActionResult> Edit([Bind(Include = "Id,Created,Updated,Title,Description,ProjectId,TicketTypeId,TicketPriorityId,TicketStatusId,OwnerUserId,AssignedUserId,IsArchived")] Ticket ticket) { var project = db.Projects.First(p => p.Id == ticket.ProjectId); if ((User.IsInRole("Admin") || (User.IsInRole("ProjectManager") && project.Users.Any(u => u.Id == User.Identity.GetUserId())) || (User.IsInRole("Developer") && project.Users.Any(u => u.Id == User.Identity.GetUserId())) || (User.IsInRole("Submitter") && ticket.OwnerUserId == User.Identity.GetUserId())) && (ticket.IsArchived == false)) { var userId = User.Identity.GetUserId(); if (ModelState.IsValid) { bool hasAssignedUserIdChanged = false; // Declaration of a two-dimensional String Array and a Counter // The array is set to the max possible size, which will likely not be used. // The arrayCounter is passed, along with the array, to the NotificationHelper to indicate the # of rows in the array string[,] arrChangedProperties = new string[7, 3]; int arrayCounter = 0; // Ticket History entries need to be created for each event // Determine which Property(s) were changed HistoryHelper ticketHistory = new HistoryHelper(User.Identity.GetUserId()); Ticket oldTicket = db.Tickets.AsNoTracking().First(t => t.Id == ticket.Id); if (oldTicket.Title != ticket.Title) { ticketHistory.AddHistoryEvent(ticket.Id, "Title", oldTicket.Title, ticket.Title); arrChangedProperties[arrayCounter, 0] = "Title"; arrChangedProperties[arrayCounter, 1] = oldTicket.Title; arrChangedProperties[arrayCounter, 2] = ticket.Title; arrayCounter++; } if (oldTicket.Description != ticket.Description) { ticketHistory.AddHistoryEvent(ticket.Id, "Description", oldTicket.Description, ticket.Description); arrChangedProperties[arrayCounter, 0] = "Description"; arrChangedProperties[arrayCounter, 1] = oldTicket.Description; arrChangedProperties[arrayCounter, 2] = ticket.Description; arrayCounter++; } if (oldTicket.TicketPriorityId != ticket.TicketPriorityId) { TicketPriority oldPriority = db.TicketPriorities.Find(oldTicket.TicketPriorityId); TicketPriority newPriority = db.TicketPriorities.Find(ticket.TicketPriorityId); ticketHistory.AddHistoryEvent(ticket.Id, "TicketPriorityId", oldPriority.Name, newPriority.Name); arrChangedProperties[arrayCounter, 0] = "Priority"; arrChangedProperties[arrayCounter, 1] = oldPriority.Name; arrChangedProperties[arrayCounter, 2] = newPriority.Name; arrayCounter++; } if (oldTicket.TicketStatusId != ticket.TicketStatusId) { TicketStatus oldStatus = db.TicketStatuses.Find(oldTicket.TicketStatusId); TicketStatus newStatus = db.TicketStatuses.Find(ticket.TicketStatusId); ticketHistory.AddHistoryEvent(ticket.Id, "TicketStatus", oldStatus.Name, newStatus.Name); arrChangedProperties[arrayCounter, 0] = "Status"; arrChangedProperties[arrayCounter, 1] = oldStatus.Name; arrChangedProperties[arrayCounter, 2] = newStatus.Name; arrayCounter++; } if (oldTicket.AssignedUserId != ticket.AssignedUserId) { ApplicationUser newUser = db.Users.Find(ticket.AssignedUserId); arrChangedProperties[arrayCounter, 0] = "Developer"; if (oldTicket.AssignedUserId == null) { ticketHistory.AddHistoryEvent(ticket.Id, "AssignedUserId", "null", newUser.FullName); arrChangedProperties[arrayCounter, 1] = "null"; } else { ApplicationUser oldUser = db.Users.Find(oldTicket.AssignedUserId); ticketHistory.AddHistoryEvent(ticket.Id, "AssignedUserId", oldUser.FullName, newUser.FullName); arrChangedProperties[arrayCounter, 1] = oldUser.FullName; } arrChangedProperties[arrayCounter, 2] = newUser.FullName; arrayCounter++; hasAssignedUserIdChanged = true; } if (oldTicket.TicketTypeId != ticket.TicketTypeId) { TicketType oldType = db.TicketTypes.Find(oldTicket.TicketTypeId); TicketType newType = db.TicketTypes.Find(ticket.TicketTypeId); ticketHistory.AddHistoryEvent(ticket.Id, "Ticket Type", oldType.Name, newType.Name); arrChangedProperties[arrayCounter, 0] = "Type"; arrChangedProperties[arrayCounter, 1] = oldType.Name; arrChangedProperties[arrayCounter, 2] = newType.Name; arrayCounter++; } if (oldTicket.IsArchived != ticket.IsArchived) { ticketHistory.AddHistoryEvent(ticket.Id, "Ticket Archived", oldTicket.IsArchived, ticket.IsArchived); //hasIsArchivedChanged = true; arrChangedProperties[arrayCounter, 0] = "Archived"; arrChangedProperties[arrayCounter, 1] = oldTicket.IsArchived.ToString(); arrChangedProperties[arrayCounter, 2] = ticket.IsArchived.ToString(); arrayCounter++; } //The ticket must be saved before calling the Notification ticket.Updated = DateTimeOffset.UtcNow; db.Entry(ticket).State = EntityState.Modified; db.SaveChanges(); //Call NotificationHelper NotificationHelper notification = new NotificationHelper(User.Identity.GetUserId()); string newAssignedUserEmailAddress = "temporary value"; if (ticket.AssignedUserId != null) { newAssignedUserEmailAddress = db.Users.Find(ticket.AssignedUserId).Email; } string oldAssignedUserEmailAddress = "temporary value"; if (oldTicket.AssignedUserId != null) { oldAssignedUserEmailAddress = db.Users.Find(oldTicket.AssignedUserId).Email; } if (User.Identity.GetUserId() == oldTicket.AssignedUserId) //If the Current User was the Assignee and... { if (hasAssignedUserIdChanged) //If the CurrentUser/oldAssignedUser has reassigned the ticket - Notification to newAssignedUser only { await notification.AddNotification(ticket.Id, arrChangedProperties, arrayCounter, newAssignedUserEmailAddress); } //If the Current User did not reassign the Ticket - no Notifications } else //If the Current User was NOT oldAssignedUser { if (User.Identity.GetUserId() == ticket.AssignedUserId) { //If the Current User is newAssignedUser - Notification to oldAssignedUser only //Assumes that the Assignee has changed since they are NOT the oldAssigneeId and ARE the newAssigneeId if (oldTicket.AssignedUserId != null) { await notification.AddNotification(ticket.Id, arrChangedProperties, arrayCounter, oldAssignedUserEmailAddress); } } else //Current User is neither the oldAssignedUser or the newAssignedUser { if (hasAssignedUserIdChanged) { if (oldTicket.AssignedUserId != null) { await notification.AddNotification(ticket.Id, arrChangedProperties, arrayCounter, oldAssignedUserEmailAddress); } await notification.AddNotification(ticket.Id, arrChangedProperties, arrayCounter, newAssignedUserEmailAddress); } else //Current User is neither assignee and the assignee has not changed. Ticket changes were made, however and the assigned Developer needs to be notified. { await notification.AddNotification(ticket.Id, arrChangedProperties, arrayCounter, newAssignedUserEmailAddress); } } } //My God, it is finally over!! return(RedirectToAction("Details", "Tickets", new { id = ticket.Id })); } ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Title", 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(ticket)); } return(RedirectToAction("Index")); }
public ActionResult Create([Bind(Include = "Id,Title,Description,ProjectId,TicketTypeId,TicketPriorityId")] Ticket ticket, ICollection <HttpPostedFileBase> files) { if (ModelState.IsValid) { ticket.Created = DateTimeOffset.UtcNow; ticket.OwnerUserId = User.Identity.GetUserId(); ticket.TicketStatusId = 1; ticket.IsArchived = false; db.Tickets.Add(ticket); db.SaveChanges(); //Now create a folder for any attachments to be added var attPath = "~/Content/Media/Ticket Attachments/" + ticket.Id; var newPath = Server.MapPath(attPath); Directory.CreateDirectory(newPath); // Now save the attachments TicketAttachment attachment = new TicketAttachment(); foreach (var att in files) { if (att != null && att.ContentLength > 0) { // Process File Info and Save File var ext = Path.GetExtension(att.FileName).ToLower(); // Gets image's extension and then sets it to lower case var filePath = "Content/Media/Ticket Attachments/" + ticket.Id; var absPath = Server.MapPath("~/" + filePath); string newFileName = att.FileName; var num = 0; while (System.IO.File.Exists(Path.Combine(absPath, newFileName))) { //Sets "filename" back to the default value newFileName = Path.GetFileNameWithoutExtension(att.FileName); //Add's parentheses after the name with a number ex. filename(4) newFileName = string.Format(newFileName + "(" + ++num + ")" + ext); //Makes sure pPic gets updated with the new filename so it could check //attach = fileName + Path.GetExtension(doc.FileName); } att.SaveAs(Path.Combine(absPath, newFileName)); // Update attachment info and add to database attachment.FileUrl = "/" + filePath + "/" + newFileName; attachment.FileName = newFileName; attachment.AuthorId = User.Identity.GetUserId(); attachment.Created = DateTime.UtcNow; attachment.TicketId = ticket.Id; attachment.IconUrl = ""; switch (ext) { case ".png": attachment.IconUrl = "~/Content/Images/Icons/512px/png.png"; break; case ".jpg": attachment.IconUrl = "~/Content/Images/Icons/512px/jpg.png"; break; case ".jpeg": attachment.IconUrl = "~/Content/Images/Icons/512px/jpeg.png"; break; case ".gif": attachment.IconUrl = "~/Content/Images/Icons/512px/gif.png"; break; case ".bmp": attachment.IconUrl = "~/Content/Images/Icons/512px/bmp.png"; break; case ".jfif": attachment.IconUrl = "~/Content/Images/Icons/512px/jfif.png"; break; case ".tiff": attachment.IconUrl = "~/Content/Images/Icons/512px/tiff.png"; break; case ".doc": attachment.IconUrl = "~/Content/Images/Icons/512px/doc.png"; break; case ".docx": attachment.IconUrl = "~/Content/Images/Icons/512px/doc.png"; break; case ".xls": attachment.IconUrl = "~/Content/Images/Icons/512px/xls.png"; break; case ".xlsx": attachment.IconUrl = "~/Content/Images/Icons/512px/xls.png"; break; case ".pdf": attachment.IconUrl = "~/Content/Images/Icons/512px/pdf.png"; break; case ".txt": attachment.IconUrl = "~/Content/Images/Icons/512px/txt.png"; break; case ".htm": attachment.IconUrl = "~/Content/Images/Icons/512px/html.png"; break; case ".html": attachment.IconUrl = "~/Content/Images/Icons/512px/html.png"; break; case ".ppt": attachment.IconUrl = "~/Content/Images/Icons/512px/ppt.png"; break; case ".pptx": attachment.IconUrl = "~/Content/Images/Icons/512px/ppt.png"; break; case ".zip": attachment.IconUrl = "~/Content/Images/Icons/512px/zip.png"; break; case ".rar": attachment.IconUrl = "~/Content/Images/Icons/512px/rar.png"; break; case ".csv": attachment.IconUrl = "~/Content/Images/Icons/512px/csv.png"; break; case ".rtf": attachment.IconUrl = "~/Content/Images/Icons/512px/rtf.png"; break; case ".xml": attachment.IconUrl = "~/Content/Images/Icons/512px/xml.png"; break; case ".mp3": attachment.IconUrl = "~/Content/Images/Icons/512px/mp3.png"; break; case ".mp4": attachment.IconUrl = "~/Content/Images/Icons/512px/mp4.png"; break; case ".wav": attachment.IconUrl = "~/Content/Images/Icons/512px/wav.png"; break; case ".avi": attachment.IconUrl = "~/Content/Images/Icons/512px/avi.png"; break; case ".wmv": attachment.IconUrl = "~/Content/Images/Icons/512px/_blank.png"; break; case ".mpg": attachment.IconUrl = "~/Content/Images/Icons/512px/mpg.png"; break; case ".flv": attachment.IconUrl = "~/Content/Images/Icons/512px/flv.png"; break; case ".mov": attachment.IconUrl = "~/Content/Images/Icons/512px/mov.png"; break; case ".css": attachment.IconUrl = "~/Content/Images/Icons/512px/css.png"; break; case ".less": attachment.IconUrl = "~/Content/Images/Icons/512px/less.png"; break; case ".sass": attachment.IconUrl = "~/Content/Images/Icons/512px/sass.png"; break; case ".dat": attachment.IconUrl = "~/Content/Images/Icons/512px/dat.png"; break; case ".cpp": attachment.IconUrl = "~/Content/Images/Icons/512px/cpp.png"; break; case ".js": attachment.IconUrl = "~/Content/Images/Icons/512px/js.png"; break; case ".py": attachment.IconUrl = "~/Content/Images/Icons/512px/py.png"; break; case ".rb": attachment.IconUrl = "~/Content/Images/Icons/512px/rb.png"; break; case ".php": attachment.IconUrl = "~/Content/Images/Icons/512px/php.png"; break; case ".dwg": attachment.IconUrl = "~/Content/Images/Icons/512px/dwg.png"; break; default: attachment.IconUrl = "~/Content/Images/Icons/512px/_blank.png"; break; } db.TicketAttachments.Add(attachment); db.SaveChanges(); } } // Now add the TicketHistory for the Ticket Creation HistoryHelper ticketHistory = new HistoryHelper(User.Identity.GetUserId()); ticketHistory.AddHistoryEvent(ticket.Id, "CreationDate", null, ticket.Created.ToString()); return(RedirectToAction("Index")); } List <Project> projects = db.Projects.ToList(); ViewBag.ProjectId = new SelectList(projects.Where(p => p.Users.Any(u => u.Id == User.Identity.GetUserId())).ToList(), "Id", "Title"); 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(ticket)); }