fix(#3): make ProcessDueRecurringExpenses atomic via withTransaction
Address review: insert + advance pair must run in one DB transaction to prevent duplicate expenses if the process is killed mid-loop.
This commit is contained in:
@@ -22,5 +22,5 @@ val domainModule = module {
|
||||
factory { GetRecurringExpenses(get()) }
|
||||
factory { UpsertRecurringExpense(get()) }
|
||||
factory { DeleteRecurringExpense(get()) }
|
||||
factory { ProcessDueRecurringExpenses(get(), get()) }
|
||||
factory { ProcessDueRecurringExpenses(get()) }
|
||||
}
|
||||
|
||||
+27
-25
@@ -1,36 +1,38 @@
|
||||
package dev.achmad.ledgerr.domain.recurring.interactor
|
||||
|
||||
import dev.achmad.ledgerr.data.local.dao.ExpenseDao
|
||||
import dev.achmad.ledgerr.data.local.dao.RecurringExpenseDao
|
||||
import androidx.room.withTransaction
|
||||
import dev.achmad.ledgerr.data.local.AppDatabase
|
||||
import dev.achmad.ledgerr.data.local.mapper.toEntity
|
||||
import dev.achmad.ledgerr.data.local.mapper.toModel
|
||||
import dev.achmad.ledgerr.domain.expense.model.Expense
|
||||
import java.time.LocalDate
|
||||
|
||||
class ProcessDueRecurringExpenses(
|
||||
private val recurringDao: RecurringExpenseDao,
|
||||
private val expenseDao: ExpenseDao,
|
||||
private val database: AppDatabase,
|
||||
) {
|
||||
suspend fun await(today: LocalDate = LocalDate.now()): List<Expense> {
|
||||
val dueTemplates = recurringDao.getDue(today.toEpochDay())
|
||||
if (dueTemplates.isEmpty()) return emptyList()
|
||||
val created = mutableListOf<Expense>()
|
||||
for (templateEntity in dueTemplates) {
|
||||
val template = templateEntity.toModel()
|
||||
val expense = Expense(
|
||||
amount = template.amount,
|
||||
categoryId = template.categoryId,
|
||||
date = template.nextDueDate,
|
||||
note = template.note,
|
||||
recurringExpenseId = template.id,
|
||||
)
|
||||
val newId = expenseDao.insert(expense.toEntity())
|
||||
created += expense.copy(id = newId)
|
||||
val advanced = template.copy(
|
||||
nextDueDate = template.interval.advance(template.nextDueDate),
|
||||
)
|
||||
recurringDao.update(advanced.toEntity())
|
||||
suspend fun await(today: LocalDate = LocalDate.now()): List<Expense> =
|
||||
database.withTransaction {
|
||||
val recurringDao = database.recurringExpenseDao()
|
||||
val expenseDao = database.expenseDao()
|
||||
val dueTemplates = recurringDao.getDue(today.toEpochDay())
|
||||
if (dueTemplates.isEmpty()) return@withTransaction emptyList()
|
||||
val created = mutableListOf<Expense>()
|
||||
for (templateEntity in dueTemplates) {
|
||||
val template = templateEntity.toModel()
|
||||
val expense = Expense(
|
||||
amount = template.amount,
|
||||
categoryId = template.categoryId,
|
||||
date = template.nextDueDate,
|
||||
note = template.note,
|
||||
recurringExpenseId = template.id,
|
||||
)
|
||||
val newId = expenseDao.insert(expense.toEntity())
|
||||
created += expense.copy(id = newId)
|
||||
val advanced = template.copy(
|
||||
nextDueDate = template.interval.advance(template.nextDueDate),
|
||||
)
|
||||
recurringDao.update(advanced.toEntity())
|
||||
}
|
||||
created
|
||||
}
|
||||
return created
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user