2024-04-09 10:44:57 -07:00

110 lines
4.7 KiB
C#

using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Services;
using Infineon.Monitoring.MonA;
using Quartz;
using System.Net.Mail;
using System.Text;
namespace FabApprovalWorkerService.Workers;
public class ExpiredTECNWorker : IJob {
private readonly ILogger<ExpiredTECNWorker> _logger;
private readonly IECNService _ecnService;
private readonly ITrainingService _trainingService;
private readonly IUserService _userService;
private readonly ISmtpService _smtpService;
private readonly IMonInClient _monInClient;
private readonly string _baseUrl;
public ExpiredTECNWorker(ILogger<ExpiredTECNWorker> logger,
IECNService ecnService,
ITrainingService trainingService,
IUserService userService,
ISmtpService smtpService,
IMonInClient monInClient) {
_logger = logger ??
throw new ArgumentNullException("ILogger not injected");
_ecnService = ecnService ??
throw new ArgumentNullException("IECNService not injected");
_trainingService = trainingService ??
throw new ArgumentNullException("ITrainingService not injected");
_userService = userService ??
throw new ArgumentNullException("IUserService not injected");
_smtpService = smtpService ??
throw new ArgumentNullException("ISmtpService not injected");
_monInClient = monInClient ??
throw new ArgumentNullException("IMonInClient not injected");
_baseUrl = Environment.GetEnvironmentVariable("FabApprovalBaseUrl") ??
throw new ArgumentNullException("FabApprovalBaseUrl environment variable not found");
}
public async Task Execute(IJobExecutionContext context) {
DateTime start = DateTime.Now;
bool isInternalError = false;
StringBuilder errorMessage = new();
string metricName = "ExpiredTECNWorker";
try {
_logger.LogInformation("Attempting to process expired TECNs");
List<ECN> expiredEcns = (await _ecnService.GetExpiredTECNs()).ToList();
List<MailAddress> tecnNotificationUserEmails = new();
if (expiredEcns.Any()) {
List<string> emails = (await _ecnService.GetTECNNotificationUserEmails()).ToList();
foreach (string email in emails) tecnNotificationUserEmails.Add(new MailAddress(email));
}
foreach (ECN ecn in expiredEcns) {
List<int> trainingIds = (await _trainingService.GetTrainingIdsForECN(ecn.ECNNumber)).ToList();
foreach (int trainingId in trainingIds) {
await _trainingService.DeleteTrainingAssignment(trainingId);
List<int> trainingAssignmentIds =
(await _trainingService.GetTrainingAssignmentIdsForTraining(trainingId)).ToList();
foreach (int assignmentId in trainingAssignmentIds) {
await _trainingService.DeleteDocAssignment(assignmentId);
}
await _trainingService.MarkTrainingAsComplete(trainingId);
}
string recipientEmail = await _userService.GetUserEmail(ecn.OriginatorID);
List<MailAddress> recipientEamils = new List<MailAddress>() {
new MailAddress(recipientEmail)
};
string subject = "Notice of Expired TECN";
StringBuilder bodyBuilder = new();
bodyBuilder.Append($"Good day, TECN# {ecn.ECNNumber} expired on {ecn.ExpirationDate.ToString("MMMM dd, yyyy")}. ");
bodyBuilder.Append($"<br />Review TECN <a href='{_baseUrl}/ECN/Edit?IssueID={ecn.ECNNumber}'> here. </a>");
await _smtpService.SendEmail(recipientEamils, tecnNotificationUserEmails, subject, bodyBuilder.ToString());
}
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append("An exception occurred when attempting to process expired TECNs. ");
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);
}
}
}
}