public async Task <IActionResult> UpdateAxisInstance(int userId, int axisInstanceId, int userWeight) { try { var userFromRepo = await _repo.User.GetUser(userId, false); if (userFromRepo.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var axisIntsnaceFromRepo = await _repo.AxisInstance.GetAxisInstance(axisInstanceId); if (axisIntsnaceFromRepo != null) { // Validate goal's status IList <int> axisInstanceIds = new List <int>() { axisInstanceId }; List <Goal> goals = (List <Goal>) await _repo.Goal.GetGoalsByAxisInstanceIds(axisInstanceIds); if (goals != null && goals.Count != 0 && goals.First().Status == Constants.PUBLISHED) { return(BadRequest("Trop tard! Les objectifs sont déjà validés.")); } //Proceed with update var oldUserWeight = axisIntsnaceFromRepo.UserWeight; axisIntsnaceFromRepo.UserWeight = userWeight; _repo.AxisInstance.UpdateAxisInstance(axisIntsnaceFromRepo); await _repo.AxisInstance.SaveAllAsync(); // Log the update of user's weight var user = _repo.User.GetUser(userId, true).Result; var efil = new EvaluationFileInstanceLog { Title = axisIntsnaceFromRepo.EvaluationFileInstance.Title, Created = DateTime.Now, Log = $"La pondération de l\'employée est modifié de {oldUserWeight} à {userWeight} pour '{axisIntsnaceFromRepo.EvaluationFileInstance.Title}' par {user.FirstName} {user.LastName}." }; _repo.EvaluationFileInstanceLog.AddEvaluationFileInstanceLog(efil); await _repo.EvaluationFileInstanceLog.SaveAllAsync(); } return(NoContent()); } catch (Exception ex) { _logger.LogError($"Something went wrong inside UpdateAxisInstance endpoint: {ex.Message}"); return(StatusCode(500, "Internal server error: " + ex.Message)); } }
public async Task <IActionResult> DeleteEvaluationSheet(int id, int userId) { try { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var evaluationFileInstanceFromRepo = await _repo.EvaluationFileInstance.GetEvaluationFileInstance(id); if (evaluationFileInstanceFromRepo == null) { return(NotFound()); } if (evaluationFileInstanceFromRepo.Status == Constants.PUBLISHED || evaluationFileInstanceFromRepo.Status == Constants.ARCHIVED) { return(BadRequest("Vous ne pouvez pas supprimer cette fiche d\'evaluation car elle est publiée ou bien archivée.")); } _repo.EvaluationFileInstance.DeleteEvaluationFileInstance(evaluationFileInstanceFromRepo); await _repo.EvaluationFileInstance.SaveAllAsync(); // Log deletion var efil = new EvaluationFileInstanceLog { Title = evaluationFileInstanceFromRepo.Title, Created = DateTime.Now, Log = $"{evaluationFileInstanceFromRepo.Title} a été supprimé par l'utilisateur avec l'identifiant: ${userId}." }; _repo.EvaluationFileInstanceLog.AddEvaluationFileInstanceLog(efil); await _repo.EvaluationFileInstanceLog.SaveAllAsync(); return(Ok()); } catch (Exception ex) { _logger.LogError($"Something went wrong inside DeleteEvaluationSheet endpoint: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public async Task <IActionResult> CreateEvaluationSheet(int evaluationFileId, IEnumerable <User> users) { try { // Escape users who already have an instance of file evaluation var usersWithoutInstance = new List <User>(); var userIds = new List <int>(); foreach (var user in users) { userIds.Add(user.Id); } var usersWithInstance = await _repo.EvaluationFileInstance.GetUsersWithInstanceFileEvaluation(evaluationFileId, userIds); if (usersWithInstance == null || usersWithInstance.Count() == 0) { usersWithoutInstance = users.ToList(); } else { foreach (var user in users) { var alreadyHasAnInstance = false; foreach (var u in usersWithInstance) { if (user.Id == u.Id) { alreadyHasAnInstance = true; break; } } if (!alreadyHasAnInstance) { usersWithoutInstance.Add(user); } } } // Create behavioral skill instances if they don't exist var skillIds = await _repo.EvaluationFile.GetEvaluationFileBehavioralSkillIds(evaluationFileId); var behavioralSkillInstancesFromRepo = await _repo.BehavioralSkillInstance.GetBehavioralSkillInstancesByBSIds(skillIds); if (behavioralSkillInstancesFromRepo == null || behavioralSkillInstancesFromRepo.Count() == 0) { var behavioralSkillListFromRepo = await _repo.BehavioralSkill.GetBehavioralSkillsByIds(skillIds); foreach (var bs in behavioralSkillListFromRepo) { var newBehavioralSkillInstance = new BehavioralSkillInstance() { Skill = bs.Skill, Definition = bs.Definition, LevelOne = bs.LevelOne, LevelOneDescription = bs.LevelOneDescription, LevelOneGrade = bs.LevelOneGrade, LevelTwo = bs.LevelTwo, LevelTwoDescription = bs.LevelTwoDescription, LevelTwoGrade = bs.LevelTwoGrade, LevelThree = bs.LevelThree, LevelThreeDescription = bs.LevelThreeDescription, LevelThreeGrade = bs.LevelThreeGrade, LevelFour = bs.LevelFour, LevelFourDescription = bs.LevelFourDescription, LevelFourGrade = bs.LevelFourGrade, BehavioralSkillId = bs.Id }; _repo.BehavioralSkillInstance.AddBehavioralSkillInstance(newBehavioralSkillInstance); } await _repo.BehavioralSkillInstance.SaveAllAsync(); } // Create for them evaluation file instance foreach user var evaluationFileFromRepo = await _repo.EvaluationFile.GetEvaluationFile(evaluationFileId); foreach (var user in usersWithoutInstance) { var evaluationFileInstance = new EvaluationFileInstance() { Title = evaluationFileFromRepo.Year + " - " + user.FirstName + " " + user.LastName, Year = evaluationFileFromRepo.Year, Status = Constants.DRAFT, Created = DateTime.Now, OwnerId = user.Id, StrategyTitle = evaluationFileFromRepo.Strategy.Title, StrategyDescription = evaluationFileFromRepo.Strategy.Description, EvaluationFileId = evaluationFileId }; _repo.EvaluationFileInstance.AddEvaluationFileInstance(evaluationFileInstance); } await _repo.EvaluationFileInstance.SaveAllAsync(); // Add Logs and axis instances to each evaluation file instance which doesn't have them yet var evaluationFileInstances = await _repo.EvaluationFileInstance.GetEvaluationFileInstancesByEvaluationFileId(evaluationFileId); foreach (var efi in evaluationFileInstances) { var efil = new EvaluationFileInstanceLog { Title = efi.Title, Created = DateTime.Now, Log = $"{efi.Title} a été générée pour {efi.Owner.FirstName} {efi.Owner.LastName}." }; _repo.EvaluationFileInstanceLog.AddEvaluationFileInstanceLog(efil); } await _repo.EvaluationFileInstanceLog.SaveAllAsync(); var axisFromRepo = await _repo.Axis.GetAxisListDetailed(evaluationFileFromRepo.StrategyId); var evaluationFileInstancesToProcess = evaluationFileInstances.Where(efi => efi.AxisInstances.Count() == 0).ToList(); foreach (var efi in evaluationFileInstancesToProcess) { foreach (var axis in axisFromRepo) { foreach (var ap in axis.AxisPoles) { if (ap.PoleId == efi.Owner.Department.PoleId) { var newAxisInstance = new AxisInstance() { Title = axis.Title, Description = axis.Description, EvaluationFileInstanceId = efi.Id, PoleName = ap.Pole.Name, PoleWeight = ap.Weight, UserWeight = ap.Weight, Created = DateTime.Now }; efi.AxisInstances.Add(newAxisInstance); _repo.AxisInstance.AddAxisInstance(newAxisInstance); } } } } await _repo.AxisInstance.SaveAllAsync(); // Add behavioral skill instances to each evaluation file instance which doesn't have them yet behavioralSkillInstancesFromRepo = await _repo.BehavioralSkillInstance.GetBehavioralSkillInstancesByBSIds(skillIds); foreach (var efi in evaluationFileInstancesToProcess) { foreach (var bsi in behavioralSkillInstancesFromRepo) { efi.BehavioralSkillInstances.Add(new EvaluationFileInstanceBehavioralSkillInstance { BehavioralSkillInstance = bsi }); _repo.EvaluationFileInstance.UpdateEvaluationFileInstance(efi); } } await _repo.EvaluationFileInstance.SaveAllAsync(); return(NoContent()); } catch (Exception ex) { _logger.LogError($"Something went wrong inside CreateEvaluationSheet endpoint: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public async Task <IActionResult> AddFinalEvaluation(int userId, int sheetId, FinalEvaluationDto finalEvaluation) { try { var sheetFromRepo = await _repo.EvaluationFileInstance.GetEvaluationFileInstance(sheetId); if (sheetFromRepo == null) { return(NotFound()); } if (!await IsItAllowed(sheetFromRepo.OwnerId)) { return(Unauthorized()); } var processUpdate = false; // Case of evaluation by Owner if (!string.IsNullOrEmpty(finalEvaluation.OwnerComment)) { sheetFromRepo.OwnerValidationDateTime = finalEvaluation.OwnerValidationDateTime; sheetFromRepo.OwnerComment = finalEvaluation.OwnerComment; if (sheetFromRepo.ValidatorId > 0) { sheetFromRepo.Status = Constants.PUBLISHED; } processUpdate = true; } // Case of evaluation by Evaluator if (!string.IsNullOrEmpty(finalEvaluation.ValidatorComment)) { sheetFromRepo.ValidatorValidationDateTime = finalEvaluation.ValidatorValidationDateTime; sheetFromRepo.ValidatorComment = finalEvaluation.ValidatorComment; sheetFromRepo.ValidatorId = finalEvaluation.ValidatorId; if (sheetFromRepo.OwnerComment != null) { sheetFromRepo.Status = Constants.PUBLISHED; } processUpdate = true; } if (processUpdate) { _repo.EvaluationFileInstance.UpdateEvaluationFileInstance(sheetFromRepo); await _repo.EvaluationFileInstance.SaveAllAsync(); // Log the update of user's weight var user = _repo.User.GetUser(userId, true).Result; var efil = new EvaluationFileInstanceLog { Title = sheetFromRepo.Title, Created = DateTime.Now, Log = $"Une évaluation finale a été sauveguardée avec succés' par {user.FirstName} {user.LastName}." }; _repo.EvaluationFileInstanceLog.AddEvaluationFileInstanceLog(efil); await _repo.EvaluationFileInstanceLog.SaveAllAsync(); } return(NoContent()); } catch (Exception ex) { _logger.LogError($"Something went wrong inside AddFinalEvaluation endpoint: {ex.Message}"); return(StatusCode(500, "Internal server error: " + ex.Message)); } }
public async Task <IActionResult> CascadeGoal(int userId, int model, IEnumerable <GoalForCascadeDto> goalsCascadeDto) { try { if (goalsCascadeDto.Count() > 0) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } // Set not published or archived axis instance id foreach evaluatee foreach (var goalCascadeDto in goalsCascadeDto) { var axisInstanceId = _repo.EvaluationFileInstance.GetAxisInstanceByUserIdAndAxisTitle(goalCascadeDto.EvaluateeId, model, goalCascadeDto.AxisInstanceTitle, goalCascadeDto.ParentGoalId).Result; goalCascadeDto.GoalForCreationDto.AxisInstanceId = axisInstanceId; } // Create a new goal for each evaluatee who has an axis instance foreach (var goalCascadeDto in goalsCascadeDto) { if (goalCascadeDto.GoalForCreationDto.AxisInstanceId != 0) { var goal = _mapper.Map <Goal>(goalCascadeDto.GoalForCreationDto); goal.ParentGoalId = goalCascadeDto.ParentGoalId; _repo.Goal.AddGoal(goal); } } await _repo.Goal.SaveAllAsync(); // Log new goal has been assigned by the evaluator var evaluateeIds = new List <int>(); var efilList = new List <EvaluationFileInstanceLog>(); foreach (var goalCascadeDto in goalsCascadeDto) { if (goalCascadeDto.GoalForCreationDto.AxisInstanceId != 0) { evaluateeIds.Add(goalCascadeDto.EvaluateeId); var sheet = _repo.EvaluationFileInstance.GetEvaluationFileInstanceByUserId(goalCascadeDto.EvaluateeId, model).Result; var evaluator = _repo.User.GetUser(userId, true).Result; var efil = new EvaluationFileInstanceLog { Title = sheet.Title, Created = DateTime.Now, Log = $"L'objectif: '{goalCascadeDto.GoalForCreationDto.Description}', a été ajouté à la fiche '{sheet.Title}' par l'évaluateur {evaluator.FirstName} {evaluator.LastName}." }; efilList.Add(efil); } else { var evaluatee = _repo.User.GetUser(goalCascadeDto.EvaluateeId, false).Result; await SendNotificationsForEvaluator(goalCascadeDto.EvaluateeId, $"Le sous-objectif: '{goalCascadeDto.GoalForCreationDto.Description}', n'a pas été cascadé au collaborateur {evaluatee.FirstName} {evaluatee.LastName}."); } } await LogForSheet(efilList); // Send Notifications string emailContent = "Un nouvel objectif vous a été attribué par votre évaluateur."; await SendNotificationsForSubordinate(userId, emailContent, evaluateeIds); } return(NoContent()); } catch (Exception ex) { _logger.LogError($"Something went wrong inside CascadeGoal endpoint: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public async Task <IActionResult> CreateEvaluationSheet(int evaluationFileId, IEnumerable <User> users) { try { // Escape users who already have an instance of file evaluation var usersWithoutInstance = new List <User>(); var userIds = new List <int>(); foreach (var user in users) { userIds.Add(user.Id); } var usersWithInstance = await _repo.EvaluationFileInstance.GetUsersWithInstanceFileEvaluation(evaluationFileId, userIds); if (usersWithInstance == null || usersWithInstance.Count() == 0) { usersWithoutInstance = users.ToList(); } else { foreach (var user in users) { var alreadyHasAnInstance = false; foreach (var u in usersWithInstance) { if (user.Id == u.Id) { alreadyHasAnInstance = true; break; } } if (!alreadyHasAnInstance) { usersWithoutInstance.Add(user); } } } // Escape users whose pole do not belong to the model var usersToProcess = new List <User>(); var model = await _repo.EvaluationFile.GetModelWithAxisPoles(evaluationFileId); foreach (var user in usersWithoutInstance) { var userFromRepo = await _repo.User.GetUser(user.Id, true); var poleIdOfUser = userFromRepo.Department.Pole.Id; foreach (var axis in model.Strategy.AxisList) { if (axis.AxisPoles.Any(p => p.PoleId == poleIdOfUser)) { usersToProcess.Add(user); break; } } } // Create behavioral skill instances if they don't exist var skillIds = await _repo.EvaluationFile.GetEvaluationFileBehavioralSkillIds(evaluationFileId); var behavioralSkillInstancesFromRepo = await _repo.BehavioralSkillInstance.GetBehavioralSkillInstancesByBSIds(skillIds); if (behavioralSkillInstancesFromRepo == null || behavioralSkillInstancesFromRepo.Count() == 0) { var behavioralSkillListFromRepo = await _repo.BehavioralSkill.GetBehavioralSkillsByIds(skillIds); foreach (var bs in behavioralSkillListFromRepo) { var newBehavioralSkillInstance = new BehavioralSkillInstance() { Skill = bs.Skill, Definition = bs.Definition, LevelOne = bs.LevelOne, LevelOneDescription = bs.LevelOneDescription, LevelOneGrade = bs.LevelOneGrade, LevelTwo = bs.LevelTwo, LevelTwoDescription = bs.LevelTwoDescription, LevelTwoGrade = bs.LevelTwoGrade, LevelThree = bs.LevelThree, LevelThreeDescription = bs.LevelThreeDescription, LevelThreeGrade = bs.LevelThreeGrade, LevelFour = bs.LevelFour, LevelFourDescription = bs.LevelFourDescription, LevelFourGrade = bs.LevelFourGrade, BehavioralSkillId = bs.Id }; _repo.BehavioralSkillInstance.AddBehavioralSkillInstance(newBehavioralSkillInstance); } await _repo.BehavioralSkillInstance.SaveAllAsync(); } // Create for them evaluation file instance foreach user var evaluationFileFromRepo = await _repo.EvaluationFile.GetEvaluationFile(evaluationFileId); foreach (var user in usersToProcess) { var evaluationFileInstance = new EvaluationFileInstance() { Title = evaluationFileFromRepo.Year + " - " + user.FirstName + " " + user.LastName, Year = evaluationFileFromRepo.Year, Status = Constants.DRAFT, Created = DateTime.Now, OwnerId = user.Id, StrategyTitle = evaluationFileFromRepo.Strategy.Title, StrategyDescription = evaluationFileFromRepo.Strategy.Description, EvaluationFileId = evaluationFileId }; _repo.EvaluationFileInstance.AddEvaluationFileInstance(evaluationFileInstance); } await _repo.EvaluationFileInstance.SaveAllAsync(); // Add Logs and axis instances to each evaluation file instance which doesn't have them yet var evaluationFileInstances = await _repo.EvaluationFileInstance.GetEvaluationFileInstancesByEvaluationFileId(evaluationFileId); foreach (var efi in evaluationFileInstances) { var efil = new EvaluationFileInstanceLog { Title = efi.Title, Created = DateTime.Now, Log = $"{efi.Title} a été générée pour {efi.Owner.FirstName} {efi.Owner.LastName}." }; _repo.EvaluationFileInstanceLog.AddEvaluationFileInstanceLog(efil); } await _repo.EvaluationFileInstanceLog.SaveAllAsync(); var axisFromRepo = await _repo.Axis.GetAxisListDetailed(evaluationFileFromRepo.StrategyId); var evaluationFileInstancesToProcess = evaluationFileInstances.Where(efi => efi.AxisInstances.Count() == 0).ToList(); foreach (var efi in evaluationFileInstancesToProcess) { foreach (var axis in axisFromRepo) { foreach (var ap in axis.AxisPoles) { if (ap.PoleId == efi.Owner.Department.PoleId) { var newAxisInstance = new AxisInstance() { Title = axis.Title, Description = axis.Description, EvaluationFileInstanceId = efi.Id, PoleName = ap.Pole.Name, PoleWeight = ap.Weight, UserWeight = ap.Weight, Created = DateTime.Now }; efi.AxisInstances.Add(newAxisInstance); _repo.AxisInstance.AddAxisInstance(newAxisInstance); } } } } await _repo.AxisInstance.SaveAllAsync(); // Add behavioral skill instances to each evaluation file instance which doesn't have them yet behavioralSkillInstancesFromRepo = await _repo.BehavioralSkillInstance.GetBehavioralSkillInstancesByBSIds(skillIds); foreach (var efi in evaluationFileInstancesToProcess) { foreach (var bsi in behavioralSkillInstancesFromRepo) { efi.BehavioralSkillInstances.Add(new EvaluationFileInstanceBehavioralSkillInstance { BehavioralSkillInstance = bsi }); _repo.EvaluationFileInstance.UpdateEvaluationFileInstance(efi); } } await _repo.EvaluationFileInstance.SaveAllAsync(); // Send Notification var emailContent = "Les utilisateurs suivants ont déjà une fiche d'évaluation pour cette année ou leur pôle n'est pas ajouté dans le modèle: "; foreach (var user in users) { if (!usersToProcess.Any(up => up.Id == user.Id)) { emailContent += user.FirstName + " " + user.LastName + ", "; } } emailContent = emailContent.TrimEnd(','); await SendNotification(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value), emailContent); return(NoContent()); } catch (Exception ex) { _logger.LogError($"Something went wrong inside CreateEvaluationSheet endpoint: {ex.Message}"); return(StatusCode(500, "Internal server error: " + ex.Message)); } }