From e12ab5a5f82b6295d8fa3097b83a1713489c7f53 Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 14 Aug 2024 13:29:01 +0200 Subject: [PATCH] Extracted --- .../net/dankito/banking/fints/FinTsClient.kt | 22 ++------------- .../banking/fints/mapper/FinTsModelMapper.kt | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt index 18cec50e..71dcc2b1 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/FinTsClient.kt @@ -50,7 +50,7 @@ open class FinTsClient @JvmOverloads constructor( open suspend fun getAccountDataAsync(param: GetAccountDataParameter): GetAccountDataResponse { val finTsServerAddress = finTsServerAddressFinder.findFinTsServerAddress(param.bankCode) if (finTsServerAddress.isNullOrBlank()) { - return GetAccountDataResponse(ErrorCode.BankDoesNotSupportFinTs3, "Either bank does not FinTS 3.0 or we don't know its FinTS server address", null, listOf()) + return GetAccountDataResponse(ErrorCode.BankDoesNotSupportFinTs3, "Either bank does not support FinTS 3.0 or we don't know its FinTS server address", null, listOf()) } val bank = mapper.mapToBankData(param, finTsServerAddress) @@ -94,25 +94,7 @@ open class FinTsClient @JvmOverloads constructor( protected open suspend fun getAccountData(param: GetAccountDataParameter, bank: BankData, account: AccountData): GetAccountTransactionsResponse { val context = JobContext(JobContextType.GetTransactions, this.callback, product, bank, account) - val retrieveTransactionsFrom = when (param.retrieveTransactions) { - RetrieveTransactions.No -> LocalDate.todayAtEuropeBerlin() // TODO: implement RetrieveTransactions.No - RetrieveTransactions.OfLast90Days -> calculate90DaysAgo() - RetrieveTransactions.AccordingToRetrieveFromAndTo -> param.retrieveTransactionsFrom - else -> null - } - - val retrieveTransactionsTo = when (param.retrieveTransactions) { - RetrieveTransactions.AccordingToRetrieveFromAndTo -> param.retrieveTransactionsTo - else -> null - } - - return jobExecutor.getTransactionsAsync(context, GetAccountTransactionsParameter(bank, account, param.retrieveBalance, retrieveTransactionsFrom, - retrieveTransactionsTo, abortIfTanIsRequired = param.abortIfTanIsRequired)) - } - - private fun calculate90DaysAgo(): LocalDate? { - // Europe/Berlin: we're communicating with German bank servers, so we have to use their time zone - return LocalDate.todayAtEuropeBerlin().minusDays(90) + return jobExecutor.getTransactionsAsync(context, mapper.toGetAccountTransactionsParameter(param, bank, account)) } diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/mapper/FinTsModelMapper.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/mapper/FinTsModelMapper.kt index b9586afd..507f0b22 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/mapper/FinTsModelMapper.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/mapper/FinTsModelMapper.kt @@ -1,8 +1,11 @@ package net.dankito.banking.fints.mapper +import kotlinx.datetime.LocalDate import net.dankito.banking.client.model.* import net.dankito.banking.client.model.AccountTransaction import net.dankito.banking.client.model.parameter.FinTsClientParameter +import net.dankito.banking.client.model.parameter.GetAccountDataParameter +import net.dankito.banking.client.model.parameter.RetrieveTransactions import net.dankito.banking.client.model.response.ErrorCode import net.dankito.banking.fints.messages.datenelemente.abgeleiteteformate.Laenderkennzeichen import net.dankito.banking.fints.model.* @@ -10,6 +13,8 @@ import net.dankito.banking.fints.response.client.FinTsClientResponse import net.dankito.banking.fints.response.client.GetAccountTransactionsResponse import net.dankito.banking.fints.response.segments.AccountType import net.dankito.banking.fints.util.BicFinder +import net.dankito.utils.multiplatform.extensions.minusDays +import net.dankito.utils.multiplatform.extensions.todayAtEuropeBerlin open class FinTsModelMapper { @@ -96,6 +101,29 @@ open class FinTsModelMapper { } + open fun toGetAccountTransactionsParameter(param: GetAccountDataParameter, bank: BankData, account: AccountData): GetAccountTransactionsParameter { + val retrieveTransactionsFrom = when (param.retrieveTransactions) { + RetrieveTransactions.No -> LocalDate.todayAtEuropeBerlin() // TODO: implement RetrieveTransactions.No + RetrieveTransactions.OfLast90Days -> calculate90DaysAgo() + RetrieveTransactions.AccordingToRetrieveFromAndTo -> param.retrieveTransactionsFrom + else -> null + } + + val retrieveTransactionsTo = when (param.retrieveTransactions) { + RetrieveTransactions.AccordingToRetrieveFromAndTo -> param.retrieveTransactionsTo + else -> null + } + + return GetAccountTransactionsParameter(bank, account, param.retrieveBalance, retrieveTransactionsFrom, + retrieveTransactionsTo, abortIfTanIsRequired = param.abortIfTanIsRequired) + } + + open fun calculate90DaysAgo(): LocalDate { + // Europe/Berlin: we're communicating with German bank servers, so we have to use their time zone + return LocalDate.todayAtEuropeBerlin().minusDays(90) + } + + open fun mapErrorCode(response: FinTsClientResponse): ErrorCode? { return when { response.didReceiveResponse == false -> ErrorCode.NetworkError