From 1368ece0232d7bb872809011f35dd08fbeda7a02 Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 4 Sep 2024 01:13:54 +0200 Subject: [PATCH] Using now BankingClient's BankingModelService to find new transactions in retrieved transactions --- .../entities/AccountTransactionEntity.kt | 6 ++++ .../banking/ui/service/BankingService.kt | 35 ++++--------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/entities/AccountTransactionEntity.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/entities/AccountTransactionEntity.kt index 4b36c7f..0383147 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/entities/AccountTransactionEntity.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/entities/AccountTransactionEntity.kt @@ -104,4 +104,10 @@ class AccountTransactionEntity( transaction.transactionReferenceNumber, transaction.relatedReferenceNumber ) + + + override val identifier: String by lazy { + "$userAccountId ${super.identifier}" + } + } \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt index d3418f6..ce57395 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt @@ -10,6 +10,7 @@ import net.codinux.banking.client.model.options.GetAccountDataOptions import net.codinux.banking.client.model.options.RetrieveTransactions import net.codinux.banking.client.model.request.GetAccountDataRequest import net.codinux.banking.client.model.response.* +import net.codinux.banking.client.service.BankingModelService import net.codinux.banking.dataaccess.BankingRepository import net.codinux.banking.dataaccess.entities.AccountTransactionEntity import net.codinux.banking.dataaccess.entities.BankAccountEntity @@ -38,6 +39,8 @@ class BankingService( uiState.tanChallengeReceived.value = TanChallengeReceived(tanChallenge, callback) }) + private val modelService = BankingModelService() + private val log by logger() @@ -141,17 +144,11 @@ class BankingService( responses.forEach { response -> val account = (response.account as? BankAccountEntity) ?: user.accounts.first { it.identifier == response.account.identifier && it.subAccountNumber == response.account.subAccountNumber } - // TODO: this should be done in BankingClient - if (account.lastTransactionRetrievalTime == null || account.lastTransactionRetrievalTime!! < response.transactionRetrievalTime) { - account.lastTransactionRetrievalTime = response.transactionRetrievalTime - } - if (account.retrievedTransactionsFrom == null || (response.retrievedTransactionsFrom != null && account.retrievedTransactionsFrom!! < response.retrievedTransactionsFrom!!)) { - account.retrievedTransactionsFrom = response.retrievedTransactionsFrom - } - // TODO: update BankAccount and may updated Transactions in database - val newTransactions = findNewTransactions(response.bookedTransactions, account, userAccountTransactions) + val existingAccountTransactions = userAccountTransactions.filter { it.bankAccountId == account.id } + + val newTransactions = modelService.findNewTransactions(response.bookedTransactions, existingAccountTransactions) val newTransactionsEntities = if (newTransactions.isNotEmpty()) { bankingRepository.persistTransactions(account, newTransactions) @@ -167,26 +164,6 @@ class BankingService( } } - private fun findNewTransactions(retrievedTransactions: List, account: BankAccountEntity, existingTransactions: List): List { - val existingAccountTransactions = existingTransactions.filter { it.bankAccountId == account.id } - val accountId = account.id.toString() - - val existingTransactionsByIdentifier = existingAccountTransactions.associateBy { getTransactionsIdentifier(it, accountId) } - val existingTransactionsIdentifiers = existingTransactionsByIdentifier.keys - -// val (newTransactions, duplicateTransactions) = retrievedTransactions.partition { -// existingTransactionsIdentifiers.contains(getTransactionsIdentifier(it, accountId)) == false -// } -// if (duplicateTransactions.isEmpty() || newTransactions.isEmpty()) { } - - return retrievedTransactions.filter { existingTransactionsIdentifiers.contains(getTransactionsIdentifier(it, accountId)) == false } - } - - private fun getTransactionsIdentifier(transaction: AccountTransaction, accountId: String? = null): String = - with(transaction) { - "${accountId ?: ""} $amount $currency $bookingDate $valueDate $unparsedReference $sepaReference $otherPartyName $otherPartyBankCode $otherPartyAccountId" - } - private fun updateTransactionsInUi(addedTransactions: List): List { val transactionsViewModel = addedTransactions.map { AccountTransactionViewModel(it) }