using System.Globalization; using System.Web; using Newtonsoft.Json; using LCU.API.Internal.Operations.IncentiveApi.Models.Core; using LCU.API.Internal.Operations.IncentiveApi.Models.ExternalActivities; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Accounts; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Activity; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Transaction; using LCU.Library.Internal.CoreApiWrapperModel.LcuModels.Account; using LCU.Library.Internal.IncentiveApiModels.Entities; using LCU.API.Internal.Operations.IncentiveApi.Configuration; using LCU.Library.Internal.LcuHttpClient.HttpRequest; using NewAtmCheckCardActivity = LCU.API.Internal.Operations.IncentiveApi.Models.ExternalActivities.NewAtmCheckCardActivity; using NewCheckingAccountActivity = LCU.API.Internal.Operations.IncentiveApi.Models.ExternalActivities.NewCheckingAccountActivity; using NewLoanSaleActivity = LCU.API.Internal.Operations.IncentiveApi.Models.ExternalActivities.NewLoanSaleActivity; using System.Net; namespace LCU.API.Internal.Operations.IncentiveApi.DataAccess; public class CoreApiWrapperDao : ICoreApiWrapperDao { private readonly IGetRequest _getRequest; private readonly IPostRequest _postRequest; private readonly CoreApiWrapperConfiguration _coreApiWrapperConfiguration; public CoreApiWrapperDao(CoreApiWrapperConfiguration coreApiWrapperConfiguration, IGetRequest getRequest, IPostRequest postRequest) { _getRequest = getRequest; _postRequest = postRequest; _coreApiWrapperConfiguration = coreApiWrapperConfiguration; } public async Task> GetNewCheckingAccountSalesActivity(DateTime activityDt) { try { var url = _coreApiWrapperConfiguration.GetNewCheckingAccountActivityUrl + $"?newerThanDateTime={DateTimeToUrlString(activityDt)}"; var coreResponse = await _getRequest.AsyncWithNewton>(url, null, 0, 300); return coreResponse.Result; } catch (Exception ex) { Log.Error(ex, "Error Getting New Checking Account Sales from DNA"); throw; } } public async Task> GetNewCheckingDigitalBankingAccountSalesActivity(DateTime activityDt) { try { var url = _coreApiWrapperConfiguration.GetNewDigitalBankingCheckingAccountActivityUrl + $"?newerThanDateTime={DateTimeToUrlString(activityDt)}"; var coreResponse = await _getRequest.AsyncWithNewton>(url,null, 0, 300); return coreResponse.Result; } catch (Exception ex) { Log.Error(ex, "Error Getting New Checking Account Sales from DNA for Digital Banking"); throw; } } public async Task> GetNewAtmCheckCardSalesActivity(DateTime activityDt) { try { var url = _coreApiWrapperConfiguration.GetNewATMCheckCardActivityUrl + $"?newerThanDateTime={DateTimeToUrlString(activityDt)}"; var coreResponse = await _getRequest.AsyncWithNewton>(url, null, 0, 500); return coreResponse.Result; } catch (Exception ex) { Log.Error(ex, "Error Getting New ATM and Check Card Sales from DNA"); throw; } } public async Task> GetNewActivityManagerReferralsActivity(DateTime activityDt) { try { var url = _coreApiWrapperConfiguration.GetNewActivityManagerReferralsActivityUrl + $"?newerThanOriginationDateTime={DateTimeToUrlString(activityDt)}"; var coreResponse = await _getRequest.AsyncWithNewton>(url, null, 0, 300); return coreResponse.Result; } catch (Exception ex) { Log.Error(ex, "Error Getting New Activity Manager Referrals from DNA"); throw; } } public async Task> GetNewMortgageSaleActivities(DateTime activityDt) { try { var url = _coreApiWrapperConfiguration.GetNewMortgageAccountActivityUrl + $"?newerThanDateTime={DateTimeToUrlString(activityDt)}"; var coreResponse = await _getRequest.AsyncWithNewton>(url, null, 0, 300); return coreResponse.Result; } catch (Exception ex) { Log.Error(ex, "Error Getting New Mortgage Sales From DNA"); throw; } } public async Task> GetNewVehicleSaleActivities(DateTime activityDt) { try { var url = _coreApiWrapperConfiguration.GetNewVehicleSaleActivityUrl + $"?newerThanDateTime={DateTimeToUrlString(activityDt)}"; var coreResponse = await _getRequest.AsyncWithNewton>(url, null, 0, 300); return coreResponse.Result; } catch (Exception ex) { Log.Error(ex, "Error Getting New Vehicle Sales from DNA"); throw; } } public async Task> GetNewDpEligibleSaleActivities(DateTime activityDt) { try { var url = _coreApiWrapperConfiguration.GetNewDPEligibleAccountsActivityUrl + $"?newerThanDateTime={DateTimeToUrlString(activityDt)}"; var coreResponse = await _getRequest.AsyncWithNewton>(url, null, 0, 300); return coreResponse.Result; } catch (Exception ex) { Log.Error(ex, "Error Getting New DP Eligible Sales and Activities"); throw; } } public async Task> GetUpdatedAccountInformation(List accountNumbers) { try { var path = _coreApiWrapperConfiguration.GetAccountActivityAccountDetailsUrl; var uri = new Uri(path); var coreResponse = await _postRequest.Async>(uri, new ActivityAccountDetailsRequest() { AccountNumbers = accountNumbers }, 0, 300); return coreResponse; } catch (Exception ex) { Log.Error(ex, "Error Getting Updated Account Information"); throw; } } public async Task> GetActivityManagerActivities(ActivityQueryRequest request) { try { var path = _coreApiWrapperConfiguration.QueryActivityManagerActivityUrl; var uri = new Uri(path); var coreResponse = await _postRequest.Async>(uri, new ActivityQueryRequest() { ActivityIds = request.ActivityIds }, 0, 300); return coreResponse.Result; } catch (Exception ex) { Log.Error(ex, "Error Getting Updated Account Information"); throw; } } public async Task> GetTransactionHistory(string accountNumber, DateTime startDate, DateTime endDate, List transactionCodes) { try { var transactionCodesString = transactionCodes == null ? "" : string.Join(",", transactionCodes); var url = _coreApiWrapperConfiguration.GetTransactionHistoryUrl + $"?startDateRange={DateTimeToUrlString(startDate)}&endDateRange={DateTimeToUrlString(endDate)}" + $"&accountNumber={accountNumber}&transactionTypeCodes={transactionCodesString}"; var httpStatusCodes = new List() { HttpStatusCode.NoContent, HttpStatusCode.NotFound }; var responseMessage = await _getRequest.Async>(url, httpStatusCodes, 0, 300); var json = JsonConvert.SerializeObject(responseMessage); var result = JsonConvert.DeserializeObject>(json); return result ?? new List(); } catch (Exception ex) { Log.Error(ex, $"Error Getting Transaction History for AccountNumber: {accountNumber}, TransactionCodes: {transactionCodes}, from {startDate.ToString(CultureInfo.InvariantCulture)} to {endDate.ToString(CultureInfo.InvariantCulture)}"); throw; } } public async Task> GetEStatmentDeliveryMethods(DateTime activityDt) { try { var url = _coreApiWrapperConfiguration.GetEStatementDeliveryMethodsUrl + $"?newerThanDateTime={DateTimeToUrlString(activityDt)}"; var coreResponse = await _getRequest.AsyncWithNewton>(url, null, 0, 300); return coreResponse.Result; } catch (Exception ex) { Log.Error(ex, "Error Getting EStatement Delivery Methods from DNA"); throw; } } public string DateTimeToUrlString(DateTime dateTime) { var dtstring = dateTime.ToString("yyyy-MM-ddTHH:mm:ss"); return HttpUtility.UrlEncode(dtstring); } }