hot fix child transactiopns

This commit is contained in:
xds
2025-03-10 23:56:29 +03:00
parent ed8965b055
commit 4d5b89b08c
4 changed files with 66 additions and 61 deletions

View File

@@ -272,7 +272,7 @@ class SpaceController(
@GetMapping("/{spaceId}/recurrents") @GetMapping("/{spaceId}/recurrents")
suspend fun getRecurrents(@PathVariable spaceId: String): List<Recurrent> { suspend fun getRecurrents(@PathVariable spaceId: String): List<Recurrent> {
spaceService.isValidRequest(spaceId) spaceService.isValidRequest(spaceId)
return recurrentService.getRecurrents(spaceId).awaitSingleOrNull().orEmpty() return recurrentService.getRecurrents(spaceId)
} }

View File

@@ -57,6 +57,7 @@ class FinancialService(
val budget = findProjectedBudget( val budget = findProjectedBudget(
transaction.space!!.id!!, budgetId = null, transaction.date, transaction.date transaction.space!!.id!!, budgetId = null, transaction.date, transaction.date
) )
if (transaction.category.type.code == "EXPENSE") {
val budgetCategory = budget.categories.firstOrNull { it.category.id == transaction.category.id } val budgetCategory = budget.categories.firstOrNull { it.category.id == transaction.category.id }
?: throw NotFoundException("Budget category not found in the budget") ?: throw NotFoundException("Budget category not found in the budget")
if (transaction.category.type.code == "INCOME") { if (transaction.category.type.code == "INCOME") {
@@ -72,6 +73,7 @@ class FinancialService(
logger.info("updateBudgetOnCreate end") logger.info("updateBudgetOnCreate end")
budgetRepo.save(budget).awaitSingle() budgetRepo.save(budget).awaitSingle()
} }
}
suspend fun updateBudgetOnEdit( suspend fun updateBudgetOnEdit(
@@ -147,26 +149,22 @@ class FinancialService(
oldBudget: Budget, oldBudget: Budget,
newBudget: Budget newBudget: Budget
) = coroutineScope { ) = coroutineScope {
val oldCategory = findBudgetCategory(oldTransaction, oldBudget) // val oldCategory = findBudgetCategory(oldTransaction, oldBudget)
val newCategory = findBudgetCategory(newTransaction, newBudget) // val newCategory = findBudgetCategory(newTransaction, newBudget)
if (oldCategory.category.id == newCategory.category.id) {
async {
updateBudgetCategory(oldTransaction, oldBudget, -difference)
updateBudgetCategory(newTransaction, newBudget, difference)
}
} else {
async { async {
updateBudgetCategory( updateBudgetCategory(
oldTransaction, oldTransaction,
oldBudget, oldBudget,
-difference, -difference,
isCategoryChanged = true, isCategoryChanged = true,
isOldCategory = true isOldCategory = true,
isNewBudget = true
) )
updateBudgetCategory(newTransaction, newBudget, difference) updateBudgetCategory(newTransaction, newBudget, difference, isNewBudget = true)
}
} }
} }
private suspend fun findBudgetCategory(transaction: Transaction, budget: Budget): BudgetCategory { private suspend fun findBudgetCategory(transaction: Transaction, budget: Budget): BudgetCategory {
@@ -202,7 +200,8 @@ class FinancialService(
budget: Budget, budget: Budget,
difference: Double, difference: Double,
isCategoryChanged: Boolean = false, isCategoryChanged: Boolean = false,
isOldCategory: Boolean = false isOldCategory: Boolean = false,
isNewBudget: Boolean = false
): Double { ): Double {
return if (transaction.category.type.code == "EXPENSE") { return if (transaction.category.type.code == "EXPENSE") {
val sums = getBudgetSumsByCategory(transaction.category.id!!, budget) val sums = getBudgetSumsByCategory(transaction.category.id!!, budget)
@@ -215,6 +214,8 @@ class FinancialService(
if (isOldCategory) { if (isOldCategory) {
categoryBudget.currentLimit -= transaction.amount categoryBudget.currentLimit -= transaction.amount
} else categoryBudget.currentLimit += transaction.amount } else categoryBudget.currentLimit += transaction.amount
} else if (isNewBudget) {
categoryBudget.currentLimit += transaction.amount
} else categoryBudget.currentLimit += difference } else categoryBudget.currentLimit += difference
} }
budgetRepo.save(budget).awaitSingle() budgetRepo.save(budget).awaitSingle()

View File

@@ -36,7 +36,7 @@ class RecurrentService(
private val logger = LoggerFactory.getLogger(javaClass) private val logger = LoggerFactory.getLogger(javaClass)
fun getRecurrents(spaceId: String): Mono<List<Recurrent>> { suspend fun getRecurrents(spaceId: String): List<Recurrent> {
val lookupCategories = lookup("categories", "category.\$id", "_id", "categoryDetails") val lookupCategories = lookup("categories", "category.\$id", "_id", "categoryDetails")
val unwindCategory = unwind("categoryDetails") val unwindCategory = unwind("categoryDetails")
@@ -48,12 +48,13 @@ class RecurrentService(
val aggregation = val aggregation =
newAggregation(lookupCategories, unwindCategory, lookupSpace, unwindSpace, matchStage, sort) newAggregation(lookupCategories, unwindCategory, lookupSpace, unwindSpace, matchStage, sort)
// Запрос рекуррентных платежей // Запрос рекуррентных платежей
return reactiveMongoTemplate.aggregate(aggregation, "recurrents", Document::class.java).collectList() return reactiveMongoTemplate.aggregate(aggregation, "recurrents", Document::class.java)
.collectList()
.map { docs -> .map { docs ->
docs.map { doc -> docs.map { doc ->
recurrentMapper.fromDocument(doc) recurrentMapper.fromDocument(doc)
}.toList()
} }
}.awaitSingle()
} }
@@ -82,7 +83,8 @@ class RecurrentService(
val context = ReactiveSecurityContextHolder.getContext().awaitSingleOrNull() val context = ReactiveSecurityContextHolder.getContext().awaitSingleOrNull()
?: throw IllegalStateException("SecurityContext is empty!") ?: throw IllegalStateException("SecurityContext is empty!")
val user = userService.getByUserNameWoPass(context.authentication.name) val user = userService.getByUserNameWoPass(context.authentication.name)
val recurrents = getRecurrents(space.id!!).awaitSingle() val recurrents = getRecurrents(space.id!!)
if (recurrents.isNotEmpty()) {
val transactions = recurrents.map { recurrent -> val transactions = recurrents.map { recurrent ->
val transactionDate = when { val transactionDate = when {
recurrent.atDay in budget.dateFrom.dayOfMonth..daysInCurrentMonth -> { recurrent.atDay in budget.dateFrom.dayOfMonth..daysInCurrentMonth -> {
@@ -113,6 +115,8 @@ class RecurrentService(
transactionRepo.saveAll(transactions).awaitLast() transactionRepo.saveAll(transactions).awaitLast()
} }
}
fun editRecurrent(recurrent: Recurrent): Mono<Recurrent> { fun editRecurrent(recurrent: Recurrent): Mono<Recurrent> {
return if (recurrent.id != null && recurrent.atDay <= 31) recurrentRepo.save(recurrent) else Mono.error( return if (recurrent.id != null && recurrent.atDay <= 31) recurrentRepo.save(recurrent) else Mono.error(

View File

@@ -150,7 +150,7 @@ class SpaceService(
} }
launch { launch {
val recurrents = recurrentService.getRecurrents(objectId.toString()).awaitFirstOrNull().orEmpty() val recurrents = recurrentService.getRecurrents(objectId.toString())
recurrentRepo.deleteAll(recurrents).awaitFirstOrNull() recurrentRepo.deleteAll(recurrents).awaitFirstOrNull()
} }
} }