Only include cert members in Si Production group
This commit is contained in:
parent
91a15a32b6
commit
95088a3e64
@ -1,3 +1,5 @@
|
||||
using System.Text;
|
||||
|
||||
namespace FabApprovalWorkerService.Models;
|
||||
|
||||
public class UserCertificationRecord {
|
||||
@ -10,4 +12,17 @@ public class UserCertificationRecord {
|
||||
public bool IsEpiProCertified { get; set; }
|
||||
public bool IsFqaCertified { get; set; }
|
||||
public bool IsFqaAssessmentCertified { get; set; }
|
||||
|
||||
public override string ToString() {
|
||||
StringBuilder sb = new();
|
||||
sb.Append($"FirstName: {FirstName}, ");
|
||||
sb.Append($"LastName: {LastName}, ");
|
||||
sb.Append($"Email: {Email}, ");
|
||||
sb.Append($"IsCleansCertified: {IsCleansCertified}, ");
|
||||
sb.Append($"IsPackagingLabelingCertified: {IsPackagingLabelingCertified}, ");
|
||||
sb.Append($"IsEpiProCertified: {IsEpiProCertified}, ");
|
||||
sb.Append($"IsFqaCertified: {IsFqaCertified}, ");
|
||||
sb.Append($"IsFqaAssessmentCertified: {IsFqaAssessmentCertified}");
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,14 +15,36 @@ public class CertificationTrainingGroupWorker : IJob {
|
||||
|
||||
private readonly ILogger<CertificationTrainingGroupWorker> _logger;
|
||||
private readonly ITrainingService _trainingService;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IMonInClient _monInClient;
|
||||
|
||||
private readonly string _siProductionGroupName;
|
||||
private readonly string _cleansTrainingGroupName;
|
||||
private readonly string _asmHtrTrainingGroupName;
|
||||
private readonly string _epiProTrainingGroupName;
|
||||
private readonly string _fqaTrainingGroupName;
|
||||
private readonly string _packagingAndLabelingTrainingGroupName;
|
||||
|
||||
public CertificationTrainingGroupWorker(ILogger<CertificationTrainingGroupWorker> logger,
|
||||
ITrainingService trainingService,
|
||||
IUserService userService,
|
||||
IMonInClient monInClient) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_trainingService = trainingService ?? throw new ArgumentNullException("ITrainingService not injected");
|
||||
_userService = userService ?? throw new ArgumentNullException("IUserService not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInClient not injected");
|
||||
_siProductionGroupName = Environment.GetEnvironmentVariable("SiProductionTrainingGroupName") ??
|
||||
throw new ArgumentNullException("SiProductionTrainingGroupName 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 Execute(IJobExecutionContext context) {
|
||||
@ -39,8 +61,63 @@ public class CertificationTrainingGroupWorker : IJob {
|
||||
|
||||
_logger.LogInformation($"Attempting to update certification training group membership. Remaining retries: {remainingRetries}");
|
||||
|
||||
_logger.LogInformation("Deleting ASM/HTR training group members");
|
||||
int asmHtrGroupId = await _trainingService.GetTrainingGroupIdByName(_asmHtrTrainingGroupName);
|
||||
await _trainingService.DeleteAllUsersFromTrainingGroup(asmHtrGroupId);
|
||||
|
||||
_logger.LogInformation("Deleting EpiPro training group members");
|
||||
int epiProGroupId = await _trainingService.GetTrainingGroupIdByName(_epiProTrainingGroupName);
|
||||
await _trainingService.DeleteAllUsersFromTrainingGroup(epiProGroupId);
|
||||
|
||||
_logger.LogInformation("Deleting FQA training group members");
|
||||
int fqaGroupId = await _trainingService.GetTrainingGroupIdByName(_fqaTrainingGroupName);
|
||||
await _trainingService.DeleteAllUsersFromTrainingGroup(fqaGroupId);
|
||||
|
||||
_logger.LogInformation("Deleting Packaging & Labeling training group members");
|
||||
int packagingAndLabelingGroupId = await _trainingService.GetTrainingGroupIdByName(_packagingAndLabelingTrainingGroupName);
|
||||
await _trainingService.DeleteAllUsersFromTrainingGroup(packagingAndLabelingGroupId);
|
||||
|
||||
_logger.LogInformation("Deleting Cleans training group members");
|
||||
int cleansGroupId = await _trainingService.GetTrainingGroupIdByName(_cleansTrainingGroupName);
|
||||
await _trainingService.DeleteAllUsersFromTrainingGroup(cleansGroupId);
|
||||
|
||||
IEnumerable<UserCertificationRecord> certRecords = await _trainingService.GetUserCertificationRecords();
|
||||
isSuccessful = await _trainingService.UpdateTrainingGroupMembership(certRecords);
|
||||
|
||||
if (certRecords is null || certRecords.Count() == 0)
|
||||
throw new ArgumentNullException("certRecords cannot be null or empty");
|
||||
|
||||
int siProductionTrainingGroupId = await _trainingService.GetTrainingGroupIdByName(_siProductionGroupName);
|
||||
|
||||
List<Task> queryTasks = new();
|
||||
foreach (UserCertificationRecord record in certRecords) {
|
||||
_logger.LogInformation($"Processing cert record {{{record}}}");
|
||||
|
||||
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 && user.IsActive) {
|
||||
bool userIsInSiProduction = await _trainingService.IsUserMemberOfTrainingGroup(user.UserID, siProductionTrainingGroupId);
|
||||
if (userIsInSiProduction) {
|
||||
if (record.IsEpiProCertified) queryTasks.Add(_trainingService.AddUserToTrainingGroup(epiProGroupId, user));
|
||||
if (record.IsPackagingLabelingCertified) queryTasks.Add(_trainingService.AddUserToTrainingGroup(packagingAndLabelingGroupId, user));
|
||||
if (record.IsCleansCertified) queryTasks.Add(_trainingService.AddUserToTrainingGroup(cleansGroupId, user));
|
||||
if (record.IsFqaCertified) queryTasks.Add(_trainingService.AddUserToTrainingGroup(fqaGroupId, user));
|
||||
if (record.IsAnyLevelCertified) queryTasks.Add(_trainingService.AddUserToTrainingGroup(asmHtrGroupId, user));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Task.WhenAll(queryTasks);
|
||||
|
||||
isSuccessful = true;
|
||||
}
|
||||
|
||||
if (isSuccessful) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user