Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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 IBankIntegrationService Resolve(string bankName)
        {
            IBankIntegrationService service = _bankIntegrationServices
                                              .FirstOrDefault(s => s.BankName.Equals(bankName, StringComparison.OrdinalIgnoreCase));

            if (service == null)
            {
                throw new Exception("Bank Integration Service is not supported");
            }

            return(service);
        }
Exemplo n.º 4
0
        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);
        }