Beispiel #1
0
        public async Task SeedData(bool overwrite)
        {
            using var unitOfWork = _unitOfWork();

            const string demoUserName = "******";
            var          user         = await unitOfWork.UserRepository.FindSingleTracked(x => x.Username == demoUserName);

            if (user != null)
            {
                if (overwrite)
                {
                    _logger.Information("DemoUser already exists, will delete all existing data for demo user");
                }
                else
                {
                    _logger.Information("DemoUser already exists, overwrite is disabled. Not seeding new data");
                    return;
                }
            }
            else
            {
                _logger.Information("Creating demo user");
                await _authenticationService.CreateUser(demoUserName, demoUserName);

                user = await unitOfWork.UserRepository.FindSingleTracked(x => x.Username == demoUserName);
            }

            unitOfWork.Context.OverwriteUserIdProvider(new SeedUserIdProvider(user.Id));

            await DeleteExistingBankAccounts(unitOfWork);

            await _categorySeed.Value.SeedStandardCategoriesForUser(unitOfWork, user.Id);

            await unitOfWork.SaveAsync();

            _logger.Information("Creating demo bank accounts");
            var mainBankAccount      = InsertBankAccount(unitOfWork, user, "Checking 1", BankAccountType.Checking);
            var secondaryBankAccount = InsertBankAccount(unitOfWork, user, "Checking 2", BankAccountType.Checking);
            var savingsAccount       = InsertBankAccount(unitOfWork, user, "Savings", BankAccountType.Savings);
            var investmentAccount    = InsertBankAccount(unitOfWork, user, "Investments", BankAccountType.Investment);

            await DeleteCategoriesAndTransactions(unitOfWork);
            await InsertCategoriesAndTransactions(unitOfWork, user, mainBankAccount, savingsAccount);

            await _bankAccountCalculationService.UpdateCurrentBalanceForBankAccount(unitOfWork, mainBankAccount.Id);

            await _bankAccountCalculationService.UpdateCurrentBalanceForBankAccount(unitOfWork, secondaryBankAccount.Id);

            await _bankAccountCalculationService.UpdateCurrentBalanceForBankAccount(unitOfWork, savingsAccount.Id);

            await _bankAccountCalculationService.UpdateCurrentBalanceForBankAccount(unitOfWork, investmentAccount.Id);

            await unitOfWork.SaveAsync();

            _logger.Information("Data seed completed, login with DemoUser/DemoUser");
        }
Beispiel #2
0
        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);
        }