public async Task <(int skippedTransactions, int savedTransactions)> SaveImport(ImportModel model) { var userId = _userIdProvider.GetCurrentUserId(); var importCacheKey = CreateImportCacheKey(userId); var cachedModel = SinanceCacheHandler.RetrieveCache <ImportModel>(importCacheKey); if (cachedModel == null) { throw new NotFoundException(nameof(ImportModel)); } using var unitOfWork = _unitOfWork(); var bankAccount = await VerifyBankAccount(model, unitOfWork); var skippedTransactions = model.ImportRows.Count(item => item.ExistsInDatabase || !item.Import); var savedTransactions = await _bankFileImportHandler.SaveImportResultToDatabase(unitOfWork : unitOfWork, bankAccountId : bankAccount.Id, userId : userId, importRows : model.ImportRows, cachedImportRows : cachedModel.ImportRows); // Update the current balance of bank account and refresh them await _bankAccountCalculationService.UpdateCurrentBalanceForBankAccount(unitOfWork, model.BankAccountId); await unitOfWork.SaveAsync(); // Clear the cache entry SinanceCacheHandler.ClearCache(importCacheKey); return(skippedTransactions, savedTransactions); }
public async Task <(int skippedTransactions, int savedTransactions)> SaveImport(ImportModel model) { var userId = await _authenticationService.GetCurrentUserId(); var importCacheKey = CreateImportCacheKey(userId); var cachedModel = SinanceCacheHandler.RetrieveCache <ImportModel>(importCacheKey); if (cachedModel == null) { throw new NotFoundException(nameof(ImportModel)); } using var unitOfWork = _unitOfWork(); var bankAccount = await unitOfWork.BankAccountRepository.FindSingleTracked(x => x.Id == model.BankAccountId && x.UserId == userId); if (bankAccount == null) { throw new NotFoundException(nameof(BankAccountEntity)); } var skippedTransactions = model.ImportRows.Count(item => item.ExistsInDatabase || !item.Import); var savedTransactions = await ImportHandler.SaveImportResultToDatabase(unitOfWork, bankAccountId : bankAccount.Id, importRows : model.ImportRows, cachedImportRows : cachedModel.ImportRows, userId : bankAccount.UserId); // Update the current balance of bank account and refresh them bankAccount.CurrentBalance = await TransactionHandler.CalculateCurrentBalanceForBankAccount(unitOfWork, bankAccount); await unitOfWork.SaveAsync(); // Clear the cache entry SinanceCacheHandler.ClearCache(importCacheKey); return(skippedTransactions, savedTransactions); }