Added training reminder worker
This commit is contained in:
@ -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");
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user