public async Task <IActionResult> Create(MergeModel model) { var assignedBy = UserMgr.GetUserId(HttpContext.User); Bug bug = model.Bugs; bug.BugCreationTimeStamp = DateTime.Now; bug.AssignedById = Convert.ToInt32(assignedBy); _context.Add(bug); await _context.SaveChangesAsync(); var user = UserMgr.Users.FirstOrDefault(m => m.Id == bug.AssignedToId); var userAssignedBy = UserMgr.Users.FirstOrDefault(m => m.Id == bug.AssignedById); var role = await UserMgr.GetRolesAsync(user); var roleAssignedBy = await UserMgr.GetRolesAsync(userAssignedBy); //check if the user assigned to the bug is already in the bug history model var userAssigned = _context.BugAssignmentHistories.FirstOrDefault(m => m.AssignedToId == bug.AssignedToId && m.BugId == bug.BugId); var notification = new Notification { NotificationType = NotificationTypeEnum.BugAssigned, NotificationMessage = $"Hey {user.FirstName}, you've" + " been assigned a new Bug by " + $"({roleAssignedBy[0]}) {userAssignedBy.UserName}!", ReceipientId = bug.AssignedToId, SenderId = bug.AssignedById, BugId = bug.BugId }; _context.Add(notification); var bugHistory = new BugAssignmentHistory { BugId = bug.BugId, BugStatus = bug.BugStatus, AssignedToId = bug.AssignedToId, RoleName = role[0] }; if (userAssigned == null) { _context.Add(bugHistory); } else { _context.Update(bugHistory); } await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Bug")); }
public async Task <IActionResult> Edit(int id, Bug bug) { var userAssignedTo = await UserMgr.FindByIdAsync(bug.AssignedToId.ToString()); var currentUserId = UserMgr.GetUserId(HttpContext.User); var currentUser = await UserMgr.FindByIdAsync(currentUserId); var currentUserRole = await UserMgr.GetRolesAsync(currentUser); var roleAssignedTo = await UserMgr.GetRolesAsync(userAssignedTo); if (id != bug.BugId) { return(RedirectToAction("Error", "Home")); } /* * Depending on who the bug is assigned to, set the current user * to be the one assigned by the bug. */ if (await UserMgr.IsInRoleAsync(userAssignedTo, "Tech Lead")) { bug.AssignedById = Convert.ToInt32(currentUserId); } else if (await UserMgr.IsInRoleAsync(userAssignedTo, "Developer") && User.IsInRole("Tech Lead")) { bug.AssignedById = Convert.ToInt32(currentUserId); } else if (await UserMgr.IsInRoleAsync(userAssignedTo, "QA") && User.IsInRole("Tech Lead")) { bug.AssignedById = Convert.ToInt32(currentUserId); } /* * Don't send a notification if the current user is assigned * to the bug they are editing. */ if (currentUserId != bug.AssignedToId.ToString()) { var notification = new Notification { NotificationType = NotificationTypeEnum.BugUpdated, NotificationMessage = $"Hey {userAssignedTo.FirstName}, Bug" + $" #{bug.BugId} was just updated by " + $"({currentUserRole[0]}) {currentUser.UserName}!", ReceipientId = bug.AssignedToId, SenderId = currentUser.Id, BugId = bug.BugId }; _context.Add(notification); } /* * Check if the user has been added to the history table, * if not then create a new object. Otherwise just update the status. */ var bugAssignedHistory = _context.BugAssignmentHistories.FirstOrDefault(m => m.AssignedToId == bug.AssignedToId && m.BugId == bug.BugId); if (bugAssignedHistory == null) { var bugHistory = new BugAssignmentHistory { BugId = bug.BugId, BugStatus = bug.BugStatus, AssignedToId = bug.AssignedToId, RoleName = roleAssignedTo[0] }; _context.Add(bugHistory); } else { bugAssignedHistory.BugStatus = bug.BugStatus; _context.Update(bugAssignedHistory); } _context.Bugs.Update(bug); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Bug")); }