hot fix child transactiopns

This commit is contained in:
xds
2025-03-10 13:24:32 +03:00
parent 94df5d72c3
commit ed8965b055
2 changed files with 23 additions and 19 deletions

View File

@@ -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)
}

View File

@@ -176,7 +176,7 @@ class FinancialService(
} else {
budget.incomeCategories.firstOrNull { it.category.id == transaction.category.id }
?:addCategoryToBudget(transaction.category, budget)
?: addCategoryToBudget(transaction.category, budget)
}
}
@@ -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()
}
}
}