Only include cert members in Si Production group
This commit is contained in:
@ -1,7 +1,5 @@
|
||||
using FabApprovalWorkerService.Models;
|
||||
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
@ -19,7 +17,10 @@ public interface ITrainingService {
|
||||
Task<int> GetEcnNumberByTrainingId(int trainingId);
|
||||
Task<IEnumerable<int>> GetTrainingAssignmentIdsByUserId(int userId);
|
||||
Task<IEnumerable<UserCertificationRecord>> GetUserCertificationRecords();
|
||||
Task<bool> UpdateTrainingGroupMembership(IEnumerable<UserCertificationRecord> certRecords);
|
||||
Task<int> GetTrainingGroupIdByName(string groupName);
|
||||
Task DeleteAllUsersFromTrainingGroup(int groupId);
|
||||
Task AddUserToTrainingGroup(int groupId, User user);
|
||||
Task<bool> IsUserMemberOfTrainingGroup(int userId, int groupId);
|
||||
}
|
||||
|
||||
public class TrainingService : ITrainingService {
|
||||
@ -29,12 +30,6 @@ public class TrainingService : ITrainingService {
|
||||
|
||||
private readonly string _userCertRecordsFilePath;
|
||||
|
||||
private readonly string _cleansTrainingGroupName;
|
||||
private readonly string _asmHtrTrainingGroupName;
|
||||
private readonly string _epiProTrainingGroupName;
|
||||
private readonly string _fqaTrainingGroupName;
|
||||
private readonly string _packagingAndLabelingTrainingGroupName;
|
||||
|
||||
public TrainingService(ILogger<TrainingService> logger, IDalService dalService, IUserService userService) {
|
||||
_logger = logger ??
|
||||
throw new ArgumentNullException("ILogger not injected");
|
||||
@ -44,16 +39,6 @@ public class TrainingService : ITrainingService {
|
||||
throw new ArgumentNullException("IUserService not injected");
|
||||
_userCertRecordsFilePath = Environment.GetEnvironmentVariable("UserCertificationRecordsFilePath") ??
|
||||
throw new ArgumentNullException("UserCertificationRecordsFilePath environment variable not found");
|
||||
_asmHtrTrainingGroupName = Environment.GetEnvironmentVariable("AsmHtrTrainingGroupName") ??
|
||||
throw new ArgumentNullException("AsmHtrTrainingGroupName environment variable not found");
|
||||
_cleansTrainingGroupName = Environment.GetEnvironmentVariable("CleansTrainingGroupName") ??
|
||||
throw new ArgumentNullException("CleansTrainingGroupName environment variable not found");
|
||||
_epiProTrainingGroupName = Environment.GetEnvironmentVariable("EpiProTrainingGroupName") ??
|
||||
throw new ArgumentNullException("EpiProTrainingGroupName environment variable not found");
|
||||
_fqaTrainingGroupName = Environment.GetEnvironmentVariable("FqaTrainingGroupName") ??
|
||||
throw new ArgumentNullException("FqaTrainingGroupName environment variable not found");
|
||||
_packagingAndLabelingTrainingGroupName = Environment.GetEnvironmentVariable("PackagingAndLabelingTrainingGroupName") ??
|
||||
throw new ArgumentNullException("PackagingAndLabelingTrainingGroupName environment variable not found");
|
||||
}
|
||||
|
||||
public async Task DeleteDocAssignment(int trainingAssignmentId) {
|
||||
@ -278,63 +263,7 @@ public class TrainingService : ITrainingService {
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateTrainingGroupMembership(IEnumerable<UserCertificationRecord> certRecords) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to update certification training group membership.");
|
||||
|
||||
if (certRecords.IsNullOrEmpty())
|
||||
throw new ArgumentNullException("certRecords cannot be null or empty");
|
||||
|
||||
int asmHtrGroupId = await GetGroupId(_asmHtrTrainingGroupName);
|
||||
await DeleteAllUsersFromTrainingGroup(asmHtrGroupId);
|
||||
|
||||
int epiProGroupId = await GetGroupId(_epiProTrainingGroupName);
|
||||
await DeleteAllUsersFromTrainingGroup(epiProGroupId);
|
||||
|
||||
int fqaGroupId = await GetGroupId(_fqaTrainingGroupName);
|
||||
await DeleteAllUsersFromTrainingGroup(fqaGroupId);
|
||||
|
||||
int packagingAndLabelingGroupId = await GetGroupId(_packagingAndLabelingTrainingGroupName);
|
||||
await DeleteAllUsersFromTrainingGroup(packagingAndLabelingGroupId);
|
||||
|
||||
int cleansGroupId = await GetGroupId(_cleansTrainingGroupName);
|
||||
await DeleteAllUsersFromTrainingGroup(cleansGroupId);
|
||||
|
||||
List<Task> queryTasks = new();
|
||||
foreach (UserCertificationRecord record in certRecords) {
|
||||
if (record is not null) {
|
||||
User user = null;
|
||||
try {
|
||||
user = await _userService.GetUserByEmail(record.Email);
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append($"An exception occurred when attempting to get user for email {record.Email}. ");
|
||||
errMsgBuilder.Append($"Exception: {ex.Message}");
|
||||
}
|
||||
|
||||
if (user is not null) {
|
||||
if (record.IsEpiProCertified) queryTasks.Add(AddUserToTrainingGroup(epiProGroupId, user));
|
||||
if (record.IsPackagingLabelingCertified) queryTasks.Add(AddUserToTrainingGroup(packagingAndLabelingGroupId, user));
|
||||
if (record.IsCleansCertified) queryTasks.Add(AddUserToTrainingGroup(cleansGroupId, user));
|
||||
if (record.IsFqaCertified) queryTasks.Add(AddUserToTrainingGroup(fqaGroupId, user));
|
||||
if (record.IsAnyLevelCertified) queryTasks.Add(AddUserToTrainingGroup(asmHtrGroupId, user));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Task.WhenAll(queryTasks);
|
||||
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append("An exception occurred when attempting to update certification training group membership. ");
|
||||
errMsgBuilder.Append($"Exception: {ex.Message}");
|
||||
_logger.LogError(errMsgBuilder.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<int> GetGroupId(string groupName) {
|
||||
public async Task<int> GetTrainingGroupIdByName(string groupName) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get {groupName} training group ID");
|
||||
|
||||
@ -356,7 +285,7 @@ public class TrainingService : ITrainingService {
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DeleteAllUsersFromTrainingGroup(int groupId) {
|
||||
public async Task DeleteAllUsersFromTrainingGroup(int groupId) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to delete all members of training group {groupId}");
|
||||
|
||||
@ -374,7 +303,7 @@ public class TrainingService : ITrainingService {
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AddUserToTrainingGroup(int groupId, User user) {
|
||||
public async Task AddUserToTrainingGroup(int groupId, User user) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to add user to training group {groupId}");
|
||||
|
||||
@ -394,4 +323,22 @@ public class TrainingService : ITrainingService {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> IsUserMemberOfTrainingGroup(int userId, int groupId) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to determine if {userId} is a member of training group {groupId}");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append("select ID from TrainingGroupMembers ");
|
||||
queryBuilder.Append($"where TrainingGroupID = '{groupId}' ");
|
||||
queryBuilder.Append($"and UserID = {userId};");
|
||||
|
||||
return (await _dalService.QueryAsync<int>(queryBuilder.ToString())).Count() > 0;
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append($"An exception occurred when attempting to determine if {userId} ");
|
||||
errMsgBuilder.Append($"is a member of training group {groupId}. Exception: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user