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 <IActionResult> ReadBankStatements(IFormFileCollection csvFiles, [FromForm] short bankCode) { BankCode.ValidateCode(bankCode); // TODO : better way to do this? var user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier); if (string.IsNullOrEmpty(user?.Value)) { return(Unauthorized()); } var dto = new CsvFileReaderDTO() { FilesOwnerId = int.Parse(user.Value), BankCode = bankCode, CsvFiles = csvFiles }; var result = await _csvReaderService.ReadTransactionsFromFiles(dto); return(Ok(result)); }