Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
smaugfm committed Nov 26, 2023
1 parent e815144 commit d93c94f
Show file tree
Hide file tree
Showing 21 changed files with 473 additions and 187 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ tasks {
}

fun <T : KotlinCommonCompilerOptions> KotlinCompilationTask<T>.optIn() {
compilerOptions.freeCompilerArgs.add(
compilerOptions.freeCompilerArgs.addAll(
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
)
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/io/github/smaugfm/monobudget/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.github.smaugfm.monobudget.common.telegram.TelegramApi
import io.github.smaugfm.monobudget.common.telegram.TelegramCallbackHandler
import io.github.smaugfm.monobudget.common.util.injectAll
import io.github.smaugfm.monobudget.common.verify.ApplicationStartupVerifier
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
Expand All @@ -23,7 +22,6 @@ import kotlin.system.exitProcess

private val log = KotlinLogging.logger {}

@OptIn(ExperimentalCoroutinesApi::class)
class Application<TTransaction, TNewTransaction> :
KoinComponent {
private val telegramApi by inject<TelegramApi>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.smaugfm.monobudget.common.account

import io.github.smaugfm.monobudget.common.misc.ConcurrentExpiringMap
import io.github.smaugfm.monobudget.common.model.financial.StatementItem
import kotlinx.coroutines.Deferred
import kotlin.time.Duration.Companion.minutes

abstract class TransferCache<TTransaction> :
ConcurrentExpiringMap<StatementItem, Deferred<TTransaction>>(1.minutes)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.Deferred

private val log = KotlinLogging.logger {}

abstract class TransferBetweenAccountsDetector<TTransaction>(
abstract class TransferDetector<TTransaction>(
private val bankAccounts: BankAccountService,
private val ctx: StatementProcessingContext,
private val cache: ConcurrentExpiringMap<StatementItem, Deferred<TTransaction>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.smaugfm.monobudget.common.lifecycle

import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.monobudget.common.account.BankAccountService
import io.github.smaugfm.monobudget.common.account.TransferBetweenAccountsDetector
import io.github.smaugfm.monobudget.common.account.TransferDetector
import io.github.smaugfm.monobudget.common.telegram.TelegramMessageSender
import io.github.smaugfm.monobudget.common.transaction.TransactionFactory
import io.github.smaugfm.monobudget.common.transaction.TransactionMessageFormatter
Expand All @@ -14,7 +14,7 @@ abstract class StatementItemProcessor<TTransaction, TNewTransaction>(
private val ctx: StatementProcessingContext,
private val transactionFactory: TransactionFactory<TTransaction, TNewTransaction>,
private val bankAccounts: BankAccountService,
private val transferDetector: TransferBetweenAccountsDetector<TTransaction>,
private val transferDetector: TransferDetector<TTransaction>,
private val messageFormatter: TransactionMessageFormatter<TTransaction>,
private val telegramMessageSender: TelegramMessageSender,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data class StatementProcessingContext(
private val map: MutableMap<String, Any> = mutableMapOf(),
val attempt: Int = 0,
) {
suspend fun execIfNotSet(
suspend fun execIfFirst(
key: String,
block: suspend () -> Unit,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class TelegramMessageSender(
) {
val chatId = bankAccounts.getTelegramChatIdByAccountId(accountId)
if (chatId == null) {
log.error { "Failed to map Monobank account to telegram chat id. Account: $accountId" }
log.error { "Failed to map bank account id to telegram chat id. Account: $accountId" }
return
}

log.info { "Sending message to telegramChatId=$chatId. monoAccountId=$accountId)" }
log.info { "Sending message to telegramChatId=$chatId. bankAccountId=$accountId)" }
telegramApi.sendMessage(
chatId = ChatId.IntegerId(chatId),
text = newMessage.message,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.github.smaugfm.monobudget.common.util

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonBuilder
import kotlinx.serialization.json.JsonNamingStrategy

fun makeJson(convertSnakeCase: Boolean = false): Json = Json { buildJson(convertSnakeCase) }

@OptIn(ExperimentalSerializationApi::class)
fun JsonBuilder.buildJson(convertSnakeCase: Boolean = false) {
if (convertSnakeCase) {
namingStrategy = JsonNamingStrategy.SnakeCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class LunchmoneyCategoryService(
null,
).awaitSingle()

return budgets.firstOrNull { it.categoryId != null && it.categoryId == categoryId }
return budgets.firstOrNull { categoryId == it.categoryId }
}

private fun toCategoryBudget(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.github.smaugfm.monobudget.lunchmoney
import io.github.smaugfm.lunchmoney.model.LunchmoneyInsertTransaction
import io.github.smaugfm.lunchmoney.model.LunchmoneyTransaction
import io.github.smaugfm.monobudget.common.account.BankAccountService
import io.github.smaugfm.monobudget.common.account.TransferBetweenAccountsDetector
import io.github.smaugfm.monobudget.common.account.TransferDetector
import io.github.smaugfm.monobudget.common.lifecycle.StatementItemProcessor
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingScopeComponent
Expand All @@ -19,7 +19,7 @@ class LunchmoneyStatementItemProcessor(
ctx: StatementProcessingContext,
transactionFactory: TransactionFactory<LunchmoneyTransaction, LunchmoneyInsertTransaction>,
bankAccounts: BankAccountService,
transferDetector: TransferBetweenAccountsDetector<LunchmoneyTransaction>,
transferDetector: TransferDetector<LunchmoneyTransaction>,
messageFormatter: TransactionMessageFormatter<LunchmoneyTransaction>,
telegramMessageSender: TelegramMessageSender,
) : StatementItemProcessor<LunchmoneyTransaction, LunchmoneyInsertTransaction>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class LunchmoneyTransactionCreator(
"Existing LunchmoneyTransaction: $existingTransaction"
}

ctx.execIfNotSet("transactionUpdated") {
ctx.execIfFirst("transactionUpdated") {
api.updateTransaction(
transactionId = existingTransaction.id,
transaction =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.smaugfm.monobudget.lunchmoney

import io.github.smaugfm.lunchmoney.model.LunchmoneyTransaction
import io.github.smaugfm.monobudget.common.account.TransferCache
import org.koin.core.annotation.Single

@Single
class LunchmoneyTransferCache : TransferCache<LunchmoneyTransaction>()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.smaugfm.monobudget.lunchmoney

import io.github.smaugfm.lunchmoney.model.LunchmoneyTransaction
import io.github.smaugfm.monobudget.common.account.BankAccountService
import io.github.smaugfm.monobudget.common.account.TransferDetector
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingScopeComponent
import org.koin.core.annotation.Scope
import org.koin.core.annotation.Scoped

@Scoped
@Scope(StatementProcessingScopeComponent::class)
class LunchmoneyTransferDetector(
bankAccounts: BankAccountService,
ctx: StatementProcessingContext,
cache: LunchmoneyTransferCache,
) : TransferDetector<LunchmoneyTransaction>(bankAccounts, ctx, cache)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.smaugfm.monobudget.ynab

import io.github.smaugfm.monobudget.common.account.BankAccountService
import io.github.smaugfm.monobudget.common.account.TransferBetweenAccountsDetector
import io.github.smaugfm.monobudget.common.account.TransferDetector
import io.github.smaugfm.monobudget.common.lifecycle.StatementItemProcessor
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingScopeComponent
Expand All @@ -19,7 +19,7 @@ class YnabStatementItemProcessor(
ctx: StatementProcessingContext,
transactionFactory: TransactionFactory<YnabTransactionDetail, YnabSaveTransaction>,
bankAccounts: BankAccountService,
transferDetector: TransferBetweenAccountsDetector<YnabTransactionDetail>,
transferDetector: TransferDetector<YnabTransactionDetail>,
messageFormatter: TransactionMessageFormatter<YnabTransactionDetail>,
telegramMessageSender: TelegramMessageSender,
) : StatementItemProcessor<YnabTransactionDetail, YnabSaveTransaction>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.smaugfm.monobudget.ynab

import io.github.smaugfm.monobudget.common.account.TransferCache
import io.github.smaugfm.monobudget.ynab.model.YnabTransactionDetail
import org.koin.core.annotation.Single

@Single
class YnabTransferCache : TransferCache<YnabTransactionDetail>()
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package io.github.smaugfm.monobudget.ynab

import io.github.smaugfm.monobudget.common.account.BankAccountService
import io.github.smaugfm.monobudget.common.account.TransferBetweenAccountsDetector
import io.github.smaugfm.monobudget.common.account.TransferDetector
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingScopeComponent
import io.github.smaugfm.monobudget.common.misc.ConcurrentExpiringMap
import io.github.smaugfm.monobudget.common.model.financial.StatementItem
import io.github.smaugfm.monobudget.ynab.model.YnabTransactionDetail
import kotlinx.coroutines.Deferred
import org.koin.core.annotation.Scope
import org.koin.core.annotation.Scoped
import kotlin.time.Duration.Companion.minutes

@Scoped
@Scope(StatementProcessingScopeComponent::class)
class YnabMonoTransferBetweenAccountsDetector(
class YnabTransferDetector(
bankAccounts: BankAccountService,
ctx: StatementProcessingContext,
) : TransferBetweenAccountsDetector<YnabTransactionDetail>(
cache: YnabTransferCache,
) : TransferDetector<YnabTransactionDetail>(
bankAccounts,
ctx,
cache,
) {
companion object {
private val cache =
ConcurrentExpiringMap<StatementItem, Deferred<YnabTransactionDetail>>(1.minutes)
}
}
)
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.github.smaugfm.monobudget.common.mono
package io.github.smaugfm.monobudget.common.account

import assertk.assertThat
import assertk.assertions.isInstanceOf
import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.smaugfm.monobudget.TestBase
import io.github.smaugfm.monobudget.common.account.BankAccountService
import io.github.smaugfm.monobudget.common.account.MaybeTransferStatement.NotTransfer
import io.github.smaugfm.monobudget.common.account.MaybeTransferStatement.Transfer
import io.github.smaugfm.monobudget.common.account.TransferBetweenAccountsDetector
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingContext
import io.github.smaugfm.monobudget.common.lifecycle.StatementProcessingScopeComponent
import io.github.smaugfm.monobudget.common.misc.ConcurrentExpiringMap
Expand All @@ -29,19 +27,19 @@ import kotlin.time.Duration.Companion.minutes

private val log = KotlinLogging.logger { }

class MonoTransferBetweenAccountsDetectorTest : TestBase() {
class TransferDetectorTest : TestBase() {
companion object {
private val cache = ConcurrentExpiringMap<StatementItem, Deferred<Any>>(1.minutes)
}

class TestDetector(
bankAccounts: BankAccountService,
ctx: StatementProcessingContext,
) : TransferBetweenAccountsDetector<Any>(
bankAccounts,
ctx,
cache,
)
) : TransferDetector<Any>(
bankAccounts,
ctx,
cache,
)

override fun KoinApplication.testKoinApplication() {
modules(
Expand All @@ -55,7 +53,7 @@ class MonoTransferBetweenAccountsDetectorTest : TestBase() {

@Timeout(2, unit = TimeUnit.SECONDS)
@Test
fun test() {
fun `Detects transfer`() {
val ctx1 = StatementProcessingContext(statementItem1())
val ctx2 = StatementProcessingContext(statementItem2())

Expand Down
Loading

0 comments on commit d93c94f

Please sign in to comment.