2024-04-09 10:44:57 -07:00

118 lines
5.5 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");
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;
}
}
}