using System.Net.Http.Headers; using System.Text; using System.Text.Json; using MesaFabApproval.Shared.Models; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.Caching.Memory; using MudBlazor; namespace MesaFabApproval.Client.Services; public interface IPCRBService { Task IdIsValid(string id); Task IdIsValid(int id); Task> GetAllPCRBs(bool bypassCache); Task CreateNewPCRB(PCRB pcrb); Task GetPCRBByPlanNumber(int planNumber, bool bypassCache); Task GetPCRBByTitle(string title, bool bypassCache); Task UpdatePCRB(PCRB pcrb); Task DeletePCRB(int planNumber); Task UploadAttachment(PCRBAttachment attachment); Task> 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> GetActionItemsForPlanNumber(int planNumber, bool bypassCache); Task CreateNewAttendee(PCRBAttendee attendee); Task UpdateAttendee(PCRBAttendee attendee); Task DeleteAttendee(int id); Task> GetAttendeesByPlanNumber(int planNumber, bool bypassCache); Task CreatePCR3Document(PCR3Document document); Task UpdatePCR3Document(PCR3Document document); Task> GetPCR3DocumentsForPlanNumber(int planNumber, bool bypassCache); Task NotifyNewApprovals(PCRB pcrb); Task NotifyApprovers(PCRBNotification notification); Task NotifyOriginator(PCRBNotification notification); Task NotifyResponsiblePerson(PCRBActionItemNotification notification); Task CreateFollowUp(PCRBFollowUp followUp); Task> GetFollowUpsByPlanNumber(int planNumber, bool bypassCache); Task UpdateFollowUp(PCRBFollowUp followUp); Task DeleteFollowUp(int id); } 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 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(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 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(responseContent, jsonSerializerOptions); if (pcrb is null) isMatch = false; } else { isMatch = false; } } catch (Exception) { isMatch = false; } return isMatch; } public async Task> GetAllPCRBs(bool bypassCache) { try { IEnumerable? allPCRBs = null; if (!bypassCache) allPCRBs = _cache.Get>("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>(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? allPCRBs = _cache.Get>("allPCRBs"); if (allPCRBs is not null) { List pcrbList = allPCRBs.ToList(); pcrbList.Add(newPCRB); _cache.Set("allPCRBs", pcrbList); } } public async Task 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{planNumber}"); if (pcrb is null && !bypassCache) pcrb = _cache.Get>("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(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? allPCRBs = _cache.Get>("allPCRBs"); if (allPCRBs is not null) { List pcrbList = allPCRBs.ToList(); pcrbList.RemoveAll(p => p.PlanNumber == planNumber); pcrbList.Add(pcrb); _cache.Set("allPCRBs", pcrbList); } } } return pcrb; } public async Task 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{title}"); if (pcrb is null && !bypassCache) pcrb = _cache.Get>("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(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? allPCRBs = _cache.Get>("allPCRBs"); if (allPCRBs is not null) { List 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? allPCRBs = _cache.Get>("allPCRBs"); if (allPCRBs is not null) { List 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 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> GetAttachmentsByPlanNumber(int planNumber, bool bypassCache) { if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB Plan#"); IEnumerable? attachments = null; if (!bypassCache) attachments = _cache.Get>($"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>(responseContent, jsonSerializerOptions) ?? new List(); 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> GetActionItemsForPlanNumber(int planNumber, bool bypassCache) { if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB Plan#"); IEnumerable? actionItems = null; if (!bypassCache) actionItems = _cache.Get>($"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>(responseContent, jsonSerializerOptions) ?? new List(); 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> GetAttendeesByPlanNumber(int planNumber, bool bypassCache) { if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB Plan#"); IEnumerable? attendees = null; if (!bypassCache) attendees = _cache.Get>($"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>(responseContent, jsonSerializerOptions) ?? new List(); 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> GetPCR3DocumentsForPlanNumber(int planNumber, bool bypassCache) { if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB Plan#"); IEnumerable? documents = null; if (!bypassCache) documents = _cache.Get>($"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>(responseContent, jsonSerializerOptions) ?? new List(); 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}"); } public async Task CreateFollowUp(PCRBFollowUp followUp) { if (followUp is null) throw new ArgumentNullException("follow up cannot be null"); HttpClient httpClient = _httpClientFactory.CreateClient("API"); HttpRequestMessage requestMessage = new(HttpMethod.Post, $"pcrb/followUp") { Content = new StringContent(JsonSerializer.Serialize(followUp), Encoding.UTF8, "application/json") }; HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage); if (!responseMessage.IsSuccessStatusCode) throw new Exception(responseMessage.ReasonPhrase); await GetFollowUpsByPlanNumber(followUp.PlanNumber, true); } public async Task> GetFollowUpsByPlanNumber(int planNumber, bool bypassCache) { if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB Plan#"); IEnumerable? followUps = null; if (!bypassCache) followUps = _cache.Get>($"pcrbFollowUps{planNumber}"); if (followUps is null) { HttpClient httpClient = _httpClientFactory.CreateClient("API"); HttpRequestMessage requestMessage = new(HttpMethod.Get, $"pcrb/followUps?planNumber={planNumber}&bypassCache={bypassCache}"); HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage); if (responseMessage.IsSuccessStatusCode) { string responseContent = await responseMessage.Content.ReadAsStringAsync(); JsonSerializerOptions jsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; followUps = JsonSerializer.Deserialize>(responseContent, jsonSerializerOptions) ?? new List(); if (followUps.Count() > 0) _cache.Set($"pcrbFollowUps{planNumber}", followUps, DateTimeOffset.Now.AddMinutes(5)); } else { throw new Exception(responseMessage.ReasonPhrase); } } return followUps; } public async Task UpdateFollowUp(PCRBFollowUp followUp) { if (followUp is null) throw new ArgumentNullException("follow up cannot be null"); HttpClient httpClient = _httpClientFactory.CreateClient("API"); HttpRequestMessage requestMessage = new(HttpMethod.Put, $"pcrb/followUp") { Content = new StringContent(JsonSerializer.Serialize(followUp), Encoding.UTF8, "application/json") }; HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage); if (!responseMessage.IsSuccessStatusCode) throw new Exception(responseMessage.ReasonPhrase); } public async Task DeleteFollowUp(int id) { if (id <= 0) throw new ArgumentException($"{id} is not a valid PCRB follow up ID"); HttpClient httpClient = _httpClientFactory.CreateClient("API"); HttpRequestMessage requestMessage = new(HttpMethod.Delete, $"pcrb/followUp?id={id}"); HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage); if (!responseMessage.IsSuccessStatusCode) throw new Exception(responseMessage.ReasonPhrase); } }