From 61eecc104b40c6191ac48221ae582c122b7dafca Mon Sep 17 00:00:00 2001 From: Kumar Rishi Date: Tue, 11 Jun 2024 00:44:12 +0530 Subject: [PATCH] [FIX] Migrate TransactionDao calls to TransactionRepository part - 1 (#3228) * [FIX] Migrate TransactionDao calls to TransactionRepository part - 1 * [FIX] Migrate TransactionDao calls to TransactionRepository part - 2 * [FIX 3132] Migrate TransactionDao calls to TransactionRepository part - 3 * [FIX 3132] Migration of TransactionDao to TransactionRepository * [FIX 3132] Fixing Unit Cases * [FIX 3132] Changes for FakeRepo, simplify by deletion * [FIX 3132] Changes for FakeRepo, simplify by deletion * [FIX 3132] Detekt Changes --- .../transaction/EditTransactionViewModel.kt | 16 ++- .../CustomerJourneyCardsProvider.kt | 6 +- .../importdata/csv/domain/CSVImporterV2.kt | 12 ++- .../loans/loandetails/LoanDetailsViewModel.kt | 13 ++- .../ivy/planned/edit/EditPlannedViewModel.kt | 8 +- .../ivy/transactions/TransactionsViewModel.kt | 6 +- .../backup/BackupDataUseCaseAndroidTest.kt | 6 +- .../com/ivy/data/backup/BackupDataUseCase.kt | 2 +- .../data/repository/TransactionRepository.kt | 50 +++++++++ .../impl/TransactionRepositoryImpl.kt | 102 +++++++++++++++++- .../ivy/data/backup/BackupDataUseCaseTest.kt | 5 +- .../impl/TransactionRepositoryImplTest.kt | 53 +++++++++ .../data/model/primitive/NonNegativeLong.kt | 21 ++++ .../legacy/datamodel/temp/TransactionExt.kt | 7 ++ .../domain/action/transaction/TrnByIdAct.kt | 15 ++- .../transaction/SaveTrnLocallyAct.kt | 8 +- .../logic/PlannedPaymentsGenerator.kt | 22 ++-- .../deprecated/logic/PlannedPaymentsLogic.kt | 25 +++-- .../deprecated/logic/WalletAccountLogic.kt | 52 ++++----- .../deprecated/logic/WalletCategoryLogic.kt | 62 ++++++----- .../deprecated/logic/csv/CSVImporter.kt | 12 ++- .../loantrasactions/LoanTransactionsCore.kt | 17 +-- 22 files changed, 401 insertions(+), 119 deletions(-) create mode 100644 shared/data/model/src/main/kotlin/com/ivy/data/model/primitive/NonNegativeLong.kt diff --git a/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewModel.kt b/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewModel.kt index b7db3dda8d..5e4f2b3626 100644 --- a/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewModel.kt +++ b/screen/edit-transaction/src/main/java/com/ivy/transaction/EditTransactionViewModel.kt @@ -14,20 +14,22 @@ import com.ivy.base.legacy.refreshWidget import com.ivy.base.model.TransactionType import com.ivy.data.db.dao.read.LoanDao import com.ivy.data.db.dao.read.SettingsDao -import com.ivy.data.db.dao.write.WriteTransactionDao import com.ivy.data.model.Category import com.ivy.data.model.CategoryId import com.ivy.data.model.Tag import com.ivy.data.model.TagId +import com.ivy.data.model.TransactionId import com.ivy.data.model.primitive.AssociationId import com.ivy.data.model.primitive.NotBlankTrimmedString import com.ivy.data.repository.CategoryRepository import com.ivy.data.repository.TagRepository +import com.ivy.data.repository.TransactionRepository import com.ivy.data.repository.mapper.TagMapper +import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.domain.features.Features import com.ivy.legacy.data.EditTransactionDisplayLoan import com.ivy.legacy.datamodel.Account -import com.ivy.legacy.datamodel.toEntity +import com.ivy.legacy.datamodel.temp.toDomain import com.ivy.legacy.domain.deprecated.logic.AccountCreator import com.ivy.legacy.utils.computationThread import com.ivy.legacy.utils.convertUTCToLocal @@ -100,7 +102,8 @@ class EditTransactionViewModel @Inject constructor( private val categoryRepository: CategoryRepository, private val trnByIdAct: TrnByIdAct, private val accountByIdAct: AccountByIdAct, - private val transactionWriter: WriteTransactionDao, + private val transactionRepo: TransactionRepository, + private val transactionMapper: TransactionMapper, private val tagRepository: TagRepository, private val tagMapper: TagMapper, private val features: Features @@ -595,7 +598,7 @@ class EditTransactionViewModel @Inject constructor( viewModelScope.launch { ioThread { loadedTransaction?.let { - transactionWriter.flagDeleted(it.id) + transactionRepo.deleteById(TransactionId(it.id)) } closeScreen() } @@ -706,7 +709,10 @@ class EditTransactionViewModel @Inject constructor( accountsChanged = false } - transactionWriter.save(loadedTransaction().toEntity()) + loadedTransaction().toDomain(transactionMapper)?.let { + transactionRepo.save(it) + } + refreshWidget(WalletBalanceWidgetReceiver::class.java) } diff --git a/screen/home/src/main/java/com/ivy/home/customerjourney/CustomerJourneyCardsProvider.kt b/screen/home/src/main/java/com/ivy/home/customerjourney/CustomerJourneyCardsProvider.kt index cb40fe7c05..9423453471 100644 --- a/screen/home/src/main/java/com/ivy/home/customerjourney/CustomerJourneyCardsProvider.kt +++ b/screen/home/src/main/java/com/ivy/home/customerjourney/CustomerJourneyCardsProvider.kt @@ -4,7 +4,7 @@ import com.ivy.base.legacy.SharedPrefs import com.ivy.base.legacy.stringRes import com.ivy.base.model.TransactionType import com.ivy.data.db.dao.read.PlannedPaymentRuleDao -import com.ivy.data.db.dao.read.TransactionDao +import com.ivy.data.repository.TransactionRepository import com.ivy.design.l0_system.Blue import com.ivy.design.l0_system.Blue3 import com.ivy.design.l0_system.Gradient @@ -25,14 +25,14 @@ import javax.inject.Inject @Deprecated("Legacy code") class CustomerJourneyCardsProvider @Inject constructor( - private val transactionDao: TransactionDao, + private val transactionRepository: TransactionRepository, private val plannedPaymentRuleDao: PlannedPaymentRuleDao, private val sharedPrefs: SharedPrefs, private val ivyContext: IvyWalletCtx ) { suspend fun loadCards(): List { - val trnCount = transactionDao.countHappenedTransactions() + val trnCount = transactionRepository.countHappenedTransactions().value val plannedPaymentsCount = plannedPaymentRuleDao.countPlannedPayments() return ACTIVE_CARDS diff --git a/screen/import-data/src/main/java/com/ivy/importdata/csv/domain/CSVImporterV2.kt b/screen/import-data/src/main/java/com/ivy/importdata/csv/domain/CSVImporterV2.kt index b71056e86b..72ad53e5c7 100644 --- a/screen/import-data/src/main/java/com/ivy/importdata/csv/domain/CSVImporterV2.kt +++ b/screen/import-data/src/main/java/com/ivy/importdata/csv/domain/CSVImporterV2.kt @@ -8,7 +8,6 @@ import com.ivy.data.backup.CSVRow import com.ivy.data.backup.ImportResult import com.ivy.data.db.dao.read.AccountDao import com.ivy.data.db.dao.read.SettingsDao -import com.ivy.data.db.dao.write.WriteTransactionDao import com.ivy.data.model.Category import com.ivy.data.model.CategoryId import com.ivy.data.model.primitive.ColorInt @@ -17,6 +16,8 @@ import com.ivy.data.model.primitive.NotBlankTrimmedString import com.ivy.data.repository.AccountRepository import com.ivy.data.repository.CategoryRepository import com.ivy.data.repository.CurrencyRepository +import com.ivy.data.repository.TransactionRepository +import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.design.IVY_COLOR_PICKER_COLORS_FREE import com.ivy.importdata.csv.ImportantFields import com.ivy.importdata.csv.OptionalFields @@ -38,7 +39,8 @@ import com.ivy.importdata.csv.CSVRow as CSVRowNew class CSVImporterV2 @Inject constructor( private val settingsDao: SettingsDao, - private val transactionWriter: WriteTransactionDao, + private val transactionRepository: TransactionRepository, + private val transactionMapper: TransactionMapper, private val accountDao: AccountDao, private val categoryRepository: CategoryRepository, private val currencyRepository: CurrencyRepository, @@ -108,7 +110,11 @@ class CSVImporterV2 @Inject constructor( 0.0 } onProgress(0.5 + progressPercent / 2) - transactionWriter.save(transaction.toEntity()) + with(transactionMapper) { + transaction.toEntity().toDomain().getOrNull()?.let { + transactionRepository.save(it) + } + } } return ImportResult( diff --git a/screen/loans/src/main/java/com/ivy/loans/loandetails/LoanDetailsViewModel.kt b/screen/loans/src/main/java/com/ivy/loans/loandetails/LoanDetailsViewModel.kt index d45dd81a2d..f0cd4f45bf 100644 --- a/screen/loans/src/main/java/com/ivy/loans/loandetails/LoanDetailsViewModel.kt +++ b/screen/loans/src/main/java/com/ivy/loans/loandetails/LoanDetailsViewModel.kt @@ -10,11 +10,13 @@ import com.ivy.base.legacy.Transaction import com.ivy.base.model.LoanRecordType import com.ivy.data.db.dao.read.LoanRecordDao import com.ivy.data.db.dao.read.SettingsDao -import com.ivy.data.db.dao.read.TransactionDao +import com.ivy.data.repository.TransactionRepository +import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.frp.test.TestIdlingResource import com.ivy.legacy.datamodel.Account import com.ivy.legacy.datamodel.Loan import com.ivy.legacy.datamodel.LoanRecord +import com.ivy.legacy.datamodel.temp.toLegacy import com.ivy.legacy.datamodel.temp.toLegacyDomain import com.ivy.legacy.domain.deprecated.logic.AccountCreator import com.ivy.legacy.utils.computationThread @@ -52,7 +54,8 @@ class LoanDetailsViewModel @Inject constructor( private val loanCreator: LoanCreator, private val loanRecordCreator: LoanRecordCreator, private val settingsDao: SettingsDao, - private val transactionDao: TransactionDao, + private val transactionRepository: TransactionRepository, + private val transactionMapper: TransactionMapper, private val accountCreator: AccountCreator, private val loanTransactionsLogic: LoanTransactionsLogic, private val nav: Navigation, @@ -247,7 +250,7 @@ class LoanDetailsViewModel @Inject constructor( displayLoanRecords.value = ioThread { loanRecordDao.findAllByLoanId(loanId = loanId) }.map { val trans = ioThread { - transactionDao.findLoanRecordTransaction( + transactionRepository.findLoanRecordTransaction( it.id ) } @@ -307,7 +310,9 @@ class LoanDetailsViewModel @Inject constructor( } associatedTransaction = ioThread { - transactionDao.findLoanTransaction(loanId = loan.value!!.id)?.toLegacyDomain() + transactionRepository.findLoanTransaction(loanId = loan.value!!.id).let { + it?.toLegacy(transactionMapper) + } } associatedTransaction?.let { diff --git a/screen/planned-payments/src/main/java/com/ivy/planned/edit/EditPlannedViewModel.kt b/screen/planned-payments/src/main/java/com/ivy/planned/edit/EditPlannedViewModel.kt index eb7ea015d0..c79c7a3b43 100644 --- a/screen/planned-payments/src/main/java/com/ivy/planned/edit/EditPlannedViewModel.kt +++ b/screen/planned-payments/src/main/java/com/ivy/planned/edit/EditPlannedViewModel.kt @@ -10,11 +10,11 @@ import com.ivy.data.db.dao.read.AccountDao import com.ivy.data.db.dao.read.PlannedPaymentRuleDao import com.ivy.data.db.dao.read.SettingsDao import com.ivy.data.db.dao.write.WritePlannedPaymentRuleDao -import com.ivy.data.db.dao.write.WriteTransactionDao import com.ivy.data.model.Category import com.ivy.data.model.CategoryId import com.ivy.data.model.IntervalType import com.ivy.data.repository.CategoryRepository +import com.ivy.data.repository.TransactionRepository import com.ivy.legacy.datamodel.Account import com.ivy.legacy.datamodel.PlannedPaymentRule import com.ivy.legacy.datamodel.temp.toLegacyDomain @@ -52,7 +52,7 @@ class EditPlannedViewModel @Inject constructor( private val accountCreator: AccountCreator, private val accountsAct: AccountsAct, private val plannedPaymentRuleWriter: WritePlannedPaymentRuleDao, - private val transactionWriter: WriteTransactionDao, + private val transactionRepository: TransactionRepository ) : ComposeViewModel() { private val transactionType = mutableStateOf(TransactionType.INCOME) @@ -466,8 +466,8 @@ class EditPlannedViewModel @Inject constructor( deleteTransactionModalVisible.value = false ioThread { loadedRule?.let { - plannedPaymentRuleWriter.flagDeleted(it.id) - transactionWriter.flagDeletedByRecurringRuleIdAndNoDateTime( + plannedPaymentRuleWriter.deleteById(it.id) + transactionRepository.flagDeletedByRecurringRuleIdAndNoDateTime( recurringRuleId = it.id ) } diff --git a/screen/transactions/src/main/java/com/ivy/transactions/TransactionsViewModel.kt b/screen/transactions/src/main/java/com/ivy/transactions/TransactionsViewModel.kt index 0808c60c71..12812e1b5b 100644 --- a/screen/transactions/src/main/java/com/ivy/transactions/TransactionsViewModel.kt +++ b/screen/transactions/src/main/java/com/ivy/transactions/TransactionsViewModel.kt @@ -15,7 +15,6 @@ import com.ivy.base.model.TransactionType import com.ivy.data.db.dao.read.AccountDao import com.ivy.data.db.dao.write.WriteCategoryDao import com.ivy.data.db.dao.write.WritePlannedPaymentRuleDao -import com.ivy.data.db.dao.write.WriteTransactionDao import com.ivy.data.model.AccountId import com.ivy.data.model.Category import com.ivy.data.model.CategoryId @@ -25,6 +24,7 @@ import com.ivy.data.model.primitive.NotBlankTrimmedString import com.ivy.data.repository.AccountRepository import com.ivy.data.repository.CategoryRepository import com.ivy.data.repository.TagRepository +import com.ivy.data.repository.TransactionRepository import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.design.l0_system.RedLight import com.ivy.frp.then @@ -89,7 +89,7 @@ class TransactionsViewModel @Inject constructor( private val calcAccIncomeExpenseAct: CalcAccIncomeExpenseAct, private val calcTrnsIncomeExpenseAct: LegacyCalcTrnsIncomeExpenseAct, private val exchangeAct: ExchangeAct, - private val transactionWriter: WriteTransactionDao, + private val transactionRepository: TransactionRepository, private val categoryWriter: WriteCategoryDao, private val plannedPaymentRuleWriter: WritePlannedPaymentRuleDao, private val transactionMapper: TransactionMapper, @@ -728,7 +728,7 @@ class TransactionsViewModel @Inject constructor( private suspend fun deleteAccount(accountId: UUID) { ioThread { - transactionWriter.flagDeletedByAccountId(accountId = accountId) + transactionRepository.flagDeletedByAccountId(accountId = accountId) plannedPaymentRuleWriter.flagDeletedByAccountId(accountId = accountId) accountRepository.deleteById(AccountId(accountId)) diff --git a/shared/data/core/src/androidTest/java/com/ivy/data/backup/BackupDataUseCaseAndroidTest.kt b/shared/data/core/src/androidTest/java/com/ivy/data/backup/BackupDataUseCaseAndroidTest.kt index a61bf7a0b9..38c828596b 100644 --- a/shared/data/core/src/androidTest/java/com/ivy/data/backup/BackupDataUseCaseAndroidTest.kt +++ b/shared/data/core/src/androidTest/java/com/ivy/data/backup/BackupDataUseCaseAndroidTest.kt @@ -7,15 +7,15 @@ import androidx.room.Room import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry +import com.ivy.base.TestDispatchersProvider import com.ivy.base.di.KotlinxSerializationModule import com.ivy.base.legacy.SharedPrefs +import com.ivy.data.DataObserver import com.ivy.data.db.IvyRoomDatabase import com.ivy.data.file.FileSystem import com.ivy.data.repository.fake.FakeAccountRepository import com.ivy.data.repository.fake.FakeCurrencyRepository import com.ivy.data.repository.mapper.AccountMapper -import com.ivy.base.TestDispatchersProvider -import com.ivy.data.DataObserver import io.kotest.matchers.collections.shouldBeEmpty import io.kotest.matchers.ints.shouldBeGreaterThan import kotlinx.coroutines.runBlocking @@ -45,6 +45,7 @@ class BackupDataUseCaseAndroidTest { plannedPaymentRuleDao = db.plannedPaymentRuleDao, settingsDao = db.settingsDao, transactionDao = db.transactionDao, + transactionWriter = db.writeTransactionDao, sharedPrefs = SharedPrefs(appContext), accountRepository = FakeAccountRepository( accountDao = db.accountDao, @@ -59,7 +60,6 @@ class BackupDataUseCaseAndroidTest { ) ), categoryWriter = db.writeCategoryDao, - transactionWriter = db.writeTransactionDao, settingsWriter = db.writeSettingsDao, budgetWriter = db.writeBudgetDao, loanWriter = db.writeLoanDao, diff --git a/shared/data/core/src/main/java/com/ivy/data/backup/BackupDataUseCase.kt b/shared/data/core/src/main/java/com/ivy/data/backup/BackupDataUseCase.kt index d251db8ec6..56fea69387 100644 --- a/shared/data/core/src/main/java/com/ivy/data/backup/BackupDataUseCase.kt +++ b/shared/data/core/src/main/java/com/ivy/data/backup/BackupDataUseCase.kt @@ -48,11 +48,11 @@ class BackupDataUseCase @Inject constructor( private val plannedPaymentRuleDao: PlannedPaymentRuleDao, private val settingsDao: SettingsDao, private val transactionDao: TransactionDao, + private val transactionWriter: WriteTransactionDao, private val sharedPrefs: SharedPrefs, private val accountRepository: AccountRepository, private val accountMapper: AccountMapper, private val categoryWriter: WriteCategoryDao, - private val transactionWriter: WriteTransactionDao, private val settingsWriter: WriteSettingsDao, private val budgetWriter: WriteBudgetDao, private val loanWriter: WriteLoanDao, diff --git a/shared/data/core/src/main/java/com/ivy/data/repository/TransactionRepository.kt b/shared/data/core/src/main/java/com/ivy/data/repository/TransactionRepository.kt index e6cfce7510..4f993ba294 100644 --- a/shared/data/core/src/main/java/com/ivy/data/repository/TransactionRepository.kt +++ b/shared/data/core/src/main/java/com/ivy/data/repository/TransactionRepository.kt @@ -1,5 +1,6 @@ package com.ivy.data.repository +import com.ivy.base.model.TransactionType import com.ivy.data.model.AccountId import com.ivy.data.model.CategoryId import com.ivy.data.model.Expense @@ -7,7 +8,9 @@ import com.ivy.data.model.Income import com.ivy.data.model.Transaction import com.ivy.data.model.TransactionId import com.ivy.data.model.Transfer +import com.ivy.data.model.primitive.NonNegativeLong import java.time.LocalDateTime +import java.util.UUID interface TransactionRepository { suspend fun findById(id: TransactionId): Transaction? @@ -23,40 +26,87 @@ interface TransactionRepository { startDate: LocalDateTime, endDate: LocalDateTime ): List + suspend fun findAllByAccountAndBetween( accountId: AccountId, startDate: LocalDateTime, endDate: LocalDateTime ): List + suspend fun findAllToAccountAndBetween( toAccountId: AccountId, startDate: LocalDateTime, endDate: LocalDateTime ): List + suspend fun findAllDueToBetween( startDate: LocalDateTime, endDate: LocalDateTime ): List + suspend fun findAllDueToBetweenByCategory( startDate: LocalDateTime, endDate: LocalDateTime, categoryId: CategoryId ): List + suspend fun findAllDueToBetweenByCategoryUnspecified( startDate: LocalDateTime, endDate: LocalDateTime, ): List + suspend fun findAllDueToBetweenByAccount( startDate: LocalDateTime, endDate: LocalDateTime, accountId: AccountId ): List + suspend fun findAllByCategoryAndTypeAndBetween( + categoryId: UUID, + type: TransactionType, + startDate: LocalDateTime, + endDate: LocalDateTime + ): List + + suspend fun findAllUnspecifiedAndTypeAndBetween( + type: TransactionType, + startDate: LocalDateTime, + endDate: LocalDateTime + ): List + + suspend fun findAllUnspecifiedAndBetween( + startDate: LocalDateTime, + endDate: LocalDateTime + ): List + + suspend fun findAllByCategoryAndBetween( + categoryId: UUID, + startDate: LocalDateTime, + endDate: LocalDateTime + ): List + + suspend fun findAllByRecurringRuleId(recurringRuleId: UUID): List + suspend fun flagDeletedByAccountId(accountId: UUID) + suspend fun save(value: Transaction) suspend fun saveMany(value: List) suspend fun flagDeleted(id: TransactionId) + suspend fun flagDeletedByRecurringRuleIdAndNoDateTime(recurringRuleId: UUID) suspend fun deleteById(id: TransactionId) suspend fun deleteAllByAccountId(accountId: AccountId) suspend fun deleteAll() + + suspend fun countHappenedTransactions(): NonNegativeLong + suspend fun findLoanTransaction( + loanId: UUID + ): Transaction? + + suspend fun findLoanRecordTransaction( + loanRecordId: UUID + ): Transaction? + + suspend fun findAllByLoanId( + loanId: UUID + ): List } \ No newline at end of file diff --git a/shared/data/core/src/main/java/com/ivy/data/repository/impl/TransactionRepositoryImpl.kt b/shared/data/core/src/main/java/com/ivy/data/repository/impl/TransactionRepositoryImpl.kt index 7e423696f6..3be372558f 100644 --- a/shared/data/core/src/main/java/com/ivy/data/repository/impl/TransactionRepositoryImpl.kt +++ b/shared/data/core/src/main/java/com/ivy/data/repository/impl/TransactionRepositoryImpl.kt @@ -9,11 +9,13 @@ import com.ivy.data.model.AccountId import com.ivy.data.model.CategoryId import com.ivy.data.model.Expense import com.ivy.data.model.Income +import com.ivy.data.model.TagId import com.ivy.data.model.Transaction import com.ivy.data.model.TransactionId import com.ivy.data.model.Transfer import com.ivy.data.model.primitive.AssociationId -import com.ivy.data.model.TagId +import com.ivy.data.model.primitive.NonNegativeLong +import com.ivy.data.model.primitive.toNonNegative import com.ivy.data.repository.TagRepository import com.ivy.data.repository.TransactionRepository import com.ivy.data.repository.mapper.TransactionMapper @@ -173,6 +175,74 @@ class TransactionRepositoryImpl @Inject constructor( } ) + override suspend fun findAllByCategoryAndTypeAndBetween( + categoryId: UUID, + type: TransactionType, + startDate: LocalDateTime, + endDate: LocalDateTime + ): List = retrieveTrns( + dbCall = { + transactionDao.findAllByCategoryAndTypeAndBetween( + categoryId = categoryId, + type = type, + startDate = startDate, + endDate = endDate + ) + } + ) + + override suspend fun findAllUnspecifiedAndTypeAndBetween( + type: TransactionType, + startDate: LocalDateTime, + endDate: LocalDateTime + ): List = retrieveTrns( + dbCall = { + transactionDao.findAllUnspecifiedAndTypeAndBetween( + type = type, + startDate = startDate, + endDate = endDate + ) + } + ) + + override suspend fun findAllUnspecifiedAndBetween( + startDate: LocalDateTime, + endDate: LocalDateTime + ): List = retrieveTrns( + dbCall = { + transactionDao.findAllUnspecifiedAndBetween( + startDate = startDate, + endDate = endDate + ) + } + ) + + override suspend fun findAllByCategoryAndBetween( + categoryId: UUID, + startDate: LocalDateTime, + endDate: LocalDateTime + ): List = retrieveTrns( + dbCall = { + transactionDao.findAllByCategoryAndBetween( + categoryId = categoryId, + startDate = startDate, + endDate = endDate + ) + } + ) + + override suspend fun findAllByRecurringRuleId(recurringRuleId: UUID): List = retrieveTrns( + dbCall = { + transactionDao.findAllByRecurringRuleId(recurringRuleId) + } + ) + + override suspend fun flagDeletedByAccountId(accountId: UUID) { + withContext(dispatchersProvider.io) { + writeTransactionDao.flagDeletedByAccountId(accountId) + } + } + override suspend fun findById( id: TransactionId ): Transaction? = withContext(dispatchersProvider.io) { @@ -217,6 +287,12 @@ class TransactionRepositoryImpl @Inject constructor( } } + override suspend fun flagDeletedByRecurringRuleIdAndNoDateTime(recurringRuleId: UUID) { + withContext(dispatchersProvider.io) { + writeTransactionDao.flagDeletedByRecurringRuleIdAndNoDateTime(recurringRuleId) + } + } + override suspend fun deleteById(id: TransactionId) { withContext(dispatchersProvider.io) { writeTransactionDao.deleteById(id.value) @@ -235,6 +311,30 @@ class TransactionRepositoryImpl @Inject constructor( } } + override suspend fun countHappenedTransactions(): NonNegativeLong = withContext(dispatchersProvider.io) { + transactionDao.countHappenedTransactions().toNonNegative() + } + + override suspend fun findLoanTransaction(loanId: UUID): Transaction? = + withContext(dispatchersProvider.io) { + transactionDao.findLoanTransaction(loanId)?.let { + with(mapper) { it.toDomain() }.getOrNull() + } + } + + override suspend fun findLoanRecordTransaction(loanRecordId: UUID): Transaction? = + withContext(dispatchersProvider.io) { + transactionDao.findLoanRecordTransaction(loanRecordId)?.let { + with(mapper) { it.toDomain() }.getOrNull() + } + } + + override suspend fun findAllByLoanId(loanId: UUID): List = retrieveTrns( + dbCall = { + transactionDao.findAllByLoanId(loanId) + } + ) + private suspend fun retrieveTrns( dbCall: suspend () -> List, retrieveTags: suspend (TransactionEntity) -> List = { emptyList() }, diff --git a/shared/data/core/src/test/java/com/ivy/data/backup/BackupDataUseCaseTest.kt b/shared/data/core/src/test/java/com/ivy/data/backup/BackupDataUseCaseTest.kt index 5cb73dba15..9ab5f19c5a 100644 --- a/shared/data/core/src/test/java/com/ivy/data/backup/BackupDataUseCaseTest.kt +++ b/shared/data/core/src/test/java/com/ivy/data/backup/BackupDataUseCaseTest.kt @@ -50,10 +50,10 @@ class BackupDataUseCaseTest { loanRecordDao = loanRecordDao, loanDao = loanDao, plannedPaymentRuleDao = plannedPaymentDao, - settingsDao = settingsDao, transactionDao = transactionDao, - categoryWriter = categoryDao, transactionWriter = transactionDao, + settingsDao = settingsDao, + categoryWriter = categoryDao, settingsWriter = settingsDao, budgetWriter = budgetDao, loanWriter = loanDao, @@ -90,7 +90,6 @@ class BackupDataUseCaseTest { // when val freshBackupUseCase = newBackupDataUseCase() val reImportedDataRes = freshBackupUseCase.importJson(exportedJson, onProgress = {}) - // then reImportedDataRes shouldBe importedDataRes diff --git a/shared/data/core/src/test/java/com/ivy/data/repository/impl/TransactionRepositoryImplTest.kt b/shared/data/core/src/test/java/com/ivy/data/repository/impl/TransactionRepositoryImplTest.kt index c59649dda6..66c30e6e1d 100644 --- a/shared/data/core/src/test/java/com/ivy/data/repository/impl/TransactionRepositoryImplTest.kt +++ b/shared/data/core/src/test/java/com/ivy/data/repository/impl/TransactionRepositoryImplTest.kt @@ -23,6 +23,7 @@ import com.ivy.data.repository.TagRepository import com.ivy.data.repository.TransactionRepository import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.data.validTransactionEntity +import io.kotest.matchers.longs.shouldBeGreaterThanOrEqual import io.kotest.matchers.shouldBe import io.kotest.property.Arb import io.kotest.property.arbitrary.arbitrary @@ -348,6 +349,33 @@ class TransactionRepositoryImplTest { ) } + @Test + fun findAllByCategoryAndTypeAndBetween() { + val categoryId = ModelFixtures.CategoryId + val trnType = TransactionType.EXPENSE + val startDate = Arb.localDateTime().next() + val endDate = Arb.localDateTime().next() + + transactionsTestCase( + daoMethod = { + transactionDao.findAllByCategoryAndTypeAndBetween( + categoryId = categoryId.value, + type = trnType, + startDate = startDate, + endDate = endDate, + ) + }, + repoMethod = { + repository.findAllByCategoryAndTypeAndBetween( + categoryId = categoryId.value, + type = trnType, + startDate = startDate, + endDate = endDate, + ) + } + ) + } + @Test fun save() = runTest { // given @@ -377,6 +405,23 @@ class TransactionRepositoryImplTest { savedTrns.toSet() shouldBe setOf(trn1, trn2) } + @Test + fun flagDeletedByAccountId() = runTest { + val accountId = ModelFixtures.AccountId + // given + repository = newRepository(fakeDao = FakeTransactionDao()) + val trn = mockkFakeTrnMapping(account = accountId) + repository.save(trn) + + // when + repository.flagDeletedByAccountId(accountId.value) + + // then + repository.findAllIncomeByAccount(accountId) shouldBe emptyList() + repository.findAllExpenseByAccount(accountId) shouldBe emptyList() + repository.findAllTransferByAccount(accountId) shouldBe emptyList() + } + @Test fun flagDeleted() = runTest { // given @@ -423,6 +468,14 @@ class TransactionRepositoryImplTest { repository.findAll() shouldBe listOf(trnAcc2) } + @Test + fun countNumberOfTransactions() = runTest { + // given + repository = newRepository(fakeDao = FakeTransactionDao()) + + repository.countHappenedTransactions().value shouldBeGreaterThanOrEqual 0L + } + @Test fun deleteAll() = runTest { // given diff --git a/shared/data/model/src/main/kotlin/com/ivy/data/model/primitive/NonNegativeLong.kt b/shared/data/model/src/main/kotlin/com/ivy/data/model/primitive/NonNegativeLong.kt new file mode 100644 index 0000000000..b75dbf90ca --- /dev/null +++ b/shared/data/model/src/main/kotlin/com/ivy/data/model/primitive/NonNegativeLong.kt @@ -0,0 +1,21 @@ +package com.ivy.data.model.primitive + +import arrow.core.raise.Raise +import arrow.core.raise.ensure +import com.ivy.data.model.exact.Exact + +@JvmInline +value class NonNegativeLong private constructor(val value: Long) { + companion object : Exact { + override val exactName = "NonNegativeLong" + + override fun Raise.spec(raw: Long): NonNegativeLong { + ensure(raw >= 0) { "$raw is not >= 0" } + return NonNegativeLong(raw) + } + + val Zero = NonNegativeLong.unsafe(0L) + } +} + +fun Long.toNonNegative(): NonNegativeLong = NonNegativeLong.unsafe(this) diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/datamodel/temp/TransactionExt.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/datamodel/temp/TransactionExt.kt index edc3743549..b04ef7bdfe 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/datamodel/temp/TransactionExt.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/datamodel/temp/TransactionExt.kt @@ -6,6 +6,7 @@ import com.ivy.data.db.entity.TransactionEntity import com.ivy.data.model.Tag import com.ivy.data.model.Transaction import com.ivy.data.repository.mapper.TransactionMapper +import com.ivy.legacy.datamodel.toEntity import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toImmutableList @@ -14,6 +15,12 @@ fun Transaction.toLegacy(mapper: TransactionMapper): LegacyTransaction { return with(mapper) { toEntity().toLegacyDomain() } } +suspend fun LegacyTransaction.toDomain(mapper: TransactionMapper): Transaction? { + return with(mapper) { + toEntity().toDomain().getOrNull() + } +} + fun TransactionEntity.toLegacyDomain( tags: ImmutableList = persistentListOf() ): LegacyTransaction = LegacyTransaction( diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/action/transaction/TrnByIdAct.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/action/transaction/TrnByIdAct.kt index 7e7e40f5ba..4a45bbb443 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/action/transaction/TrnByIdAct.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/action/transaction/TrnByIdAct.kt @@ -1,19 +1,24 @@ package com.ivy.wallet.domain.action.transaction import com.ivy.base.legacy.Transaction -import com.ivy.data.db.dao.read.TransactionDao +import com.ivy.data.model.TransactionId +import com.ivy.data.repository.TransactionRepository +import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.frp.action.FPAction import com.ivy.frp.then -import com.ivy.legacy.datamodel.temp.toLegacyDomain +import com.ivy.legacy.datamodel.temp.toLegacy import java.util.UUID import javax.inject.Inject class TrnByIdAct @Inject constructor( - private val transactionDao: TransactionDao + private val transactionRepo: TransactionRepository, + private val mapper: TransactionMapper ) : FPAction() { override suspend fun UUID.compose(): suspend () -> Transaction? = suspend { this // transactionId - } then transactionDao::findById then { - it?.toLegacyDomain() + } then { + transactionRepo.findById(TransactionId(it)) + } then { + it?.toLegacy(mapper) } } diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/action/viewmodel/transaction/SaveTrnLocallyAct.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/action/viewmodel/transaction/SaveTrnLocallyAct.kt index 9923eaecc8..9ffd6bbefb 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/action/viewmodel/transaction/SaveTrnLocallyAct.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/action/viewmodel/transaction/SaveTrnLocallyAct.kt @@ -1,18 +1,20 @@ package com.ivy.wallet.domain.action.viewmodel.transaction import com.ivy.base.legacy.Transaction -import com.ivy.data.db.dao.write.WriteTransactionDao +import com.ivy.data.repository.TransactionRepository import com.ivy.frp.action.FPAction import com.ivy.frp.then import com.ivy.legacy.datamodel.toEntity import javax.inject.Inject class SaveTrnLocallyAct @Inject constructor( - private val writeTransactionDao: WriteTransactionDao, + private val transactionRepo: TransactionRepository, ) : FPAction() { override suspend fun Transaction.compose(): suspend () -> Unit = { this.copy( isSynced = false ).toEntity() - } then writeTransactionDao::save then {} + } then { + transactionRepo::save then {} + } } diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/PlannedPaymentsGenerator.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/PlannedPaymentsGenerator.kt index c565ce7cde..2932f68397 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/PlannedPaymentsGenerator.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/PlannedPaymentsGenerator.kt @@ -1,17 +1,17 @@ package com.ivy.wallet.domain.deprecated.logic import com.ivy.base.legacy.Transaction -import com.ivy.data.db.dao.read.TransactionDao -import com.ivy.data.db.dao.write.WriteTransactionDao +import com.ivy.data.repository.TransactionRepository +import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.legacy.datamodel.PlannedPaymentRule -import com.ivy.legacy.datamodel.toEntity +import com.ivy.legacy.datamodel.temp.toDomain import com.ivy.legacy.incrementDate import java.time.LocalDateTime import javax.inject.Inject class PlannedPaymentsGenerator @Inject constructor( - private val transactionDao: TransactionDao, - private val transactionWriter: WriteTransactionDao + private val transactionMapper: TransactionMapper, + private val transactionRepository: TransactionRepository ) { companion object { private const val GENERATED_INSTANCES_LIMIT = 72 @@ -19,7 +19,7 @@ class PlannedPaymentsGenerator @Inject constructor( suspend fun generate(rule: PlannedPaymentRule) { // delete all not happened transactions - transactionWriter.flagDeletedByRecurringRuleIdAndNoDateTime( + transactionRepository.flagDeletedByRecurringRuleIdAndNoDateTime( recurringRuleId = rule.id ) @@ -31,7 +31,7 @@ class PlannedPaymentsGenerator @Inject constructor( } private suspend fun generateOneTime(rule: PlannedPaymentRule) { - val trns = transactionDao.findAllByRecurringRuleId(recurringRuleId = rule.id) + val trns = transactionRepository.findAllByRecurringRuleId(recurringRuleId = rule.id) if (trns.isEmpty()) { generateTransaction(rule, rule.startDate!!) @@ -42,7 +42,7 @@ class PlannedPaymentsGenerator @Inject constructor( val startDate = rule.startDate!! val endDate = startDate.plusYears(3) - val trns = transactionDao.findAllByRecurringRuleId(recurringRuleId = rule.id) + val trns = transactionRepository.findAllByRecurringRuleId(recurringRuleId = rule.id) var trnsToSkip = trns.size var generatedTransactions = 0 @@ -74,7 +74,6 @@ class PlannedPaymentsGenerator @Inject constructor( } private suspend fun generateTransaction(rule: PlannedPaymentRule, dueDate: LocalDateTime) { - transactionWriter.save( Transaction( type = rule.type, accountId = rule.accountId, @@ -87,7 +86,8 @@ class PlannedPaymentsGenerator @Inject constructor( dateTime = null, toAccountId = null, isSynced = false - ).toEntity() - ) + ).toDomain(transactionMapper)?.let { + transactionRepository.save(it) + } } } diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/PlannedPaymentsLogic.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/PlannedPaymentsLogic.kt index fc8d928d5e..46e481a831 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/PlannedPaymentsLogic.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/PlannedPaymentsLogic.kt @@ -7,14 +7,15 @@ import com.ivy.data.db.dao.read.PlannedPaymentRuleDao import com.ivy.data.db.dao.read.SettingsDao import com.ivy.data.db.dao.read.TransactionDao import com.ivy.data.db.dao.write.WritePlannedPaymentRuleDao -import com.ivy.data.db.dao.write.WriteTransactionDao import com.ivy.data.model.IntervalType -import com.ivy.data.temp.migration.settleNow +import com.ivy.data.model.TransactionId import com.ivy.data.repository.TransactionRepository +import com.ivy.data.repository.mapper.TransactionMapper +import com.ivy.data.temp.migration.settleNow import com.ivy.legacy.datamodel.Account import com.ivy.legacy.datamodel.PlannedPaymentRule +import com.ivy.legacy.datamodel.temp.toDomain import com.ivy.legacy.datamodel.temp.toLegacyDomain -import com.ivy.legacy.datamodel.toEntity import com.ivy.legacy.utils.ioThread import com.ivy.legacy.utils.timeNowUTC import com.ivy.wallet.domain.deprecated.logic.currency.ExchangeRatesLogic @@ -28,7 +29,7 @@ class PlannedPaymentsLogic @Inject constructor( private val settingsDao: SettingsDao, private val exchangeRatesLogic: ExchangeRatesLogic, private val accountDao: AccountDao, - private val transactionWriter: WriteTransactionDao, + private val transactionMapper: TransactionMapper, private val plannedPaymentRuleWriter: WritePlannedPaymentRuleDao, private val transactionRepository: TransactionRepository ) { @@ -179,9 +180,11 @@ class PlannedPaymentsLogic @Inject constructor( ioThread { if (skipTransaction) { - transactionWriter.flagDeleted(paidTransaction.id) + transactionRepository.deleteById(TransactionId(paidTransaction.id)) } else { - transactionWriter.save(paidTransaction.toEntity()) + paidTransaction.toDomain(transactionMapper)?.let { + transactionRepository.save(it) + } } if (plannedPaymentRule != null && plannedPaymentRule.oneTime) { @@ -211,7 +214,7 @@ class PlannedPaymentsLogic @Inject constructor( ioThread { if (skipTransaction) { - transactionRepository.flagDeleted(paidTransaction.id) + transactionRepository.deleteById(paidTransaction.id) } else { transactionRepository.save(paidTransaction) } @@ -251,7 +254,7 @@ class PlannedPaymentsLogic @Inject constructor( ioThread { if (skipTransaction) { paidTransactions.forEach { paidTransaction -> - transactionRepository.flagDeleted(paidTransaction.id) + transactionRepository.deleteById(paidTransaction.id) } } else { paidTransactions.forEach { paidTransaction -> @@ -301,11 +304,13 @@ class PlannedPaymentsLogic @Inject constructor( ioThread { if (skipTransaction) { paidTransactions.forEach { paidTransaction -> - transactionWriter.flagDeleted(paidTransaction.id) + transactionRepository.deleteById(TransactionId(paidTransaction.id)) } } else { paidTransactions.forEach { paidTransaction -> - transactionWriter.save(paidTransaction.toEntity()) + paidTransaction.toDomain(transactionMapper)?.let { + transactionRepository.save(it) + } } } diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/WalletAccountLogic.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/WalletAccountLogic.kt index 1f9e88fa8b..09c49ad3a0 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/WalletAccountLogic.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/WalletAccountLogic.kt @@ -4,17 +4,17 @@ import arrow.core.getOrElse import com.ivy.base.legacy.SharedPrefs import com.ivy.base.legacy.Transaction import com.ivy.base.model.TransactionType -import com.ivy.data.db.dao.write.WriteTransactionDao import com.ivy.data.model.AccountId import com.ivy.data.model.Expense import com.ivy.data.model.Income import com.ivy.data.repository.CurrencyRepository -import com.ivy.data.temp.migration.getValue import com.ivy.data.repository.TransactionRepository +import com.ivy.data.repository.mapper.TransactionMapper +import com.ivy.data.temp.migration.getValue import com.ivy.legacy.data.model.filterOverdue import com.ivy.legacy.data.model.filterUpcoming import com.ivy.legacy.datamodel.Account -import com.ivy.legacy.datamodel.toEntity +import com.ivy.legacy.datamodel.temp.toDomain import com.ivy.legacy.utils.timeNowUTC import com.ivy.wallet.domain.action.viewmodel.account.AccountDataAct import com.ivy.wallet.domain.pure.data.ClosedTimeRange @@ -26,7 +26,7 @@ import kotlin.math.absoluteValue @Deprecated("Migrate to FP Style") class WalletAccountLogic @Inject constructor( private val transactionRepository: TransactionRepository, - private val transactionWriter: WriteTransactionDao, + private val transactionMapper: TransactionMapper, private val accountDataAct: AccountDataAct, private val sharedPrefs: SharedPrefs, private val currencyRepository: CurrencyRepository, @@ -49,32 +49,32 @@ class WalletAccountLogic @Inject constructor( when { finalDiff < 0 -> { // add income - transactionWriter.save( - Transaction( - type = TransactionType.INCOME, - title = adjustTransactionTitle, - amount = diff.absoluteValue.toBigDecimal(), - toAmount = diff.absoluteValue.toBigDecimal(), - dateTime = timeNowUTC(), - accountId = account.id, - isSynced = trnIsSyncedFlag - ).toEntity() - ) + Transaction( + type = TransactionType.INCOME, + title = adjustTransactionTitle, + amount = diff.absoluteValue.toBigDecimal(), + toAmount = diff.absoluteValue.toBigDecimal(), + dateTime = timeNowUTC(), + accountId = account.id, + isSynced = trnIsSyncedFlag + ).toDomain(transactionMapper)?.let { + transactionRepository.save(it) + } } finalDiff > 0 -> { // add expense - transactionWriter.save( - Transaction( - type = TransactionType.EXPENSE, - title = adjustTransactionTitle, - amount = diff.absoluteValue.toBigDecimal(), - toAmount = diff.absoluteValue.toBigDecimal(), - dateTime = timeNowUTC(), - accountId = account.id, - isSynced = trnIsSyncedFlag - ).toEntity() - ) + Transaction( + type = TransactionType.EXPENSE, + title = adjustTransactionTitle, + amount = diff.absoluteValue.toBigDecimal(), + toAmount = diff.absoluteValue.toBigDecimal(), + dateTime = timeNowUTC(), + accountId = account.id, + isSynced = trnIsSyncedFlag + ).toDomain(transactionMapper)?.let { + transactionRepository.save(it) + } } } } diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/WalletCategoryLogic.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/WalletCategoryLogic.kt index 6758669c26..b76c9ce0c3 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/WalletCategoryLogic.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/WalletCategoryLogic.kt @@ -5,14 +5,15 @@ import com.ivy.base.legacy.TransactionHistoryItem import com.ivy.base.model.TransactionType import com.ivy.data.db.dao.read.AccountDao import com.ivy.data.db.dao.read.SettingsDao -import com.ivy.data.db.dao.read.TransactionDao import com.ivy.data.model.Category import com.ivy.data.model.CategoryId import com.ivy.data.repository.TransactionRepository +import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.legacy.data.model.filterOverdue import com.ivy.legacy.data.model.filterOverdueLegacy import com.ivy.legacy.data.model.filterUpcoming import com.ivy.legacy.data.model.filterUpcomingLegacy +import com.ivy.legacy.datamodel.temp.toLegacy import com.ivy.legacy.datamodel.temp.toLegacyDomain import com.ivy.legacy.domain.pure.transaction.LegacyTrnDateDividers import com.ivy.wallet.domain.deprecated.logic.currency.ExchangeRatesLogic @@ -25,8 +26,8 @@ class WalletCategoryLogic @Inject constructor( private val accountDao: AccountDao, private val settingsDao: SettingsDao, private val exchangeRatesLogic: ExchangeRatesLogic, - private val transactionDao: TransactionDao, - private val transactionRepository: TransactionRepository + private val transactionRepository: TransactionRepository, + private val transactionMapper: TransactionMapper ) { suspend fun calculateCategoryBalance( @@ -64,13 +65,13 @@ class WalletCategoryLogic @Inject constructor( range: com.ivy.legacy.data.model.FromToTimeRange, accountFilterSet: Set = emptySet(), ): Double { - return transactionDao + return transactionRepository .findAllByCategoryAndTypeAndBetween( categoryId = category.id.value, type = TransactionType.INCOME, startDate = range.from(), endDate = range.to() - ).map { it.toLegacyDomain() } + ).map { it.toLegacy(transactionMapper) } .filter { accountFilterSet.isEmpty() || accountFilterSet.contains(it.accountId) } @@ -101,16 +102,19 @@ class WalletCategoryLogic @Inject constructor( range: com.ivy.legacy.data.model.FromToTimeRange, accountFilterSet: Set = emptySet(), ): Double { - return transactionDao + return transactionRepository .findAllByCategoryAndTypeAndBetween( categoryId = category.id.value, type = TransactionType.EXPENSE, startDate = range.from(), endDate = range.to() ) + .map { + it.toLegacy(transactionMapper) + } .filter { accountFilterSet.isEmpty() || accountFilterSet.contains(it.accountId) - }.map { it.toLegacyDomain() } + } .sumInBaseCurrency( exchangeRatesLogic = exchangeRatesLogic, settingsDao = settingsDao, @@ -138,12 +142,12 @@ class WalletCategoryLogic @Inject constructor( } suspend fun calculateUnspecifiedIncome(range: com.ivy.legacy.data.model.FromToTimeRange): Double { - return transactionDao + return transactionRepository .findAllUnspecifiedAndTypeAndBetween( type = TransactionType.INCOME, startDate = range.from(), endDate = range.to() - ).map { it.toLegacyDomain() } + ).map { it.toLegacy(transactionMapper) } .sumInBaseCurrency( exchangeRatesLogic = exchangeRatesLogic, settingsDao = settingsDao, @@ -152,12 +156,12 @@ class WalletCategoryLogic @Inject constructor( } suspend fun calculateUnspecifiedExpenses(range: com.ivy.legacy.data.model.FromToTimeRange): Double { - return transactionDao + return transactionRepository .findAllUnspecifiedAndTypeAndBetween( type = TransactionType.EXPENSE, startDate = range.from(), endDate = range.to() - ).map { it.toLegacyDomain() } + ).map { it.toLegacy(transactionMapper) } .sumInBaseCurrency( exchangeRatesLogic = exchangeRatesLogic, settingsDao = settingsDao, @@ -191,12 +195,12 @@ class WalletCategoryLogic @Inject constructor( transactions: List = emptyList() ): List { val trans = transactions.ifEmpty { - transactionDao + transactionRepository .findAllByCategoryAndBetween( categoryId = category.id.value, startDate = range.from(), endDate = range.to() - ).map { it.toLegacyDomain() } + ).map { it.toLegacy(transactionMapper) } } return trans.filter { @@ -206,11 +210,11 @@ class WalletCategoryLogic @Inject constructor( suspend fun historyUnspecified(range: com.ivy.legacy.data.model.FromToTimeRange): List { return with(LegacyTrnDateDividers) { - transactionDao + transactionRepository .findAllUnspecifiedAndBetween( startDate = range.from(), endDate = range.to() - ).map { it.toLegacyDomain() } + ).map { it.toLegacy(transactionMapper) } .withDateDividers( exchangeRatesLogic = exchangeRatesLogic, settingsDao = settingsDao, @@ -270,12 +274,14 @@ class WalletCategoryLogic @Inject constructor( category: Category, range: com.ivy.legacy.data.model.FromToTimeRange ): List { - return transactionDao.findAllDueToBetweenByCategory( - categoryId = category.id.value, + return transactionRepository.findAllDueToBetweenByCategory( + categoryId = CategoryId(category.id.value), startDate = range.upcomingFrom(), endDate = range.to() ) - .map { it.toLegacyDomain() } + .map { + it.toLegacy(transactionMapper) + } .filterUpcomingLegacy() } @@ -292,11 +298,13 @@ class WalletCategoryLogic @Inject constructor( @Deprecated("Uses legacy Transaction") suspend fun upcomingUnspecifiedLegacy(range: com.ivy.legacy.data.model.FromToTimeRange): List { - return transactionDao.findAllDueToBetweenByCategoryUnspecified( + return transactionRepository.findAllDueToBetweenByCategoryUnspecified( startDate = range.upcomingFrom(), endDate = range.to() ) - .map { it.toLegacyDomain() } + .map { + it.toLegacy(transactionMapper) + } .filterUpcomingLegacy() } @@ -360,12 +368,14 @@ class WalletCategoryLogic @Inject constructor( category: Category, range: com.ivy.legacy.data.model.FromToTimeRange ): List { - return transactionDao.findAllDueToBetweenByCategory( - categoryId = category.id.value, + return transactionRepository.findAllDueToBetweenByCategory( + categoryId = CategoryId(category.id.value), startDate = range.from(), endDate = range.overdueTo() ) - .map { it.toLegacyDomain() } + .map { + it.toLegacy(transactionMapper) + } .filterOverdueLegacy() } @@ -383,11 +393,13 @@ class WalletCategoryLogic @Inject constructor( @Deprecated("Uses legacy Transaction") suspend fun overdueUnspecifiedLegacy(range: com.ivy.legacy.data.model.FromToTimeRange): List { - return transactionDao.findAllDueToBetweenByCategoryUnspecified( + return transactionRepository.findAllDueToBetweenByCategoryUnspecified( startDate = range.from(), endDate = range.overdueTo() ) - .map { it.toLegacyDomain() } + .map { + it.toLegacy(transactionMapper) + } .filterOverdueLegacy() } diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/csv/CSVImporter.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/csv/CSVImporter.kt index 2f89473006..30e73b76e0 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/csv/CSVImporter.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/csv/CSVImporter.kt @@ -8,7 +8,6 @@ import com.ivy.data.backup.CSVRow import com.ivy.data.backup.ImportResult import com.ivy.data.db.dao.read.AccountDao import com.ivy.data.db.dao.read.SettingsDao -import com.ivy.data.db.dao.write.WriteTransactionDao import com.ivy.data.model.Category import com.ivy.data.model.CategoryId import com.ivy.data.model.primitive.ColorInt @@ -17,6 +16,8 @@ import com.ivy.data.model.primitive.NotBlankTrimmedString import com.ivy.data.repository.AccountRepository import com.ivy.data.repository.CategoryRepository import com.ivy.data.repository.CurrencyRepository +import com.ivy.data.repository.TransactionRepository +import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.design.IVY_COLOR_PICKER_COLORS_FREE import com.ivy.design.l0_system.Green import com.ivy.design.l0_system.IvyDark @@ -47,7 +48,8 @@ class CSVImporter @Inject constructor( private val settingsDao: SettingsDao, private val accountDao: AccountDao, private val categoryRepository: CategoryRepository, - private val writeTransactionDao: WriteTransactionDao, + private val transactionRepository: TransactionRepository, + private val transactionMapper: TransactionMapper, private val accountRepository: AccountRepository, private val currencyRepository: CurrencyRepository, ) { @@ -140,7 +142,11 @@ class CSVImporter @Inject constructor( 0.0 } onProgress(0.5 + progressPercent / 2) - writeTransactionDao.save(transaction.toEntity()) + with(transactionMapper) { + transaction.toEntity().toDomain().getOrNull()?.let { + transactionRepository.save(it) + } + } } return ImportResult( diff --git a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/loantrasactions/LoanTransactionsCore.kt b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/loantrasactions/LoanTransactionsCore.kt index e58795e118..51bde7e71a 100644 --- a/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/loantrasactions/LoanTransactionsCore.kt +++ b/temp/legacy-code/src/main/java/com/ivy/legacy/domain/deprecated/logic/loantrasactions/LoanTransactionsCore.kt @@ -11,21 +11,23 @@ import com.ivy.data.db.dao.read.SettingsDao import com.ivy.data.db.dao.read.TransactionDao import com.ivy.data.db.dao.write.WriteLoanDao import com.ivy.data.db.dao.write.WriteLoanRecordDao -import com.ivy.data.db.dao.write.WriteTransactionDao import com.ivy.data.model.Category import com.ivy.data.model.CategoryId import com.ivy.data.model.LoanType +import com.ivy.data.model.TransactionId import com.ivy.data.model.primitive.ColorInt import com.ivy.data.model.primitive.IconAsset import com.ivy.data.model.primitive.NotBlankTrimmedString import com.ivy.data.repository.CategoryRepository +import com.ivy.data.repository.TransactionRepository +import com.ivy.data.repository.mapper.TransactionMapper import com.ivy.design.IVY_COLOR_PICKER_COLORS_FREE import com.ivy.legacy.IvyWalletCtx import com.ivy.legacy.datamodel.Account import com.ivy.legacy.datamodel.Loan import com.ivy.legacy.datamodel.LoanRecord +import com.ivy.legacy.datamodel.temp.toDomain import com.ivy.legacy.datamodel.temp.toLegacyDomain -import com.ivy.legacy.datamodel.toEntity import com.ivy.legacy.utils.computationThread import com.ivy.legacy.utils.ioThread import com.ivy.legacy.utils.timeNowUTC @@ -48,7 +50,8 @@ class LoanTransactionsCore @Inject constructor( private val settingsDao: SettingsDao, private val accountsDao: AccountDao, private val exchangeRatesLogic: ExchangeRatesLogic, - private val writeTransactionDao: WriteTransactionDao, + private val transactionRepo: TransactionRepository, + private val transactionMapper: TransactionMapper, private val writeLoanRecordDao: WriteLoanRecordDao, private val writeLoanDao: WriteLoanDao, ) { @@ -197,14 +200,16 @@ class LoanTransactionsCore @Inject constructor( ) ioThread { - writeTransactionDao.save(modifiedTransaction.toEntity()) + modifiedTransaction.toDomain(transactionMapper)?.let { + transactionRepo.save(it) + } } } private suspend fun deleteTransaction(transaction: Transaction?) { ioThread { transaction?.let { - writeTransactionDao.flagDeleted(it.id) + transactionRepo.deleteById(TransactionId(it.id)) } } } @@ -276,7 +281,7 @@ class LoanTransactionsCore @Inject constructor( } reCalculateLoanAmount || loanRecordCurrenciesChanged || - oldLonRecordConvertedAmount == null -> { + oldLonRecordConvertedAmount == null -> { ioThread { exchangeRatesLogic.convertAmount( baseCurrency = baseCurrency(),