fix recurrents
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
package space.luminic.budgerapp.services
|
||||
|
||||
import org.bson.Document
|
||||
import org.bson.types.ObjectId
|
||||
import org.springframework.data.mongodb.core.ReactiveMongoTemplate
|
||||
import org.springframework.data.mongodb.core.aggregation.Aggregation.*
|
||||
import org.springframework.data.mongodb.core.query.Criteria
|
||||
import org.springframework.data.mongodb.core.query.Query
|
||||
import org.springframework.security.core.context.ReactiveSecurityContextHolder
|
||||
import org.springframework.stereotype.Service
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
import space.luminic.budgerapp.models.Category
|
||||
import space.luminic.budgerapp.models.Space
|
||||
import space.luminic.budgerapp.models.SpaceInvite
|
||||
import space.luminic.budgerapp.models.Tag
|
||||
import space.luminic.budgerapp.repos.*
|
||||
import java.time.LocalDateTime
|
||||
import java.util.UUID
|
||||
@@ -22,7 +27,11 @@ class SpaceService(
|
||||
private val reactiveMongoTemplate: ReactiveMongoTemplate,
|
||||
private val categoryRepo: CategoryRepo,
|
||||
private val recurrentRepo: RecurrentRepo,
|
||||
private val transactionRepo: TransactionRepo
|
||||
private val transactionRepo: TransactionRepo,
|
||||
private val financialService: FinancialService,
|
||||
private val categoryService: CategoryService,
|
||||
private val recurrentService: RecurrentService,
|
||||
private val tagRepo: TagRepo
|
||||
) {
|
||||
|
||||
fun isValidRequest(spaceId: String): Mono<Space> {
|
||||
@@ -101,27 +110,29 @@ class SpaceService(
|
||||
val objectId = ObjectId(space.id)
|
||||
|
||||
return Mono.`when`(
|
||||
budgetRepo.findBySpaceId(objectId)
|
||||
.flatMap { budgetRepo.delete(it) }
|
||||
financialService.findProjectedBudgets(objectId)
|
||||
.flatMapMany { Flux.fromIterable(it) }
|
||||
.flatMap { budgetRepo.deleteById(it.id!!) }
|
||||
.then(),
|
||||
|
||||
transactionRepo.findBySpaceId(objectId)
|
||||
.flatMap { transactionRepo.delete(it) }
|
||||
.then(),
|
||||
financialService.getTransactions(objectId.toString())
|
||||
.flatMapMany { Flux.fromIterable(it) }
|
||||
.flatMap { transactionRepo.deleteById(it.id!!) }
|
||||
.then(),
|
||||
|
||||
categoryRepo.findBySpaceId(objectId)
|
||||
.flatMap { categoryRepo.delete(it) }
|
||||
.then(),
|
||||
categoryService.getCategories(objectId.toString(), null, "name", "ASC")
|
||||
.flatMapMany { Flux.fromIterable(it) }
|
||||
.flatMap { categoryRepo.deleteById(it.id!!) }
|
||||
.then(),
|
||||
|
||||
recurrentRepo.findRecurrentsBySpaceId(objectId)
|
||||
.flatMap { recurrentRepo.delete(it) }
|
||||
.then()
|
||||
recurrentService.getRecurrents(objectId.toString())
|
||||
.flatMapMany { Flux.fromIterable(it) }
|
||||
.flatMap { recurrentRepo.deleteById(it.id!!) }
|
||||
.then()
|
||||
).then(spaceRepo.deleteById(space.id!!)) // Исправлено: удаление по ID
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
fun createInviteSpace(spaceId: String): Mono<SpaceInvite> {
|
||||
return ReactiveSecurityContextHolder.getContext()
|
||||
.map { it.authentication }
|
||||
@@ -247,6 +258,106 @@ class SpaceService(
|
||||
}
|
||||
}
|
||||
|
||||
fun findTag(space: Space, tagCode: String): Mono<Tag> {
|
||||
val lookupSpaces = lookup("spaces", "space.\$id", "_id", "spaceDetails")
|
||||
val unwindSpace = unwind("spaceDetails")
|
||||
val matchCriteria = mutableListOf<Criteria>()
|
||||
// Добавляем фильтры
|
||||
matchCriteria.add(Criteria.where("spaceDetails._id").`is`(ObjectId(space.id)))
|
||||
matchCriteria.add(Criteria.where("code").`is`(tagCode))
|
||||
val match = match(Criteria().andOperator(*matchCriteria.toTypedArray()))
|
||||
val aggregationBuilder = mutableListOf(
|
||||
lookupSpaces,
|
||||
unwindSpace,
|
||||
match.takeIf { matchCriteria.isNotEmpty() },
|
||||
).filterNotNull()
|
||||
|
||||
val aggregation = newAggregation(aggregationBuilder)
|
||||
return reactiveMongoTemplate.aggregate(
|
||||
aggregation, "tags", Document::class.java
|
||||
).next()
|
||||
.map { doc ->
|
||||
Tag(
|
||||
id = doc.getObjectId("_id").toString(),
|
||||
space = Space(id = doc.get("spaceDetails", Document::class.java).getObjectId("_id").toString()),
|
||||
code = doc.getString("code"),
|
||||
name = doc.getString("name")
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun createTag(space: Space, tag: Tag): Mono<Tag> {
|
||||
tag.space = space
|
||||
return findTag(space, tag.code)
|
||||
.flatMap { existingTag ->
|
||||
Mono.error<Tag>(IllegalArgumentException("Tag with code ${existingTag.code} already exists"))
|
||||
}
|
||||
.switchIfEmpty(tagRepo.save(tag))
|
||||
}
|
||||
|
||||
fun deleteTag(space: Space, tagCode: String): Mono<Void> {
|
||||
return findTag(space, tagCode)
|
||||
.switchIfEmpty(Mono.error(IllegalArgumentException("Tag with code $tagCode not found")))
|
||||
.flatMap { tag ->
|
||||
categoryService.getCategories(space.id!!, sortBy = "name", direction = "ASC", tagCode = tag.code)
|
||||
.flatMapMany { cats ->
|
||||
Flux.fromIterable(cats)
|
||||
.map { cat ->
|
||||
cat.tags.removeIf { it.code == tagCode } // Изменяем список тегов
|
||||
cat
|
||||
}
|
||||
.flatMap { categoryRepo.save(it) } // Сохраняем обновлённые категории
|
||||
}
|
||||
.then(tagRepo.deleteById(tag.id!!)) // Удаляем тег только после обновления категорий
|
||||
}
|
||||
}
|
||||
|
||||
fun getTags(space: Space): Mono<List<Tag>> {
|
||||
val lookupSpaces = lookup("spaces", "space.\$id", "_id", "spaceDetails")
|
||||
val unwindSpace = unwind("spaceDetails")
|
||||
val matchCriteria = mutableListOf<Criteria>()
|
||||
// Добавляем фильтры
|
||||
matchCriteria.add(Criteria.where("spaceDetails._id").`is`(ObjectId(space.id)))
|
||||
val match = match(Criteria().andOperator(*matchCriteria.toTypedArray()))
|
||||
val aggregationBuilder = mutableListOf(
|
||||
|
||||
lookupSpaces,
|
||||
unwindSpace,
|
||||
match.takeIf { matchCriteria.isNotEmpty() },
|
||||
).filterNotNull()
|
||||
|
||||
val aggregation = newAggregation(aggregationBuilder)
|
||||
return reactiveMongoTemplate.aggregate(
|
||||
aggregation, "tags", Document::class.java
|
||||
)
|
||||
.collectList() // Преобразуем Flux<Transaction> в Mono<List<Transaction>>
|
||||
.map { docs ->
|
||||
docs.map { doc ->
|
||||
Tag(
|
||||
id = doc.getObjectId("_id").toString(),
|
||||
space = Space(id = doc.get("spaceDetails", Document::class.java).getObjectId("_id").toString()),
|
||||
code = doc.getString("code"),
|
||||
name = doc.getString("name")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun regenSpaceCategory(): Mono<Category> {
|
||||
return getSpace("67af3c0f652da946a7dd9931")
|
||||
.flatMap { space ->
|
||||
categoryService.findCategory(id= "677bc767c7857460a491bd4f")
|
||||
.flatMap { category -> // заменил map на flatMap
|
||||
category.space = space
|
||||
category.name = "Сбережения"
|
||||
category.description = "Отчисления в накопления или инвестиционные счета"
|
||||
category.icon = "💰"
|
||||
categoryRepo.save(category) // теперь возвращаем Mono<Category>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fun regenSpaces(): Mono<List<Space>> {
|
||||
// return spaceRepo.findAll()
|
||||
// .flatMap { space ->
|
||||
|
||||
Reference in New Issue
Block a user