wishlists + statics + some fixes

This commit is contained in:
xds
2025-03-03 10:38:07 +03:00
parent db0ada5ee8
commit 3b9f0e566c
16 changed files with 566 additions and 14 deletions

View File

@@ -23,7 +23,7 @@ import org.springframework.data.mongodb.core.ReactiveMongoTemplate
import org.springframework.data.mongodb.core.aggregation.Aggregation.*
import org.springframework.data.mongodb.core.aggregation.DateOperators.DateToString
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.context.ReactiveSecurityContextHolder
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
@@ -169,16 +169,34 @@ class FinancialService(
}
}
private fun findBudgetCategory(transaction: Transaction, budget: Budget): BudgetCategory {
private suspend fun findBudgetCategory(transaction: Transaction, budget: Budget): BudgetCategory {
return if (transaction.category.type.code == "EXPENSE") {
budget.categories.firstOrNull { it.category.id == transaction.category.id }
?: throw RuntimeException("Budget category not found for expense")
?: addCategoryToBudget(transaction.category, budget)
} else {
budget.incomeCategories.firstOrNull { it.category.id == transaction.category.id }
?: throw RuntimeException("Budget category not found for income")
?:addCategoryToBudget(transaction.category, budget)
}
}
private suspend fun addCategoryToBudget(category: Category, budget: Budget): BudgetCategory {
val sums = getBudgetSumsByCategory(category.id!!, budget)
val categoryBudget = BudgetCategory(
currentSpent = sums.getDouble("instantAmount"),
currentPlanned = sums.getDouble("plannedAmount"),
currentLimit = sums.getDouble("plannedAmount"),
category = category
)
if (category.type.code == "EXPENSE") {
budget.categories.add(categoryBudget)
} else {
budget.incomeCategories.add(categoryBudget)
}
budgetRepo.save(budget).awaitSingle()
return categoryBudget
}
private suspend fun updateBudgetCategory(
transaction: Transaction,
budget: Budget,
@@ -826,7 +844,7 @@ class FinancialService(
suspend fun createTransaction(space: Space, transaction: Transaction): Transaction {
val securityContextHolder = SecurityContextHolder.getContext()
val securityContextHolder = ReactiveSecurityContextHolder.getContext().awaitSingle()
val user = userService.getByUserNameWoPass(securityContextHolder.authentication.name)
if (space.users.none { it.id.toString() == user.id }) {
throw IllegalArgumentException("User does not have access to this Space")
@@ -903,7 +921,7 @@ class FinancialService(
suspend fun deleteTransaction(transaction: Transaction) = coroutineScope {
transactionsRepo.deleteById(transaction.id!!).awaitSingle()
transactionsRepo.deleteById(transaction.id!!).awaitFirstOrNull()
launch { updateBudgetOnDelete(transaction) }
}