public async Task ChangeTimeSheetTaskStatus(int timesheetId, int taskId, short statusCode, string byUserId) { var dbTask = db.ProjectTasks.FirstOrDefault(k => k.ID == taskId); if (dbTask == null) { throw new ClientException("record not found"); } bool hasChange = dbTask.StatusCode != statusCode; if (!hasChange) { return; } // append project's status modification db.ProjectTaskStatusModifications.Add(new DataSets.ProjectTaskStatusModification() { ProjectTaskId = dbTask.ID, StatusCode = dbTask.StatusCode, ModifiedByUserId = dbTask.StatusByUserId, DateModified = DateTime.Now }); // set new status by user dbTask.StatusCode = statusCode; dbTask.StatusByUserId = byUserId; db.SaveChanges(); // notify supervisor int?userTeamId = db.Users.Select(k => new { k.Id, k.TeamId }).FirstOrDefault(k => k.Id == byUserId)?.TeamId; if (userTeamId.HasValue) { // get supervisor of that team string supervisorId = db.Teams.Select(k => new { k.SupervisorId, k.ID }).First(k => k.ID == userTeamId.Value).SupervisorId; // timesheet id needed await notificationMethods.Send(byUserId, supervisorId, $"Task status changed to {((ProjectTaskStatus)statusCode).ToString()}", NotificationType.Default, true, timesheetId, null, taskId); } else { // teams that have the task's project List <int> teamsIds = db.TeamsProjects.Where(k => k.ProjectId == dbTask.ProjectId).Select(k => k.TeamId).ToList(); // send to supervisors foreach (var teamId in teamsIds) { string supervisorId = db.Teams.Select(k => new { k.SupervisorId, k.ID }).First(k => k.ID == teamId).SupervisorId; await notificationMethods.Send(byUserId, supervisorId, $"Task status changed to {((ProjectTaskStatus)statusCode).ToString()}", NotificationType.Default, true, timesheetId, null, taskId); } } }
private async Task NotifyAdmins(string byUserId, string message, int projectId, NotificationType notificationType = NotificationType.Information) { List <string> adminIds = db.Users.Where(k => k.Id != byUserId && k.RoleCode == (short)ApplicationUserRole.Admin).Select(k => k.Id).ToList(); foreach (string toUserId in adminIds) { await notificationMethods.Send(byUserId, toUserId, message, notificationType, true, null, projectId); } }
//public Superviser GetTeamSupervisorLogs(TeamSaveModel model) //{ //} public async Task <Team> Save(TeamSaveModel model) { string supervisorId = model.supervisorId; string assignedById = model.GetAssignedByUserId(); string addedByUserId = model.GetAddedByUserId(); if (supervisorId == null || assignedById == null) { throw new Exception("supervisor and assigned by user are required"); } if (model.userIds == null || model.userIds.Count == 0) { throw new ClientException("at least one team member is required"); } if (model.id.HasValue) { // check if name already exist bool nameExist = _context.Teams.Any(k => k.ID != model.id.Value && k.Name == model.name); if (nameExist) { throw new ClientException($"team exist under name {model.name}"); } // # save team # // get team var dbTeam = _context.Teams.FirstOrDefault(k => k.ID == model.id.Value); if (dbTeam == null) { throw new ClientException("record not found"); } if (dbTeam.SupervisorId != supervisorId) { // supervisor has changed string removedSupervisorId = dbTeam.SupervisorId; string addedSupervisorId = supervisorId; // end session and notify removed supervisor await observerHub.Clients.User(removedSupervisorId).SendAsync("SessionEnd", $"You have been removed from supervising a team"); await notificationMethods.Send(assignedById, removedSupervisorId, $"You have been removed from supervising team \"{dbTeam.Name}\"", NotificationType.Important); var dbRemovedSupervisor = _context.Users.First(k => k.Id == removedSupervisorId); dbRemovedSupervisor.NotificationFlag = true; dbRemovedSupervisor.SecurityStamp = Guid.NewGuid().ToString("D"); // end session and notify new supervisor await observerHub.Clients.User(addedSupervisorId).SendAsync("SessionEnd", "You have been assigned as a supervisor to a team"); await notificationMethods.Send(assignedById, addedSupervisorId, $"You have been assigned as a supervisor to team \"{dbTeam.Name}\"", NotificationType.Important); var dbNewSupervisor = _context.Users.First(k => k.Id == addedSupervisorId); dbNewSupervisor.NotificationFlag = true; dbNewSupervisor.SecurityStamp = Guid.NewGuid().ToString("D"); // move current to logs _context.SupervisorLogs.Add(new DataSets.SupervisorLog() { TeamId = dbTeam.ID, UserId = dbTeam.SupervisorId, DateAssigned = dbTeam.DateAssigned, AssignedByUserId = dbTeam.AssignedByUserId, }); // set new values dbTeam.SupervisorId = supervisorId; dbTeam.DateAssigned = DateTime.Now; dbTeam.AssignedByUserId = assignedById; } // set model values dbTeam.Name = model.name; // add/remove team members await AddRemoveTeamsUsersFromContext(dbTeam, model.userIds, assignedById); // save changes _context.SaveChanges(); // return the record return(GetById(dbTeam.ID)); } else { // # new team # // check if name already exist bool nameExist = _context.Teams.Any(k => k.Name == model.name); // validation if (nameExist) { throw new ClientException($"team exist under name {model.name}"); } if (addedByUserId == null) { throw new Exception("added user claim was not provided"); } DataSets.Team dbTeam = new DataSets.Team() { Name = model.name, AddedByUserId = addedByUserId, AssignedByUserId = assignedById, DateAssigned = DateTime.Now, DateAdded = DateTime.Now, SupervisorId = supervisorId, }; // end session and notify new supervisor await observerHub.Clients.User(supervisorId).SendAsync("SessionEnd", "You have been assigned as a supervisor to a team"); await notificationMethods.Send(addedByUserId, supervisorId, $"You have been assigned as a supervisor to team \"{dbTeam.Name}\"", NotificationType.Important); var dbSupervisor = _context.Users.First(k => k.Id == supervisorId); dbSupervisor.NotificationFlag = true; dbSupervisor.SecurityStamp = Guid.NewGuid().ToString("D"); // add the team _context.Teams.Add(dbTeam); // save changes _context.SaveChanges(); // set team members await AddRemoveTeamsUsersFromContext(dbTeam, model.userIds, addedByUserId); // save changes on teamsteams _context.SaveChanges(); return(GetById(dbTeam.ID)); } }
public async Task <TimeSheet> Save(TimeSheetSaveModel model) { IQueryable <DataSets.TimeSheet> conflictQuery = db.TimeSheets.Where(c => c.UserId == model.userId && ((model.fromDate >= c.FromDate && model.fromDate <= c.ToDate) || (model.toDate >= c.FromDate && model.toDate <= c.ToDate))); void ensureNoConflict(DataSets.TimeSheet ts) { if (ts != null) { throw new ClientException($"Dates {model.fromDate.ToShortDateString()}/{model.toDate.ToShortDateString()} Fall betweeen user's timesheet dates {ts.FromDate.ToShortDateString()}/{ts.ToDate.ToShortDateString()}"); } } if (model.fromDate >= model.toDate) { throw new ClientException("from date must be before to date"); } if (model.id.HasValue) { var possibleConflictTimeSheet = conflictQuery.Where(k => k.ID != model.id.Value).FirstOrDefault(); ensureNoConflict(possibleConflictTimeSheet); // # save timeSheet # // get timeSheet var dbTimeSheet = db.TimeSheets.FirstOrDefault(k => k.ID == model.id.Value); if (dbTimeSheet == null) { throw new ClientException("record not found"); } // check activities var minActivity = db.TimeSheetActivities .OrderBy(k => k.FromDate) .FirstOrDefault(); var maxActivity = db.TimeSheetActivities .OrderByDescending(k => k.FromDate) .FirstOrDefault(); if (minActivity != null && model.fromDate > minActivity.FromDate) { throw new ClientException($"from date should be before {minActivity.FromDate.Date.ToDisplayDate()} which is the earliest activity made on that date on the schedule"); } if (maxActivity != null && model.toDate < maxActivity.FromDate) { throw new ClientException($"to date should be after {maxActivity.FromDate.Date.ToDisplayDate()} which is the latest activity made on that date on the schedule"); } // update values dbTimeSheet.FromDate = model.fromDate; dbTimeSheet.ToDate = model.toDate; db.SaveChanges(); await notificationMethods.Send(model.GetAddedByUser(), model.userId, $"schedule time was modified", NotificationType.Information, true, dbTimeSheet.ID); var parsed = _mapper.Map <TimeSheet>(dbTimeSheet); parsed.HasTasks = db.TimeSheetTasks.Any(k => k.TimeSheetId == parsed.ID); var addedByUser = db.Users.First(k => k.Id == dbTimeSheet.AddedByUserId); parsed.AddedByUserName = addedByUser.FirstName + " " + addedByUser.LastName; return(parsed); } else { var possibleConflictTimeSheet = conflictQuery.FirstOrDefault(); ensureNoConflict(possibleConflictTimeSheet); // # new timesheet # DataSets.TimeSheet dbTimeSheet = new DataSets.TimeSheet() { UserId = model.userId, FromDate = model.fromDate, ToDate = model.toDate, DateAdded = DateTime.Now, AddedByUserId = model.GetAddedByUser() }; // add the timeSheet db.TimeSheets.Add(dbTimeSheet); // save changes on teamstimeSheets db.SaveChanges(); await notificationMethods.Send(model.GetAddedByUser(), model.userId, $"New schedule added", NotificationType.Default, true, dbTimeSheet.ID); var parsed = _mapper.Map <TimeSheet>(dbTimeSheet); var addedByUser = db.Users.First(k => k.Id == dbTimeSheet.AddedByUserId); parsed.AddedByUserName = addedByUser.FirstName + " " + addedByUser.LastName; return(parsed); } //DataSets.TimeSheet dbTimeSheet = new DataSets.TimeSheet() //{ // FromDate = fromDate, // ToDate = toDate, // DateAdded = DateTime.Now, // UserId = userId //}; //db.TimeSheets.Add(dbTimeSheet); //if (db.SaveChanges() > 0) //{ // return _mapper.Map<TimeSheet>(dbTimeSheet); //} }