public IActionResult GetExternalAccounts() { string clientId = Request.HttpContext.Items["ClientId"]?.ToString(); List <Datafeed> datafeeds = _datafeedProcessor.GetDatafeeds(clientId); List <ExternalAccount> accounts = new List <ExternalAccount>(); foreach (var datafeed in datafeeds) { IDatafeedAPI datafeedApi = DatafeedManager.ResolveApiType(datafeed.Provider); if (datafeedApi is TrueLayerAPI trueLayerAPI) { trueLayerAPI._ClientId = _appSettings.TrueLayer_ClientID; trueLayerAPI._Secret = _appSettings.TrueLayer_ClientSecret; trueLayerAPI.SetMode(_appSettings.TrueLayer_Mode); trueLayerAPI._datafeedDataService = new FinanceAPIMongoDataService.DataService.DatafeedDataService(_appSettings.MongoDB_ConnectionString); } accounts.AddRange(datafeedApi.GetExternalAccounts(clientId, datafeed.AccessKey, datafeed.VendorID, datafeed.VendorName, datafeed.Provider)); } List <ExternalAccount> mappedAccounts = _datafeedProcessor.GetExternalAccounts(clientId); foreach (ExternalAccount account in mappedAccounts) { if (!accounts.Any(a => a.AccountID == account.AccountID)) { accounts.Add(account); } } return(Json(accounts)); }
private decimal ProcessExternalAccount(ExternalAccount externalAccount, IDatafeedAPI datafeedApi, Account account, out decimal availableBalance) { string encryptedAccessKey = _datafeedDataService.GetAccessKeyForExternalAccount(externalAccount.Provider, externalAccount.VendorID, Task.ClientID); availableBalance = 0; if (string.IsNullOrEmpty(externalAccount?.AccountID) || string.IsNullOrEmpty(encryptedAccessKey) || datafeedApi == null) { return(0); } List <Transaction> transactions = datafeedApi.GetAccountTransactions(externalAccount.AccountID, encryptedAccessKey, out decimal accountBalance, out availableBalance); Log($"Fetched [{transactions.Count}] transactions from provider"); List <Transaction> sortedTransactions = new List <Transaction>(); foreach (var transaction in transactions) { transaction.ClientID = Task.ClientID; transaction.AccountID = account.ID; transaction.AccountName = account?.AccountName ?? "Unknown"; if (sortedTransactions.Where(t => t.ID == transaction.ID).Count() == 0) { sortedTransactions.Add(transaction); } } //Run Algorithms sortedTransactions = MerchantAlgorithm(sortedTransactions); sortedTransactions = VendorAlgorithm(sortedTransactions); // Remove any pending transactions that have now been settled (Pending transaction not supplied by provider anymore indicates that it has settled under a different transaction id) var exitingPendingTransactions = _transactionDataService.GetTransactions(account.ClientID).Where(t => t.Status == Status.PENDING && t.Owner != "User"); foreach (var transaction in exitingPendingTransactions) { if (!sortedTransactions.Where(t => t.Status == Status.PENDING).Any(t => t.ID == transaction.ID)) { _transactionDataService.DeleteTransaction(transaction.ID, transaction.ClientID); } } //Add All sorted transactions foreach (Transaction transaction in sortedTransactions) { bool?imported = _transactionDataService.ImportDatafeedTransaction(transaction); } return(accountBalance); }