PCRB webassembly
This commit is contained in:
@ -42,7 +42,7 @@ public class ApprovalService : IApprovalService {
|
||||
queryBuilder.Append("insert into Approval (IssueID, RoleName, SubRole, UserID, SubRoleID, ItemStatus, ");
|
||||
queryBuilder.Append("AssignedDate, DocumentTypeID, DisplayDeniedDocument, Step, TaskID) ");
|
||||
queryBuilder.Append($"values ({approval.IssueID}, '{approval.RoleName}', '{approval.SubRole}', {approval.UserID}, ");
|
||||
queryBuilder.Append($"{approval.SubRoleID}, 0, '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"{approval.SubRoleID}, 0, '{approval.AssignedDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"3, 0, {approval.Step}, {approval.TaskID});");
|
||||
|
||||
int rowsCreated = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
@ -145,7 +145,7 @@ public class ApprovalService : IApprovalService {
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append("select src.SubRoleCategoryID, sr.SubRole as SubRoleName, src.SubRoleCategoryItem, sr.SubRoleID ");
|
||||
queryBuilder.Append("from SubRole sr join SubRoleCategory src on sr.SubRoleCategoryID=src.SubRoleCategoryID ");
|
||||
queryBuilder.Append($"where sr.RoleID={roleId} and sr.SubRole='{subRoleName}'");
|
||||
queryBuilder.Append($"where sr.RoleID={roleId} and sr.SubRole='{subRoleName}' and sr.Inactive=0");
|
||||
|
||||
subRoles = (await _dalService.QueryAsync<SubRole>(queryBuilder.ToString())).ToList();
|
||||
|
||||
@ -182,7 +182,7 @@ public class ApprovalService : IApprovalService {
|
||||
if (memberIds is null || memberIds.Count() <= 0)
|
||||
throw new Exception($"No members found in sub role {subRoleId}");
|
||||
|
||||
_cache.Set($"approvalMemberIds{subRoleId}", memberIds, DateTimeOffset.Now.AddHours(1));
|
||||
_cache.Set($"approvalMemberIds{subRoleId}", memberIds, DateTimeOffset.Now.AddMinutes(5));
|
||||
}
|
||||
|
||||
members = new();
|
||||
@ -194,7 +194,7 @@ public class ApprovalService : IApprovalService {
|
||||
|
||||
if (members.Count() <= 0) throw new Exception("No users found with IDs matching those found in SubRole");
|
||||
|
||||
_cache.Set($"approvalMembers{subRoleId}", members, DateTimeOffset.Now.AddHours(1));
|
||||
_cache.Set($"approvalMembers{subRoleId}", members, DateTimeOffset.Now.AddMinutes(5));
|
||||
}
|
||||
|
||||
return members;
|
||||
@ -249,7 +249,7 @@ public class ApprovalService : IApprovalService {
|
||||
queryBuilder.Append($"NotifyDate='{approval.NotifyDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"AssignedDate='{approval.AssignedDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"CompletedDate='{approval.CompletedDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"Comments='{approval.Comments}', ");
|
||||
queryBuilder.Append($"Comments='{approval.Comments.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"TaskID={approval.TaskID} ");
|
||||
queryBuilder.Append($"where ApprovalID={approval.ApprovalID};");
|
||||
|
||||
|
@ -147,7 +147,7 @@ public class AuthenticationService : IAuthenticationService {
|
||||
Audience = _jwtAudience,
|
||||
Subject = identity,
|
||||
NotBefore = DateTime.Now,
|
||||
Expires = DateTime.Now.AddHours(2),
|
||||
Expires = DateTime.Now.AddHours(8),
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
using System.Net;
|
||||
using System.Data;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
|
||||
using MesaFabApproval.API.Utilities;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Utilities;
|
||||
|
||||
@ -30,6 +32,7 @@ public interface IMRBService {
|
||||
Task NotifyOriginator(MRBNotification notification);
|
||||
Task NotifyQAPreApprover(MRBNotification notification);
|
||||
Task DeleteMRB(int mrbNumber);
|
||||
Task ConvertActionsToCsvFile(int mrbNumber, string path);
|
||||
}
|
||||
|
||||
public class MRBService : IMRBService {
|
||||
@ -130,9 +133,6 @@ public class MRBService : IMRBService {
|
||||
_cache.Set("allMrbs", allMrbs, DateTimeOffset.Now.AddHours(1));
|
||||
}
|
||||
|
||||
if (allMrbs is null || allMrbs.Count() == 0)
|
||||
throw new Exception("No MRBs found");
|
||||
|
||||
return allMrbs;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"An exception occurred when attempting to get all MRBs. Exception: {ex.Message}");
|
||||
@ -236,7 +236,7 @@ public class MRBService : IMRBService {
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update MRB set OriginatorID = {mrb.OriginatorID}, ");
|
||||
queryBuilder.Append($"Title = '{mrb.Title}', ");
|
||||
queryBuilder.Append($"Title = '{mrb.Title.Replace("'", "''")}', ");
|
||||
if (mrb.SubmittedDate < DateTimeUtilities.MIN_DT)
|
||||
mrb.SubmittedDate = DateTimeUtilities.MIN_DT;
|
||||
queryBuilder.Append($"SubmittedDate = '{mrb.SubmittedDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
@ -250,19 +250,19 @@ public class MRBService : IMRBService {
|
||||
if (mrb.ApprovalDate > DateTimeUtilities.MAX_DT)
|
||||
mrb.ApprovalDate = DateTimeUtilities.MAX_DT;
|
||||
queryBuilder.Append($"ApprovalDate = '{mrb.ApprovalDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"IssueDescription = '{mrb.IssueDescription}', ");
|
||||
queryBuilder.Append($"IssueDescription = '{mrb.IssueDescription.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"CustomerImpacted = {Convert.ToInt32(mrb.CustomerImpacted)}, ");
|
||||
queryBuilder.Append($"Department = '{mrb.Department}', ");
|
||||
queryBuilder.Append($"Process = '{mrb.Process}', ");
|
||||
queryBuilder.Append($"Department = '{mrb.Department.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"Process = '{mrb.Process.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"Val = {mrb.Val}, ");
|
||||
queryBuilder.Append($"RMANo = {mrb.RMANo}, ");
|
||||
queryBuilder.Append($"PCRBNo = '{mrb.PCRBNo}', ");
|
||||
queryBuilder.Append($"SpecsImpacted = {Convert.ToInt32(mrb.SpecsImpacted)}, ");
|
||||
queryBuilder.Append($"TrainingRequired = {Convert.ToInt32(mrb.TrainingRequired)}, ");
|
||||
queryBuilder.Append($"Status = '{mrb.Status}', StageNo = {mrb.StageNo}, ");
|
||||
queryBuilder.Append($"CustomerImpactedName = '{mrb.CustomerImpactedName}', ");
|
||||
queryBuilder.Append($"CustomerImpactedName = '{mrb.CustomerImpactedName.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"ProcessECNNumber = '{mrb.ProcessECNNumber}', ");
|
||||
queryBuilder.Append($"Tool = '{mrb.Tool}', Category = '{mrb.Category}' ");
|
||||
queryBuilder.Append($"Tool = '{mrb.Tool.Replace("'", "''")}', Category = '{mrb.Category.Replace("'", "''")}' ");
|
||||
queryBuilder.Append($"where MRBNumber = {mrb.MRBNumber};");
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
@ -353,10 +353,10 @@ public class MRBService : IMRBService {
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update MRBAction set Action = '{mrbAction.Action}', ");
|
||||
queryBuilder.Append($"Customer = '{mrbAction.Customer}', ");
|
||||
queryBuilder.Append($"Customer = '{mrbAction.Customer.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"Quantity = {mrbAction.Quantity}, ");
|
||||
queryBuilder.Append($"PartNumber = '{mrbAction.PartNumber}', ");
|
||||
queryBuilder.Append($"LotNumber = '{mrbAction.LotNumber}', ");
|
||||
queryBuilder.Append($"PartNumber = '{mrbAction.PartNumber.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"LotNumber = '{mrbAction.LotNumber.Replace("'", "''")}', ");
|
||||
if (mrbAction.AssignedDate < DateTimeUtilities.MIN_DT)
|
||||
mrbAction.AssignedDate = DateTimeUtilities.MIN_DT;
|
||||
queryBuilder.Append($"AssignedDate= '{mrbAction.AssignedDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
@ -364,9 +364,9 @@ public class MRBService : IMRBService {
|
||||
mrbAction.CompletedDate = DateTimeUtilities.MAX_DT;
|
||||
queryBuilder.Append($"CompletedDate= '{mrbAction.CompletedDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"CompletedByUserID={mrbAction.CompletedByUserID}, ");
|
||||
queryBuilder.Append($"ConvertFrom='{mrbAction.ConvertFrom}', ");
|
||||
queryBuilder.Append($"ConvertTo='{mrbAction.ConvertTo}', ");
|
||||
queryBuilder.Append($"Justification='{mrbAction.Justification}' ");
|
||||
queryBuilder.Append($"ConvertFrom='{mrbAction.ConvertFrom.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"ConvertTo='{mrbAction.ConvertTo.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"Justification='{mrbAction.Justification.Replace("'", "''")}' ");
|
||||
queryBuilder.Append($"where ActionID={mrbAction.ActionID};");
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
@ -427,7 +427,7 @@ public class MRBService : IMRBService {
|
||||
string encodedName = WebUtility.HtmlEncode(file.FileName);
|
||||
string path = $"{_mrbAttachmentPath}\\{mrbNumber}\\{encodedName}";
|
||||
|
||||
await SaveFileToFileSystem(file, path);
|
||||
await FileUtilities.SaveFileToFileSystem(file, path);
|
||||
await SaveAttachmentInDb(file, path, mrbNumber);
|
||||
|
||||
UploadResult uploadResult = new() {
|
||||
@ -471,7 +471,7 @@ public class MRBService : IMRBService {
|
||||
string encodedName = WebUtility.HtmlEncode(file.FileName);
|
||||
string path = $"{_mrbAttachmentPath}\\{actionId}\\{encodedName}";
|
||||
|
||||
taskList.Add(SaveFileToFileSystem(file, path));
|
||||
taskList.Add(FileUtilities.SaveFileToFileSystem(file, path));
|
||||
taskList.Add(SaveActionAttachmentInDb(file, path, actionId));
|
||||
|
||||
UploadResult uploadResult = new() {
|
||||
@ -755,24 +755,138 @@ public class MRBService : IMRBService {
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveFileToFileSystem(IFormFile file, string path) {
|
||||
public async Task ConvertActionsToCsvFile(int mrbNumber, string path) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to save file to file system");
|
||||
_logger.LogInformation($"Attempting to convert MRB {mrbNumber} actions to a CSV file");
|
||||
|
||||
if (file is null) throw new ArgumentNullException("File cannot be null");
|
||||
if (!(await MRBNumberIsValid(mrbNumber))) throw new ArgumentException($"{mrbNumber} is not a valid ");
|
||||
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Path cannot be null or empty");
|
||||
|
||||
if (File.Exists(path)) throw new Exception($"A file already exists with name {file.FileName}");
|
||||
if (File.Exists(path)) File.Delete(path);
|
||||
|
||||
string? directoryPath = Path.GetDirectoryName(path);
|
||||
if (!string.IsNullOrWhiteSpace(directoryPath))
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
|
||||
using (Stream stream = File.Create(path)) {
|
||||
await file.CopyToAsync(stream);
|
||||
IEnumerable<MRBAction> actions = await GetMRBActionsForMRB(mrbNumber, false);
|
||||
|
||||
DataTable dt = await ConvertActionsToDataTable(actions);
|
||||
|
||||
using StreamWriter sw = new StreamWriter(path, false);
|
||||
|
||||
for (int i = 0; i < dt.Columns.Count; i++) {
|
||||
sw.Write(dt.Columns[i]);
|
||||
if (i < dt.Columns.Count - 1) sw.Write(",");
|
||||
}
|
||||
|
||||
sw.Write(sw.NewLine);
|
||||
|
||||
foreach (DataRow dr in dt.Rows) {
|
||||
for (int i = 0; i < dt.Columns.Count; i++) {
|
||||
if (!Convert.IsDBNull(dr[i])) {
|
||||
string? value = dr[i].ToString();
|
||||
if (value is null) {
|
||||
sw.Write("");
|
||||
} else if (value.Contains(',')) {
|
||||
value = String.Format("\"{0}\"", value);
|
||||
sw.Write(value);
|
||||
} else {
|
||||
sw.Write(dr[i].ToString());
|
||||
}
|
||||
}
|
||||
|
||||
if (i < dt.Columns.Count - 1) {
|
||||
sw.Write(",");
|
||||
}
|
||||
}
|
||||
|
||||
sw.Write(sw.NewLine);
|
||||
}
|
||||
|
||||
sw.Close();
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"An exception occurred when attempting to save file to file system. Exception: {ex.Message}");
|
||||
_logger.LogError($"Unable to convert MRB {mrbNumber} actions to a CSV file, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<DataTable> ConvertActionsToDataTable(IEnumerable<MRBAction> actions) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to convert MRB actions to a DataTable");
|
||||
|
||||
if (actions is null) throw new ArgumentNullException("MRB actions cannot be null");
|
||||
|
||||
DataTable dt = new();
|
||||
|
||||
if (actions.Count() > 0 && actions.First().Action.Equals("Convert", StringComparison.InvariantCultureIgnoreCase)) {
|
||||
dt.Columns.Add("Action", typeof(string));
|
||||
dt.Columns.Add("From Customer", typeof(string));
|
||||
dt.Columns.Add("From Part Number", typeof(string));
|
||||
dt.Columns.Add("Batch Number / Lot Number", typeof(string));
|
||||
dt.Columns.Add("Qty", typeof(string));
|
||||
dt.Columns.Add("To Customer", typeof(string));
|
||||
dt.Columns.Add("To Part Number", typeof(string));
|
||||
dt.Columns.Add("Assigned Date", typeof(string));
|
||||
dt.Columns.Add("Completed Date", typeof(string));
|
||||
dt.Columns.Add("Completed By", typeof(string));
|
||||
|
||||
foreach (MRBAction action in actions) {
|
||||
if (action.CompletedByUser is null && action.CompletedByUserID > 0)
|
||||
action.CompletedByUser = await _userService.GetUserByUserId(action.CompletedByUserID);
|
||||
|
||||
string convertFromCustomer = string.Empty;
|
||||
string convertFromPart = string.Empty;
|
||||
string convertToCustomer = string.Empty;
|
||||
string convertToPart = string.Empty;
|
||||
|
||||
string[] convertFrom = action.ConvertFrom.Split(" ");
|
||||
if (convertFrom.Length > 1) {
|
||||
convertFromCustomer = convertFrom[0];
|
||||
foreach (string partStr in convertFrom.Skip(1))
|
||||
convertFromPart += partStr;
|
||||
}
|
||||
|
||||
string[] convertTo = action.ConvertTo.Split(" ");
|
||||
if (convertTo.Length > 1) {
|
||||
convertToCustomer = convertTo[0];
|
||||
foreach (string partStr in convertTo.Skip(1))
|
||||
convertToPart += partStr;
|
||||
}
|
||||
|
||||
dt.Rows.Add(action.Action, convertFromCustomer, convertFromPart, action.Quantity.ToString(),
|
||||
convertToCustomer, convertToPart,
|
||||
DateTimeUtilities.GetDateAsStringMinDefault(action.AssignedDate),
|
||||
DateTimeUtilities.GetDateAsStringMaxDefault(action.CompletedDate),
|
||||
action.CompletedByUser is null ? "" : action.CompletedByUser.GetFullName());
|
||||
}
|
||||
} else {
|
||||
dt.Columns.Add("Action", typeof(string));
|
||||
dt.Columns.Add("Customer", typeof(string));
|
||||
dt.Columns.Add("Qty", typeof(string));
|
||||
dt.Columns.Add("Convert From", typeof(string));
|
||||
dt.Columns.Add("Convert To", typeof(string));
|
||||
dt.Columns.Add("Part Number", typeof(string));
|
||||
dt.Columns.Add("Batch Number / Lot Number", typeof(string));
|
||||
dt.Columns.Add("Justification", typeof(string));
|
||||
dt.Columns.Add("Assigned Date", typeof(string));
|
||||
dt.Columns.Add("Completed Date", typeof(string));
|
||||
dt.Columns.Add("Completed By", typeof(string));
|
||||
|
||||
foreach (MRBAction action in actions) {
|
||||
if (action.CompletedByUser is null && action.CompletedByUserID > 0)
|
||||
action.CompletedByUser = await _userService.GetUserByUserId(action.CompletedByUserID);
|
||||
|
||||
dt.Rows.Add(action.Action, action.Customer, action.Quantity.ToString(), action.ConvertFrom, action.ConvertTo,
|
||||
action.PartNumber, action.LotNumber, action.Justification,
|
||||
DateTimeUtilities.GetDateAsStringMinDefault(action.AssignedDate),
|
||||
DateTimeUtilities.GetDateAsStringMaxDefault(action.CompletedDate),
|
||||
action.CompletedByUser is null ? "" : action.CompletedByUser.GetFullName());
|
||||
}
|
||||
}
|
||||
|
||||
return dt;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to convert MRB actions to a DataTable, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,41 @@
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
|
||||
using MesaFabApproval.API.Utilities;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Utilities;
|
||||
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace MesaFabApproval.API.Services;
|
||||
|
||||
public interface IPCRBService {
|
||||
public Task CreateNewPCRB(PCRB pcrb);
|
||||
public Task<IEnumerable<PCRB>> GetAllPCRBs(bool bypassCache);
|
||||
public Task<PCRB> GetPCRBByPlanNumber(int planNumber, bool bypassCache);
|
||||
public Task<PCRB> GetPCRBByTitle(string title, bool bypassCache);
|
||||
public Task UpdatePCRB(PCRB pcrb);
|
||||
public Task DeletePCRB(int planNumber);
|
||||
Task CreateNewPCRB(PCRB pcrb);
|
||||
Task<IEnumerable<PCRB>> GetAllPCRBs(bool bypassCache);
|
||||
Task<PCRB> GetPCRBByPlanNumber(int planNumber, bool bypassCache);
|
||||
Task<PCRB> GetPCRBByTitle(string title, bool bypassCache);
|
||||
Task UpdatePCRB(PCRB pcrb);
|
||||
Task DeletePCRB(int planNumber);
|
||||
Task<UploadResult> UploadAttachment(IFormFile file, PCRBAttachment attachment);
|
||||
Task<IEnumerable<PCRBAttachment>> GetAttachmentsByPlanNumber(int planNumber, bool bypassCache);
|
||||
Task UpdateAttachment(PCRBAttachment attachment);
|
||||
Task DeleteAttachment(PCRBAttachment attachment);
|
||||
Task CreateNewActionItem(PCRBActionItem actionItem);
|
||||
Task UpdateActionItem(PCRBActionItem actionItem);
|
||||
Task DeleteActionItem(int id);
|
||||
Task<IEnumerable<PCRBActionItem>> GetActionItemsForPlanNumber(int planNumber, bool bypassCache);
|
||||
Task CreateNewAttendee(PCRBAttendee attendee);
|
||||
Task UpdateAttendee(PCRBAttendee attendee);
|
||||
Task DeleteAttendee(int id);
|
||||
Task<IEnumerable<PCRBAttendee>> GetAttendeesByPlanNumber(int planNumber, bool bypassCache);
|
||||
Task CreatePCR3Document(PCR3Document document);
|
||||
Task UpdatePCR3Document(PCR3Document document);
|
||||
Task<IEnumerable<PCR3Document>> GetPCR3DocumentsForPlanNumber(int planNumber, bool bypassCache);
|
||||
Task NotifyNewApprovals(PCRB pcrb);
|
||||
Task NotifyApprovers(PCRBNotification notification);
|
||||
Task NotifyOriginator(PCRBNotification notification);
|
||||
Task NotifyResponsiblePerson(PCRBActionItemNotification notification);
|
||||
}
|
||||
|
||||
public class PCRBService : IPCRBService {
|
||||
@ -20,16 +43,29 @@ public class PCRBService : IPCRBService {
|
||||
private readonly IDalService _dalService;
|
||||
private readonly IMemoryCache _cache;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
private readonly IApprovalService _approvalService;
|
||||
private readonly ISmtpService _smtpService;
|
||||
|
||||
private readonly string _pcrbAttachmentPath;
|
||||
private readonly string _siteBaseUrl;
|
||||
|
||||
public PCRBService(ILogger<PCRBService> logger,
|
||||
IDalService dalService,
|
||||
IMemoryCache cache,
|
||||
IUserService userService) {
|
||||
IUserService userService,
|
||||
IApprovalService approvalService,
|
||||
ISmtpService smtpService) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_dalService = dalService ?? throw new ArgumentNullException("IDalService not injected");
|
||||
_cache = cache ?? throw new ArgumentNullException("IMemoryCache not injected");
|
||||
_userService = userService ?? throw new ArgumentNullException("IUserService not injected");
|
||||
_pcrbAttachmentPath = Environment.GetEnvironmentVariable("FabApprovalPcrbAttachmentPath") ??
|
||||
throw new ArgumentNullException("FabApprovalPcrbAttachmentPath environment variable not found");
|
||||
_approvalService = approvalService ??
|
||||
throw new ArgumentNullException("IApprovalService not injected");
|
||||
_smtpService = smtpService ?? throw new ArgumentNullException("ISmtpService not injected");
|
||||
_siteBaseUrl = Environment.GetEnvironmentVariable("NewFabApprovalBaseUrl") ??
|
||||
throw new ArgumentNullException("FabApprovalBaseUrl environment variable not found");
|
||||
}
|
||||
|
||||
public async Task CreateNewPCRB(PCRB pcrb) {
|
||||
@ -160,11 +196,12 @@ public class PCRBService : IPCRBService {
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update CCChangeControl set OwnerID={pcrb.OwnerID}, ");
|
||||
queryBuilder.Append($"Title='{pcrb.Title}', ChangeLevel='{pcrb.ChangeLevel}', ");
|
||||
queryBuilder.Append($"CurrentStep={pcrb.CurrentStep}, ReasonForChange='{pcrb.ReasonForChange}', ");
|
||||
queryBuilder.Append($"ChangeDescription='{pcrb.ChangeDescription}', ");
|
||||
queryBuilder.Append($"Title='{pcrb.Title.Replace("'", "''")}', ChangeLevel='{pcrb.ChangeLevel}', ");
|
||||
queryBuilder.Append($"CurrentStep={pcrb.CurrentStep}, ReasonForChange='{pcrb.ReasonForChange.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"ChangeDescription='{pcrb.ChangeDescription.Replace("'", "''")}', ");
|
||||
queryBuilder.Append($"IsITAR={Convert.ToInt32(pcrb.IsITAR)}, ");
|
||||
queryBuilder.Append($"InsertTimeStamp='{pcrb.InsertTimeStamp.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"ClosedDate='{pcrb.ClosedDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"LastUpdateDate='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}' ");
|
||||
queryBuilder.Append($"where PlanNumber={pcrb.PlanNumber}");
|
||||
|
||||
@ -211,4 +248,520 @@ public class PCRBService : IPCRBService {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<UploadResult> UploadAttachment(IFormFile file, PCRBAttachment attachment) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to upload attachment");
|
||||
|
||||
UploadResult? uploadResult = null;
|
||||
|
||||
if (file is null) throw new ArgumentNullException("File cannot be null");
|
||||
if (file.Length <= 0) throw new ArgumentException("File size cannot be zero");
|
||||
if (attachment is null) throw new ArgumentNullException("Attachment cannot be null");
|
||||
|
||||
try {
|
||||
string encodedName = WebUtility.HtmlEncode(file.FileName);
|
||||
string path = $"{_pcrbAttachmentPath}\\{attachment.PlanNumber}\\{attachment.Step}\\{encodedName}";
|
||||
|
||||
attachment.Path = path;
|
||||
|
||||
await FileUtilities.SaveFileToFileSystem(file, path);
|
||||
await SaveAttachmentInDb(file, attachment);
|
||||
|
||||
uploadResult = new() {
|
||||
UploadSuccessful = true,
|
||||
FileName = file.FileName
|
||||
};
|
||||
} catch (Exception ex) {
|
||||
uploadResult = new() {
|
||||
UploadSuccessful = false,
|
||||
FileName = file.FileName,
|
||||
Error = ex.Message
|
||||
};
|
||||
}
|
||||
|
||||
return uploadResult;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to upload attachment, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<PCRBAttachment>> GetAttachmentsByPlanNumber(int planNumber, bool bypassCache) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get all attachments for PCRB Plan# {planNumber}");
|
||||
|
||||
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB Plan#");
|
||||
|
||||
IEnumerable<PCRBAttachment>? attachments = null;
|
||||
|
||||
if (!bypassCache)
|
||||
attachments = _cache.Get<IEnumerable<PCRBAttachment>>($"pcrbAttachments{planNumber}");
|
||||
|
||||
if (attachments is null) {
|
||||
string sql = $"select * from CCAttachment where PlanNumber={planNumber}";
|
||||
|
||||
attachments = await _dalService.QueryAsync<PCRBAttachment>(sql);
|
||||
|
||||
_cache.Set($"pcrbAttachments{planNumber}", attachments, DateTimeOffset.Now.AddMinutes(15));
|
||||
}
|
||||
|
||||
return attachments;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to get all attachments for PCRB Plan# {planNumber}, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateAttachment(PCRBAttachment attachment) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to update an attachment");
|
||||
|
||||
if (attachment is null)
|
||||
throw new ArgumentNullException("attachment cannot be null");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update CCAttachment ");
|
||||
queryBuilder.Append($"set Title='{attachment.Title.Replace("'", "''")}' where ID={attachment.ID}");
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
|
||||
if (rowsAffected <= 0) throw new Exception("update failed in database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to update attachment, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteAttachment(PCRBAttachment attachment) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to update an attachment");
|
||||
|
||||
if (attachment is null)
|
||||
throw new ArgumentNullException("attachment cannot be null");
|
||||
|
||||
if (!File.Exists(attachment.Path)) throw new FileNotFoundException("No file found at provided path");
|
||||
|
||||
File.Delete(attachment.Path);
|
||||
|
||||
string sql = $"delete from CCAttachment where ID={attachment.ID}";
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(sql);
|
||||
|
||||
if (rowsAffected <= 0) throw new Exception("update failed in database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to update attachment, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateNewActionItem(PCRBActionItem actionItem) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to create new action item");
|
||||
|
||||
if (actionItem is null) throw new ArgumentNullException("action item cannot be null");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append("insert into CCPCRBActionItem (Name, Gating, ClosedStatus, ClosedDate, ");
|
||||
queryBuilder.Append("ClosedByID, UploadedByID, UploadedDateTime, ResponsiblePersonID, PlanNumber, ");
|
||||
queryBuilder.Append($"Step) values ('{actionItem.Name}', {Convert.ToInt32(actionItem.Gating)}, ");
|
||||
queryBuilder.Append($"{Convert.ToInt32(actionItem.ClosedStatus)}, ");
|
||||
DateTime closedDateCopy = actionItem.ClosedDate ?? DateTimeUtilities.MAX_DT;
|
||||
queryBuilder.Append($"'{closedDateCopy.ToString("yyyy-MM-dd HH:mm:ss")}', {actionItem.ClosedByID}, ");
|
||||
queryBuilder.Append($"{actionItem.UploadedByID}, '{actionItem.UploadedDateTime.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"{actionItem.ResponsiblePersonID}, {actionItem.PlanNumber}, ");
|
||||
queryBuilder.Append($"{actionItem.Step});");
|
||||
|
||||
int rowsCreated = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
|
||||
if (rowsCreated <= 0) throw new Exception("unable to insert new action item in the database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to create new action item, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateActionItem(PCRBActionItem actionItem) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to update an action item");
|
||||
|
||||
if (actionItem is null)
|
||||
throw new ArgumentNullException("action item cannot be null");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update CCPCRBActionItem set Name='{actionItem.Name.Replace("'", "''")}', Gating={Convert.ToInt32(actionItem.Gating)}, ");
|
||||
queryBuilder.Append($"ClosedStatus={Convert.ToInt32(actionItem.ClosedStatus)}, ");
|
||||
DateTime closedDateCopy = actionItem.ClosedDate ?? DateTimeUtilities.MAX_DT;
|
||||
queryBuilder.Append($"ClosedDate='{closedDateCopy.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"ClosedByID={actionItem.ClosedByID}, ResponsiblePersonID={actionItem.ResponsiblePersonID}, ");
|
||||
queryBuilder.Append($"Step={actionItem.Step} where ID={actionItem.ID}");
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
|
||||
if (rowsAffected <= 0) throw new Exception("update failed in database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to update attachment, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteActionItem(int id) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to delete action item {id}");
|
||||
|
||||
if (id <= 0) throw new ArgumentException($"{id} is not a valid PCRB action item ID");
|
||||
|
||||
string sql = $"delete from CCPCRBActionItem where ID={id}";
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(sql);
|
||||
|
||||
if (rowsAffected <= 0) throw new Exception("delete operation failed in database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to delete action item {id}, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<PCRBActionItem>> GetActionItemsForPlanNumber(int planNumber, bool bypassCache) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get all action items for PCRB plan# {planNumber}");
|
||||
|
||||
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB plan#");
|
||||
|
||||
IEnumerable<PCRBActionItem>? actionItems = null;
|
||||
|
||||
if (!bypassCache)
|
||||
actionItems = _cache.Get<IEnumerable<PCRBActionItem>>($"pcrbActionItems{planNumber}");
|
||||
|
||||
if (actionItems is null) {
|
||||
string sql = $"select * from CCPCRBActionItem where PlanNumber={planNumber}";
|
||||
|
||||
actionItems = await _dalService.QueryAsync<PCRBActionItem>(sql);
|
||||
|
||||
_cache.Set($"pcrbActionItems{planNumber}", actionItems, DateTimeOffset.Now.AddMinutes(15));
|
||||
}
|
||||
|
||||
return actionItems;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to get all action items for PCRB plan# {planNumber}, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateNewAttendee(PCRBAttendee attendee) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to create new attendee");
|
||||
|
||||
if (attendee is null) throw new ArgumentNullException("attendee item cannot be null");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append("insert into CCPCRBAttendee (PlanNumber, JobTitle, Location, Attended, AttendeeID, Step) ");
|
||||
queryBuilder.Append($"values ({attendee.PlanNumber}, '{attendee.JobTitle}', '{attendee.Location}', ");
|
||||
queryBuilder.Append($"{Convert.ToInt32(attendee.Attended)}, {attendee.AttendeeID}, ");
|
||||
queryBuilder.Append($"{attendee.Step});");
|
||||
|
||||
int rowsCreated = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
|
||||
if (rowsCreated <= 0) throw new Exception("unable to insert new attendee in the database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to create new attendee, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateAttendee(PCRBAttendee attendee) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to update an attendee");
|
||||
|
||||
if (attendee is null)
|
||||
throw new ArgumentNullException("attendee cannot be null");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update CCPCRBAttendee set JobTitle='{attendee.JobTitle}', ");
|
||||
queryBuilder.Append($"Location='{attendee.Location}', Attended={Convert.ToInt32(attendee.Attended)}, ");
|
||||
queryBuilder.Append($"AttendeeID={attendee.AttendeeID}, ");
|
||||
queryBuilder.Append($"Step={attendee.Step} where ID={attendee.ID}");
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
|
||||
if (rowsAffected <= 0) throw new Exception("update failed in database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to update attendee, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteAttendee(int id) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to delete attendee {id}");
|
||||
|
||||
if (id <= 0) throw new ArgumentException($"{id} is not a valid attendee ID");
|
||||
|
||||
string sql = $"delete from CCPCRBAttendee where ID={id}";
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(sql);
|
||||
|
||||
if (rowsAffected <= 0) throw new Exception("delete operation failed in database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to delete attendee {id}, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<PCRBAttendee>> GetAttendeesByPlanNumber(int planNumber, bool bypassCache) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get all attendees for PCRB plan# {planNumber}");
|
||||
|
||||
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB plan#");
|
||||
|
||||
IEnumerable<PCRBAttendee>? attendees = null;
|
||||
|
||||
if (!bypassCache)
|
||||
attendees = _cache.Get<IEnumerable<PCRBAttendee>>($"pcrbAttendees{planNumber}");
|
||||
|
||||
if (attendees is null) {
|
||||
string sql = $"select * from CCPCRBAttendee where PlanNumber={planNumber}";
|
||||
|
||||
attendees = await _dalService.QueryAsync<PCRBAttendee>(sql);
|
||||
|
||||
_cache.Set($"pcrbAttendees{planNumber}", attendees, DateTimeOffset.Now.AddMinutes(15));
|
||||
}
|
||||
|
||||
return attendees;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to get all attendees for PCRB plan# {planNumber}, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreatePCR3Document(PCR3Document document) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to create new PCR3 document");
|
||||
|
||||
if (document is null) throw new ArgumentNullException("document item cannot be null");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append("insert into CCPCR3Document (PlanNumber, DocType) ");
|
||||
queryBuilder.Append($"values ({document.PlanNumber}, '{document.DocType}')");
|
||||
|
||||
int rowsCreated = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
|
||||
if (rowsCreated <= 0) throw new Exception("unable to insert new PCR3 document in the database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to create new PCR3 document, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdatePCR3Document(PCR3Document document) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to update a PCR3 document");
|
||||
|
||||
if (document is null) throw new ArgumentNullException("document cannot be null");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update CCPCR3Document set DocNumbers='{document.DocNumbers}', ");
|
||||
queryBuilder.Append($"Comment='{document.Comment.Replace("'", "''")}', ECNNumber={document.ECNNumber}, ");
|
||||
queryBuilder.Append($"CompletedDate='{document.CompletedDate.ToString("yyyy-MM-dd HH:mm:ss")}', ");
|
||||
queryBuilder.Append($"CompletedByID={document.CompletedByID} ");
|
||||
queryBuilder.Append($"where ID={document.ID}");
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
|
||||
if (rowsAffected <= 0) throw new Exception("update failed in database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to update PCR3 document, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<PCR3Document>> GetPCR3DocumentsForPlanNumber(int planNumber, bool bypassCache) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get all PCR3 documents for PCRB plan# {planNumber}");
|
||||
|
||||
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB plan#");
|
||||
|
||||
IEnumerable<PCR3Document>? documents = null;
|
||||
|
||||
if (!bypassCache)
|
||||
documents = _cache.Get<IEnumerable<PCR3Document>>($"pcr3Documents{planNumber}");
|
||||
|
||||
if (documents is null) {
|
||||
string sql = $"select * from CCPCR3Document where PlanNumber={planNumber}";
|
||||
|
||||
documents = await _dalService.QueryAsync<PCR3Document>(sql);
|
||||
|
||||
_cache.Set($"pcr3Documents{planNumber}", documents, DateTimeOffset.Now.AddMinutes(15));
|
||||
}
|
||||
|
||||
return documents;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to get all PCR3 documents for PCRB plan# {planNumber}, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task NotifyNewApprovals(PCRB pcrb) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to notify approvers");
|
||||
|
||||
if (pcrb is null) throw new ArgumentNullException("PCRB cannot be null");
|
||||
|
||||
IEnumerable<Approval> approvals = await _approvalService.GetApprovalsForIssueId(pcrb.PlanNumber, true);
|
||||
|
||||
List<Approval> approvalsNeedingNotification = approvals.Where(a => a.Step == pcrb.CurrentStep &&
|
||||
a.NotifyDate <= DateTimeUtilities.MIN_DT &&
|
||||
a.AssignedDate > DateTimeUtilities.MIN_DT).ToList();
|
||||
|
||||
HashSet<string> emailsAlreadySent = new();
|
||||
foreach (Approval approval in approvalsNeedingNotification) {
|
||||
User user = await _userService.GetUserByUserId(approval.UserID);
|
||||
|
||||
if (!emailsAlreadySent.Contains(user.Email.ToLower())) {
|
||||
emailsAlreadySent.Add(user.Email);
|
||||
|
||||
List<MailAddress> toAddresses = new();
|
||||
toAddresses.Add(new MailAddress(user.Email.ToLower()));
|
||||
|
||||
List<MailAddress> ccAddresses = new();
|
||||
|
||||
string subject = $"[New Task] Mesa Fab Approval - PCRB# {pcrb.PlanNumber} - {pcrb.Title}";
|
||||
|
||||
StringBuilder bodyBuilder = new();
|
||||
bodyBuilder.Append($"PCRB# {pcrb.PlanNumber} [{pcrb.Title}] PCR{approval.Step} is ready for your approval. ");
|
||||
bodyBuilder.Append($"The assigned role is {approval.SubRoleCategoryItem}. <br /> <br />");
|
||||
bodyBuilder.Append($"Click {_siteBaseUrl}/redirect?redirectPath=pcrb/{approval.IssueID} to view the PCRB.");
|
||||
|
||||
await _smtpService.SendEmail(toAddresses, ccAddresses, subject, bodyBuilder.ToString());
|
||||
|
||||
approval.NotifyDate = DateTime.Now;
|
||||
await _approvalService.UpdateApproval(approval);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to notify approvers, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task NotifyApprovers(PCRBNotification notification) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to send notification to approvers");
|
||||
|
||||
if (notification is null) throw new ArgumentNullException("notification cannot be null");
|
||||
if (notification.PCRB is null) throw new ArgumentNullException("PCRB cannot be null");
|
||||
if (string.IsNullOrWhiteSpace(notification.Message)) throw new ArgumentException("message cannot be null or empty");
|
||||
|
||||
IEnumerable<Approval> approvals = await _approvalService.GetApprovalsForIssueId(notification.PCRB.PlanNumber, true);
|
||||
|
||||
HashSet<string> emailsAlreadySent = new();
|
||||
foreach (Approval approval in approvals) {
|
||||
User user = await _userService.GetUserByUserId(approval.UserID);
|
||||
|
||||
if (!emailsAlreadySent.Contains(user.Email)) {
|
||||
emailsAlreadySent.Add(user.Email);
|
||||
|
||||
List<MailAddress> toAddresses = new();
|
||||
toAddresses.Add(new MailAddress(user.Email));
|
||||
|
||||
List<MailAddress> ccAddresses = new();
|
||||
|
||||
string subject = $"[Update] Mesa Fab Approval - PCRB# {notification.PCRB.PlanNumber} - {notification.PCRB.Title}";
|
||||
|
||||
StringBuilder bodyBuilder = new();
|
||||
bodyBuilder.Append($"{notification.Message} <br /> <br />");
|
||||
bodyBuilder.Append($"Click {_siteBaseUrl}/redirect?redirectPath=pcrb/{approval.IssueID} to view the PCRB.");
|
||||
|
||||
await _smtpService.SendEmail(toAddresses, ccAddresses, subject, bodyBuilder.ToString());
|
||||
|
||||
approval.NotifyDate = DateTime.Now;
|
||||
await _approvalService.UpdateApproval(approval);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to send notification to originator, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task NotifyOriginator(PCRBNotification notification) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to send notification to originator");
|
||||
|
||||
if (notification is null) throw new ArgumentNullException("notification cannot be null");
|
||||
if (notification.PCRB is null) throw new ArgumentNullException("PCRB cannot be null");
|
||||
if (string.IsNullOrWhiteSpace(notification.Message)) throw new ArgumentException("message cannot be null or empty");
|
||||
|
||||
User user = await _userService.GetUserByUserId(notification.PCRB.OwnerID);
|
||||
|
||||
List<MailAddress> toAddresses = new();
|
||||
toAddresses.Add(new MailAddress(user.Email));
|
||||
|
||||
List<MailAddress> ccAddresses = new();
|
||||
|
||||
string subject = $"[Update] Mesa Fab Approval - PCRB# {notification.PCRB.PlanNumber} - {notification.PCRB.Title}";
|
||||
|
||||
StringBuilder bodyBuilder = new();
|
||||
bodyBuilder.Append($"{notification.Message} <br /> <br />");
|
||||
bodyBuilder.Append($"Click {_siteBaseUrl}/redirect?redirectPath=pcrb/{notification.PCRB.PlanNumber} to view the PCRB.");
|
||||
|
||||
await _smtpService.SendEmail(toAddresses, ccAddresses, subject, bodyBuilder.ToString());
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to send notification to originator, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task NotifyResponsiblePerson(PCRBActionItemNotification notification) {
|
||||
try {
|
||||
_logger.LogInformation("Attempting to notify responsible person");
|
||||
|
||||
if (notification is null) throw new ArgumentNullException("notification cannot be null");
|
||||
if (string.IsNullOrWhiteSpace(notification.Message))
|
||||
throw new ArgumentException("message cannot be null or empty");
|
||||
|
||||
if (notification.ActionItem.ResponsiblePerson is null)
|
||||
notification.ActionItem.ResponsiblePerson = await _userService.GetUserByUserId(notification.ActionItem.ResponsiblePersonID);
|
||||
|
||||
List<MailAddress> toAddresses = new();
|
||||
toAddresses.Add(new MailAddress(notification.ActionItem.ResponsiblePerson.Email));
|
||||
|
||||
List<MailAddress> ccAddresses = new();
|
||||
|
||||
string subject = $"[New Task] Mesa Fab Approval - PCRB# {notification.PCRB.PlanNumber} - {notification.PCRB.Title}";
|
||||
|
||||
StringBuilder bodyBuilder = new();
|
||||
bodyBuilder.Append($"{notification.Message} <br /> <br />");
|
||||
bodyBuilder.Append($"Click {_siteBaseUrl}/redirect?redirectPath=pcrb/{notification.PCRB.PlanNumber} to view the PCRB.");
|
||||
|
||||
await _smtpService.SendEmail(toAddresses, ccAddresses, subject, bodyBuilder.ToString());
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to notify responsible person, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveAttachmentInDb(IFormFile file, PCRBAttachment attachment) {
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to save attachment to database");
|
||||
|
||||
if (file is null) throw new ArgumentNullException("File cannot be null");
|
||||
if (string.IsNullOrWhiteSpace(attachment.Path)) throw new ArgumentException("Path cannot be null or empty");
|
||||
if (attachment.PlanNumber <= 0) throw new ArgumentException($"{attachment.PlanNumber} is not a valid PCRB Plan#");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append("insert into CCAttachment (PlanNumber, FileName, UploadDateTime, Path, UploadedByID, Title, ");
|
||||
queryBuilder.Append($"Step) values ({attachment.PlanNumber}, '{file.FileName}', ");
|
||||
queryBuilder.Append($"'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}', '{attachment.Path}', {attachment.UploadedByID}, ");
|
||||
queryBuilder.Append($"'{attachment.Title}', {attachment.Step});");
|
||||
|
||||
int rowsAffected = await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
|
||||
if (rowsAffected <= 0)
|
||||
throw new Exception("Unable to insert attachment in database");
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Unable to save file to DB, because {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user