+ notifications
This commit is contained in:
@@ -1,18 +1,29 @@
|
||||
package space.luminic.finance.services.telegram
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.launch
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Qualifier
|
||||
import org.springframework.stereotype.Service
|
||||
import space.luminic.finance.dtos.TransactionDTO
|
||||
import space.luminic.finance.models.Transaction
|
||||
import space.luminic.finance.repos.TransactionRepo
|
||||
import space.luminic.finance.services.CategoryServiceImpl
|
||||
import space.luminic.finance.services.NotificationService
|
||||
import space.luminic.finance.services.TxActionType
|
||||
|
||||
@Service("transactionsServiceTelegram")
|
||||
class TransactionsServiceImpl(
|
||||
private val transactionRepo: TransactionRepo,
|
||||
@Qualifier("spaceServiceTelegram") private val spaceService: SpaceService,
|
||||
private val categoryService: CategoryServiceImpl
|
||||
): TransactionService {
|
||||
private val categoryService: CategoryServiceImpl,
|
||||
private val notificationService: NotificationService
|
||||
) : TransactionService {
|
||||
private val logger = LoggerFactory.getLogger(this.javaClass)
|
||||
private val serviceScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
|
||||
|
||||
|
||||
override fun createTransaction(
|
||||
spaceId: Int,
|
||||
@@ -21,23 +32,31 @@ class TransactionsServiceImpl(
|
||||
chatId: Long,
|
||||
messageId: Long
|
||||
): Int {
|
||||
val space = spaceService.getSpace(spaceId, userId)
|
||||
val category = transaction.categoryId?.let { categoryService.getCategory(spaceId, it) }
|
||||
val transaction = Transaction(
|
||||
space = space,
|
||||
type = transaction.type,
|
||||
kind = transaction.kind,
|
||||
category = category,
|
||||
comment = transaction.comment,
|
||||
amount = transaction.amount,
|
||||
fees = transaction.fees,
|
||||
date = transaction.date,
|
||||
tgChatId = chatId,
|
||||
tgMessageId = messageId,
|
||||
)
|
||||
return transactionRepo.create(transaction, userId)
|
||||
val space = spaceService.getSpace(spaceId, userId)
|
||||
val category = transaction.categoryId?.let { categoryService.getCategory(spaceId, it) }
|
||||
val transaction = Transaction(
|
||||
space = space,
|
||||
type = transaction.type,
|
||||
kind = transaction.kind,
|
||||
category = category,
|
||||
comment = transaction.comment,
|
||||
amount = transaction.amount,
|
||||
fees = transaction.fees,
|
||||
date = transaction.date,
|
||||
tgChatId = chatId,
|
||||
tgMessageId = messageId,
|
||||
)
|
||||
serviceScope.launch {
|
||||
runCatching {
|
||||
if (space.owner.id != userId) {
|
||||
notificationService.sendTXNotification(TxActionType.CREATE, space, userId, transaction)
|
||||
}
|
||||
}.onFailure {
|
||||
logger.error("Error while transaction notification", it)
|
||||
}
|
||||
}
|
||||
return transactionRepo.create(transaction, userId)
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user