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