PCRB webassembly
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user