diff --git a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/BankingRepository.kt b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/BankingRepository.kt index 4ef7a91..1ad58a8 100644 --- a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/BankingRepository.kt +++ b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/BankingRepository.kt @@ -1,6 +1,9 @@ package net.codinux.banking.persistence +import kotlinx.datetime.Instant +import kotlinx.datetime.LocalDate import net.codinux.banking.client.model.AccountTransaction +import net.codinux.banking.client.model.Amount import net.codinux.banking.client.model.BankAccess import net.codinux.banking.client.model.securitiesaccount.Holding import net.codinux.banking.persistence.entities.AccountTransactionEntity @@ -30,6 +33,8 @@ interface BankingRepository { suspend fun updateAccount(bank: BankAccountEntity, userSetDisplayName: String?, hideAccount: Boolean, includeInAutomaticAccountsUpdate: Boolean) + suspend fun updateAccount(account: BankAccountEntity, balance: Amount?, lastAccountUpdateTime: Instant, retrievedTransactionsFrom: LocalDate?) + suspend fun deleteBank(bank: BankAccessEntity) diff --git a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/InMemoryBankingRepository.kt b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/InMemoryBankingRepository.kt index 39bd676..3519f2d 100644 --- a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/InMemoryBankingRepository.kt +++ b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/InMemoryBankingRepository.kt @@ -1,6 +1,9 @@ package net.codinux.banking.persistence +import kotlinx.datetime.Instant +import kotlinx.datetime.LocalDate import net.codinux.banking.client.model.AccountTransaction +import net.codinux.banking.client.model.Amount import net.codinux.banking.client.model.BankAccess import net.codinux.banking.client.model.securitiesaccount.Holding import net.codinux.banking.persistence.entities.AccountTransactionEntity @@ -56,6 +59,10 @@ class InMemoryBankingRepository( // no-op } + override suspend fun updateAccount(account: BankAccountEntity, balance: Amount?, lastAccountUpdateTime: Instant, retrievedTransactionsFrom: LocalDate?) { + // no-op + } + override suspend fun deleteBank(bank: BankAccessEntity) { this.banks.remove(bank) } diff --git a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/SqliteBankingRepository.kt b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/SqliteBankingRepository.kt index 953f464..9edb01a 100644 --- a/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/SqliteBankingRepository.kt +++ b/BankingPersistence/src/commonMain/kotlin/net/codinux/banking/persistence/SqliteBankingRepository.kt @@ -141,18 +141,32 @@ open class SqliteBankingRepository : BankingRepository { } } - override suspend fun updateAccount(bank: BankAccountEntity, userSetDisplayName: String?, hideAccount: Boolean, includeInAutomaticAccountsUpdate: Boolean) { + override suspend fun updateAccount(account: BankAccountEntity, userSetDisplayName: String?, hideAccount: Boolean, includeInAutomaticAccountsUpdate: Boolean) { bankQueries.transaction { - if (bank.userSetDisplayName != userSetDisplayName) { - bankQueries.updateBankAccountUserSetDisplayName(userSetDisplayName, bank.id) + if (account.userSetDisplayName != userSetDisplayName) { + bankQueries.updateBankAccountUserSetDisplayName(userSetDisplayName, account.id) } - if (bank.hideAccount != hideAccount) { - bankQueries.updateBankAccountHideAccount(hideAccount, bank.id) + if (account.hideAccount != hideAccount) { + bankQueries.updateBankAccountHideAccount(hideAccount, account.id) } - if (bank.includeInAutomaticAccountsUpdate != includeInAutomaticAccountsUpdate) { - bankQueries.updateBankAccountIncludeInAutomaticAccountsUpdate(includeInAutomaticAccountsUpdate, bank.id) + if (account.includeInAutomaticAccountsUpdate != includeInAutomaticAccountsUpdate) { + bankQueries.updateBankAccountIncludeInAutomaticAccountsUpdate(includeInAutomaticAccountsUpdate, account.id) + } + } + } + + override suspend fun updateAccount(account: BankAccountEntity, balance: Amount?, lastAccountUpdateTime: Instant, retrievedTransactionsFrom: LocalDate?) { + bankQueries.transaction { + if (balance != null && account.balance != balance) { + bankQueries.updateBankAccountBalance(mapAmount(balance), account.id) + } + + bankQueries.updateBankAccountLastAccountUpdateTime(mapInstant(lastAccountUpdateTime), account.id) + + if (retrievedTransactionsFrom != null && (account.retrievedTransactionsFrom == null || account.retrievedTransactionsFrom!! > retrievedTransactionsFrom)) { + bankQueries.updateBankAccountRetrievedTransactionsFrom(mapDate(retrievedTransactionsFrom), account.id) } } } diff --git a/BankingPersistence/src/commonMain/sqldelight/net/codinux/banking/persistence/Bank.sq b/BankingPersistence/src/commonMain/sqldelight/net/codinux/banking/persistence/Bank.sq index cd5978a..f99c1ec 100644 --- a/BankingPersistence/src/commonMain/sqldelight/net/codinux/banking/persistence/Bank.sq +++ b/BankingPersistence/src/commonMain/sqldelight/net/codinux/banking/persistence/Bank.sq @@ -187,6 +187,22 @@ SELECT BankAccount.* FROM BankAccount; +updateBankAccountBalance: +UPDATE BankAccount +SET balance = ? +WHERE id = ?; + +updateBankAccountLastAccountUpdateTime: +UPDATE BankAccount +SET lastAccountUpdateTime = ? +WHERE id = ?; + +updateBankAccountRetrievedTransactionsFrom: +UPDATE BankAccount +SET retrievedTransactionsFrom = ? +WHERE id = ?; + + updateBankAccountUserSetDisplayName: UPDATE BankAccount SET userSetDisplayName = ? diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt index bc68a80..3c178bb 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/BankingService.kt @@ -275,7 +275,11 @@ class BankingService( val transactionsViewModel = updateTransactionsInUi(newTransactionsEntities) uiState.dispatchNewTransactionsRetrieved(AccountTransactionsRetrievedEvent(bank, account, transactionsViewModel, response.holdings)) + + bankingRepository.updateAccount(account, response.balance, response.transactionsRetrievalTime, response.retrievedTransactionsFrom) } + + notifyBanksListUpdated() // notify about updated values like account balance } catch (e: Throwable) { log.error(e) { "Could not save updated account transactions for bank $bank" } }