From f8dd296b47cdbbd42a6119c1a16ef6456eca36b8 Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 16 Oct 2024 20:04:59 +0200 Subject: [PATCH] Implemented setting preferredTanMethods for updateAccountTransactionsAsync() and transferMoneyAsync() --- .../kotlin/net/codinux/banking/client/BankingClient.kt | 6 +++++- .../net/codinux/banking/client/BankingClientForUser.kt | 6 +++++- .../codinux/banking/client/BankingClientForUserBase.kt | 3 ++- .../codinux/banking/client/BankingClientExtensions.kt | 9 +++++---- .../banking/client/fints4k/FinTs4kBankingClient.kt | 8 +++++--- .../net/codinux/banking/client/fints4k/FinTs4kMapper.kt | 6 ++---- 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClient.kt b/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClient.kt index a8a460a9..236c5064 100644 --- a/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClient.kt +++ b/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClient.kt @@ -8,6 +8,7 @@ import net.codinux.banking.client.model.response.GetAccountDataResponse import net.codinux.banking.client.model.response.GetTransactionsResponse import net.codinux.banking.client.model.response.Response import net.codinux.banking.client.model.response.TransferMoneyResponse +import net.codinux.banking.client.model.tan.TanMethodType interface BankingClient { @@ -41,7 +42,10 @@ interface BankingClient { * * Optionally specify which [accounts] should be updated. If not specified all accounts will be updated. */ - suspend fun updateAccountTransactionsAsync(bank: BankAccess, accounts: List? = null): Response> + suspend fun updateAccountTransactionsAsync( + bank: BankAccess, accounts: List? = null, + preferredTanMethodsIfSelectedTanMethodIsNotAvailable: List? = TanMethodType.TanMethodsPreferredByMostApplications + ): Response> suspend fun transferMoneyAsync(bankCode: String, loginName: String, password: String, recipientName: String, diff --git a/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClientForUser.kt b/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClientForUser.kt index 84195fdd..ea993888 100644 --- a/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClientForUser.kt +++ b/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClientForUser.kt @@ -9,6 +9,7 @@ import net.codinux.banking.client.model.response.GetAccountDataResponse import net.codinux.banking.client.model.response.GetTransactionsResponse import net.codinux.banking.client.model.response.Response import net.codinux.banking.client.model.response.TransferMoneyResponse +import net.codinux.banking.client.model.tan.TanMethodType interface BankingClientForUser { @@ -40,7 +41,10 @@ interface BankingClientForUser { * Updates account's transactions beginning from [BankAccount.lastAccountUpdateTime]. * This may requires TAN if [BankAccount.lastAccountUpdateTime] is older than 90 days. */ - suspend fun updateAccountTransactionsAsync(accounts: List? = null): Response> + suspend fun updateAccountTransactionsAsync( + accounts: List? = null, + preferredTanMethodsIfSelectedTanMethodIsNotAvailable: List? = TanMethodType.TanMethodsPreferredByMostApplications + ): Response> suspend fun transferMoneyAsync(recipientName: String, recipientAccountIdentifier: String, amount: Amount, paymentReference: String? = null): Response diff --git a/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClientForUserBase.kt b/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClientForUserBase.kt index 00da7c5d..99e4e1d7 100644 --- a/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClientForUserBase.kt +++ b/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/BankingClientForUserBase.kt @@ -10,6 +10,7 @@ import net.codinux.banking.client.model.request.TransferMoneyRequest import net.codinux.banking.client.model.request.TransferMoneyRequestForUser import net.codinux.banking.client.model.response.GetTransactionsResponse import net.codinux.banking.client.model.response.Response +import net.codinux.banking.client.model.tan.TanMethodType abstract class BankingClientForUserBase( protected val credentials: AccountCredentials, @@ -25,7 +26,7 @@ abstract class BankingClientForUserBase( } } - override suspend fun updateAccountTransactionsAsync(accounts: List?): Response> = + override suspend fun updateAccountTransactionsAsync(accounts: List?, preferredTanMethodsIfSelectedTanMethodIsNotAvailable: List?): Response> = client.updateAccountTransactionsAsync(bank, accounts) diff --git a/BankingClient/src/jvmMain/kotlin/net/codinux/banking/client/BankingClientExtensions.kt b/BankingClient/src/jvmMain/kotlin/net/codinux/banking/client/BankingClientExtensions.kt index 1c5b39a2..d53b524b 100644 --- a/BankingClient/src/jvmMain/kotlin/net/codinux/banking/client/BankingClientExtensions.kt +++ b/BankingClient/src/jvmMain/kotlin/net/codinux/banking/client/BankingClientExtensions.kt @@ -8,6 +8,7 @@ import net.codinux.banking.client.model.options.GetAccountDataOptions import net.codinux.banking.client.model.request.GetAccountDataRequest import net.codinux.banking.client.model.request.TransferMoneyRequest import net.codinux.banking.client.model.request.TransferMoneyRequestForUser +import net.codinux.banking.client.model.tan.TanMethodType /* BankingClient */ @@ -19,8 +20,8 @@ fun BankingClient.getAccountData(request: GetAccountDataRequest) = runBlocking { getAccountDataAsync(request) } -fun BankingClient.updateAccountTransactions(bank: BankAccess, accounts: List? = null) = runBlocking { - updateAccountTransactionsAsync(bank, accounts) +fun BankingClient.updateAccountTransactions(bank: BankAccess, accounts: List? = null, preferredTanMethodsIfSelectedTanMethodIsNotAvailable: List? = TanMethodType.TanMethodsPreferredByMostApplications) = runBlocking { + updateAccountTransactionsAsync(bank, accounts, preferredTanMethodsIfSelectedTanMethodIsNotAvailable) } @@ -44,8 +45,8 @@ fun BankingClientForUser.getAccountData(options: GetAccountDataOptions) = runBlo getAccountDataAsync(options) } -fun BankingClientForUser.updateAccountTransactions() = runBlocking { - updateAccountTransactionsAsync() +fun BankingClientForUser.updateAccountTransactions(accounts: List? = null, preferredTanMethodsIfSelectedTanMethodIsNotAvailable: List? = TanMethodType.TanMethodsPreferredByMostApplications) = runBlocking { + updateAccountTransactionsAsync(accounts, preferredTanMethodsIfSelectedTanMethodIsNotAvailable) } diff --git a/FinTs4jBankingClient/src/commonMain/kotlin/net/codinux/banking/client/fints4k/FinTs4kBankingClient.kt b/FinTs4jBankingClient/src/commonMain/kotlin/net/codinux/banking/client/fints4k/FinTs4kBankingClient.kt index 36168d24..b1afd8a2 100644 --- a/FinTs4jBankingClient/src/commonMain/kotlin/net/codinux/banking/client/fints4k/FinTs4kBankingClient.kt +++ b/FinTs4jBankingClient/src/commonMain/kotlin/net/codinux/banking/client/fints4k/FinTs4kBankingClient.kt @@ -9,9 +9,9 @@ import net.codinux.banking.client.model.options.GetAccountDataOptions import net.codinux.banking.client.model.request.GetAccountDataRequest import net.codinux.banking.client.model.request.TransferMoneyRequestForUser import net.codinux.banking.client.model.response.* +import net.codinux.banking.client.model.tan.TanMethodType import net.codinux.banking.fints.FinTsClient import net.codinux.banking.fints.config.FinTsClientConfiguration -import net.codinux.banking.fints.model.BankData open class FinTs4kBankingClient( config: FinTsClientConfiguration = FinTsClientConfiguration(), @@ -35,12 +35,14 @@ open class FinTs4kBankingClient( return mapper.map(response, request.bankInfo) } - override suspend fun updateAccountTransactionsAsync(bank: BankAccess, accounts: List?): Response> { + override suspend fun updateAccountTransactionsAsync(bank: BankAccess, accounts: List?, preferredTanMethodsIfSelectedTanMethodIsNotAvailable: List?): Response> { val accountsToRequest = (accounts ?: bank.accounts).filter { it.supportsAnyFeature(BankAccountFeatures.RetrieveBalance, BankAccountFeatures.RetrieveTransactions) } if (accountsToRequest.isNotEmpty()) { val responses = accountsToRequest.map { account -> - val parameter = mapper.mapToUpdateAccountTransactionsParameter(bank, account) + val preferredTanMethods = listOf(bank.selectedTanMethod.type) + (preferredTanMethodsIfSelectedTanMethodIsNotAvailable ?: emptyList()) + + val parameter = mapper.mapToUpdateAccountTransactionsParameter(bank, account, preferredTanMethods) val response = client.getAccountDataAsync(parameter) diff --git a/FinTs4jBankingClient/src/commonMain/kotlin/net/codinux/banking/client/fints4k/FinTs4kMapper.kt b/FinTs4jBankingClient/src/commonMain/kotlin/net/codinux/banking/client/fints4k/FinTs4kMapper.kt index 419fd8ad..8598b9fc 100644 --- a/FinTs4jBankingClient/src/commonMain/kotlin/net/codinux/banking/client/fints4k/FinTs4kMapper.kt +++ b/FinTs4jBankingClient/src/commonMain/kotlin/net/codinux/banking/client/fints4k/FinTs4kMapper.kt @@ -62,18 +62,16 @@ open class FinTs4kMapper { bank.serverAddress, bank.bic, bank.name ) - open fun mapToUpdateAccountTransactionsParameter(bank: BankAccess, account: BankAccount): GetAccountDataParameter { + open fun mapToUpdateAccountTransactionsParameter(bank: BankAccess, account: BankAccount, preferredTanMethods: List? = null): GetAccountDataParameter { val defaults = GetAccountDataOptions() val accountIdentifier = BankAccountIdentifierImpl(account.identifier, account.subAccountNumber, account.iban) val from = account.lastAccountUpdateTime?.toLocalDateTime(TimeZone.EuropeBerlin)?.date // TODO: in case lastTransactionsUpdateTime is not set, this would retrieve all transactions (and require a TAN im most cases) val retrieveTransactions = if (from != null) RetrieveTransactions.AccordingToRetrieveFromAndTo else RetrieveTransactions.valueOf(defaults.retrieveTransactions.name) -// val preferredTanMethods = listOf(mapTanMethodType(bank.selectedTanMethod.type)) // TODO: currently we aren't saving TanMethods in database, re-enable as soon as TanMethods get saved - val preferredTanMethods = defaults.preferredTanMethods?.map { mapTanMethodType(it) } return GetAccountDataParameter(bank.domesticBankCode, bank.loginName, bank.password!!, listOf(accountIdentifier), true, retrieveTransactions, from, - preferredTanMethods = preferredTanMethods, + preferredTanMethods = preferredTanMethods?.map { mapTanMethodType(it) }, preferredTanMedium = bank.selectedTanMediumIdentifier, finTsModel = bank.clientData as? BankData, serializedFinTsModel = bank.serializedClientData