fix category delete

This commit is contained in:
xds
2025-03-18 12:54:37 +03:00
parent 4d5b89b08c
commit 711348b386

View File

@@ -3,6 +3,7 @@ package space.luminic.budgerapp.services
import kotlinx.coroutines.reactive.awaitFirstOrNull import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactive.awaitSingle import kotlinx.coroutines.reactive.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
import org.bson.Document import org.bson.Document
import org.bson.types.ObjectId import org.bson.types.ObjectId
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@@ -135,22 +136,26 @@ class CategoryService(
suspend fun deleteCategory(space: Space, categoryId: String) { suspend fun deleteCategory(space: Space, categoryId: String) {
findCategory(space, categoryId) findCategory(space, categoryId)
val transactions = financialService.getTransactions(space.id!!, categoryId = categoryId).awaitSingle() val transactions = financialService.getTransactions(space.id!!, categoryId = categoryId).awaitSingle()
val otherCategory = try { if (transactions.isNotEmpty()) {
findCategory(space, name = "Другое") val otherCategory = try {
} catch (nfe: NotFoundException) { findCategory(space, name = "Другое")
categoryRepo.save( } catch (nfe: NotFoundException) {
Category( categoryRepo.save(
space = space, Category(
type = CategoryType("EXPENSE", "Траты"), space = space,
name = "Другое", type = CategoryType("EXPENSE", "Траты"),
description = "Категория для других трат", name = "Другое",
icon = "🚮" description = "Категория для других трат",
) icon = "🚮"
).awaitSingle() )
} ).awaitSingle()
transactions.map { transaction -> }
transaction.category = otherCategory
financialService.editTransaction(transaction)
transactions.map { transaction ->
transaction.category = otherCategory
financialService.editTransaction(transaction)
}
} }
val budgets = financialService.findProjectedBudgets( val budgets = financialService.findProjectedBudgets(
ObjectId(space.id), ObjectId(space.id),
@@ -171,7 +176,7 @@ class CategoryService(
budget.categories.removeIf { it.category.id == categoryId } budget.categories.removeIf { it.category.id == categoryId }
budgetRepo.save(budget) budgetRepo.save(budget)
} }
categoryRepo.deleteById(categoryId).awaitSingle() categoryRepo.deleteById(categoryId).awaitSingleOrNull()
} }
} }