100 lines
4.4 KiB
C#
100 lines
4.4 KiB
C#
using FabApprovalWorkerService.Models;
|
|
|
|
using System.Text;
|
|
|
|
namespace FabApprovalWorkerService.Services;
|
|
|
|
public interface IECNService {
|
|
Task<IEnumerable<ECN>> GetExpiringTECNs();
|
|
Task<IEnumerable<ECN>> GetExpiredTECNs();
|
|
Task<IEnumerable<string>> GetTECNNotificationUserEmails();
|
|
}
|
|
|
|
public class ECNService : IECNService {
|
|
private readonly ILogger<ECNService> _logger;
|
|
private readonly IDalService _dalService;
|
|
|
|
public ECNService(ILogger<ECNService> logger, IDalService dalService) {
|
|
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
|
_dalService = dalService ?? throw new ArgumentNullException("IDalService not injected");
|
|
}
|
|
|
|
public async Task<IEnumerable<ECN>> GetExpiredTECNs() {
|
|
try {
|
|
_logger.LogInformation("Attempting to get all TECNs expired in the last day");
|
|
|
|
string today = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
string yesterday = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
queryBuilder.Append("select ECNNumber, IsTECN, ExpirationDate, ExtensionDate, OriginatorID, Title ");
|
|
queryBuilder.Append($"from ECN where IsTECN = 1 and ");
|
|
queryBuilder.Append($"ExpirationDate between '{yesterday}' ");
|
|
queryBuilder.Append($"and '{today}'");
|
|
|
|
IEnumerable<ECN> expiredTecns = (await _dalService.QueryAsync<ECN>(queryBuilder.ToString()))
|
|
.Where(e => e.ExtensionDate is null || e.ExtensionDate < DateTime.Now)
|
|
.ToList();
|
|
|
|
_logger.LogInformation($"Found {expiredTecns.Count()} expired TECNs");
|
|
|
|
return expiredTecns;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append("An exception occurred when attempting to get all TECNs expired in the last day. ");
|
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<ECN>> GetExpiringTECNs() {
|
|
try {
|
|
_logger.LogInformation("Attempting to get all TECNs expiring in the next five days");
|
|
|
|
string today = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
string fiveDaysFromToday = DateTime.Now.AddDays(5).ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
queryBuilder.Append("select ECNNumber, IsTECN, ExpirationDate, ExtensionDate, OriginatorID, Title ");
|
|
queryBuilder.Append($"from ECN where IsTECN = 1 and ");
|
|
queryBuilder.Append($"ExpirationDate between '{today}' ");
|
|
queryBuilder.Append($"and '{fiveDaysFromToday}'");
|
|
|
|
IEnumerable<ECN> expiringTecns = (await _dalService.QueryAsync<ECN>(queryBuilder.ToString()))
|
|
.Where(e => e.ExtensionDate is null || e.ExtensionDate <= DateTime.Now.AddDays(5))
|
|
.ToList();
|
|
|
|
_logger.LogInformation($"Found {expiringTecns.Count()} expiring TECNs");
|
|
|
|
return expiringTecns;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append("An exception occurred when attempting to get all TECNs expiring in the next five days. ");
|
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
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;
|
|
}
|
|
}
|
|
}
|