190 lines
9.0 KiB
C#
190 lines
9.0 KiB
C#
using Serilog;
|
|
using CsvHelper;
|
|
using CsvHelper.Configuration;
|
|
using Newtonsoft.Json;
|
|
using System.Text;
|
|
using System.Globalization;
|
|
using TimeZoneConverter;
|
|
using LCU.Api.Internal.DataCleanUp.Models;
|
|
using LCU.Library.Internal.FileOperationsModels.Models;
|
|
using LCU.Library.Internal.DataCleanUpModel.Models;
|
|
using LCU.Api.Internal.DataCleanUp.Configuration;
|
|
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests;
|
|
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Accounts;
|
|
|
|
namespace LCU.Api.Internal.DataCleanUp.Services
|
|
{
|
|
public class AddFundsToIndirectSavingsAccountsService : IAddFundsToIndirectSavingsAccountsService
|
|
{
|
|
private readonly ICoreApiWrapperService _coreApiWrapperService;
|
|
private readonly IFileOperationsService _fileOperationsService;
|
|
private readonly IndirectSavingsRequirementsDefaults _indirectSavingsDefaults;
|
|
private DataCleanUpConfiguration _dataCleanUpConfiguration;
|
|
private List<IndirectSavingsAccountsSummary> _addFundsToIndirectAccountsSummaries = new List<IndirectSavingsAccountsSummary>();
|
|
|
|
public AddFundsToIndirectSavingsAccountsService(ICoreApiWrapperService coreApiWrapperService, IFileOperationsService fileOperationsService, IndirectSavingsRequirementsDefaults indirectSavingsDefaults, DataCleanUpConfiguration dataCleanUpConfiguration)
|
|
{
|
|
_coreApiWrapperService = coreApiWrapperService;
|
|
_fileOperationsService = fileOperationsService;
|
|
_indirectSavingsDefaults = indirectSavingsDefaults;
|
|
_dataCleanUpConfiguration = dataCleanUpConfiguration;
|
|
}
|
|
|
|
public async Task<DataCleanUpResult> CleanUp(DataCleanUpRequest dataCleanUpRequest)
|
|
{
|
|
Log.Information($"Starting Data CleanUp {dataCleanUpRequest.CleanUpType} at {DateTime.Now}");
|
|
var cleanUpResult = new DataCleanUpResult(dataCleanUpRequest, true, new List<string>());
|
|
var indirectFundRequest = JsonConvert.DeserializeObject<IndirectFundingRequest>(dataCleanUpRequest.DataCleanUpPayload);
|
|
ArgumentNullException.ThrowIfNull(indirectFundRequest);
|
|
|
|
try
|
|
{
|
|
var request = BuildRequestForAccountsToFund();
|
|
|
|
var accountsToFundList = await _coreApiWrapperService.GetAccountsByMajorMinorBranchAndStatus(request);
|
|
|
|
if (accountsToFundList != null)
|
|
{
|
|
Log.Information($"Data CleanUp # of Indirect Accounts To Be Funded {accountsToFundList.Count()} at {DateTime.Now}");
|
|
|
|
foreach (var accountToFund in accountsToFundList)
|
|
{
|
|
cleanUpResult = await HandleSavingsToFundsRequest(cleanUpResult, accountToFund);
|
|
}
|
|
}
|
|
|
|
await HandleExportResultsAddFundsToSavingsAccounts(indirectFundRequest);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Error while Handling Indirect Savings Account to Funds.");
|
|
cleanUpResult.Successful = false;
|
|
cleanUpResult.ResponseStack.Add($"Error while Handling Indirect Savings Account to Funds. " +
|
|
$"Exception:{exception}");
|
|
}
|
|
|
|
Log.Information($"Ending Data CleanUp {dataCleanUpRequest.CleanUpType} at {DateTime.Now}");
|
|
return cleanUpResult;
|
|
}
|
|
|
|
public async Task<DataCleanUpResult> HandleSavingsToFundsRequest(DataCleanUpResult cleanUpResult, AccountsByMajorMinorBranchAndStatusResponse accountToBeFunded)
|
|
{
|
|
try
|
|
{
|
|
cleanUpResult.Successful = false;
|
|
cleanUpResult.ResponseStack = new List<string>();
|
|
|
|
var listFundsToAccountsRequest = _coreApiWrapperService.BuildFundsToAccountRequests(accountToBeFunded.AccountNumber.ToString(), _indirectSavingsDefaults.GLAccountNumber, _indirectSavingsDefaults);
|
|
var basicResponse = await _coreApiWrapperService.PostFundsToAccount(listFundsToAccountsRequest);
|
|
ArgumentNullException.ThrowIfNull(basicResponse);
|
|
|
|
foreach (var response in basicResponse)
|
|
{
|
|
cleanUpResult.Successful = response.Success;
|
|
cleanUpResult.ResponseStack.AddRange(response.Errors);
|
|
}
|
|
|
|
var addFundstoAccountsSummary = HandleCreatingAccountSummary(accountToBeFunded, cleanUpResult);
|
|
_addFundsToIndirectAccountsSummaries.Add(addFundstoAccountsSummary);
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
cleanUpResult.Successful = false;
|
|
cleanUpResult.ResponseStack.Add($"Error while Handling Indirect Savings Account to Funds. " +
|
|
$"AccountNumber: {accountToBeFunded.AccountNumber}, " +
|
|
$"Exception:{exception}" + "</br>");
|
|
|
|
var addFundstoAccountsSummary = HandleCreatingAccountSummary(accountToBeFunded, cleanUpResult);
|
|
_addFundsToIndirectAccountsSummaries.Add(addFundstoAccountsSummary);
|
|
}
|
|
return cleanUpResult;
|
|
}
|
|
|
|
public AccountsByMajorMinorBranchAndStatusRequest BuildRequestForAccountsToFund()
|
|
{
|
|
var coreRequests = _indirectSavingsDefaults.FundingCoreRequests;
|
|
|
|
var request = new AccountsByMajorMinorBranchAndStatusRequest()
|
|
{
|
|
MajorTypes = coreRequests.MajorAccountTypes,
|
|
MinorTypes = coreRequests.MinorAccountTypes,
|
|
AccountStatuses = coreRequests.AccountStatusTypes,
|
|
BranchCodes = coreRequests.BranchCodeTypes
|
|
};
|
|
|
|
return request;
|
|
}
|
|
|
|
public IndirectSavingsAccountsSummary HandleCreatingAccountSummary(AccountsByMajorMinorBranchAndStatusResponse accountToBeFunded, DataCleanUpResult cleanUpResult)
|
|
{
|
|
var addFundstoAccountsSummary = new IndirectSavingsAccountsSummary();
|
|
return addFundstoAccountsSummary.TransformNewSummary(accountToBeFunded.AccountNumber, _indirectSavingsDefaults.FundsDefaults.FundAmount,
|
|
accountToBeFunded.BranchCode, accountToBeFunded.Name,
|
|
string.Join(";", cleanUpResult.ResponseStack.ToArray()));
|
|
}
|
|
|
|
public async Task HandleExportResultsAddFundsToSavingsAccounts(IndirectFundingRequest request)
|
|
{
|
|
try
|
|
{
|
|
var summaryTotal = new List<IndirectSavingsAccountResult>()
|
|
{
|
|
new IndirectSavingsAccountResult
|
|
{
|
|
TotalAccountsFunded = _addFundsToIndirectAccountsSummaries.Where(x => x.AmountFunded != "0").Count().ToString(),
|
|
TotalAccountsNotFunded = _addFundsToIndirectAccountsSummaries.Where(x => x.AmountFunded == "0").Count().ToString()
|
|
}
|
|
};
|
|
|
|
var csvConfig = new CsvConfiguration(CultureInfo.CurrentCulture)
|
|
{
|
|
HasHeaderRecord = true,
|
|
Delimiter = ",",
|
|
Encoding = Encoding.UTF8
|
|
};
|
|
|
|
var mem = new MemoryStream();
|
|
var writer = new StreamWriter(mem);
|
|
var csvWriter = new CsvWriter(writer, csvConfig);
|
|
ArgumentNullException.ThrowIfNull(csvWriter);
|
|
|
|
csvWriter.WriteHeader<IndirectSavingsAccountsSummary>();
|
|
csvWriter.NextRecord();
|
|
csvWriter.WriteRecords(_addFundsToIndirectAccountsSummaries);
|
|
csvWriter.NextRecord();
|
|
csvWriter.WriteHeader<IndirectSavingsAccountResult>();
|
|
csvWriter.NextRecord();
|
|
csvWriter.WriteRecords(summaryTotal);
|
|
writer.Flush();
|
|
var result = Encoding.UTF8.GetString(mem.ToArray());
|
|
|
|
var dateString = GetCurrentDateTimeString("yyyy-MM-dd-HHmmss");
|
|
|
|
var writeFileRequest = new WriteFileRequest()
|
|
{
|
|
DestinationPath = request.SummaryFileDestinationPath,
|
|
FileName = string.Join("-", request.Filename, dateString),
|
|
Overwrite = false,
|
|
Extension = "csv",
|
|
FileData = result,
|
|
};
|
|
|
|
await _fileOperationsService.WriteFile(writeFileRequest);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
throw new Exception($"Export Add Funds Summary Error, {exception.Message}");
|
|
}
|
|
}
|
|
|
|
public string GetCurrentDateTimeString(string format)
|
|
{
|
|
var assumedTimezone = TZConvert.GetTimeZoneInfo(_dataCleanUpConfiguration.TimeZone);
|
|
var currentDateTime = TimeZoneInfo.ConvertTime(DateTime.Now, assumedTimezone);
|
|
|
|
return currentDateTime.ToString(format);
|
|
}
|
|
}
|
|
}
|