PCRB follow up client side logic

This commit is contained in:
Chase Tucker
2025-03-19 10:01:35 -07:00
parent 4871668a90
commit cc4781b990
45 changed files with 3082 additions and 1008 deletions

View File

@ -34,6 +34,7 @@ public interface IPCRBService {
Task UpdatePCR3Document(PCR3Document document);
Task<IEnumerable<PCR3Document>> GetPCR3DocumentsForPlanNumber(int planNumber, bool bypassCache);
Task NotifyNewApprovals(PCRB pcrb);
Task NotifyApprover(PCRBNotification notification);
Task NotifyApprovers(PCRBNotification notification);
Task NotifyOriginator(PCRBNotification notification);
Task NotifyResponsiblePerson(PCRBActionItemNotification notification);
@ -41,6 +42,10 @@ public interface IPCRBService {
Task<IEnumerable<PCRBFollowUp>> GetFollowUpsByPlanNumber(int planNumber, bool bypassCache);
Task UpdateFollowUp(PCRBFollowUp followUp);
Task DeleteFollowUp(int id);
Task CreateFollowUpComment(PCRBFollowUpComment comment);
Task<IEnumerable<PCRBFollowUpComment>> GetFollowUpCommentsByPlanNumber(int planNumber, bool bypassCache);
Task UpdateFollowUpComment(PCRBFollowUpComment comment);
Task DeleteFollowUpComment(int id);
}
public class PCRBService : IPCRBService {
@ -110,6 +115,7 @@ public class PCRBService : IPCRBService {
foreach (PCRB pcrb in allPCRBs) {
if (string.IsNullOrWhiteSpace(pcrb.OwnerName) && pcrb.OwnerID > 0)
pcrb.OwnerName = (await _userService.GetUserByUserId(pcrb.OwnerID)).GetFullName();
pcrb.FollowUps = await GetFollowUpsByPlanNumber(pcrb.PlanNumber, bypassCache);
}
_cache.Set("allPCRBs", allPCRBs, DateTimeOffset.Now.AddHours(1));
@ -144,6 +150,8 @@ public class PCRBService : IPCRBService {
if (string.IsNullOrWhiteSpace(pcrb.OwnerName) && pcrb.OwnerID > 0)
pcrb.OwnerName = (await _userService.GetUserByUserId(pcrb.OwnerID)).GetFullName();
pcrb.FollowUps = await GetFollowUpsByPlanNumber(pcrb.PlanNumber, bypassCache);
_cache.Set($"pcrb{planNumber}", pcrb, DateTimeOffset.Now.AddHours(1));
_cache.Set($"pcrb{pcrb.Title}", pcrb, DateTimeOffset.Now.AddHours(1));
}
@ -659,6 +667,62 @@ public class PCRBService : IPCRBService {
}
}
public async Task NotifyApprover(PCRBNotification notification) {
try {
_logger.LogInformation("Attempting to send a notification to an approver");
if (notification is null) throw new ArgumentNullException("notification cannot be null");
if (notification.PCRB is null) throw new ArgumentNullException("PCRB cannot be null");
if (notification.Approval is null) throw new ArgumentNullException("approval cannot be null");
User user = await _userService.GetUserByUserId(notification.Approval.UserID);
List<MailAddress> toAddresses = new();
toAddresses.Add(new MailAddress(user.Email));
List<string> ccEmails = new List<string>();
if (notification.NotifyQaPreApprover) {
IEnumerable<User> qaPreApprovers = await GetQAPreApprovers();
foreach (User qaPreApprover in qaPreApprovers) {
if (!ccEmails.Contains(qaPreApprover.Email))
ccEmails.Add(qaPreApprover.Email);
}
}
List<MailAddress> ccAddresses = new();
foreach (string email in ccEmails) {
ccAddresses.Add(new MailAddress(email));
}
StringBuilder sb = new();
string subject = string.Empty;
if (!string.IsNullOrWhiteSpace(notification.Subject)) {
subject = notification.Subject;
} else {
sb.Append($"[Approval Update] Mesa Fab Approval - PCRB# {notification.PCRB.PlanNumber} - ");
sb.Append($"{notification.PCRB.Title}");
subject = sb.ToString();
}
sb.Clear();
sb.Append($"{notification.Message} <br /> <br />");
sb.Append($"Click {_siteBaseUrl}/redirect?redirectPath=pcrb/{notification.PCRB.PlanNumber} ");
sb.Append("to view the PCRB.");
await _smtpService.SendEmail(toAddresses, ccAddresses, subject, sb.ToString());
notification.Approval.NotifyDate = DateTime.Now;
await _approvalService.UpdateApproval(notification.Approval);
} catch (Exception ex) {
_logger.LogError($"Unable to send notification to approver, because {ex.Message}");
throw;
}
}
public async Task NotifyApprovers(PCRBNotification notification) {
try {
_logger.LogInformation("Attempting to send notification to approvers");
@ -712,9 +776,28 @@ public class PCRBService : IPCRBService {
List<MailAddress> toAddresses = new();
toAddresses.Add(new MailAddress(user.Email));
List<MailAddress> ccAddresses = new();
List<string> ccEmails = new List<string>();
string subject = $"[Update] Mesa Fab Approval - PCRB# {notification.PCRB.PlanNumber} - {notification.PCRB.Title}";
if (notification.NotifyQaPreApprover) {
IEnumerable<User> qaPreApprovers = await GetQAPreApprovers();
foreach (User qaPreApprover in qaPreApprovers) {
if (!ccEmails.Contains(qaPreApprover.Email))
ccEmails.Add(qaPreApprover.Email);
}
}
List<MailAddress> ccAddresses = new();
foreach (string email in ccEmails) {
ccAddresses.Add(new MailAddress(email));
}
string subject = string.Empty;
if (!string.IsNullOrWhiteSpace(notification.Subject)) {
subject = notification.Subject;
} else {
subject = $"[Update] Mesa Fab Approval - PCRB# {notification.PCRB.PlanNumber} - {notification.PCRB.Title}";
}
StringBuilder bodyBuilder = new();
bodyBuilder.Append($"{notification.Message} <br /> <br />");
@ -763,8 +846,8 @@ public class PCRBService : IPCRBService {
if (followUp is null) throw new ArgumentNullException("follow up cannot be null");
StringBuilder queryBuilder = new();
queryBuilder.Append("insert into CCPCRBFollowUp (PlanNumber, Step, FollowUpDate, CompletedDate) ");
queryBuilder.Append("values (@PlanNumber, @Step, @FollowUpDate, @CompletedDate)");
queryBuilder.Append("insert into CCPCRBFollowUp (PlanNumber, Step, FollowUpDate, CompletedDate, UpdateDate) ");
queryBuilder.Append("values (@PlanNumber, @Step, @FollowUpDate, @CompletedDate, @UpdateDate)");
int rowsReturned = await _dalService.ExecuteAsync<PCRBFollowUp>(queryBuilder.ToString(), followUp);
@ -792,7 +875,7 @@ public class PCRBService : IPCRBService {
followUps = await _dalService.QueryAsync<PCRBFollowUp>(sql, new { PlanNumber = planNumber });
if (followUps is not null)
_cache.Set($"pcrbFollowUps{planNumber}", followUps, DateTimeOffset.Now.AddMinutes(15));
_cache.Set($"pcrbFollowUps{planNumber}", followUps, DateTimeOffset.Now.AddHours(1));
}
return followUps ?? new List<PCRBFollowUp>();
@ -811,7 +894,8 @@ public class PCRBService : IPCRBService {
StringBuilder queryBuilder = new();
queryBuilder.Append("update CCPCRBFollowUp set Step=@Step, FollowUpDate=@FollowUpDate, IsComplete=@IsComplete, ");
queryBuilder.Append("IsDeleted=@IsDeleted, CompletedDate=@CompletedDate, Comments=@Comments ");
queryBuilder.Append("IsDeleted=@IsDeleted, CompletedDate=@CompletedDate, IsPendingApproval=@IsPendingApproval, ");
queryBuilder.Append("UpdateDate=@UpdateDate ");
queryBuilder.Append("where ID=@ID");
int rowsAffected = await _dalService.ExecuteAsync<PCRBFollowUp>(queryBuilder.ToString(), followUp);
@ -840,6 +924,89 @@ public class PCRBService : IPCRBService {
}
}
public async Task CreateFollowUpComment(PCRBFollowUpComment comment) {
try {
_logger.LogInformation("Attempting to create PCRB follow up");
if (comment is null) throw new ArgumentNullException("comment cannot be null");
StringBuilder queryBuilder = new();
queryBuilder.Append("insert into CCPCRBFollowUpComments (PlanNumber, FollowUpID, Comment, CommentDate, UserID) ");
queryBuilder.Append("values (@PlanNumber, @FollowUpID, @Comment, @CommentDate, @UserID)");
int rowsReturned = await _dalService.ExecuteAsync<PCRBFollowUpComment>(queryBuilder.ToString(), comment);
if (rowsReturned <= 0) throw new Exception("unable to insert new follow up comment in the database");
} catch (Exception ex) {
_logger.LogError($"Unable to create new follow up comment, because {ex.Message}");
throw;
}
}
public async Task<IEnumerable<PCRBFollowUpComment>> GetFollowUpCommentsByPlanNumber(int planNumber, bool bypassCache) {
try {
_logger.LogInformation($"Attempting to fetch follow up comments for PCRB {planNumber}");
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB Plan#");
IEnumerable<PCRBFollowUpComment>? comments = new List<PCRBFollowUpComment>();
if (!bypassCache)
comments = _cache.Get<IEnumerable<PCRBFollowUpComment>>($"pcrbFollowUpComments{planNumber}");
if (comments is null || comments.Count() == 0) {
string sql = "select * from CCPCRBFollowUpComments where PlanNumber=@PlanNumber";
comments = await _dalService.QueryAsync<PCRBFollowUpComment>(sql, new { PlanNumber = planNumber });
if (comments is not null)
_cache.Set($"pcrbFollowUpComments{planNumber}", comments, DateTimeOffset.Now.AddHours(1));
}
return comments ?? new List<PCRBFollowUpComment>();
} catch (Exception ex) {
_logger.LogError($"Unable to fetch follow up comments for PCRB {planNumber}, because {ex.Message}");
throw;
}
}
public async Task UpdateFollowUpComment(PCRBFollowUpComment comment) {
try {
_logger.LogInformation("Attempting to update a follow up");
if (comment is null)
throw new ArgumentNullException("comment cannot be null");
StringBuilder queryBuilder = new();
queryBuilder.Append("update CCPCRBFollowUpComments set Comment=@Comment, CommentDate=@CommentDate, ");
queryBuilder.Append("UserID=@UserID where ID=@ID");
int rowsAffected = await _dalService.ExecuteAsync<PCRBFollowUpComment>(queryBuilder.ToString(), comment);
if (rowsAffected <= 0) throw new Exception("update failed in database");
} catch (Exception ex) {
_logger.LogError($"Unable to update follow up comment, because {ex.Message}");
throw;
}
}
public async Task DeleteFollowUpComment(int id) {
try {
_logger.LogInformation($"Attempting to delete follow up comment {id}");
if (id <= 0) throw new ArgumentException($"{id} is not a valid follow up ID");
string sql = "delete from CCPCRBFollowUpComments where ID=@ID";
int rowsAffected = await _dalService.ExecuteAsync(sql, new { ID = id });
if (rowsAffected <= 0) throw new Exception("delete operation failed in database");
} catch (Exception ex) {
_logger.LogError($"Unable to delete follow up comment {id}, because {ex.Message}");
throw;
}
}
private async Task SaveAttachmentInDb(IFormFile file, PCRBAttachment attachment) {
try {
_logger.LogInformation($"Attempting to save attachment to database");
@ -863,4 +1030,34 @@ public class PCRBService : IPCRBService {
throw;
}
}
private async Task<IEnumerable<User>> GetQAPreApprovers() {
try {
_logger.LogInformation("Attempting to fetch QA Pre-Approvers");
IEnumerable<User> qaPreApprovers = new List<User>();
int qaPreApproverRoleId = await _approvalService.GetRoleIdForRoleName("QA_PRE_APPROVAL");
if (qaPreApproverRoleId > 0) {
IEnumerable<SubRole> qaPreApproverSubRoles =
await _approvalService.GetSubRolesForSubRoleName("QA_PRE_APPROVAL", qaPreApproverRoleId);
foreach (SubRole subRole in qaPreApproverSubRoles) {
IEnumerable<User> members =
await _approvalService.GetApprovalGroupMembers(subRole.SubRoleID);
foreach (User member in members) {
if (!qaPreApprovers.Any(u => u.UserID == member.UserID))
qaPreApprovers = qaPreApprovers.Append(member);
}
}
}
return qaPreApprovers;
} catch (Exception ex) {
_logger.LogError($"Unable to fetch QA Pre-Approvers, because {ex.Message}");
throw;
}
}
}