Created CertificationTrainingGroupWorker
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
using FabApprovalWorkerService.Models;
|
||||
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace FabApprovalWorkerService.Services;
|
||||
|
||||
@ -15,17 +18,42 @@ public interface ITrainingService {
|
||||
Task UpdateTrainingAssignmentLastNotification(int trainingAssignmentId);
|
||||
Task<int> GetEcnNumberByTrainingId(int trainingId);
|
||||
Task<IEnumerable<int>> GetTrainingAssignmentIdsByUserId(int userId);
|
||||
Task<IEnumerable<UserCertificationRecord>> GetUserCertificationRecords();
|
||||
Task<bool> UpdateTrainingGroupMembership(IEnumerable<UserCertificationRecord> certRecords);
|
||||
}
|
||||
|
||||
public class TrainingService : ITrainingService {
|
||||
private ILogger<TrainingService> _logger;
|
||||
private IDalService _dalService;
|
||||
private IUserService _userService;
|
||||
|
||||
public TrainingService(ILogger<TrainingService> logger, IDalService dalService) {
|
||||
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");
|
||||
_dalService = dalService ??
|
||||
throw new ArgumentNullException("IDalService not injected");
|
||||
_userService = userService ??
|
||||
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) {
|
||||
@ -228,4 +256,142 @@ public class TrainingService : ITrainingService {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UserCertificationRecord>> GetUserCertificationRecords() {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get user certification records");
|
||||
|
||||
string jsonUserCertRecords = await File.ReadAllTextAsync(_userCertRecordsFilePath);
|
||||
|
||||
IEnumerable<UserCertificationRecord>? records =
|
||||
JsonSerializer.Deserialize<IEnumerable<UserCertificationRecord>>(jsonUserCertRecords);
|
||||
|
||||
if (records is null) throw new Exception("No user certification records found");
|
||||
|
||||
return records;
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append("An exception occurred when attempting to get user certification records. ");
|
||||
errMsgBuilder.Append($"Exception: {ex.Message}");
|
||||
_logger.LogError(errMsgBuilder.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get {groupName} training group ID");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append("select TrainingGroupID from TrainingGroups ");
|
||||
queryBuilder.Append($"where TrainingGroupName = '{groupName}' collate SQL_Latin1_General_CP1_CI_AS;");
|
||||
|
||||
int groupId = (await _dalService.QueryAsync<int>(queryBuilder.ToString())).FirstOrDefault();
|
||||
|
||||
if (groupId <= 0)
|
||||
throw new Exception($"{groupName} training group ID not found");
|
||||
|
||||
return groupId;
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append($"An exception occurred when attempting to get {groupName} training group ID. ");
|
||||
errMsgBuilder.Append($"Exception: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DeleteAllUsersFromTrainingGroup(int groupId) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to delete all members of training group {groupId}");
|
||||
|
||||
if (groupId <= 0) throw new ArgumentException($"{groupId} not a valid training group ID");
|
||||
|
||||
string sql = $"delete from TrainingGroupMembers where TrainingGroupID = {groupId};";
|
||||
|
||||
await _dalService.ExecuteAsync(sql);
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append($"An exception occurred when attempting to delete all members of training group {groupId}. ");
|
||||
errMsgBuilder.Append($"Exception: {ex.Message}");
|
||||
_logger.LogError(errMsgBuilder.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AddUserToTrainingGroup(int groupId, User user) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to add user to training group {groupId}");
|
||||
|
||||
if (groupId <= 0) throw new ArgumentException($"{groupId} is not a valid training group Id");
|
||||
if (user is null) throw new ArgumentNullException("User cannot be null");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append("insert into TrainingGroupMembers (TrainingGroupID, UserID, FullName) ");
|
||||
queryBuilder.Append($"values ({groupId}, {user.UserID}, '{user.FirstName + " " + user.LastName}');");
|
||||
|
||||
await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append($"An exception occurred when attempting to add user to training group {groupId}. ");
|
||||
errMsgBuilder.Append($"Exception: {ex.Message}");
|
||||
_logger.LogError(errMsgBuilder.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user