2024-04-11 13:20:33 -07:00

68 lines
3.3 KiB
C#

using FabApprovalWorkerService.Models;
using System.Text;
namespace FabApprovalWorkerService.Services;
public interface ICorrectiveActionService {
Task<IEnumerable<CorrectiveAction>> GetCorrectiveActionsWithFollowUpInFiveDays();
Task CreateCorrectiveActionFollowUpApproval(int caNo, int qaId);
}
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;
}
}
}