Using now BankingClient's BankingModelService to find new transactions in retrieved transactions

This commit is contained in:
dankito 2024-09-04 01:13:54 +02:00
parent 278489a320
commit 1368ece023
2 changed files with 12 additions and 29 deletions

View File

@ -104,4 +104,10 @@ class AccountTransactionEntity(
transaction.transactionReferenceNumber, transaction.relatedReferenceNumber
)
override val identifier: String by lazy {
"$userAccountId ${super.identifier}"
}
}

View File

@ -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<AccountTransaction>, account: BankAccountEntity, existingTransactions: List<AccountTransactionEntity>): List<AccountTransaction> {
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<AccountTransactionEntity>): List<AccountTransactionViewModel> {
val transactionsViewModel = addedTransactions.map { AccountTransactionViewModel(it) }