public async Task <IEnumerable <FileStatusDTO> > ReadTransactionsFromFiles(CsvFileReaderDTO dto) { IBankEstatementReader reader = _readerResolver.GetBankEstatementReader(BankCode.FromValue <BankCode>(dto.BankCode)); var fileStatusResult = new List <FileStatusDTO>(); foreach (var csv in dto.CsvFiles) { var fileStatus = new FileStatusDTO { FileName = csv.FileName }; try { Validate.IsTrue(csv.ContentType == "text/csv", "Only csv files are accepted"); Validate.IsTrue(csv.Length > 0, $"File {csv.FileName} is empty"); var parsedFile = reader.ProcessBankInformation(csv); fileStatus.TransactionsRead = parsedFile.Transactions.Count; var bankAccount = await GetFilesOwnerBankAccount(dto.FilesOwnerId, parsedFile.AgencyNumber, parsedFile.AccountNumber, dto.BankCode); RemoveExistentTransactions(parsedFile); if (parsedFile.Transactions.Count > 0) { await PersistBankEstatementInformation(parsedFile, bankAccount); } fileStatus.IsSuccessfullRead = true; fileStatus.TransactionsSaved = parsedFile.Transactions.Count; // TODO: Get initial and final date from file fileStatus.InitialDate = parsedFile.Transactions.First().Date; fileStatus.FinalDate = parsedFile.Transactions.Last().Date; } catch (Exception ex) { // TODO: create exception for file and reading cases, and another for database cases _logger.LogWarning("Message: {0}", ex.Message); fileStatus.IsSuccessfullRead = false; } finally { fileStatusResult.Add(fileStatus); } } return(fileStatusResult); }
public async Task <IEnumerable <BankAccountsResponseDTO> > GetUserBankAccounts(GetUserBankAccountsDTO dto) { var bankAccounts = await _bankAccountRepository.GetAllByUser(dto.UserId); Validate.IsTrue(bankAccounts.Count() > 0, "No bank accounts were found"); var bankAccountsDto = bankAccounts.Select(bk => new BankAccountsResponseDTO { Id = bk.Id, Agency = bk.Agency, AccountNumber = bk.Number, BankCode = bk.Code, BankName = BankCode.FromValue <BankCode>(bk.Code).Name, UserId = (int)bk.UserId }); return(bankAccountsDto); }
public async Task <BankAccountResponseDTO> GetBankAccountById(GetUserBankAccountDTO dto) { var bankAccount = await _bankAccountRepository.GetById(dto.BankAccountId); Validate.NotNull(bankAccount, "Bank account not found"); Validate.IsTrue(bankAccount.UserId == dto.UserId, "Acesso negado"); var response = new BankAccountResponseDTO() { Id = bankAccount.Id, Agency = bankAccount.Agency, AccountNumber = bankAccount.Number, BankCode = bankAccount.Code, BankName = BankCode.FromValue <BankCode>(bankAccount.Code).Name }; return(response); }
public async Task CreateBankAccountForUser(CreateUserBankAccountDTO dto) { var user = await _userRepository.GetById(dto.UserId); Validate.NotNull(user, "É necessário um usuário para criar uma conta de banco."); Validate.NotNullOrEmpty(dto.Agency, "É necessário fornecer uma agência."); Validate.NotNullOrEmpty(dto.Number, "É necessário fornecer o número da conta bancária."); BankCode.ValidateCode(dto.Code); var code = BankCode.FromValue <BankCode>(dto.Code); var bankAccount = new BankAccount(dto.Number, dto.Agency, code, dto.UserId); _bankAccountRepository.Insert(bankAccount); Validate.IsTrue(await _uow.CommitAsync(), "Não foi possível criar a conta bancária"); }