using System.Net.Mail; using System.Text; using FabApprovalWorkerService.Models; using FabApprovalWorkerService.Services; using Infineon.Monitoring.MonA; using Quartz; namespace FabApprovalWorkerService.Workers; public class ExpiredTECNWorker : IJob { private readonly ILogger _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 logger, IECNService ecnService, ITrainingService trainingService, IUserService userService, ISmtpService smtpService, IMonInClient monInClient, AppSettings appSettings) { _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 = appSettings.BaseUrl; } 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 expiredEcns = (await _ecnService.GetExpiredTECNsInPastDay()).ToList(); List tecnNotificationUserEmails = new(); if (expiredEcns.Any()) { List emails = (await _ecnService.GetTECNNotificationUserEmails()).ToList(); foreach (string email in emails) tecnNotificationUserEmails.Add(new MailAddress(email)); } foreach (ECN ecn in expiredEcns) { List trainingIds = (await _trainingService.GetTrainingIdsForECN(ecn.ECNNumber)).ToList(); foreach (int trainingId in trainingIds) { await _trainingService.DeleteTrainingAssignmentsByTrainingId(trainingId); List 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 recipientEamils = new List() { new MailAddress(recipientEmail) }; string subject = "Notice of Expired TECN"; DateTime latestExpirationDate = (ecn.ExtensionDate > ecn.ExpirationDate) ? ecn.ExtensionDate : ecn.ExpirationDate; StringBuilder bodyBuilder = new(); bodyBuilder.Append($"Good day, TECN# {ecn.ECNNumber} expired on {latestExpirationDate.ToString("MMMM dd, yyyy")}. "); bodyBuilder.Append($"
Review TECN here. "); 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); } } } }