diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt index 980e390..29b4186 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepository.kt @@ -1,6 +1,7 @@ package net.codinux.banking.dataaccess import app.cash.sqldelight.db.SqlDriver +import kotlinx.datetime.Instant import kotlinx.datetime.LocalDate import net.codinux.banking.client.model.* import net.codinux.banking.dataaccess.entities.AccountTransactionEntity @@ -48,15 +49,17 @@ open class SqliteBankingRepository( - fun getAllBankAccounts(): List = userAccountQueries.selectAllBankAccounts { id, userAccountId, identifier, accountHolderName, type, iban, subAccountNumber, productName, currency, accountLimit, isAccountTypeSupportedByApplication, features, balance, retrievedTransactionsFrom, retrievedTransactionsTo, haveAllTransactionsBeenRetrieved, countDaysForWhichTransactionsAreKept, userSetDisplayName, displayIndex, hideAccount, includeInAutomaticAccountsUpdate -> + fun getAllBankAccounts(): List = userAccountQueries.selectAllBankAccounts { id, userAccountId, identifier, accountHolderName, type, iban, subAccountNumber, productName, currency, accountLimit, balance, isAccountTypeSupportedByApplication, features, countDaysForWhichTransactionsAreKept, lastTransactionsRetrievalTime, retrievedTransactionsFrom, userSetDisplayName, displayIndex, hideAccount, includeInAutomaticAccountsUpdate -> BankAccountEntity( id, userAccountId, identifier, accountHolderName, BankAccountType.valueOf(type), iban, subAccountNumber, productName, currency, accountLimit, + mapToAmount(balance), isAccountTypeSupportedByApplication, mapEnumSet(features, BankAccountFeatures.entries), - mapToAmount(balance), mapToDate(retrievedTransactionsFrom), mapToDate(retrievedTransactionsTo), - haveAllTransactionsBeenRetrieved, mapToInt(countDaysForWhichTransactionsAreKept), + + mapToInt(countDaysForWhichTransactionsAreKept), + mapToInstant(lastTransactionsRetrievalTime), mapToDate(retrievedTransactionsFrom), mutableListOf(), mutableListOf(), @@ -77,12 +80,11 @@ open class SqliteBankingRepository( account.identifier, account.accountHolderName, mapEnum(account.type), account.iban, account.subAccountNumber, account.productName, account.currency, account.accountLimit, + mapAmount(account.balance), account.isAccountTypeSupportedByApplication, mapEnumCollectionToString(account.features), - mapAmount(account.balance), - mapDate(account.retrievedTransactionsFrom), mapDate(account.retrievedTransactionsTo), - - account.haveAllTransactionsBeenRetrieved, mapInt(account.countDaysForWhichTransactionsAreKept), + mapInt(account.countDaysForWhichTransactionsAreKept), + mapInstant(account.lastTransactionsRetrievalTime), mapDate(account.retrievedTransactionsFrom), account.userSetDisplayName, mapInt(account.displayIndex), account.hideAccount, account.includeInAutomaticAccountsUpdate @@ -235,6 +237,7 @@ open class SqliteBankingRepository( private fun mapToAmount(serializedAmount: String): Amount = Amount(serializedAmount) + @JvmName("mapDateNullable") @JsName("mapDateNullable") private fun mapDate(date: LocalDate?): String? = @@ -249,6 +252,22 @@ open class SqliteBankingRepository( private fun mapToDate(serializedDate: String): LocalDate = LocalDate.parse(serializedDate) + + @JvmName("mapInstantNullable") + @JsName("mapInstantNullable") + private fun mapInstant(instant: Instant?): String? = + instant?.let { mapInstant(it) } + + private fun mapInstant(instant: Instant): String = instant.toString() + + @JvmName("mapToInstantNullable") + @JsName("mapToInstantNullable") + private fun mapToInstant(serializedInstant: String?): Instant? = + serializedInstant?.let { mapToInstant(it) } + + private fun mapToInstant(serializedInstant: String): Instant = Instant.parse(serializedInstant) + + private fun > mapEnum(enum: Enum): String = enum.name private fun > mapToEnum(enumName: String, values: EnumEntries): E = diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/entities/BankAccountEntity.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/entities/BankAccountEntity.kt index c023996..98ffda4 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/entities/BankAccountEntity.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/dataaccess/entities/BankAccountEntity.kt @@ -1,5 +1,6 @@ package net.codinux.banking.dataaccess.entities +import kotlinx.datetime.Instant import kotlinx.datetime.LocalDate import net.codinux.banking.client.model.* @@ -16,15 +17,14 @@ class BankAccountEntity( currency: String = "EUR", accountLimit: String? = null, + balance: Amount = Amount.Zero, // TODO: add a BigDecimal library + isAccountTypeSupportedByApplication: Boolean = true, features: Set = emptySet(), - balance: Amount = Amount.Zero, // TODO: add a BigDecimal library - retrievedTransactionsFrom: LocalDate? = null, - retrievedTransactionsTo: LocalDate? = null, - - haveAllTransactionsBeenRetrieved: Boolean = false, countDaysForWhichTransactionsAreKept: Int? = null, + lastTransactionsRetrievalTime: Instant? = null, + retrievedTransactionsFrom: LocalDate? = null, bookedTransactions: MutableList = mutableListOf(), unbookedTransactions: MutableList = mutableListOf(), @@ -36,8 +36,8 @@ class BankAccountEntity( includeInAutomaticAccountsUpdate: Boolean = true ) : BankAccount( identifier, accountHolderName, type, iban, subAccountNumber, productName, currency, accountLimit, - isAccountTypeSupportedByApplication, features, balance, - retrievedTransactionsFrom, retrievedTransactionsTo, haveAllTransactionsBeenRetrieved, countDaysForWhichTransactionsAreKept, + balance, isAccountTypeSupportedByApplication, features, + lastTransactionsRetrievalTime, retrievedTransactionsFrom, countDaysForWhichTransactionsAreKept, bookedTransactions as MutableList, unbookedTransactions, userSetDisplayName, displayIndex, hideAccount, includeInAutomaticAccountsUpdate ) { @@ -45,9 +45,9 @@ class BankAccountEntity( id, userAccountId, account.identifier, account.accountHolderName, account.type, account.iban, account.subAccountNumber, account.productName, account.currency, account.accountLimit, - account.isAccountTypeSupportedByApplication, account.features, account.balance, - account.retrievedTransactionsFrom, account.retrievedTransactionsTo, - account.haveAllTransactionsBeenRetrieved, account.countDaysForWhichTransactionsAreKept, + account.balance, + account.isAccountTypeSupportedByApplication, account.features, + account.countDaysForWhichTransactionsAreKept, account.lastTransactionsRetrievalTime, account.retrievedTransactionsFrom, transactions.toMutableList(), mutableListOf(), account.userSetDisplayName, account.displayIndex, account.hideAccount, account.includeInAutomaticAccountsUpdate ) diff --git a/composeApp/src/commonMain/sqldelight/migrations/2.sqm b/composeApp/src/commonMain/sqldelight/migrations/2.sqm index 0604a50..5e62cbe 100644 --- a/composeApp/src/commonMain/sqldelight/migrations/2.sqm +++ b/composeApp/src/commonMain/sqldelight/migrations/2.sqm @@ -1,2 +1,4 @@ +import kotlin.Boolean; + ALTER TABLE BankAccount ADD COLUMN haveAllTransactionsBeenRetrieved INTEGER AS Boolean NOT NULL DEFAULT 0; \ No newline at end of file diff --git a/composeApp/src/commonMain/sqldelight/migrations/3.sqm b/composeApp/src/commonMain/sqldelight/migrations/3.sqm new file mode 100644 index 0000000..1dbf835 --- /dev/null +++ b/composeApp/src/commonMain/sqldelight/migrations/3.sqm @@ -0,0 +1,5 @@ +ALTER TABLE BankAccount +ADD COLUMN lastTransactionsRetrievalTime TEXT; + +ALTER TABLE BankAccount +DROP COLUMN haveAllTransactionsBeenRetrieved; \ No newline at end of file diff --git a/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/UserAccount.sq b/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/UserAccount.sq index 19bd4c5..af5daf1 100644 --- a/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/UserAccount.sq +++ b/composeApp/src/commonMain/sqldelight/net/codinux/banking/ui/UserAccount.sq @@ -78,15 +78,14 @@ CREATE TABLE IF NOT EXISTS BankAccount ( currency TEXT NOT NULL, accountLimit TEXT, + balance TEXT NOT NULL, + isAccountTypeSupportedByApplication INTEGER AS Boolean NOT NULL, features TEXT NOT NULL, - balance TEXT NOT NULL, - retrievedTransactionsFrom TEXT, - retrievedTransactionsTo TEXT, - - haveAllTransactionsBeenRetrieved INTEGER AS Boolean NOT NULL, countDaysForWhichTransactionsAreKept INTEGER, + lastTransactionsRetrievalTime TEXT, + retrievedTransactionsFrom TEXT, userSetDisplayName TEXT, displayIndex INTEGER NOT NULL, @@ -103,11 +102,11 @@ INSERT INTO BankAccount( identifier, accountHolderName, type, iban, subAccountNumber, productName, currency, accountLimit, + balance, + isAccountTypeSupportedByApplication, features, - balance, retrievedTransactionsFrom, retrievedTransactionsTo, - - haveAllTransactionsBeenRetrieved, countDaysForWhichTransactionsAreKept, + countDaysForWhichTransactionsAreKept, lastTransactionsRetrievalTime, retrievedTransactionsFrom, userSetDisplayName, displayIndex, @@ -119,14 +118,14 @@ VALUES( ?, ?, ?, ?, ?, ?, ?, ?, + ?, + ?, ?, ?, ?, ?, ?, ?, - ?, ?, - ?, ? ); diff --git a/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/main.kt b/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/main.kt index 9dc2a79..04026bb 100644 --- a/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/main.kt +++ b/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/main.kt @@ -28,7 +28,7 @@ fun main() = application { ) { File("data/db").mkdirs() DI.setRepository(JdbcSqliteDriver("jdbc:sqlite:data/db/Bankmeister.db").apply { - val schema = BankmeisterDb.Schema.synchronous().migrate(this, BankmeisterDb.Schema.version, 3) + val schema = BankmeisterDb.Schema.synchronous().migrate(this, BankmeisterDb.Schema.version, 4) }) App() diff --git a/composeApp/src/desktopTest/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepositoryTest.kt b/composeApp/src/desktopTest/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepositoryTest.kt index f412c66..bea20ea 100644 --- a/composeApp/src/desktopTest/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepositoryTest.kt +++ b/composeApp/src/desktopTest/kotlin/net/codinux/banking/dataaccess/SqliteBankingRepositoryTest.kt @@ -25,7 +25,7 @@ class SqliteBankingRepositoryTest { @Test fun saveUserAccount() = runTest { val bankAccounts = listOf( - BankAccount("12345", "Monika Tester", BankAccountType.CheckingAccount, balance = Amount("12.34"), retrievedTransactionsTo = LocalDate(2024, 5, 7), features = setOf(BankAccountFeatures.RetrieveBalance, BankAccountFeatures.InstantPayment), countDaysForWhichTransactionsAreKept = 320) + BankAccount("12345", "Monika Tester", BankAccountType.CheckingAccount, balance = Amount("12.34"), retrievedTransactionsFrom = LocalDate(2024, 5, 7), features = setOf(BankAccountFeatures.RetrieveBalance, BankAccountFeatures.InstantPayment), countDaysForWhichTransactionsAreKept = 320) ) val userAccount = UserAccount("12345678", "SupiDupiUser", "geheim", "Abzock-Bank", "ABCDDEBBXXX", "Monika Tester", accounts = bankAccounts, bankingGroup = BankingGroup.DKB).apply { wrongCredentialsEntered = true @@ -63,7 +63,7 @@ class SqliteBankingRepositoryTest { assertEquals(bankAccounts.first().balance, persistedBankAccount.balance) assertEquals(bankAccounts.first().retrievedTransactionsFrom, persistedBankAccount.retrievedTransactionsFrom) - assertEquals(bankAccounts.first().retrievedTransactionsTo, persistedBankAccount.retrievedTransactionsTo) + assertEquals(bankAccounts.first().lastTransactionsRetrievalTime, persistedBankAccount.lastTransactionsRetrievalTime) assertEquals(bankAccounts.first().features, persistedBankAccount.features)