Created CertificationTrainingGroupWorker

This commit is contained in:
Chase Tucker
2024-04-22 13:01:39 -07:00
parent 855990e3e2
commit 91a15a32b6
6 changed files with 235 additions and 102 deletions

View File

@ -3,7 +3,6 @@
using Microsoft.IdentityModel.Tokens;
using System.Text;
using System.Text.Json;
namespace FabApprovalWorkerService.Services;
@ -23,8 +22,7 @@ public interface IUserService {
Task<List<User>> GetAllExpiredOOOUsersAsync();
Task<string> GetUserEmail(int userId);
Task<User> GetUserById(int userId);
Task<IEnumerable<UserCertificationRecord>> GetUserCertificationRecords();
Task<bool> UpdateUserCertificationData(IEnumerable<UserCertificationRecord> certRecords);
Task<User> GetUserByEmail(string email);
}
public class UserService : IUserService {
@ -34,8 +32,6 @@ public class UserService : IUserService {
private readonly ILogger<UserService> _logger;
private readonly IDalService _dalService;
private readonly string _userCertRecordsFilePath;
public UserService(ILogger<UserService> logger, IDalService dalService) {
_logger = logger;
if (_logger is null)
@ -44,9 +40,6 @@ public class UserService : IUserService {
_dalService = dalService;
if (_dalService is null)
throw new ArgumentNullException("IDalService not injected");
_userCertRecordsFilePath = Environment.GetEnvironmentVariable("UserCertificationRecordsFilePath") ??
throw new ArgumentNullException("UserCertificationRecordsFilePath environment variable not found");
}
public async Task<List<User>> GetAllExpiredOOOUsersAsync() {
@ -397,63 +390,30 @@ public class UserService : IUserService {
return user;
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append($"An exception occurred when attempting to get email for user {userId}. ");
errMsgBuilder.Append($"An exception occurred when attempting to get user {userId}. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
throw;
}
}
public async Task<IEnumerable<UserCertificationRecord>> GetUserCertificationRecords() {
public async Task<User> GetUserByEmail(string email) {
try {
_logger.LogInformation("Attempting to get user certification records");
_logger.LogInformation($"Attempting to get user by email");
string jsonUserCertRecords = await File.ReadAllTextAsync(_userCertRecordsFilePath);
if (email.IsNullOrEmpty()) throw new ArgumentException("Email cannot be null or empty");
IEnumerable<UserCertificationRecord>? records =
JsonSerializer.Deserialize<IEnumerable<UserCertificationRecord>>(jsonUserCertRecords);
string sql = $"select * from Users where Email = '{email}' collate SQL_Latin1_General_CP1_CI_AS;";
if (records is null) throw new Exception("No user certification records found");
User? user = (await _dalService.QueryAsync<User>(sql)).FirstOrDefault();
return records;
if (user is null)
throw new Exception($"No user found for email {email}");
return user;
} 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> UpdateUserCertificationData(IEnumerable<UserCertificationRecord> certRecords) {
try {
_logger.LogInformation("Attempting to update user certification data");
if (certRecords.IsNullOrEmpty())
throw new ArgumentNullException("certRecords cannot be null or empty");
List<Task> queryTasks = new();
foreach (UserCertificationRecord record in certRecords) {
StringBuilder queryBuilder = new();
queryBuilder.Append("update Users ");
queryBuilder.Append($"set IsCleansCertified = {Convert.ToInt32(record.IsCleansCertified)}, ");
queryBuilder.Append($"IsAnyLevelCertified = {Convert.ToInt32(record.IsAnyLevelCertified)}, ");
queryBuilder.Append($"IsPackagingLabelingCertified = {Convert.ToInt32(record.IsPackagingLabelingCertified)}, ");
queryBuilder.Append($"IsEpiProCertified = {Convert.ToInt32(record.IsEpiProCertified)}, ");
queryBuilder.Append($"IsFqaCertified = {Convert.ToInt32(record.IsFqaCertified)}, ");
queryBuilder.Append($"IsFqaAssessmentCertified = {Convert.ToInt32(record.IsFqaAssessmentCertified)} ");
queryBuilder.Append($"where Email = '{record.Email}' collate SQL_Latin1_General_CP1_CI_AS;");
queryTasks.Add(_dalService.ExecuteAsync(queryBuilder.ToString()));
}
await Task.WhenAll(queryTasks);
return true;
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append("An exception occurred when attempting to update user certification data. ");
errMsgBuilder.Append("An exception occurred when attempting to get user by email. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
throw;