Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
[FIX] Migrate TransactionDao calls to TransactionRepository part - 1 (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
rishi2062 authored Jun 10, 2024
1 parent cbee82f commit 61eecc1
Show file tree
Hide file tree
Showing 22 changed files with 401 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -595,7 +598,7 @@ class EditTransactionViewModel @Inject constructor(
viewModelScope.launch {
ioThread {
loadedTransaction?.let {
transactionWriter.flagDeleted(it.id)
transactionRepo.deleteById(TransactionId(it.id))
}
closeScreen()
}
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<CustomerJourneyCardModel> {
val trnCount = transactionDao.countHappenedTransactions()
val trnCount = transactionRepository.countHappenedTransactions().value
val plannedPaymentsCount = plannedPaymentRuleDao.countPlannedPayments()

return ACTIVE_CARDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<EditPlannedScreenState, EditPlannedScreenEvent>() {

private val transactionType = mutableStateOf(TransactionType.INCOME)
Expand Down Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -59,7 +60,6 @@ class BackupDataUseCaseAndroidTest {
)
),
categoryWriter = db.writeCategoryDao,
transactionWriter = db.writeTransactionDao,
settingsWriter = db.writeSettingsDao,
budgetWriter = db.writeBudgetDao,
loanWriter = db.writeLoanDao,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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
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?
Expand All @@ -23,40 +26,87 @@ interface TransactionRepository {
startDate: LocalDateTime,
endDate: LocalDateTime
): List<Transaction>

suspend fun findAllByAccountAndBetween(
accountId: AccountId,
startDate: LocalDateTime,
endDate: LocalDateTime
): List<Transaction>

suspend fun findAllToAccountAndBetween(
toAccountId: AccountId,
startDate: LocalDateTime,
endDate: LocalDateTime
): List<Transaction>

suspend fun findAllDueToBetween(
startDate: LocalDateTime,
endDate: LocalDateTime
): List<Transaction>

suspend fun findAllDueToBetweenByCategory(
startDate: LocalDateTime,
endDate: LocalDateTime,
categoryId: CategoryId
): List<Transaction>

suspend fun findAllDueToBetweenByCategoryUnspecified(
startDate: LocalDateTime,
endDate: LocalDateTime,
): List<Transaction>

suspend fun findAllDueToBetweenByAccount(
startDate: LocalDateTime,
endDate: LocalDateTime,
accountId: AccountId
): List<Transaction>

suspend fun findAllByCategoryAndTypeAndBetween(
categoryId: UUID,
type: TransactionType,
startDate: LocalDateTime,
endDate: LocalDateTime
): List<Transaction>

suspend fun findAllUnspecifiedAndTypeAndBetween(
type: TransactionType,
startDate: LocalDateTime,
endDate: LocalDateTime
): List<Transaction>

suspend fun findAllUnspecifiedAndBetween(
startDate: LocalDateTime,
endDate: LocalDateTime
): List<Transaction>

suspend fun findAllByCategoryAndBetween(
categoryId: UUID,
startDate: LocalDateTime,
endDate: LocalDateTime
): List<Transaction>

suspend fun findAllByRecurringRuleId(recurringRuleId: UUID): List<Transaction>
suspend fun flagDeletedByAccountId(accountId: UUID)

suspend fun save(value: Transaction)
suspend fun saveMany(value: List<Transaction>)

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<Transaction>
}
Loading

0 comments on commit 61eecc1

Please sign in to comment.