hot fix child transactiopns
This commit is contained in:
@@ -272,7 +272,7 @@ class SpaceController(
|
||||
@GetMapping("/{spaceId}/recurrents")
|
||||
suspend fun getRecurrents(@PathVariable spaceId: String): List<Recurrent> {
|
||||
spaceService.isValidRequest(spaceId)
|
||||
return recurrentService.getRecurrents(spaceId).awaitSingleOrNull().orEmpty()
|
||||
return recurrentService.getRecurrents(spaceId)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ class FinancialService(
|
||||
val budget = findProjectedBudget(
|
||||
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 }
|
||||
?: throw NotFoundException("Budget category not found in the budget")
|
||||
if (transaction.category.type.code == "INCOME") {
|
||||
@@ -72,6 +73,7 @@ class FinancialService(
|
||||
logger.info("updateBudgetOnCreate end")
|
||||
budgetRepo.save(budget).awaitSingle()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
suspend fun updateBudgetOnEdit(
|
||||
@@ -147,26 +149,22 @@ class FinancialService(
|
||||
oldBudget: Budget,
|
||||
newBudget: Budget
|
||||
) = coroutineScope {
|
||||
val oldCategory = findBudgetCategory(oldTransaction, oldBudget)
|
||||
val newCategory = findBudgetCategory(newTransaction, newBudget)
|
||||
// val oldCategory = findBudgetCategory(oldTransaction, oldBudget)
|
||||
// val newCategory = findBudgetCategory(newTransaction, newBudget)
|
||||
|
||||
|
||||
if (oldCategory.category.id == newCategory.category.id) {
|
||||
async {
|
||||
updateBudgetCategory(oldTransaction, oldBudget, -difference)
|
||||
updateBudgetCategory(newTransaction, newBudget, difference)
|
||||
}
|
||||
} else {
|
||||
async {
|
||||
updateBudgetCategory(
|
||||
oldTransaction,
|
||||
oldBudget,
|
||||
-difference,
|
||||
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 {
|
||||
@@ -202,7 +200,8 @@ class FinancialService(
|
||||
budget: Budget,
|
||||
difference: Double,
|
||||
isCategoryChanged: Boolean = false,
|
||||
isOldCategory: Boolean = false
|
||||
isOldCategory: Boolean = false,
|
||||
isNewBudget: Boolean = false
|
||||
): Double {
|
||||
return if (transaction.category.type.code == "EXPENSE") {
|
||||
val sums = getBudgetSumsByCategory(transaction.category.id!!, budget)
|
||||
@@ -215,6 +214,8 @@ class FinancialService(
|
||||
if (isOldCategory) {
|
||||
categoryBudget.currentLimit -= transaction.amount
|
||||
} else categoryBudget.currentLimit += transaction.amount
|
||||
} else if (isNewBudget) {
|
||||
categoryBudget.currentLimit += transaction.amount
|
||||
} else categoryBudget.currentLimit += difference
|
||||
}
|
||||
budgetRepo.save(budget).awaitSingle()
|
||||
|
||||
@@ -36,7 +36,7 @@ class RecurrentService(
|
||||
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 unwindCategory = unwind("categoryDetails")
|
||||
|
||||
@@ -48,12 +48,13 @@ class RecurrentService(
|
||||
val aggregation =
|
||||
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 ->
|
||||
docs.map { doc ->
|
||||
recurrentMapper.fromDocument(doc)
|
||||
}.toList()
|
||||
}
|
||||
}.awaitSingle()
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +83,8 @@ class RecurrentService(
|
||||
val context = ReactiveSecurityContextHolder.getContext().awaitSingleOrNull()
|
||||
?: throw IllegalStateException("SecurityContext is empty!")
|
||||
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 transactionDate = when {
|
||||
recurrent.atDay in budget.dateFrom.dayOfMonth..daysInCurrentMonth -> {
|
||||
@@ -113,6 +115,8 @@ class RecurrentService(
|
||||
transactionRepo.saveAll(transactions).awaitLast()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
fun editRecurrent(recurrent: Recurrent): Mono<Recurrent> {
|
||||
return if (recurrent.id != null && recurrent.atDay <= 31) recurrentRepo.save(recurrent) else Mono.error(
|
||||
|
||||
@@ -150,7 +150,7 @@ class SpaceService(
|
||||
}
|
||||
|
||||
launch {
|
||||
val recurrents = recurrentService.getRecurrents(objectId.toString()).awaitFirstOrNull().orEmpty()
|
||||
val recurrents = recurrentService.getRecurrents(objectId.toString())
|
||||
recurrentRepo.deleteAll(recurrents).awaitFirstOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user