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 <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); }