Implemented fetching account data from bank if not found locally (e.g. if persisted data got deleted)
This commit is contained in:
parent
04fba18316
commit
2d30047c75
|
@ -50,6 +50,8 @@ open class fints4kBankingClient(
|
|||
|
||||
protected val bankDataMapper = BankDataMapper()
|
||||
|
||||
protected var didTryToGetAccountDataFromBank = false
|
||||
|
||||
|
||||
protected val bank = bankDataMapper.mapFromBankInfo(bankInfo)
|
||||
|
||||
|
@ -83,10 +85,9 @@ open class fints4kBankingClient(
|
|||
|
||||
|
||||
override fun getTransactionsAsync(bankAccount: BankAccount, parameter: GetTransactionsParameter, callback: (GetTransactionsResponse) -> Unit) {
|
||||
val account = mapper.findAccountForBankAccount(fints4kCustomer, bankAccount)
|
||||
|
||||
if (account == null) { // TODO: in this case retrieve data from bank, all data should be re-creatable
|
||||
callback(GetTransactionsResponse(bankAccount, false, "Cannot find account for ${bankAccount.identifier}")) // TODO: translate
|
||||
findAccountForBankAccount(bankAccount) { account, errorMessage ->
|
||||
if (account == null) {
|
||||
callback(GetTransactionsResponse(bankAccount, false, errorMessage))
|
||||
}
|
||||
else {
|
||||
val mappedParameter = GetTransactionsParameter(parameter.alsoRetrieveBalance, parameter.fromDate,
|
||||
|
@ -96,6 +97,7 @@ open class fints4kBankingClient(
|
|||
doGetTransactionsAsync(mappedParameter, account, bankAccount, callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun doGetTransactionsAsync(parameter: net.dankito.banking.fints.model.GetTransactionsParameter,
|
||||
account: AccountData, bankAccount: BankAccount, callback: (GetTransactionsResponse) -> Unit) {
|
||||
|
@ -115,10 +117,9 @@ open class fints4kBankingClient(
|
|||
|
||||
|
||||
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, "Cannot find account for ${bankAccount.identifier}")) // TODO: translate
|
||||
callback(BankingClientResponse(false, errorMessage))
|
||||
}
|
||||
else {
|
||||
val mappedData = BankTransferData(data.creditorName, data.creditorIban, data.creditorBic, data.amount.toMoney(), data.usage, data.instantPayment)
|
||||
|
@ -126,6 +127,7 @@ open class fints4kBankingClient(
|
|||
doBankTransferAsync(mappedData, account, callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun doBankTransferAsync(data: BankTransferData, account: AccountData, callback: (BankingClientResponse) -> Unit) {
|
||||
client.doBankTransferAsync(data, account) { response ->
|
||||
|
@ -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() {
|
||||
val deserializedCustomer = serializer.deserializeObject(getFints4kClientDataFile(), CustomerData::class)
|
||||
|
||||
|
|
Loading…
Reference in New Issue