Created UserCertificationWorker
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;
|
||||
|
||||
@ -20,6 +23,8 @@ 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);
|
||||
}
|
||||
|
||||
public class UserService : IUserService {
|
||||
@ -29,6 +34,8 @@ 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)
|
||||
@ -37,6 +44,9 @@ 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() {
|
||||
@ -176,7 +186,7 @@ public class UserService : IUserService {
|
||||
throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
||||
if (delegatedUserId <= 0)
|
||||
throw new ArgumentException($"Delegated user Id {delegatedUserId} is not a valid user Id");
|
||||
|
||||
|
||||
string sql = $"update UserSubRole set UserID = {delegatedUserId}, Delegated = 1 where UserID = {userId}";
|
||||
|
||||
await _dalService.ExecuteAsync(sql);
|
||||
@ -236,7 +246,7 @@ public class UserService : IUserService {
|
||||
throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
||||
if (delegatedUserId <= 0)
|
||||
throw new ArgumentException($"Delegated user Id {delegatedUserId} is not a valid user Id");
|
||||
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update Approval set UserID = {delegatedUserId}, ");
|
||||
queryBuilder.Append($"Delegated = 1 where UserID = {userId} ");
|
||||
@ -393,4 +403,60 @@ public class UserService : IUserService {
|
||||
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> 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($"Exception: {ex.Message}");
|
||||
_logger.LogError(errMsgBuilder.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user