Ejemplo n.º 1
0
        /// <summary>
        /// Process an import and converts it to a list of import rows
        /// </summary>
        /// <param name="genericRepository">Generic repository to use</param>
        /// <param name="fileInputStream">File input stream </param>
        /// <param name="userId">Id of the current user</param>
        /// <param name="importMappings">Import column mappings</param>
        /// <param name="importBank">Import bank entity</param>
        /// <param name="bankAccountId">Bank account id to use for processing</param>
        /// <returns>Created list of import rows</returns>
        public static async Task <IList <ImportRow> > CreateImportRowsFromFile(IUnitOfWork unitOfWork, Stream fileInputStream, int userId,
                                                                               IList <ImportMappingEntity> importMappings, ImportBankEntity importBank, int bankAccountId)
        {
            IList <ImportRow> importRows;

            using (var reader = new StreamReader(fileInputStream))
            {
                // Convert the raw csv file to an import rows list
                importRows = ConvertFileToImportRows(reader.BaseStream, importBank, importMappings);
            }

            if (importRows != null && importRows.Any())
            {
                var firstDate = importRows.Min(item => item.Transaction.Date);
                var lastDate  = importRows.Max(item => item.Transaction.Date);

                var existingTransactions = await unitOfWork.TransactionRepository
                                           .FindAll(transaction => transaction.UserId == userId && transaction.Date >= firstDate && transaction.Date <= lastDate);

                foreach (var importRow in importRows)
                {
                    var transactionExists = existingTransactions.Any(transaction => importRow.Transaction.Amount == transaction.Amount &&
                                                                     importRow.Transaction.Name.Equals(transaction.Name.Trim(),
                                                                                                       StringComparison.CurrentCultureIgnoreCase) &&
                                                                     importRow.Transaction.Date == transaction.Date &&
                                                                     transaction.BankAccountId == bankAccountId);

                    importRow.ExistsInDatabase = transactionExists;
                    importRow.Import           = !transactionExists;
                }

                var categoryMappings = await unitOfWork.CategoryMappingRepository.FindAll(item => item.Category.UserId == userId, "Category");

                CategoryHandler.SetTransactionCategories(transactions: importRows.Select(item => item.Transaction), categoryMappings: categoryMappings);
            }

            return(importRows);
        }
Ejemplo n.º 2
0
        private static async Task MapCategoriesToRows(IUnitOfWork unitOfWork, int userId, IList <ImportRow> rowsToImport)
        {
            var categoryMappings = await unitOfWork.CategoryMappingRepository.FindAll(item => item.Category.UserId == userId, "Category");

            CategoryHandler.SetTransactionCategories(transactions: rowsToImport.Select(item => item.Transaction), categoryMappings: categoryMappings);
        }