2024-04-11 13:20:33 -07:00

88 lines
4.1 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 CAFollowUpWorker : IJob {
private readonly ILogger<CAFollowUpWorker> _logger;
private readonly ICorrectiveActionService _caService;
private readonly IUserService _userService;
private readonly ISmtpService _smtpService;
private readonly IMonInClient _monInClient;
private readonly string _baseUrl;
public CAFollowUpWorker(ILogger<CAFollowUpWorker> logger,
ICorrectiveActionService caService,
IUserService userService,
ISmtpService smtpService,
IMonInClient monInClient) {
_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 = 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 = "CAFollowUpWorker";
try {
_logger.LogInformation("Attempting to create follow up approvals for CAs needing it in five days");
IEnumerable<CorrectiveAction> 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<MailAddress> recipients = new List<MailAddress>() {
new MailAddress(qaEmail)
};
IEnumerable<MailAddress> ccRecipients = new List<MailAddress>();
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. <br/><br/>");
bodyBuilder.Append($"{_baseUrl}/CorrectiveAction/Edit?issueID={ca.CANo} <br/><br/>");
bodyBuilder.Append("If you have any questions or trouble, please contact a site administrator.");
bodyBuilder.Append("<br/><br/> 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);
}
}
}
}