public async Task <List <BankAccountTransactionModel> > GetBankAccountTransactionsAsync(Guid userId, Guid assetId) { string cacheKey = $"{assetId}_{nameof(BankAccountTransactionModel)}s"; AssetBankAccount account = await _bankAccountRepository.GetByAssetIdAsync(userId, assetId); if (account == null) { throw new Exception("Asset bank account not found."); } if (_cache.TryGetValue(cacheKey, out List <BankAccountTransactionModel> transactions)) { return(transactions); } IBankIntegrationService bankAccountservice = _bankIntegrationServiceResolver.Resolve(account.BankName); // TODO: Remove hard-coded dates. DateTime now = DateTime.UtcNow; DateTime from = new DateTime(now.Year, now.Month, 1); var requestParams = new AccountStatementRequestParams(account.Token, account.BankAccountId, account.BankClientId, from, now); var accountTransactions = await bankAccountservice.GetAccountTransactionsAsync(requestParams); transactions = accountTransactions .Select(t => new BankAccountTransactionModel(assetId, t.TransactionDate, t.Description, t.Currency, t.Amount, t.Balance)) .ToList(); _cache.Set(cacheKey, transactions, TimeSpan.FromMinutes(5)); return(transactions); }
public async Task <BankAccountBalance> GetBankAccountBalanceAsync(Guid userId, Guid assetId) { AssetBankAccount account = await _bankAccountRepository.GetByAssetIdAsync(userId, assetId); if (account == null) { throw new Exception("Asset bank account not found."); } BankModel bank = _bankService.GetBank(account.BankName); TimeSpan syncSpan = DateTime.UtcNow - account.LastSyncDateTime.Value; BankAccountBalance accountBalance = null; if (syncSpan.TotalSeconds > bank.SyncFreqInSeconds) { IBankIntegrationService bankAccountservice = _bankIntegrationServiceResolver.Resolve(account.BankName); var requestParams = new BankAccountsRequestParams(account.Token, account.BankClientId, account.BankAccountId); IReadOnlyCollection <ExternalAccountBalanceModel> accountBalances = await bankAccountservice.GetAccountsAsync(requestParams); ExternalAccountBalanceModel external = accountBalances.FirstOrDefault(ab => ab.AccountId == account.BankAccountId); accountBalance = new BankAccountBalance(external.BankName, external.AccountId, external.Currency, external.Balance); await _bankAccountRepository.UpdateLastSyncAsync(userId, assetId, DateTime.UtcNow, accountBalance.AccountName); await _unitOfWork.SaveChangesAsync(); } return(accountBalance); }
public BankAccountModel(AssetBankAccount account) { AssetId = account.AssetId; BankName = account.BankName; BankAccountName = account.BankAccountName; BankAccountIdMask = GetMask(account.BankAccountId); TokenMask = GetMask(account.Token); BankClientId = account.BankClientId; }
public async Task <BankAccountModel> GetBankAccountAsync(Guid userId, Guid assetId) { AssetBankAccount assetBankAccount = await _bankAccountRepository.GetByAssetIdAsync(userId, assetId); BankAccountModel result = assetBankAccount != null ? new BankAccountModel(assetBankAccount) : null; return(result); }
public async Task <AssetBankAccount> UpdateLastSyncAsync(Guid userId, Guid assetId, DateTime lastSyncDateTime, string accountName) { AssetBankAccount bankAccount = await _set.FirstAsync(ba => ba.AssetId == assetId && ba.Asset.UserId == userId); bankAccount.LastSyncDateTime = lastSyncDateTime; bankAccount.BankAccountName = accountName; EntityEntry <AssetBankAccount> result = _set.Update(bankAccount); return(result.Entity); }
public async Task <AssetBankAccount> AlterAsync(Guid userId, Guid assetId, string bankName, string token, string bankClientId) { AssetBankAccount bankAccount = await _set.FirstOrDefaultAsync(ba => ba.AssetId == assetId && ba.Asset.UserId == userId); EntityEntry <AssetBankAccount> result; if (bankAccount != null) { bankAccount.BankName = bankName; bankAccount.Token = token; bankAccount.BankClientId = bankClientId; result = _set.Update(bankAccount); } else { AssetBankAccount newAccount = new AssetBankAccount(Guid.NewGuid(), assetId, bankName, token, bankClientId); result = await _set.AddAsync(newAccount); } return(result.Entity); }
public async Task <List <ExternalBankAccountModel> > SubmitBankClientAuthDataAsync(Guid userId, Guid assetId, string bankName, string token, string bankClientId, string cardNumber) { //TODO: Catch errors. Verify if token is correct and then save it to database. Encrypt this token. BankAccountsRequestParams clientData = new BankAccountsRequestParams(token, bankClientId, cardNumber); IBankIntegrationService bankIntegrationService = _bankIntegrationServiceResolver.Resolve(bankName); IReadOnlyCollection <ExternalAccountBalanceModel> accountBalances = await bankIntegrationService.GetAccountsAsync(clientData); if (accountBalances == null) { throw new Exception($"Sync with bank {bankName} failed."); } AssetBankAccount bankAccount = await _bankAccountRepository.AlterAsync(userId, assetId, bankName, token, bankClientId); await _unitOfWork.SaveChangesAsync(); List <ExternalBankAccountModel> result = accountBalances .Select(a => new ExternalBankAccountModel(a.AccountId, a.AccountName)) .ToList(); return(result); }
public async Task DeleteAsync(Guid userId, Guid assetId) { AssetBankAccount bankAccount = await _set.FirstAsync(ba => ba.AssetId == assetId && ba.Asset.UserId == userId); _set.Remove(bankAccount); }