Added training reminder worker

This commit is contained in:
Chase Tucker
2024-04-10 09:59:55 -07:00
parent 156dee0751
commit ed89f25dad
9 changed files with 394 additions and 9 deletions

View File

@ -0,0 +1,12 @@
using Dapper.Contrib.Extensions;
namespace FabApprovalWorkerService.Models;
[Table("TrainingAssignment")]
public class TrainingAssignment {
[Key]
public int ID { get; set; }
public int TrainingID { get; set; }
public bool Status { get; set; } = false;
public bool Deleted { get; set; } = false;
public DateTime DeletedDate { get; set; }
}

View File

@ -8,6 +8,7 @@ public interface IECNService {
Task<IEnumerable<ECN>> GetExpiringTECNs();
Task<IEnumerable<ECN>> GetExpiredTECNs();
Task<IEnumerable<string>> GetTECNNotificationUserEmails();
Task<ECN> GetEcnByNumber(int ecnNumber);
}
public class ECNService : IECNService {
@ -19,6 +20,28 @@ public class ECNService : IECNService {
_dalService = dalService ?? throw new ArgumentNullException("IDalService not injected");
}
public async Task<ECN> GetEcnByNumber(int ecnNumber) {
try {
_logger.LogInformation($"Attempting to get ECN {ecnNumber}");
if (ecnNumber <= 0) throw new ArgumentException($"{ecnNumber} not a valid ECN number");
string sql = $"select * from ECN where ECNNumber = {ecnNumber}";
ECN? ecn = (await _dalService.QueryAsync<ECN>(sql)).FirstOrDefault();
if (ecn is null) throw new Exception($"ECN {ecnNumber} not found");
return ecn;
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append($"An exception occurred when attempting to get ECN {ecnNumber}. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
throw;
}
}
public async Task<IEnumerable<ECN>> GetExpiredTECNs() {
try {
_logger.LogInformation("Attempting to get all TECNs expired in the last day");

View File

@ -10,6 +10,9 @@ public interface ITrainingService {
Task DeleteTrainingAssignment(int trainingId);
Task<IEnumerable<int>> GetTrainingAssignmentIdsForTraining(int trainingId);
Task DeleteDocAssignment(int trainingAssignmentId);
Task<IEnumerable<TrainingAssignment>> GetActiveTrainingAssignments();
Task UpdateTrainingAssignmentLastNotification(int trainingAssignmentId);
Task<int> GetEcnNumberByTrainingId(int trainingId);
}
public class TrainingService : ITrainingService {
@ -121,4 +124,66 @@ public class TrainingService : ITrainingService {
throw;
}
}
public async Task<IEnumerable<TrainingAssignment>> GetActiveTrainingAssignments() {
try {
_logger.LogInformation($"Attempting to get active training assignments");
StringBuilder queryBuilder = new();
queryBuilder.Append("select ID, UserID, DateAssigned, TrainingID, status, Deleted, DeletedDate, LastNotification ");
queryBuilder.Append("from TrainingAssignments where status = 0 and (Deleted is null or Deleted = 0);");
return await _dalService.QueryAsync<TrainingAssignment>(queryBuilder.ToString());
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append($"An exception occurred when attempting to get active training assignments. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
throw;
}
}
public async Task UpdateTrainingAssignmentLastNotification(int trainingAssignmentId) {
try {
_logger.LogInformation($"Attempting to update last notification date for training assignment {trainingAssignmentId}");
if (trainingAssignmentId <= 0)
throw new ArgumentException($"{trainingAssignmentId} is not a valid training assignment Id");
StringBuilder queryBuilder = new();
queryBuilder.Append($"update TrainingAssignments set LastNotification = {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
queryBuilder.Append($"where ID = {trainingAssignmentId};");
await _dalService.ExecuteAsync(queryBuilder.ToString());
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append("An exception occurred when attempting to update last notification ");
errMsgBuilder.Append($"for training assignment {trainingAssignmentId}. Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
throw;
}
}
public async Task<int> GetEcnNumberByTrainingId(int trainingId) {
try {
_logger.LogInformation($"Attempting to get ECN number for training {trainingId}");
if (trainingId <= 0)
throw new ArgumentException($"{trainingId} is not a valid training Id");
string sql = $"select e.ECNNumber from Training t join ECN e on t.ECN = e.ECNNumber where t.TrainingID = {trainingId};";
int ecnNumber = (await _dalService.QueryAsync<int>(sql)).FirstOrDefault();
if (ecnNumber <= 0) throw new Exception($"ECN number not found for training {trainingId}");
return ecnNumber;
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append($"An exception occurred when attempting to get ECN number for training {trainingId}. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
throw;
}
}
}

View File

@ -19,6 +19,7 @@ public interface IUserService {
Task<bool> SetOOOTempProcessed(OOOTemp oOOTemp);
Task<List<User>> GetAllExpiredOOOUsersAsync();
Task<string> GetUserEmail(int userId);
Task<User> GetUserById(int userId);
}
public class UserService : IUserService {
@ -355,7 +356,7 @@ public class UserService : IUserService {
string sql = $"select Email from Users where UserID = {userId}";
string? userEmail = (await _dalService.QueryAsync<string>(sql)).ToList().FirstOrDefault();
string? userEmail = (await _dalService.QueryAsync<string>(sql)).FirstOrDefault();
if (userEmail is null)
throw new Exception($"No email found for user {userId}");
@ -369,4 +370,27 @@ public class UserService : IUserService {
throw;
}
}
public async Task<User> GetUserById(int userId) {
if (userId <= 0) throw new ArgumentException($"{userId} not a valid UserID");
try {
_logger.LogInformation($"Attempting to get user {userId}");
string sql = $"select * from Users where UserID = {userId}";
User? user = (await _dalService.QueryAsync<User>(sql)).FirstOrDefault();
if (user is null)
throw new Exception($"No user found for id {userId}");
return user;
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append($"An exception occurred when attempting to get email for user {userId}. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
throw;
}
}
}

View File

@ -14,15 +14,34 @@ drop table if exists TrainingAssignments;
create table TrainingAssignments (
ID integer primary key,
UserID integer not null,
DateAssigned text not null,
TrainingID integer not null,
Deleted integer default 0,
status integer default 0,
DeletedDate text
DeletedDate text,
LastNotification text
);
insert into TrainingAssignments (TrainingID)
values (1), (1), (2), (2), (3), (3), (4), (4), (5), (5), (6), (6),
(7), (7), (8), (8), (9), (9);
insert into TrainingAssignments (TrainingID, UserID, DateAssigned)
values (1, 1, '2024-02-01 00:00:00.000'),
(1, 11, '2024-04-01 00:00:00.000'),
(2, 23, '2024-02-01 00:00:00.000'),
(2, 18, '2024-02-01 00:00:00.000'),
(3, 5, '2024-02-01 00:00:00.000'),
(3, 25, '2024-02-01 00:00:00.000'),
(4, 15, '2024-04-01 00:00:00.000'),
(4, 12, '2024-02-01 00:00:00.000'),
(5, 9, '2024-02-01 00:00:00.000'),
(5, 19, '2024-02-01 00:00:00.000'),
(6, 13, '2024-02-01 00:00:00.000'),
(6, 3, '2024-04-01 00:00:00.000'),
(7, 29, '2024-02-01 00:00:00.000'),
(7, 17, '2024-02-01 00:00:00.000'),
(8, 8, '2024-02-01 00:00:00.000'),
(8, 4, '2024-02-01 00:00:00.000'),
(9, 17, '2024-02-01 00:00:00.000'),
(9, 16, '2024-04-01 00:00:00.000');
drop table if exists TrainingDocAcks;

View File

@ -0,0 +1,71 @@
using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Services;
using Infineon.Monitoring.MonA;
using Quartz;
using System.Text;
namespace FabApprovalWorkerService.Workers;
public class TrainingNotificationWorker : IJob {
private readonly ILogger<TrainingNotificationWorker> _logger;
private readonly ITrainingService _trainingService;
private readonly IUserService _userService;
private readonly IECNService _ecnService;
private readonly ISmtpService _smtpService;
private readonly IMonInClient _monInClient;
public TrainingNotificationWorker(ILogger<TrainingNotificationWorker> logger,
ITrainingService trainingService,
IUserService userService,
IECNService ecnService,
ISmtpService smtpService,
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");
_ecnService = ecnService ?? throw new ArgumentNullException("IECNService not injected");
_smtpService = smtpService ?? throw new ArgumentNullException("ISmtpService not injected");
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInClient not injected");
}
public async Task Execute(IJobExecutionContext context) {
DateTime start = DateTime.Now;
bool isInternalError = false;
StringBuilder errorMessage = new();
string metricName = "TrainingNotificationWorker";
try {
_logger.LogInformation("Attempting to send training notifications");
IEnumerable<TrainingAssignment> trainingAssignments = await _trainingService.GetActiveTrainingAssignments();
foreach (TrainingAssignment trainingAssignment in trainingAssignments) {
ECN ecn = await _ecnService.GetEcnByNumber
User user = await _userService.GetUserById(trainingAssignment.UserID);
}
_logger.LogInformation("Successfully sent training notifications");
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append("An exception occurred when attempting to send training notifications. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
isInternalError = true;
} finally {
DateTime end = DateTime.Now;
double latencyInMS = (end - start).TotalMilliseconds;
_monInClient.PostMetric(metricName + "Latency", latencyInMS);
if (isInternalError) {
_monInClient.PostStatus(metricName, State.Critical);
} else {
_monInClient.PostStatus(metricName, State.Ok);
}
}
}
}