62 lines
2.3 KiB
Kotlin
62 lines
2.3 KiB
Kotlin
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,
|
|
private val notificationService: NotificationService
|
|
) : TransactionService {
|
|
private val logger = LoggerFactory.getLogger(this.javaClass)
|
|
private val serviceScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
|
|
|
|
|
|
override fun createTransaction(
|
|
spaceId: Int,
|
|
userId: Int,
|
|
transaction: TransactionDTO.CreateTransactionDTO,
|
|
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,
|
|
)
|
|
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)
|
|
}
|
|
|
|
|
|
} |