Adding
This commit is contained in:
+61
@@ -0,0 +1,61 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Collections.CollectionsApiLib.Models;
|
||||
using LCU.API.Collections.RepoApi.Configuration;
|
||||
using LCU.Shared.Collections.RepoApiLib.Models.Settings;
|
||||
|
||||
namespace LCU.API.Collections.RepoApi.DataAccess
|
||||
{
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CollectionsDao : DaoClient, ICollectionsDao
|
||||
{
|
||||
private readonly RepoApiConfiguration _configuration;
|
||||
|
||||
public CollectionsDao(RepoApiConfiguration configuration,
|
||||
IHttpClientFactory clientFactory) : base(clientFactory)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public async Task<TemenosAccount> RetrieveAccountAsync(string accountNo)
|
||||
{
|
||||
var url = BuildTemenosAccountUrl(accountNo);
|
||||
var accounts = await RetrieveByIdentifierAsync<IReadOnlyCollection<TemenosAccount>>(url, accountNo);
|
||||
|
||||
if (accounts != null && accounts.Any())
|
||||
{
|
||||
return accounts.Where(x => x.AccountIdentifier == accountNo)
|
||||
.OrderByDescending(x => x.OriginalLoanDate).First();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<TemenosHistory>> RetrieveHistoryAsync(int accountId)
|
||||
{
|
||||
var url = _configuration.CollectionsLetterUrl.Replace(":accountId", accountId.ToString());
|
||||
return await RetrieveByIdentifierAsync<IReadOnlyCollection<TemenosHistory>>(url, accountId.ToString());
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<BranchEntry>> RetrieveBranchSetAsync()
|
||||
{
|
||||
var url = _configuration.CoreBranchSetUrl;
|
||||
return await RetrieveAllAsync<IReadOnlyCollection<BranchEntry>>(url);
|
||||
}
|
||||
|
||||
public async Task<TemenosConfiguration> RetrieveTemenosConfigurationAsync()
|
||||
{
|
||||
var url = _configuration.CollectionsConfigUrl;
|
||||
return await RetrieveAllAsync<TemenosConfiguration>(url);
|
||||
}
|
||||
|
||||
public string BuildTemenosAccountUrl(string accountNo)
|
||||
{
|
||||
return _configuration.CollectionAccountUrl + _configuration.OrganizationCode + accountNo;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Collections.RepoApi.Configuration;
|
||||
using LCU.Shared.Collections.RepoApiLib.Models.Consumer;
|
||||
|
||||
namespace LCU.API.Collections.RepoApi.DataAccess
|
||||
{
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CoreDao : DaoClient, ICoreDao
|
||||
{
|
||||
private readonly RepoApiConfiguration _configuration;
|
||||
|
||||
public CoreDao(RepoApiConfiguration configuration,
|
||||
IHttpClientFactory clientFactory) : base(clientFactory)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public async Task<MemberDetail> RetrieveMemberAsync(string accountNo)
|
||||
{
|
||||
var url = CreateUrlFromAccountNo(_configuration.CoreMemberUrl, accountNo);
|
||||
return await RetrieveByIdentifierAsync<MemberDetail>(url, accountNo);
|
||||
}
|
||||
|
||||
public async Task<LoanDetail> RetrieveLoanAsync(string accountNo)
|
||||
{
|
||||
var url = CreateUrlFromAccountNo(_configuration.CoreLoanUrl, accountNo);
|
||||
return await RetrieveByIdentifierAsync<LoanDetail>(url, accountNo);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<CollateralDetail>> RetrieveCollateralAsync(string accountNo)
|
||||
{
|
||||
var url = CreateUrlFromAccountNo(_configuration.CoreCollateralUrl, accountNo);
|
||||
return await RetrieveByIdentifierAsync<IReadOnlyCollection<CollateralDetail>>(url, accountNo);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<CoreTransaction>> RetrieveTransactionsAsync(string accountNo)
|
||||
{
|
||||
var url = CreateUrlFromAccountNoAndDateRange(_configuration.CoreLoanTransactions, accountNo);
|
||||
return await RetrieveByIdentifierAsync<IReadOnlyCollection<CoreTransaction>>(url, accountNo);
|
||||
}
|
||||
|
||||
private string CreateUrlFromAccountNo(string inputUrl, string accountNo)
|
||||
{
|
||||
var url = inputUrl.Replace(":loanId", accountNo);
|
||||
return url;
|
||||
}
|
||||
|
||||
private string CreateUrlFromAccountNoAndDateRange(string inputUrl, string accountNo)
|
||||
{
|
||||
var url = inputUrl.Replace(":accountNo", accountNo);
|
||||
url = url.Replace(":startDate", DateTimeForUri(DateTime.Now.AddDays(_configuration.TransactionHistoryStartDateOffset)));
|
||||
url = url.Replace(":endDate", DateTimeForUri(DateTime.Now));
|
||||
url += FormatArrayValuesIntoQuery(_configuration.TransactionTypeCodes, _configuration.TransactionTypeCodeQueryPrefix);
|
||||
return url;
|
||||
}
|
||||
|
||||
private string DateTimeForUri(DateTime dateTime)
|
||||
{
|
||||
return dateTime.ToString(_configuration.TransactionHistoryParameterDateFormat);
|
||||
}
|
||||
|
||||
private string FormatArrayValuesIntoQuery(List<string> arrayValues, string queryPrefix)
|
||||
{
|
||||
var returnString = "";
|
||||
foreach (var arrayValue in arrayValues) {
|
||||
returnString += (queryPrefix + arrayValue);
|
||||
}
|
||||
|
||||
return returnString;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Collections.RepoApi.Exceptions;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
|
||||
namespace LCU.API.Collections.RepoApi.DataAccess
|
||||
{
|
||||
public abstract class DaoClient
|
||||
{
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
|
||||
protected DaoClient(IHttpClientFactory clientFactory)
|
||||
{
|
||||
_clientFactory = clientFactory;
|
||||
}
|
||||
|
||||
public async Task<T> RetrieveByIdentifierAsync<T>(string url, string identifier)
|
||||
{
|
||||
// Temporary log to illustrate this working completely
|
||||
Log.Information($"Retrieve Async called for {typeof(T).Name} with {url}");
|
||||
using (var client = _clientFactory.CreateClient())
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
var response = await client.SendAsync(request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
try
|
||||
{
|
||||
var retrievedObject = JsonConvert.DeserializeObject<T>(
|
||||
await response.Content.ReadAsStringAsync());
|
||||
|
||||
return retrievedObject;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex,$"Error Deserializing {typeof(T).Name} for {identifier}");
|
||||
throw new ServerErrorException();
|
||||
}
|
||||
}
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
|
||||
Log.Error($"Retrieve {typeof(T).Name} Bad Status Code {response.StatusCode}, for { url}");
|
||||
throw new MissingRequirementException(typeof(T).Name, $"Failed to retrieve {typeof(T).Name} for {identifier}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<T> RetrieveAllAsync<T>(string url)
|
||||
{
|
||||
// Temporary log
|
||||
Log.Information($"Retrieve All Async called for {typeof(T).Name} with {url}");
|
||||
using (var client = _clientFactory.CreateClient())
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
var response = await client.SendAsync(request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
try
|
||||
{
|
||||
var retrievedObject = JsonConvert.DeserializeObject<T>(
|
||||
await response.Content.ReadAsStringAsync());
|
||||
|
||||
return retrievedObject;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex,$"Error Deserializing {typeof(T).Name} for {url}");
|
||||
throw new ServerErrorException();
|
||||
}
|
||||
}
|
||||
|
||||
Log.Error($"Retrieve {typeof(T).Name} Bad Status Code {response.StatusCode}, for { url}");
|
||||
throw new MissingRequirementException(typeof(T).Name, $"Failed to retrieve {typeof(T).Name} for {url}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Collections.CollectionsApiLib.Models;
|
||||
using LCU.Shared.Collections.RepoApiLib.Models.Settings;
|
||||
|
||||
namespace LCU.API.Collections.RepoApi.DataAccess
|
||||
{
|
||||
public interface ICollectionsDao
|
||||
{
|
||||
Task<TemenosAccount> RetrieveAccountAsync(string accountNo);
|
||||
Task<IReadOnlyCollection<TemenosHistory>> RetrieveHistoryAsync(int accountId);
|
||||
Task<IReadOnlyCollection<BranchEntry>> RetrieveBranchSetAsync();
|
||||
Task<TemenosConfiguration> RetrieveTemenosConfigurationAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Shared.Collections.RepoApiLib.Models.Consumer;
|
||||
|
||||
namespace LCU.API.Collections.RepoApi.DataAccess
|
||||
{
|
||||
public interface ICoreDao
|
||||
{
|
||||
Task<MemberDetail> RetrieveMemberAsync(string accountNo);
|
||||
Task<LoanDetail> RetrieveLoanAsync(string accountNo);
|
||||
Task<IReadOnlyCollection<CollateralDetail>> RetrieveCollateralAsync(string accountNo);
|
||||
Task<IReadOnlyCollection<CoreTransaction>> RetrieveTransactionsAsync(string accountNo);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user