768 lines
33 KiB
C#
768 lines
33 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
using MesaFabApproval.Shared.Models;
|
|
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|
using Microsoft.AspNetCore.StaticFiles;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
using MudBlazor;
|
|
|
|
namespace MesaFabApproval.Client.Services;
|
|
|
|
public interface IPCRBService {
|
|
Task<string> IdIsValid(string id);
|
|
Task<bool> IdIsValid(int id);
|
|
Task<IEnumerable<PCRB>> GetAllPCRBs(bool bypassCache);
|
|
Task CreateNewPCRB(PCRB pcrb);
|
|
Task<PCRB> GetPCRBByPlanNumber(int planNumber, bool bypassCache);
|
|
Task<PCRB> GetPCRBByTitle(string title, bool bypassCache);
|
|
Task UpdatePCRB(PCRB pcrb);
|
|
Task DeletePCRB(int planNumber);
|
|
Task UploadAttachment(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 {
|
|
private readonly IMemoryCache _cache;
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
private readonly ISnackbar _snackbar;
|
|
private readonly IUserService _userService;
|
|
|
|
public PCRBService(IMemoryCache cache,
|
|
IHttpClientFactory httpClientFactory,
|
|
ISnackbar snackbar,
|
|
IUserService userService) {
|
|
_cache = cache ?? throw new ArgumentNullException("IMemoryCache not injected");
|
|
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException("IHttpClientFactory not injected");
|
|
_snackbar = snackbar ?? throw new ArgumentNullException("ISnackbar not injected");
|
|
_userService = userService ?? throw new ArgumentNullException("IUserService not injected");
|
|
}
|
|
|
|
public async Task<string> IdIsValid(string id) {
|
|
bool isMatch = true;
|
|
if (string.IsNullOrWhiteSpace(id)) isMatch = false;
|
|
|
|
try {
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Get, $"pcrb/getByPlanNumber?planNumber={id}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (responseMessage.IsSuccessStatusCode) {
|
|
string responseContent = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
JsonSerializerOptions jsonSerializerOptions = new() {
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
PCRB? pcrb = JsonSerializer.Deserialize<PCRB>(responseContent, jsonSerializerOptions);
|
|
|
|
if (pcrb is null) isMatch = false;
|
|
} else {
|
|
isMatch = false;
|
|
}
|
|
} catch (Exception) {
|
|
isMatch = false;
|
|
}
|
|
|
|
if (!isMatch) return $"{id} is not a valid PCRB#";
|
|
return null;
|
|
}
|
|
|
|
public async Task<bool> IdIsValid(int id) {
|
|
bool isMatch = true;
|
|
if (id <= 0) isMatch = false;
|
|
|
|
try {
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Get, $"pcrb/getByPlanNumber?planNumber={id}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (responseMessage.IsSuccessStatusCode) {
|
|
string responseContent = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
JsonSerializerOptions jsonSerializerOptions = new() {
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
PCRB? pcrb = JsonSerializer.Deserialize<PCRB>(responseContent, jsonSerializerOptions);
|
|
|
|
if (pcrb is null) isMatch = false;
|
|
} else {
|
|
isMatch = false;
|
|
}
|
|
} catch (Exception) {
|
|
isMatch = false;
|
|
}
|
|
|
|
return isMatch;
|
|
}
|
|
|
|
public async Task<IEnumerable<PCRB>> GetAllPCRBs(bool bypassCache) {
|
|
try {
|
|
IEnumerable<PCRB>? allPCRBs = null;
|
|
if (!bypassCache) allPCRBs = _cache.Get<IEnumerable<PCRB>>("allPCRBs");
|
|
|
|
if (allPCRBs is null) {
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Get, $"pcrb/all?bypassCache={bypassCache}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (responseMessage.IsSuccessStatusCode) {
|
|
string responseContent = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
JsonSerializerOptions jsonSerializerOptions = new() {
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
allPCRBs = JsonSerializer.Deserialize<IEnumerable<PCRB>>(responseContent, jsonSerializerOptions) ??
|
|
throw new Exception("Unable to parse PCRBs from API response");
|
|
|
|
_cache.Set($"allPCRBs", allPCRBs, DateTimeOffset.Now.AddMinutes(15));
|
|
} else {
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
}
|
|
|
|
return allPCRBs;
|
|
} catch (Exception) {
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task CreateNewPCRB(PCRB pcrb) {
|
|
if (pcrb is null) throw new ArgumentNullException("PCRB cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(pcrb),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
|
|
PCRB newPCRB = await GetPCRBByTitle(pcrb.Title, true);
|
|
|
|
_cache.Set($"pcrb{pcrb.PlanNumber}", pcrb, DateTimeOffset.Now.AddHours(1));
|
|
_cache.Set($"pcrb{pcrb.Title}", pcrb, DateTimeOffset.Now.AddHours(1));
|
|
|
|
IEnumerable<PCRB>? allPCRBs = _cache.Get<IEnumerable<PCRB>>("allPCRBs");
|
|
if (allPCRBs is not null) {
|
|
List<PCRB> pcrbList = allPCRBs.ToList();
|
|
pcrbList.Add(newPCRB);
|
|
_cache.Set("allPCRBs", pcrbList);
|
|
}
|
|
}
|
|
|
|
public async Task<PCRB> GetPCRBByPlanNumber(int planNumber, bool bypassCache) {
|
|
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB plan #");
|
|
|
|
PCRB? pcrb = null;
|
|
if (!bypassCache) pcrb = _cache.Get<PCRB>($"pcrb{planNumber}");
|
|
|
|
if (pcrb is null && !bypassCache)
|
|
pcrb = _cache.Get<IEnumerable<PCRB>>("allPCRBs")?.FirstOrDefault(m => m.PlanNumber == planNumber);
|
|
|
|
if (pcrb is null) {
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage =
|
|
new(HttpMethod.Get, $"pcrb/getByPlanNumber?planNumber={planNumber}&bypassCache={bypassCache}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
|
|
string responseContent = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
JsonSerializerOptions jsonSerializerOptions = new() {
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
pcrb = JsonSerializer.Deserialize<PCRB>(responseContent, jsonSerializerOptions) ??
|
|
throw new Exception("unable to parse PCRB from API response");
|
|
|
|
_cache.Set($"pcrb{pcrb.PlanNumber}", pcrb, DateTimeOffset.Now.AddHours(1));
|
|
_cache.Set($"pcrb{pcrb.Title}", pcrb, DateTimeOffset.Now.AddHours(1));
|
|
|
|
if (bypassCache) {
|
|
IEnumerable<PCRB>? allPCRBs = _cache.Get<IEnumerable<PCRB>>("allPCRBs");
|
|
if (allPCRBs is not null) {
|
|
List<PCRB> pcrbList = allPCRBs.ToList();
|
|
pcrbList.RemoveAll(p => p.PlanNumber == planNumber);
|
|
pcrbList.Add(pcrb);
|
|
_cache.Set("allPCRBs", pcrbList);
|
|
}
|
|
}
|
|
}
|
|
|
|
return pcrb;
|
|
}
|
|
|
|
public async Task<PCRB> GetPCRBByTitle(string title, bool bypassCache) {
|
|
if (string.IsNullOrWhiteSpace(title)) throw new ArgumentNullException("title cannot be null");
|
|
|
|
PCRB? pcrb = null;
|
|
if (!bypassCache) pcrb = _cache.Get<PCRB>($"pcrb{title}");
|
|
|
|
if (pcrb is null && !bypassCache)
|
|
pcrb = _cache.Get<IEnumerable<PCRB>>("allPCRBs")?.FirstOrDefault(m => m.Title.Equals(title));
|
|
|
|
if (pcrb is null) {
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage =
|
|
new(HttpMethod.Get, $"pcrb/getByTitle?title={title}&bypassCache={bypassCache}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
|
|
string responseContent = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
JsonSerializerOptions jsonSerializerOptions = new() {
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
pcrb = JsonSerializer.Deserialize<PCRB>(responseContent, jsonSerializerOptions) ??
|
|
throw new Exception("unable to parse PCRB from API response");
|
|
|
|
_cache.Set($"pcrb{pcrb.Title}", pcrb, DateTimeOffset.Now.AddHours(1));
|
|
_cache.Set($"pcrb{pcrb.PlanNumber}", pcrb, DateTimeOffset.Now.AddHours(1));
|
|
|
|
if (bypassCache) {
|
|
IEnumerable<PCRB>? allPCRBs = _cache.Get<IEnumerable<PCRB>>("allPCRBs");
|
|
if (allPCRBs is not null) {
|
|
List<PCRB> pcrbList = allPCRBs.ToList();
|
|
pcrbList.RemoveAll(p => p.PlanNumber == pcrb.PlanNumber);
|
|
pcrbList.Add(pcrb);
|
|
_cache.Set("allPCRBs", pcrbList);
|
|
}
|
|
}
|
|
}
|
|
|
|
return pcrb;
|
|
}
|
|
|
|
public async Task UpdatePCRB(PCRB pcrb) {
|
|
if (pcrb is null) throw new ArgumentNullException("PCRB cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Put, $"pcrb");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(pcrb),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
|
|
_cache.Set($"pcrb{pcrb.PlanNumber}", pcrb, DateTimeOffset.Now.AddHours(1));
|
|
_cache.Set($"pcrb{pcrb.Title}", pcrb, DateTimeOffset.Now.AddHours(1));
|
|
|
|
IEnumerable<PCRB>? allPCRBs = _cache.Get<IEnumerable<PCRB>>("allPCRBs");
|
|
if (allPCRBs is not null) {
|
|
List<PCRB> pcrbList = allPCRBs.ToList();
|
|
pcrbList.RemoveAll(m => m.PlanNumber == pcrb.PlanNumber);
|
|
pcrbList.Add(pcrb);
|
|
_cache.Set("allPCRBs", pcrbList);
|
|
}
|
|
}
|
|
|
|
public async Task DeletePCRB(int planNumber) {
|
|
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB plan #");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Delete, $"pcrb?planNumber={planNumber}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode) throw new Exception(responseMessage.ReasonPhrase);
|
|
|
|
IEnumerable<PCRB> allPCRBs = await GetAllPCRBs(true);
|
|
_cache.Set("allPCRBs", allPCRBs);
|
|
}
|
|
|
|
public async Task UploadAttachment(PCRBAttachment attachment) {
|
|
if (attachment is null) throw new ArgumentNullException("attachment cannot be null");
|
|
if (attachment.File is null) throw new ArgumentNullException("file cannot be null");
|
|
if (attachment.File.Size <= 0) throw new ArgumentException("file size must be greater than zero");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/attachment");
|
|
|
|
using MultipartFormDataContent content = new MultipartFormDataContent();
|
|
|
|
try {
|
|
long maxFileSize = 1024L * 1024L * 1024L * 2L;
|
|
StreamContent fileContent = new StreamContent(attachment.File.OpenReadStream(maxFileSize));
|
|
|
|
FileExtensionContentTypeProvider contentTypeProvider = new FileExtensionContentTypeProvider();
|
|
|
|
const string defaultContentType = "application/octet-stream";
|
|
|
|
if (!contentTypeProvider.TryGetContentType(attachment.File.Name, out string? contentType)) {
|
|
contentType = defaultContentType;
|
|
}
|
|
|
|
fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
|
|
|
|
content.Add(content: fileContent, name: "\"file\"", fileName: attachment.File.Name);
|
|
} catch (Exception ex) {
|
|
_snackbar.Add(ex.Message);
|
|
}
|
|
|
|
content.Add(new StringContent(attachment.PlanNumber.ToString()), "PlanNumber");
|
|
content.Add(new StringContent(attachment.FileName), "FileName");
|
|
content.Add(new StringContent(attachment.UploadedByID.ToString()), "UploadedByID");
|
|
content.Add(new StringContent(attachment.Title), "Title");
|
|
content.Add(new StringContent(attachment.UploadDateTime.ToString("yyyy-MM-dd HH:mm:ss")), "UploadDateTime");
|
|
content.Add(new StringContent(attachment.Step.ToString()), "Step");
|
|
|
|
requestMessage.Content = content;
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
|
|
await GetAttachmentsByPlanNumber(attachment.PlanNumber, true);
|
|
}
|
|
|
|
public async Task<IEnumerable<PCRBAttachment>> GetAttachmentsByPlanNumber(int planNumber, bool bypassCache) {
|
|
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) {
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Get, $"pcrb/attachments?planNumber={planNumber}&bypassCache={bypassCache}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (responseMessage.IsSuccessStatusCode) {
|
|
string responseContent = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
JsonSerializerOptions jsonSerializerOptions = new() {
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
attachments = JsonSerializer.Deserialize<IEnumerable<PCRBAttachment>>(responseContent, jsonSerializerOptions) ??
|
|
new List<PCRBAttachment>();
|
|
|
|
if (attachments.Count() > 0) {
|
|
foreach (PCRBAttachment attachment in attachments) {
|
|
attachment.UploadedBy = await _userService.GetUserByUserId(attachment.UploadedByID);
|
|
}
|
|
|
|
_cache.Set($"pcrbAttachments{planNumber}", attachments, DateTimeOffset.Now.AddMinutes(5));
|
|
}
|
|
} else {
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
}
|
|
|
|
return attachments;
|
|
}
|
|
|
|
public async Task UpdateAttachment(PCRBAttachment attachment) {
|
|
if (attachment is null) throw new ArgumentNullException("attachment cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Put, $"pcrb/attachment");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(attachment),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
|
|
public async Task DeleteAttachment(PCRBAttachment attachment) {
|
|
if (attachment is null) throw new ArgumentNullException("attachment cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Delete, $"pcrb/attachment");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(attachment),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
|
|
public async Task CreateNewActionItem(PCRBActionItem actionItem) {
|
|
if (actionItem is null) throw new ArgumentNullException("action item cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/actionItem");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(actionItem),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
|
|
await GetActionItemsForPlanNumber(actionItem.PlanNumber, true);
|
|
}
|
|
|
|
public async Task UpdateActionItem(PCRBActionItem actionItem) {
|
|
if (actionItem is null) throw new ArgumentNullException("action item cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Put, $"pcrb/actionItem");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(actionItem),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
|
|
public async Task DeleteActionItem(int id) {
|
|
if (id <= 0) throw new ArgumentException($"{id} is not a valid PCRB action item ID");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Delete, $"pcrb/actionItem?id={id}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode) throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
|
|
public async Task<IEnumerable<PCRBActionItem>> GetActionItemsForPlanNumber(int planNumber, bool bypassCache) {
|
|
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) {
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Get, $"pcrb/actionItems?planNumber={planNumber}&bypassCache={bypassCache}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (responseMessage.IsSuccessStatusCode) {
|
|
string responseContent = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
JsonSerializerOptions jsonSerializerOptions = new() {
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
actionItems = JsonSerializer.Deserialize<IEnumerable<PCRBActionItem>>(responseContent, jsonSerializerOptions) ??
|
|
new List<PCRBActionItem>();
|
|
|
|
if (actionItems.Count() > 0) {
|
|
foreach (PCRBActionItem actionItem in actionItems) {
|
|
actionItem.UploadedBy = await _userService.GetUserByUserId(actionItem.UploadedByID);
|
|
if (actionItem.ResponsiblePersonID > 0)
|
|
actionItem.ResponsiblePerson = await _userService.GetUserByUserId(actionItem.ResponsiblePersonID);
|
|
if (actionItem.ClosedByID > 0)
|
|
actionItem.ClosedBy = await _userService.GetUserByUserId(actionItem.ClosedByID);
|
|
}
|
|
|
|
_cache.Set($"pcrbActionItems{planNumber}", actionItems, DateTimeOffset.Now.AddMinutes(5));
|
|
}
|
|
} else {
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
}
|
|
|
|
return actionItems;
|
|
}
|
|
|
|
public async Task CreateNewAttendee(PCRBAttendee attendee) {
|
|
if (attendee is null) throw new ArgumentNullException("attendee cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/attendee");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(attendee),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
|
|
await GetAttendeesByPlanNumber(attendee.PlanNumber, true);
|
|
}
|
|
|
|
public async Task UpdateAttendee(PCRBAttendee attendee) {
|
|
if (attendee is null) throw new ArgumentNullException("attendee cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Put, $"pcrb/attendee");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(attendee),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
|
|
public async Task DeleteAttendee(int id) {
|
|
if (id <= 0) throw new ArgumentException($"{id} is not a valid PCRB attendee ID");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Delete, $"pcrb/attendee?id={id}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode) throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
|
|
public async Task<IEnumerable<PCRBAttendee>> GetAttendeesByPlanNumber(int planNumber, bool bypassCache) {
|
|
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) {
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Get, $"pcrb/attendees?planNumber={planNumber}&bypassCache={bypassCache}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (responseMessage.IsSuccessStatusCode) {
|
|
string responseContent = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
JsonSerializerOptions jsonSerializerOptions = new() {
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
attendees = JsonSerializer.Deserialize<IEnumerable<PCRBAttendee>>(responseContent, jsonSerializerOptions) ??
|
|
new List<PCRBAttendee>();
|
|
|
|
if (attendees.Count() > 0) {
|
|
foreach (PCRBAttendee attendee in attendees)
|
|
attendee.Attendee = await _userService.GetUserByUserId(attendee.AttendeeID);
|
|
|
|
_cache.Set($"pcrbAttendees{planNumber}", attendees, DateTimeOffset.Now.AddMinutes(5));
|
|
}
|
|
} else {
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
}
|
|
|
|
return attendees;
|
|
}
|
|
|
|
public async Task CreatePCR3Document(PCR3Document document) {
|
|
if (document is null) throw new ArgumentNullException("document cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/pcr3Document");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(document),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
|
|
await GetPCR3DocumentsForPlanNumber(document.PlanNumber, true);
|
|
}
|
|
|
|
public async Task UpdatePCR3Document(PCR3Document document) {
|
|
if (document is null) throw new ArgumentNullException("document cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Put, $"pcrb/pcr3Document");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(document),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
|
|
public async Task<IEnumerable<PCR3Document>> GetPCR3DocumentsForPlanNumber(int planNumber, bool bypassCache) {
|
|
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) {
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Get, $"pcrb/pcr3Documents?planNumber={planNumber}&bypassCache={bypassCache}");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (responseMessage.IsSuccessStatusCode) {
|
|
string responseContent = await responseMessage.Content.ReadAsStringAsync();
|
|
|
|
JsonSerializerOptions jsonSerializerOptions = new() {
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
documents = JsonSerializer.Deserialize<IEnumerable<PCR3Document>>(responseContent, jsonSerializerOptions) ??
|
|
new List<PCR3Document>();
|
|
|
|
if (documents.Count() > 0) {
|
|
foreach (PCR3Document document in documents) {
|
|
if (document.CompletedByID > 0)
|
|
document.CompletedBy = await _userService.GetUserByUserId(document.CompletedByID);
|
|
}
|
|
|
|
_cache.Set($"pcr3Documents{planNumber}", documents, DateTimeOffset.Now.AddMinutes(5));
|
|
}
|
|
} else {
|
|
throw new Exception(responseMessage.ReasonPhrase);
|
|
}
|
|
}
|
|
|
|
return documents;
|
|
}
|
|
|
|
public async Task NotifyNewApprovals(PCRB pcrb) {
|
|
if (pcrb is null) throw new ArgumentNullException("PCRB cannot be null");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/notify/new-approvals");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(pcrb),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception($"Unable to notify new PCRB approvers, because {responseMessage.ReasonPhrase}");
|
|
}
|
|
|
|
public async Task NotifyApprovers(PCRBNotification notification) {
|
|
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");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/notify/approvers");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(notification),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception($"Unable to notify PCRB approvers, because {responseMessage.ReasonPhrase}");
|
|
}
|
|
|
|
public async Task NotifyOriginator(PCRBNotification notification) {
|
|
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");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/notify/originator");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(notification),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception($"Unable to notify PCRB originator, because {responseMessage.ReasonPhrase}");
|
|
}
|
|
|
|
public async Task NotifyResponsiblePerson(PCRBActionItemNotification notification) {
|
|
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");
|
|
|
|
HttpClient httpClient = _httpClientFactory.CreateClient("API");
|
|
|
|
HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/notify/responsiblePerson");
|
|
|
|
requestMessage.Content = new StringContent(JsonSerializer.Serialize(notification),
|
|
Encoding.UTF8,
|
|
"application/json");
|
|
|
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
|
|
|
|
if (!responseMessage.IsSuccessStatusCode)
|
|
throw new Exception($"Unable to notify PCRB responsible person, because {responseMessage.ReasonPhrase}");
|
|
}
|
|
}
|