@ -1,8 +1,11 @@
using System.Text ;
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 ;
@ -11,28 +14,52 @@ 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 ) {
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 {
@ -63,6 +90,37 @@ public class PCRBService : IPCRBService {
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 ;
@ -219,7 +277,7 @@ public class PCRBService : IPCRBService {
}
public async Task UpdatePCRB ( PCRB pcrb ) {
if ( pcrb is null ) throw new ArgumentNullException ( "M RB cannot be null" ) ;
if ( pcrb is null ) throw new ArgumentNullException ( "PC RB cannot be null" ) ;
HttpClient httpClient = _httpClientFactory . CreateClient ( "API" ) ;
@ -251,7 +309,7 @@ public class PCRBService : IPCRBService {
HttpClient httpClient = _httpClientFactory . CreateClient ( "API" ) ;
HttpRequestMessage requestMessage = new ( HttpMethod . Delete , $"pcrb/delete ?planNumber={planNumber}" ) ;
HttpRequestMessage requestMessage = new ( HttpMethod . Delete , $"pcrb?planNumber={planNumber}" ) ;
HttpResponseMessage responseMessage = await httpClient . SendAsync ( requestMessage ) ;
@ -260,4 +318,450 @@ public class PCRBService : IPCRBService {
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}" ) ;
}
}