Implemented fetching account data from bank if not found locally (e.g. if persisted data got deleted)

This commit is contained in:
dankito 2020-07-27 16:57:38 +02:00
parent 04fba18316
commit 2d30047c75
1 changed files with 40 additions and 18 deletions

View File

@ -50,6 +50,8 @@ open class fints4kBankingClient(
protected val bankDataMapper = BankDataMapper() protected val bankDataMapper = BankDataMapper()
protected var didTryToGetAccountDataFromBank = false
protected val bank = bankDataMapper.mapFromBankInfo(bankInfo) protected val bank = bankDataMapper.mapFromBankInfo(bankInfo)
@ -83,17 +85,17 @@ open class fints4kBankingClient(
override fun getTransactionsAsync(bankAccount: BankAccount, parameter: GetTransactionsParameter, callback: (GetTransactionsResponse) -> Unit) { override fun getTransactionsAsync(bankAccount: BankAccount, parameter: GetTransactionsParameter, callback: (GetTransactionsResponse) -> Unit) {
val account = mapper.findAccountForBankAccount(fints4kCustomer, bankAccount) findAccountForBankAccount(bankAccount) { account, errorMessage ->
if (account == null) {
callback(GetTransactionsResponse(bankAccount, false, errorMessage))
}
else {
val mappedParameter = GetTransactionsParameter(parameter.alsoRetrieveBalance, parameter.fromDate,
parameter.toDate, null, parameter.abortIfTanIsRequired,
{ parameter.retrievedChunkListener?.invoke(mapper.mapTransactions(bankAccount, it)) } )
if (account == null) { // TODO: in this case retrieve data from bank, all data should be re-creatable doGetTransactionsAsync(mappedParameter, account, bankAccount, callback)
callback(GetTransactionsResponse(bankAccount, false, "Cannot find account for ${bankAccount.identifier}")) // TODO: translate }
}
else {
val mappedParameter = GetTransactionsParameter(parameter.alsoRetrieveBalance, parameter.fromDate,
parameter.toDate, null, parameter.abortIfTanIsRequired,
{ parameter.retrievedChunkListener?.invoke(mapper.mapTransactions(bankAccount, it)) } )
doGetTransactionsAsync(mappedParameter, account, bankAccount, callback)
} }
} }
@ -115,15 +117,15 @@ open class fints4kBankingClient(
override fun transferMoneyAsync(data: TransferMoneyData, bankAccount: BankAccount, callback: (BankingClientResponse) -> Unit) { override fun transferMoneyAsync(data: TransferMoneyData, bankAccount: BankAccount, callback: (BankingClientResponse) -> Unit) {
val account = mapper.findAccountForBankAccount(fints4kCustomer, bankAccount) findAccountForBankAccount(bankAccount) { account, errorMessage ->
if (account == null) {
callback(BankingClientResponse(false, errorMessage))
}
else {
val mappedData = BankTransferData(data.creditorName, data.creditorIban, data.creditorBic, data.amount.toMoney(), data.usage, data.instantPayment)
if (account == null) { doBankTransferAsync(mappedData, account, callback)
callback(BankingClientResponse(false, "Cannot find account for ${bankAccount.identifier}")) // TODO: translate }
}
else {
val mappedData = BankTransferData(data.creditorName, data.creditorIban, data.creditorBic, data.amount.toMoney(), data.usage, data.instantPayment)
doBankTransferAsync(mappedData, account, callback)
} }
} }
@ -140,6 +142,26 @@ open class fints4kBankingClient(
} }
protected open fun findAccountForBankAccount(bankAccount: BankAccount, findAccountResult: (AccountData?, error: String?) -> Unit) {
val mappedAccount = mapper.findAccountForBankAccount(fints4kCustomer, bankAccount)
if (mappedAccount != null) {
findAccountResult(mappedAccount, null)
}
else if (didTryToGetAccountDataFromBank == false) { // then try to get account data by fetching data from bank
addAccountAsync { response ->
didTryToGetAccountDataFromBank = !!! response.isSuccessful
findAccountResult(mapper.findAccountForBankAccount(fints4kCustomer, bankAccount),
response.errorToShowToUser ?: response.error?.message)
}
}
else {
findAccountResult(null, "Cannot find account for ${bankAccount.identifier}") // TODO: translate
}
}
override fun restoreData() { override fun restoreData() {
val deserializedCustomer = serializer.deserializeObject(getFints4kClientDataFile(), CustomerData::class) val deserializedCustomer = serializer.deserializeObject(getFints4kClientDataFile(), CustomerData::class)