-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add integration log for verifying goodStanding * Set message with responseBody (for integration Log) * Generalization extension for P405 and P400 --------- Co-authored-by: bcgov-fw <[email protected]>
- Loading branch information
Showing
4 changed files
with
205 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.OpenApi.Services; | ||
using OFM.Infrastructure.WebAPI.Extensions; | ||
using OFM.Infrastructure.WebAPI.Models; | ||
using OFM.Infrastructure.WebAPI.Services.AppUsers; | ||
|
@@ -24,7 +25,7 @@ public P400VerifyGoodStandingProvider(IOptionsSnapshot<ExternalServices> ApiKeyB | |
_BCRegistrySettings = ApiKeyBCRegistry.Value.BCRegistryApi; | ||
_appUserService = appUserService; | ||
_d365webapiservice = d365WebApiService; | ||
_logger = loggerFactory.CreateLogger(LogCategory.Process); ; | ||
_logger = loggerFactory.CreateLogger(LogCategory.Process); | ||
_timeProvider = timeProvider; | ||
} | ||
|
||
|
@@ -38,11 +39,10 @@ public string RequestUri | |
<fetch distinct="true" no-lock="true"> | ||
<entity name="account"> | ||
<attribute name="accountid" /> | ||
<attribute name="ofm_business_number" /> | ||
<attribute name="name" /> | ||
<attribute name="modifiedon" /> | ||
<attribute name="ofm_incorporation_number" /> | ||
<attribute name="ofm_business_number" /> | ||
<attribute name="statecode" /> | ||
<attribute name="statuscode" /> | ||
<filter type="and"> | ||
<condition attribute="accountid" operator="eq" value="{_processParams?.Organization?.organizationId}" /> | ||
</filter> | ||
|
@@ -99,44 +99,91 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService | |
|
||
var startTime = _timeProvider.GetTimestamp(); | ||
|
||
string? queryValue = (!String.IsNullOrEmpty(processParams?.Organization?.incorporationNumber)) ? | ||
(_processParams?.Organization?.incorporationNumber)!.Trim() : (_processParams?.Organization?.legalName)!.Trim(); | ||
var localData = await GetData(); | ||
|
||
var legalType = "A,B,BC,BEN,C,CC,CCC,CEM,CP,CS,CUL,EPR,FI,FOR,GP,LIC,LIB,LL,LLC,LP,MF,PA,PAR,PFS,QA,QB,QC,QD,QE,REG,RLY,S,SB,SP,T,TMY,ULC,UQA,UQB,UQC,UQD,UQE,XCP,XL,XP,XS"; | ||
var status = "active"; | ||
var queryString = $"?query=value:{queryValue}::identifier:::bn:::name:" + | ||
$"&categories=legalType:{legalType}::status:{status}"; | ||
var deserializedData = JsonSerializer.Deserialize<List<D365Organization_Account>>(localData.Data.ToString()); | ||
|
||
var path = $"{_BCRegistrySettings.RegistrySearchUrl}" + $"{queryString}"; | ||
deserializedData?.ForEach(async organization => | ||
{ | ||
String organizationId = organization.accountid; | ||
String legalName = organization.name; | ||
String incorporationNumber = organization.ofm_incorporation_number; | ||
String businessNumber = organization.ofm_business_number; | ||
|
||
var client = new HttpClient(); | ||
var request = new HttpRequestMessage(HttpMethod.Get, path); | ||
request.Headers.Add("Account-Id", "1"); | ||
request.Headers.Add(_BCRegistrySettings.KeyName, _BCRegistrySettings.KeyValue); | ||
string? queryValue = (!String.IsNullOrEmpty(incorporationNumber)) ? | ||
incorporationNumber : legalName.Trim(); | ||
|
||
var response = await client.SendAsync(request); | ||
response.EnsureSuccessStatusCode(); | ||
var legalType = "A,B,BC,BEN,C,CC,CCC,CEM,CP,CS,CUL,EPR,FI,FOR,GP,LIC,LIB,LL,LLC,LP,MF,PA,PAR,PFS,QA,QB,QC,QD,QE,REG,RLY,S,SB,SP,T,TMY,ULC,UQA,UQB,UQC,UQD,UQE,XCP,XL,XP,XS"; | ||
var status = "active"; | ||
var queryString = $"?query=value:{queryValue}::identifier:::bn:::name:" + | ||
$"&categories=legalType:{legalType}::status:{status}"; | ||
|
||
BCRegistrySearchResult? searchResult = await response.Content.ReadFromJsonAsync<BCRegistrySearchResult>(); | ||
var path = $"{_BCRegistrySettings.RegistrySearchUrl}" + $"{queryString}"; | ||
|
||
if (searchResult is null || searchResult.searchResults.totalResults < 1) | ||
{ | ||
// Todo: Add a new message for this scenario or try seach by name | ||
_logger.LogError(CustomLogEvent.Process, "No results found."); | ||
return ProcessResult.PartialSuccess(ProcessId, ["No records found."], 0, 0).SimpleProcessResult; | ||
} | ||
var client = new HttpClient(); | ||
var request = new HttpRequestMessage(HttpMethod.Get, path); | ||
request.Headers.Add("Account-Id", "1"); | ||
request.Headers.Add(_BCRegistrySettings.KeyName, _BCRegistrySettings.KeyValue); | ||
|
||
if (searchResult.searchResults.totalResults > 1) | ||
{ | ||
// ToDo: Process and filter the result further | ||
_logger.LogError(CustomLogEvent.Process, "More than one records returned. Please resolve this issue to ensure uniqueness"); | ||
return ProcessResult.PartialSuccess(ProcessId, ["Multiple results returned."], 0, 0).SimpleProcessResult; | ||
} | ||
var response = await client.SendAsync(request); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
BCRegistrySearchResult? searchResult = await response.Content.ReadFromJsonAsync<BCRegistrySearchResult>(); | ||
|
||
var responseBody = await response.Content.ReadAsStringAsync(); | ||
|
||
// Organization - Update | ||
var goodStandingStatus = 3; // 1 - Good, 2 - No Good, 3 - Error | ||
|
||
// Integration Log - Create | ||
var externalService = "BC Registries"; | ||
var subject = string.Empty; | ||
var logCategory = 1; // 1 - Info, 2 - Warning, 3 - Error, 4 - Critical | ||
var message = $"{responseBody.CleanLog()}"; | ||
|
||
if (searchResult is null || searchResult.searchResults.totalResults < 1) | ||
{ | ||
// Todo: Add a new message for this scenario or try seach by name | ||
_logger.LogError(CustomLogEvent.Process, "No results found."); | ||
|
||
goodStandingStatus = 3; | ||
logCategory = 3; | ||
subject = "No results found"; | ||
await UpdateOrganizationCreateIntegrationLog(_appUserService, _d365webapiservice, organizationId, goodStandingStatus, subject, logCategory, message, externalService); | ||
// return ProcessResult.PartialSuccess(ProcessId, ["No records found."], 0, 0).SimpleProcessResult; | ||
} | ||
|
||
if (searchResult.searchResults.totalResults > 1) | ||
{ | ||
// ToDo: Process and filter the result further | ||
_logger.LogError(CustomLogEvent.Process, "More than one records returned. Please resolve this issue to ensure uniqueness"); | ||
|
||
goodStandingStatus = 3; | ||
logCategory = 3; | ||
subject = "Multiple results returned"; | ||
await UpdateOrganizationCreateIntegrationLog(_appUserService, _d365webapiservice, organizationId, goodStandingStatus, subject, logCategory, message, externalService); | ||
// return ProcessResult.PartialSuccess(ProcessId, ["Multiple results returned."], 0, 0).SimpleProcessResult; | ||
} | ||
|
||
if (searchResult.searchResults.totalResults == 1) | ||
{ | ||
goodStandingStatus = searchResult.searchResults.results.First().goodStanding ? 1 : 2; // 1 - Good, 2 - No Good, 3 - Error | ||
logCategory = 1; // 1 - Info, 2 - Warning, 3 - Error, 4 - Critical | ||
subject = "One result found"; | ||
await UpdateOrganizationCreateIntegrationLog(_appUserService, _d365webapiservice, organizationId, goodStandingStatus, subject, logCategory, message, externalService); | ||
// return ProcessResult.Completed(ProcessId).SimpleProcessResult; | ||
} | ||
|
||
var statement = $"accounts({_processParams?.Organization?.organizationId.ToString()})"; | ||
}); | ||
|
||
return ProcessResult.Completed(ProcessId).SimpleProcessResult; | ||
} | ||
|
||
private async Task<JsonObject> UpdateOrganizationCreateIntegrationLog(ID365AppUserService appUserService, ID365WebApiService d365WebApiService, String organizationId, int goodStandingStatus, string subject, int category, string message, string externalService) | ||
{ | ||
var statement = $"accounts({organizationId})"; | ||
var payload = new JsonObject { | ||
{ "ofm_good_standing_status", searchResult.searchResults.results.First().goodStanding ? 1 : 0}, | ||
{ "ofm_good_standing_status", goodStandingStatus}, | ||
{ "ofm_good_standing_validated_on", DateTime.UtcNow } | ||
}; | ||
|
||
|
@@ -152,6 +199,17 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService | |
return ProcessResult.Failure(ProcessId, new String[] { responseBody }, 0, 0).SimpleProcessResult; | ||
} | ||
|
||
var entitySetName = "ofm_integration_logs"; | ||
var payload2 = new JsonObject { | ||
{ "ofm_subject", subject }, | ||
{ "ofm_category", category}, | ||
{ "ofm_message", message}, | ||
{ "ofm_service_name", externalService}, | ||
{ "[email protected]", $"/accounts({organizationId})"} | ||
}; | ||
var requestBody2 = JsonSerializer.Serialize(payload2); | ||
var CreateResponse = await d365WebApiService.SendCreateRequestAsync(appUserService.AZSystemAppUser, entitySetName, requestBody2); | ||
|
||
return ProcessResult.Completed(ProcessId).SimpleProcessResult; | ||
} | ||
} |
Oops, something went wrong.