using System.Net.Mail; using System.Text; using FabApprovalWorkerService.Models; using FabApprovalWorkerService.Services; using Infineon.Monitoring.MonA; using Quartz; namespace FabApprovalWorkerService.Workers; public class CAFollowUpWorker : IJob { private readonly ILogger _logger; private readonly ICorrectiveActionService _caService; private readonly IUserService _userService; private readonly ISmtpService _smtpService; private readonly IMonInClient _monInClient; private readonly string _baseUrl; public CAFollowUpWorker(ILogger logger, ICorrectiveActionService caService, IUserService userService, ISmtpService smtpService, IMonInClient monInClient, AppSettings appSettings) { _logger = logger ?? throw new ArgumentNullException("ILogger not injected"); _caService = caService ?? throw new ArgumentNullException("ICorrectiveActionService 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 = "CAFollowUpWorker"; try { _logger.LogInformation("Attempting to create follow up approvals for CAs needing it in five days"); IEnumerable followUpCAs = (await _caService.GetCorrectiveActionsWithFollowUpInFiveDays()) .ToList(); foreach (CorrectiveAction ca in followUpCAs) { await _caService.CreateCorrectiveActionFollowUpApproval(ca.CANo, ca.QAID); string qaEmail = await _userService.GetUserEmail(ca.QAID); IEnumerable recipients = new List() { new MailAddress(qaEmail) }; IEnumerable ccRecipients = new List(); string subject = $"Fab Approval CA Follow Up - CA# {ca.CANo} - {ca.CATitle}"; StringBuilder bodyBuilder = new(); bodyBuilder.Append($"Corrective Action# {ca.CANo} is ready for follow up. Please log on to Fab Approval "); bodyBuilder.Append("and review this item.

"); bodyBuilder.Append($"{_baseUrl}/CorrectiveAction/Edit?issueID={ca.CANo}

"); bodyBuilder.Append("If you have any questions or trouble, please contact a site administrator."); bodyBuilder.Append("

Thank You!"); await _smtpService.SendEmail(recipients, ccRecipients, subject, bodyBuilder.ToString()); } _logger.LogInformation("Successfully created follow up approvals for CAs needing it in five days"); } catch (Exception ex) { StringBuilder errMsgBuilder = new(); errMsgBuilder.Append("An exception occurred when attempting to create follow up approvals for "); errMsgBuilder.Append($"CAs needing it in five days. 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); } } } }