hot fix child transactiopns
This commit is contained in:
@@ -3,6 +3,7 @@ package space.luminic.budgerapp.controllers
|
|||||||
import kotlinx.coroutines.reactor.awaitSingle
|
import kotlinx.coroutines.reactor.awaitSingle
|
||||||
import kotlinx.coroutines.reactor.awaitSingleOrNull
|
import kotlinx.coroutines.reactor.awaitSingleOrNull
|
||||||
import org.bson.Document
|
import org.bson.Document
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.http.HttpStatus
|
import org.springframework.http.HttpStatus
|
||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
import org.springframework.web.bind.annotation.*
|
import org.springframework.web.bind.annotation.*
|
||||||
@@ -24,6 +25,8 @@ class SpaceController(
|
|||||||
private val recurrentService: RecurrentService
|
private val recurrentService: RecurrentService
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
private val log = LoggerFactory.getLogger(SpaceController::class.java)
|
||||||
|
|
||||||
|
|
||||||
data class SpaceCreateDTO(
|
data class SpaceCreateDTO(
|
||||||
val name: String,
|
val name: String,
|
||||||
@@ -94,6 +97,7 @@ class SpaceController(
|
|||||||
|
|
||||||
@GetMapping("/{spaceId}/budgets/{id}")
|
@GetMapping("/{spaceId}/budgets/{id}")
|
||||||
suspend fun getBudget(@PathVariable spaceId: String, @PathVariable id: String): BudgetDTO? {
|
suspend fun getBudget(@PathVariable spaceId: String, @PathVariable id: String): BudgetDTO? {
|
||||||
|
log.info("Getting budget for spaceId=$spaceId, id=$id")
|
||||||
spaceService.isValidRequest(spaceId)
|
spaceService.isValidRequest(spaceId)
|
||||||
return financialService.getBudget(spaceId, id)
|
return financialService.getBudget(spaceId, id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -176,7 +176,7 @@ class FinancialService(
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
budget.incomeCategories.firstOrNull { it.category.id == transaction.category.id }
|
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.plannedExpenses = transactions.get("plannedExpenses") as MutableList
|
||||||
budgetDTO.plannedIncomes = transactions["plannedIncomes"] as MutableList
|
budgetDTO.plannedIncomes = transactions["plannedIncomes"] as MutableList
|
||||||
budgetDTO.transactions = transactions["instantTransactions"] as MutableList
|
budgetDTO.transactions = transactions["instantTransactions"] as MutableList
|
||||||
|
logger.info("got budget for spaceId=$spaceId, id=$id")
|
||||||
|
|
||||||
budgetDTO
|
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 lookup = lookup("categories", "category.\$id", "_id", "categoryDetails")
|
||||||
val unwindCategory = unwind("categoryDetails")
|
val unwindCategory = unwind("categoryDetails")
|
||||||
@@ -782,7 +783,7 @@ class FinancialService(
|
|||||||
).map { doc ->
|
).map { doc ->
|
||||||
transactionsMapper.fromDocument(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
|
val amountDifference = transaction.amount - oldStateOfTransaction.amount
|
||||||
|
|
||||||
if (oldStateOfTransaction.isDone && oldStateOfTransaction.type.code == "PLANNED") {
|
if (oldStateOfTransaction.type.code == "PLANNED") {
|
||||||
handleChildTransaction(
|
handleChildTransaction(
|
||||||
oldStateOfTransaction,
|
oldStateOfTransaction,
|
||||||
transaction
|
transaction
|
||||||
@@ -883,26 +884,25 @@ class FinancialService(
|
|||||||
|
|
||||||
|
|
||||||
private suspend fun handleChildTransaction(oldTransaction: Transaction, newTransaction: Transaction) {
|
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) {
|
if (!oldTransaction.isDone && newTransaction.isDone) {
|
||||||
val newChildTransaction = newTransaction.copy(
|
val newChildTransaction = newTransaction.copy(
|
||||||
id = null, type = TransactionType("INSTANT", "Текущие"), parentId = newTransaction.id
|
id = null, type = TransactionType("INSTANT", "Текущие"), parentId = newTransaction.id
|
||||||
)
|
)
|
||||||
transactionsRepo.save(newChildTransaction).awaitSingle()
|
transactionsRepo.save(newChildTransaction).awaitSingle()
|
||||||
updateBudgetOnCreate(newChildTransaction)
|
updateBudgetOnCreate(newChildTransaction)
|
||||||
|
|
||||||
} else if (oldTransaction.isDone && !newTransaction.isDone) {
|
} 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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user