From 48df8f209d0c8dffec2c70d7fef749b6fd00c8bf Mon Sep 17 00:00:00 2001 From: xds Date: Wed, 12 Feb 2025 17:15:04 +0300 Subject: [PATCH] fix limit increase when not planned --- .../budgerapp/services/FinancialService.kt | 58 +++++-------------- 1 file changed, 14 insertions(+), 44 deletions(-) diff --git a/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt b/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt index e2adf4b..dd7cac0 100644 --- a/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt +++ b/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt @@ -50,46 +50,17 @@ class FinancialService( } return@flatMap getBudgetSumsByCategory(transaction.category.id!!, budget) .flatMap { sums -> + budgetCategory.currentPlanned = sums.getDouble("plannedAmount") ?: 0.0 budgetCategory.currentSpent = sums.getDouble("instantAmount") ?: 0.0 // При совпадении бюджетов разница просто корректирует лимит - budgetCategory.currentLimit += transaction.amount - logger.info("updateBudgetOnEdit end") + if (transaction.type.code == "PLANNED") { + budgetCategory.currentLimit += transaction.amount + } + logger.info("updateBudgetOnCreate end") budgetRepo.save(budget).then() } -// getBudgetCategories(budget.dateFrom, budget.dateTo).flatMap { categories -> -// val updatedCategoriesMono: Mono> = when (transaction.type.code) { -// "PLANNED" -> Flux.fromIterable(budget.categories) -// .map { category -> -// if (category.category.id == transaction.category.id) { -// categories[category.category.id]?.let { data -> -// category.currentSpent = data["instantAmount"] ?: 0.0 -// category.currentPlanned = data["plannedAmount"] ?: 0.0 -// category.currentLimit += transaction.amount -// } -// } -// category -// }.collectList() // -// "INSTANT" -> Flux.fromIterable(budget.categories) -// .map { category -> -// if (category.category.id == transaction.category.id) { -// categories[category.category.id]?.let { data -> -// category.currentSpent = data["instantAmount"] ?: 0.0 -// category.currentPlanned = data["plannedAmount"] ?: 0.0 -// } -// } -// category -// }.collectList() -// -// else -> Mono.just(budget.categories) // Добавляем обработку типа по умолчанию -// } -// -// updatedCategoriesMono.flatMap { updated -> -// budget.categories = updated.toMutableList() -// budgetRepo.save(budget).then() // Гарантируем завершение сохранения -// } -// } }.then() // Возвращаем корректный Mono } @@ -132,7 +103,9 @@ class FinancialService( budgetCategory.currentPlanned = sums.getDouble("plannedAmount") ?: 0.0 budgetCategory.currentSpent = sums.getDouble("instantAmount") ?: 0.0 // При совпадении бюджетов разница просто корректирует лимит - budgetCategory.currentLimit += difference + if (newTransaction.type.code == "PLANNED") { + budgetCategory.currentLimit += difference + } logger.info("updateBudgetOnEdit end") budgetRepo.save(newBudget).then() } @@ -151,7 +124,9 @@ class FinancialService( oldBudgetCategory.currentPlanned = sums.getDouble("plannedAmount") ?: 0.0 oldBudgetCategory.currentSpent = sums.getDouble("instantAmount") ?: 0.0 // В старом бюджете вычитаем разницу, так как транзакция перемещается - oldBudgetCategory.currentLimit -= difference + if (oldTransaction.type.code == "PLANNED") { + oldBudgetCategory.currentLimit -= difference + } budgetRepo.save(oldBudget).then() } } @@ -164,7 +139,9 @@ class FinancialService( newBudgetCategory.currentPlanned = sums.getDouble("plannedAmount") ?: 0.0 newBudgetCategory.currentSpent = sums.getDouble("instantAmount") ?: 0.0 // В новом бюджете прибавляем разницу - newBudgetCategory.currentLimit += difference + if (newTransaction.type.code == "PLANNED") { + newBudgetCategory.currentLimit += difference + } budgetRepo.save(newBudget).then() } } @@ -214,7 +191,6 @@ class FinancialService( }.then() // Возвращаем корректный Mono } - @Cacheable("budgetsList") fun getBudgets(sortSetting: SortSetting? = null): Mono> { val sort = if (sortSetting != null) { @@ -227,7 +203,6 @@ class FinancialService( .collectList() // Сбор Flux в Mono> } - // @Cacheable("budgets", key = "#id") fun getBudget(id: String): Mono { return budgetRepo.findById(id) @@ -279,7 +254,6 @@ class FinancialService( .switchIfEmpty(Mono.error(BudgetNotFoundException("Budget not found with id: $id"))) } - fun regenCats(): Mono { return budgetRepo.findAll() .flatMap { budget -> @@ -337,7 +311,6 @@ class FinancialService( } } - fun getBudgetByDate(date: LocalDate): Mono { return budgetRepo.findByDateFromLessThanEqualAndDateToGreaterThanEqual(date, date).switchIfEmpty(Mono.empty()) } @@ -361,7 +334,6 @@ class FinancialService( } } - fun getBudgetTransactionsByType(budgetId: String): Mono>> { return budgetRepo.findById(budgetId).flatMap { it -> getTransactionsByTypes(it.dateFrom, it.dateTo) @@ -387,7 +359,6 @@ class FinancialService( } } - @CacheEvict(cacheNames = ["budgets", "budgetsList"], allEntries = true) fun setCategoryLimit(budgetId: String, catId: String, limit: Double): Mono { return budgetRepo.findById(budgetId).flatMap { budget -> @@ -408,7 +379,6 @@ class FinancialService( } } - fun getWarns(budgetId: String, isHide: Boolean? = null): Mono> { return warnRepo.findAllByBudgetIdAndIsHide(budgetId, isHide == true).collectList() }