167 lines
7.3 KiB
C#
167 lines
7.3 KiB
C#
using FabApprovalWorkerService.Models;
|
|
|
|
using System.Text;
|
|
|
|
namespace FabApprovalWorkerService.Services;
|
|
|
|
public interface IECNService {
|
|
Task<IEnumerable<ECN>> GetExpiringTECNs();
|
|
Task<IEnumerable<ECN>> GetExpiredTECNsInPastDay();
|
|
Task<IEnumerable<string>> GetTECNNotificationUserEmails();
|
|
Task<ECN> GetEcnByNumber(int ecnNumber);
|
|
bool EcnIsExpired(ECN ecn);
|
|
}
|
|
|
|
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 bool EcnIsExpired(ECN ecn) {
|
|
try {
|
|
_logger.LogInformation("Attempting to determine if ECN is expired");
|
|
|
|
if (ecn is null) throw new ArgumentNullException("ECN cannot be null");
|
|
|
|
if (!ecn.IsTECN) return false;
|
|
if (ecn.CloseDate <= DateTime.Now) return false;
|
|
|
|
DateTime tomorrow = DateTime.Now.Date.AddDays(1);
|
|
|
|
bool isExpired = (ecn.ExpirationDate < tomorrow) && (ecn.ExtensionDate < tomorrow);
|
|
|
|
_logger.LogInformation($"ECN {ecn.ECNNumber} expired: {isExpired}");
|
|
|
|
return isExpired;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append($"An exception occurred when attempting to determine if ECN is expired. ");
|
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<ECN> GetEcnByNumber(int ecnNumber) {
|
|
try {
|
|
_logger.LogInformation($"Attempting to get ECN {ecnNumber}");
|
|
|
|
if (ecnNumber <= 0) throw new ArgumentException($"{ecnNumber} not a valid ECN number");
|
|
|
|
string sql = $"select * from ECN where ECNNumber = {ecnNumber}";
|
|
|
|
ECN? ecn = (await _dalService.QueryAsync<ECN>(sql)).FirstOrDefault();
|
|
|
|
if (ecn is null) throw new Exception($"ECN {ecnNumber} not found");
|
|
|
|
return ecn;
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append($"An exception occurred when attempting to get ECN {ecnNumber}. ");
|
|
errMsgBuilder.Append($"Exception: {ex.Message}");
|
|
_logger.LogError(errMsgBuilder.ToString());
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<ECN>> GetExpiredTECNsInPastDay() {
|
|
try {
|
|
_logger.LogInformation("Attempting to get all TECNs expired in the last day");
|
|
|
|
DateTime today = DateTime.Now.Date;
|
|
DateTime tomorrow = DateTime.Now.Date.AddDays(1);
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
queryBuilder.Append("select ECNNumber, IsTECN, ExpirationDate, ExtensionDate, OriginatorID, Title ");
|
|
queryBuilder.Append($"from ECN where IsTECN = 1 and ");
|
|
queryBuilder.Append("Cancelled = 0 and Deleted = 0 ");
|
|
queryBuilder.Append("and (CloseDate IS NULL or CloseDate = '');");
|
|
|
|
IEnumerable<ECN> activeTecns = await _dalService.QueryAsync<ECN>(queryBuilder.ToString());
|
|
|
|
_logger.LogInformation($"There are {activeTecns.Count()} active TECNs");
|
|
|
|
IEnumerable<ECN> expiredTecns = activeTecns.Where(e => ((e.ExpirationDate < tomorrow) && (e.ExpirationDate >= today)) ||
|
|
((e.ExtensionDate < tomorrow) && (e.ExtensionDate >= today)));
|
|
|
|
_logger.LogInformation($"There are {expiredTecns.Count()} TECNs with an expiration date or extension date today");
|
|
|
|
IEnumerable<ECN> expiredTecnsNotExtended = expiredTecns
|
|
.Where(e => !(e.ExtensionDate >= tomorrow))
|
|
.ToList();
|
|
|
|
_logger.LogInformation($"There are {expiredTecnsNotExtended.Count()} TECNs expiring today");
|
|
|
|
return expiredTecnsNotExtended;
|
|
} 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");
|
|
|
|
DateTime tomorrow = DateTime.Now.AddDays(1).Date;
|
|
DateTime fiveDaysFromToday = DateTime.Now.AddDays(5).Date;
|
|
|
|
StringBuilder queryBuilder = new StringBuilder();
|
|
queryBuilder.Append("select * from ECN ");
|
|
queryBuilder.Append($"where IsTECN = 1 and ");
|
|
queryBuilder.Append("Cancelled = 0 and Deleted = 0 ");
|
|
queryBuilder.Append("and (CloseDate IS NULL or CloseDate = '');");
|
|
|
|
IEnumerable<ECN> activeTecns = (await _dalService.QueryAsync<ECN>(queryBuilder.ToString()));
|
|
|
|
_logger.LogInformation($"There are {activeTecns.Count()} active TECNs");
|
|
|
|
IEnumerable<ECN> expiringTecns = activeTecns.Where(e => ((e.ExpirationDate >= tomorrow) && (e.ExpirationDate <= fiveDaysFromToday)) ||
|
|
((e.ExtensionDate >= tomorrow) && (e.ExtensionDate <= fiveDaysFromToday)));
|
|
|
|
_logger.LogInformation($"There are {expiringTecns.Count()} TECNs with an expiration date or extension date in the next 5 days");
|
|
|
|
IEnumerable<ECN> expiringTecnsNotExtended = expiringTecns
|
|
.Where(e => !(e.ExtensionDate > fiveDaysFromToday))
|
|
.ToList();
|
|
|
|
_logger.LogInformation($"There are {expiringTecnsNotExtended.Count()} TECNs expiring in the next five days");
|
|
|
|
return expiringTecnsNotExtended;
|
|
} 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;
|
|
}
|
|
}
|
|
}
|