Created ExpiringTECNWorker
This commit is contained in:
@ -7,6 +7,7 @@ namespace FabApprovalWorkerService.Services;
|
|||||||
public interface IECNService {
|
public interface IECNService {
|
||||||
Task<IEnumerable<ECN>> GetExpiringTECNs();
|
Task<IEnumerable<ECN>> GetExpiringTECNs();
|
||||||
Task<IEnumerable<ECN>> GetExpiredTECNs();
|
Task<IEnumerable<ECN>> GetExpiredTECNs();
|
||||||
|
Task<IEnumerable<string>> GetTECNNotificationUserEmails();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ECNService : IECNService {
|
public class ECNService : IECNService {
|
||||||
@ -75,4 +76,24 @@ public class ECNService : IECNService {
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<string>> GetTECNNotificationUserEmails() {
|
||||||
|
try {
|
||||||
|
_logger.LogInformation("Attempting to get TECN notification user emails");
|
||||||
|
|
||||||
|
string sql = "select u.Email from TECNNotificationsUsers t join Users u on t.UserId = u.UserID;";
|
||||||
|
|
||||||
|
IEnumerable<string> tecnNotificationUserEmails = (await _dalService.QueryAsync<string>(sql)).ToList();
|
||||||
|
|
||||||
|
_logger.LogInformation($"Found {tecnNotificationUserEmails.Count()} TECN notification user emails");
|
||||||
|
|
||||||
|
return tecnNotificationUserEmails;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
StringBuilder errMsgBuilder = new();
|
||||||
|
errMsgBuilder.Append("An exception occurred when attempting to get TECN notification user emails. ");
|
||||||
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
||||||
|
_logger.LogError(errMsgBuilder.ToString());
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
using FabApprovalWorkerService.Models;
|
using FabApprovalWorkerService.Models;
|
||||||
|
|
||||||
using NLog.Filters;
|
|
||||||
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FabApprovalWorkerService.Services;
|
namespace FabApprovalWorkerService.Services;
|
||||||
@ -20,6 +18,7 @@ public interface IUserService {
|
|||||||
Task<bool> RemoveOOOFlagForUser(int userId);
|
Task<bool> RemoveOOOFlagForUser(int userId);
|
||||||
Task<bool> SetOOOTempProcessed(OOOTemp oOOTemp);
|
Task<bool> SetOOOTempProcessed(OOOTemp oOOTemp);
|
||||||
Task<List<User>> GetAllExpiredOOOUsersAsync();
|
Task<List<User>> GetAllExpiredOOOUsersAsync();
|
||||||
|
Task<string> GetUserEmail(int userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UserService : IUserService {
|
public class UserService : IUserService {
|
||||||
@ -341,4 +340,27 @@ public class UserService : IUserService {
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<string> GetUserEmail(int userId) {
|
||||||
|
try {
|
||||||
|
if (userId <= 0) throw new ArgumentException($"User Id {userId} is not a valid user Id");
|
||||||
|
|
||||||
|
_logger.LogInformation($"Attempting to get email for user {userId}");
|
||||||
|
|
||||||
|
string sql = $"select Email from Users where UserID = {userId}";
|
||||||
|
|
||||||
|
string? userEmail = (await _dalService.QueryAsync<string>(sql)).ToList().FirstOrDefault();
|
||||||
|
|
||||||
|
if (userEmail is null)
|
||||||
|
throw new Exception($"No email found for user {userId}");
|
||||||
|
|
||||||
|
return userEmail;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
StringBuilder errMsgBuilder = new();
|
||||||
|
errMsgBuilder.Append($"An exception occurred when attempting to get email for user {userId}. ");
|
||||||
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
||||||
|
_logger.LogError(errMsgBuilder.ToString());
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
86
FabApprovalWorkerService/Workers/ExpiringTECNWorker.cs
Normal file
86
FabApprovalWorkerService/Workers/ExpiringTECNWorker.cs
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
using FabApprovalWorkerService.Models;
|
||||||
|
using FabApprovalWorkerService.Services;
|
||||||
|
|
||||||
|
using Quartz;
|
||||||
|
|
||||||
|
using System.Net.Mail;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FabApprovalWorkerService.Workers;
|
||||||
|
|
||||||
|
public class ExpiringTECNWorker : IJob {
|
||||||
|
private readonly ILogger<ExpiringTECNWorker> _logger;
|
||||||
|
private readonly IMonInWorkerClient _monInClient;
|
||||||
|
private readonly IUserService _userService;
|
||||||
|
private readonly IECNService _ecnService;
|
||||||
|
private readonly ISmtpService _smtpService;
|
||||||
|
|
||||||
|
public ExpiringTECNWorker(ILogger<ExpiringTECNWorker> logger,
|
||||||
|
IMonInWorkerClient monInClient,
|
||||||
|
IUserService userService,
|
||||||
|
IECNService ecnService,
|
||||||
|
ISmtpService smtpService) {
|
||||||
|
_logger = logger ??
|
||||||
|
throw new ArgumentNullException("ILogger not injected");
|
||||||
|
_monInClient = monInClient ??
|
||||||
|
throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||||
|
_userService = userService ??
|
||||||
|
throw new ArgumentNullException("IUserService not injected");
|
||||||
|
_ecnService = ecnService ??
|
||||||
|
throw new ArgumentNullException("IECNService not injected");
|
||||||
|
_smtpService = smtpService ??
|
||||||
|
throw new ArgumentNullException("ISmtpService not injected");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Execute(IJobExecutionContext context) {
|
||||||
|
DateTime start = DateTime.Now;
|
||||||
|
bool isInternalError = false;
|
||||||
|
StringBuilder errorMessage = new();
|
||||||
|
string metricName = "ExpiringTECNWorker";
|
||||||
|
|
||||||
|
try {
|
||||||
|
_logger.LogInformation("Attempting to notify users of expiring TECNs");
|
||||||
|
|
||||||
|
IEnumerable<ECN> expiringTECNs = await _ecnService.GetExpiringTECNs();
|
||||||
|
|
||||||
|
foreach (ECN eCN in expiringTECNs) {
|
||||||
|
string recipientEmail = await _userService.GetUserEmail(eCN.OriginatorID);
|
||||||
|
MailAddress recipientAddress = new MailAddress(recipientEmail);
|
||||||
|
List<MailAddress> recipientList = new () { recipientAddress };
|
||||||
|
|
||||||
|
IEnumerable<string> tecnNotificationUserEmails = await _ecnService.GetTECNNotificationUserEmails();
|
||||||
|
List<MailAddress> ccRecipientList = new();
|
||||||
|
foreach (string email in tecnNotificationUserEmails) {
|
||||||
|
ccRecipientList.Add(new MailAddress(email));
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder bodyBuilder = new();
|
||||||
|
bodyBuilder.Append($"Good day, TECN# {eCN.ECNNumber} will be expiring in ");
|
||||||
|
bodyBuilder.Append($"{(eCN.ExpirationDate - DateTime.Now).Days} ");
|
||||||
|
bodyBuilder.Append($"on {eCN.ExpirationDate.ToString("MMMM dd, yyyy")}. ");
|
||||||
|
bodyBuilder.Append($"<br /> Review TECN <a href='https://mesaapproval.mes.com/ECN/Edit?IssueID={eCN.ECNNumber}'> ");
|
||||||
|
bodyBuilder.Append("here </a>");
|
||||||
|
|
||||||
|
string subject = $"Notice of expiring TECN - {eCN.Title}";
|
||||||
|
|
||||||
|
await _smtpService.SendEmail(recipientList, ccRecipientList, subject, bodyBuilder.ToString());
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
StringBuilder errMsgBuilder = new();
|
||||||
|
errMsgBuilder.Append("An exception occurred when attempting to notify users of expiring TECNs. ");
|
||||||
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
||||||
|
_logger.LogError(errMsgBuilder.ToString());
|
||||||
|
isInternalError = true;
|
||||||
|
} finally {
|
||||||
|
DateTime end = DateTime.Now;
|
||||||
|
double latencyInMS = (end - start).TotalMilliseconds;
|
||||||
|
_monInClient.PostAverage(metricName + "Latency", latencyInMS);
|
||||||
|
|
||||||
|
if (isInternalError) {
|
||||||
|
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||||
|
} else {
|
||||||
|
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -142,7 +142,6 @@ internal class ECNServiceTests {
|
|||||||
Assert.That(actual.Count(), Is.EqualTo(3));
|
Assert.That(actual.Count(), Is.EqualTo(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test when responses include extension dates
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task GetExpiringTECNsWithExtensionsFromDbShouldReturnSameResults() {
|
public async Task GetExpiringTECNsWithExtensionsFromDbShouldReturnSameResults() {
|
||||||
IEnumerable<ECN> ecns = new List<ECN>() {
|
IEnumerable<ECN> ecns = new List<ECN>() {
|
||||||
@ -214,4 +213,29 @@ internal class ECNServiceTests {
|
|||||||
|
|
||||||
Assert.That(actual.Count(), Is.EqualTo(2));
|
Assert.That(actual.Count(), Is.EqualTo(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetTECNNotificationUserEmailsDbErrorShouldThrowException() {
|
||||||
|
_mockDalService.Setup(d => d.QueryAsync<string>(It.IsAny<string>())).Throws(new Exception());
|
||||||
|
|
||||||
|
_ecnService = new ECNService(_mockLogger.Object, _mockDalService.Object);
|
||||||
|
|
||||||
|
Assert.ThrowsAsync<Exception>(async Task () => await _ecnService.GetTECNNotificationUserEmails());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetTECNNotificationUserEmailsShouldReturnExpectedUserEmails() {
|
||||||
|
IEnumerable<string> userEmails = new List<string>() {
|
||||||
|
"fake1@email.com",
|
||||||
|
"fake2@email.com"
|
||||||
|
};
|
||||||
|
|
||||||
|
_mockDalService.Setup(d => d.QueryAsync<string>(It.IsAny<string>())).Returns(Task.FromResult(userEmails));
|
||||||
|
|
||||||
|
_ecnService = new ECNService(_mockLogger.Object, _mockDalService.Object);
|
||||||
|
|
||||||
|
IEnumerable<string> actualEmails = await _ecnService.GetTECNNotificationUserEmails();
|
||||||
|
|
||||||
|
Assert.That(actualEmails.Count(), Is.EqualTo(2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -443,4 +443,33 @@ internal class UserServiceTests {
|
|||||||
|
|
||||||
Assert.False(actual);
|
Assert.False(actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetUserEmailWithInvalidUserIdShouldThrowException() {
|
||||||
|
_userService = new UserService(MOCK_LOGGER, _mockDalService.Object);
|
||||||
|
|
||||||
|
Assert.ThrowsAsync<ArgumentException>(async Task() => await _userService.GetUserEmail(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetUserEmailWithDbErrorShouldThrowException() {
|
||||||
|
_mockDalService.Setup(d => d.QueryAsync<string>(It.IsAny<string>())).Throws(new Exception());
|
||||||
|
|
||||||
|
_userService = new UserService(MOCK_LOGGER, _mockDalService.Object);
|
||||||
|
|
||||||
|
Assert.ThrowsAsync<Exception>(async Task() => await _userService.GetUserEmail(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task GetUserEmailShouldReturnExpectedEmail() {
|
||||||
|
IEnumerable<string> emailsFromDb = new List<string>() { "user@email.com" };
|
||||||
|
|
||||||
|
_mockDalService.Setup(d => d.QueryAsync<string>(It.IsAny<string>())).Returns(Task.FromResult(emailsFromDb));
|
||||||
|
|
||||||
|
_userService = new UserService(MOCK_LOGGER, _mockDalService.Object);
|
||||||
|
|
||||||
|
string actualEmail = await _userService.GetUserEmail(5);
|
||||||
|
|
||||||
|
Assert.That(actualEmail, Is.EqualTo(emailsFromDb.First()));
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user