From ed8965b0550042b674ce3b10d09133d8510c5ec6 Mon Sep 17 00:00:00 2001 From: xds Date: Mon, 10 Mar 2025 13:24:32 +0300 Subject: [PATCH] hot fix child transactiopns --- .../budgerapp/controllers/SpaceController.kt | 4 ++ .../budgerapp/services/FinancialService.kt | 38 +++++++++---------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt b/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt index 89fbcf2..1669407 100644 --- a/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt +++ b/src/main/kotlin/space/luminic/budgerapp/controllers/SpaceController.kt @@ -3,6 +3,7 @@ package space.luminic.budgerapp.controllers import kotlinx.coroutines.reactor.awaitSingle import kotlinx.coroutines.reactor.awaitSingleOrNull import org.bson.Document +import org.slf4j.LoggerFactory import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.* @@ -24,6 +25,8 @@ class SpaceController( private val recurrentService: RecurrentService ) { + private val log = LoggerFactory.getLogger(SpaceController::class.java) + data class SpaceCreateDTO( val name: String, @@ -94,6 +97,7 @@ class SpaceController( @GetMapping("/{spaceId}/budgets/{id}") suspend fun getBudget(@PathVariable spaceId: String, @PathVariable id: String): BudgetDTO? { + log.info("Getting budget for spaceId=$spaceId, id=$id") spaceService.isValidRequest(spaceId) return financialService.getBudget(spaceId, id) } diff --git a/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt b/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt index b060b85..3b3eced 100644 --- a/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt +++ b/src/main/kotlin/space/luminic/budgerapp/services/FinancialService.kt @@ -176,7 +176,7 @@ class FinancialService( } else { budget.incomeCategories.firstOrNull { it.category.id == transaction.category.id } - ?:addCategoryToBudget(transaction.category, budget) + ?: addCategoryToBudget(transaction.category, budget) } } @@ -204,7 +204,7 @@ class FinancialService( isCategoryChanged: Boolean = false, isOldCategory: Boolean = false ): Double { - return if (transaction.category.type.code == "EXPENSE") { + return if (transaction.category.type.code == "EXPENSE") { val sums = getBudgetSumsByCategory(transaction.category.id!!, budget) val categoryBudget = budget.categories.firstOrNull { it.category.id == transaction.category.id } ?: throw NotFoundException("Not found category in budget") @@ -218,7 +218,7 @@ class FinancialService( } else categoryBudget.currentLimit += difference } budgetRepo.save(budget).awaitSingle() - 1.0 + 1.0 } else 0.0 } @@ -369,6 +369,7 @@ class FinancialService( budgetDTO.plannedExpenses = transactions.get("plannedExpenses") as MutableList budgetDTO.plannedIncomes = transactions["plannedIncomes"] as MutableList budgetDTO.transactions = transactions["instantTransactions"] as MutableList + logger.info("got budget for spaceId=$spaceId, id=$id") budgetDTO } @@ -755,7 +756,7 @@ class FinancialService( } - suspend fun getTransactionByParentId(parentId: String): Transaction { + suspend fun getTransactionByParentId(parentId: String): Transaction? { // Сборка агрегации val lookup = lookup("categories", "category.\$id", "_id", "categoryDetails") val unwindCategory = unwind("categoryDetails") @@ -782,7 +783,7 @@ class FinancialService( ).map { doc -> transactionsMapper.fromDocument(doc) - }.awaitFirstOrNull() ?: throw NotFoundException("Child transaction with parent id $parentId not found") + }.awaitFirstOrNull() } @@ -870,7 +871,7 @@ class FinancialService( } val amountDifference = transaction.amount - oldStateOfTransaction.amount - if (oldStateOfTransaction.isDone && oldStateOfTransaction.type.code == "PLANNED") { + if (oldStateOfTransaction.type.code == "PLANNED") { handleChildTransaction( oldStateOfTransaction, transaction @@ -883,26 +884,25 @@ class FinancialService( private suspend fun handleChildTransaction(oldTransaction: Transaction, newTransaction: Transaction) { - val childTransaction = getTransactionByParentId(newTransaction.id!!) - - logger.info("Updating child: $childTransaction") - val updatedChild = childTransaction.copy( - amount = newTransaction.amount, - category = newTransaction.category, - comment = newTransaction.comment, - user = newTransaction.user - ) - transactionsRepo.save(updatedChild).awaitSingle() - if (!oldTransaction.isDone && newTransaction.isDone) { val newChildTransaction = newTransaction.copy( id = null, type = TransactionType("INSTANT", "Текущие"), parentId = newTransaction.id ) transactionsRepo.save(newChildTransaction).awaitSingle() updateBudgetOnCreate(newChildTransaction) - } else if (oldTransaction.isDone && !newTransaction.isDone) { - deleteTransaction(childTransaction) + val childTransaction = getTransactionByParentId(newTransaction.id!!) + childTransaction?.let { deleteTransaction(it) } + } else { + val childTransaction = getTransactionByParentId(newTransaction.id!!) + childTransaction?.let { + logger.info("Updating child: $it") + it.amount = newTransaction.amount + it.category = newTransaction.category + it.comment = newTransaction.comment + it.user = newTransaction.user + transactionsRepo.save(it).awaitSingle() + } } }