Mike Phares 7be540964a Pull Request 33523 suggestions
Pull Request 33520 suggestions

Injected AppSettings instead of using GetEnvironmentVariable at Services level

When debugging only
app.Services.GetRequiredService<IPCRBService>();

Get ready to use VSCode IDE

Align .editorconfig files
2024-12-03 10:48:07 -07:00

87 lines
4.0 KiB
C#

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<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,
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<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);
}
}
}
}