2024-10-08 08:08:13 -07:00

110 lines
5.0 KiB
C#

using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Utilities;
using System.Text;
namespace FabApprovalWorkerService.Services;
public interface ICorrectiveActionService {
Task<IEnumerable<CorrectiveAction>> GetCorrectiveActionsWithFollowUpInFiveDays();
Task CreateCorrectiveActionFollowUpApproval(int caNo, int qaId);
Task<bool> CANumberIsActive(int number);
Task<CorrectiveAction> GetCorrectiveActionById(int id);
}
public class CorrectiveActionService : ICorrectiveActionService {
private readonly ILogger<CorrectiveActionService> _logger;
private readonly IDalService _dalService;
public CorrectiveActionService(ILogger<CorrectiveActionService> logger, IDalService dalService) {
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
_dalService = dalService ?? throw new ArgumentNullException("IDalService not injected");
}
public async Task CreateCorrectiveActionFollowUpApproval(int caNo, int qaId) {
try {
_logger.LogInformation($"Attempting to create a follow up approval for CA {caNo} by QA {qaId}");
if (caNo <= 0) throw new ArgumentException($"{caNo} is not a valid CA number");
if (qaId <= 0) throw new ArgumentException($"{qaId} is not a valid User Id");
string today = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
StringBuilder queryBuilder = new();
queryBuilder.Append("insert into Approval (IssueID, RoleName, SubRole, UserID, SubRoleID, ItemStatus, Step, ");
queryBuilder.Append("AssignedDate, NotifyDate,RoleAssignedDate, ApprovalType, DocumentTypeID) ");
queryBuilder.Append($"values ({caNo}, '8DQAFollowUp', '8DQAFollowUp', {qaId}, 335, 0, 2, ");
queryBuilder.Append($"'{today}', '{today}', '{today}', 1, 9);");
await _dalService.ExecuteAsync(queryBuilder.ToString());
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append($"An exception occurred when attempting to create a follow up approval for CA {caNo} by QA {qaId}. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
throw;
}
}
public async Task<IEnumerable<CorrectiveAction>> GetCorrectiveActionsWithFollowUpInFiveDays() {
try {
_logger.LogInformation("Attempting to get all CAs needing follow up in five days");
DateTime fiveDaysFromToday = DateTime.Now.Date.AddDays(5);
DateTime sixDaysFromToday = DateTime.Now.Date.AddDays(6);
StringBuilder queryBuilder = new();
queryBuilder.Append("select * from _8DCorrectiveAction where ApprovalStatus = 1 and FollowUpDate is not null ");
queryBuilder.Append($"and FollowUpDate < '{sixDaysFromToday.ToString("yyyy-MM-dd HH:mm:ss")}' ");
queryBuilder.Append($"and FollowUpDate >= '{fiveDaysFromToday.ToString("yyyy-MM-dd HH:mm:ss")}';");
return (await _dalService.QueryAsync<CorrectiveAction>(queryBuilder.ToString())).ToList();
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append($"An exception occurred when attempting to get all CAs needing follow up in five days. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
throw;
}
}
public async Task<bool> CANumberIsActive(int number) {
try {
_logger.LogInformation($"Attempting to determine if {number} is an active CA#");
if (number <= 0) return false;
StringBuilder queryBuilder = new();
queryBuilder.Append($"select count(CANo) as count from _8DCorrectiveAction ");
queryBuilder.Append($"where CANo={number} and Deleted=0 and ");
queryBuilder.Append($"(ClosedDate is null or ClosedDate >= '{DateTimeUtilities.MAX_DT.ToString("yyyy-MM-dd HH:mm")}')");
int rowsReturned = (await _dalService.QueryAsync<int>(queryBuilder.ToString())).FirstOrDefault();
return rowsReturned > 0;
} catch (Exception ex) {
_logger.LogError($"Unable to determine if {number} is an active CA#, because {ex.Message}");
throw;
}
}
public async Task<CorrectiveAction> GetCorrectiveActionById(int id) {
try {
_logger.LogInformation($"Attempting to get CA# {id}");
if (id <= 0) throw new ArgumentException($"{id} is not a valid CA#");
string sql = $"select * from _8DCorrectiveAction where CANo={id}";
CorrectiveAction? ca = (await _dalService.QueryAsync<CorrectiveAction>(sql)).FirstOrDefault();
if (ca is null) throw new Exception($"no CA found with CA# {id}");
return ca;
} catch (Exception ex) {
_logger.LogError($"Unable to get CA# {id}, because {ex.Message}");
throw;
}
}
}