Introduce 'deleted' flag to RecurringTransaction

Hard-deleting recurring transaction cannot be done reliable because a transaction may have a foreign key reference to a recurring transaction of this transaction has been created from a recurring transaction. Thus hard-deleting a recurring transaction may lead to data inconsistencies.
This commit is contained in:
2019-05-05 02:01:12 +02:00
parent 2d780267ef
commit 9b82084f21
10 changed files with 46 additions and 17 deletions

View File

@@ -12,5 +12,7 @@ import java.time.LocalDate;
public interface RecurringTransactionRepository extends CrudRepository<RecurringTransaction, Long> {
Iterable<RecurringTransaction> findRecurringTransactionsByFromAccountOrToAccount(Account fromAccount, Account toAccount);
Iterable<RecurringTransaction> findByLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(LocalDate lastOccurrence);
Iterable<RecurringTransaction> findByDeletedFalseAndLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(LocalDate lastOccurrence);
Iterable<RecurringTransaction> findByDeletedFalse();
}

View File

@@ -20,6 +20,7 @@ public class RecurringTransaction {
private LocalDate lastOccurrence;
@Enumerated(EnumType.STRING)
private HolidayWeekendType holidayWeekendType;
private boolean deleted;
public Long getId() {
return id;
@@ -88,4 +89,12 @@ public class RecurringTransaction {
public void setIntervalType(IntervalType intervalType) {
this.intervalType = intervalType;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}

View File

@@ -44,12 +44,12 @@ public class RecurringTransactionService {
private TransactionService transactionService;
public Iterable<RecurringTransaction> getAll() {
return this.recurringTransactionRepository.findAll();
return this.recurringTransactionRepository.findByDeletedFalse();
}
public Iterable<RecurringTransaction> getAllActive() {
return this.recurringTransactionRepository
.findByLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(LocalDate.now());
.findByDeletedFalseAndLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(LocalDate.now());
}
public Iterable<RecurringTransaction> getAllForAccount(String accountKey) {
@@ -79,7 +79,7 @@ public class RecurringTransactionService {
// Visible for unit tests
/* package */ Iterable<RecurringTransaction> getAllDueToday(LocalDate now) {
final Iterable<RecurringTransaction> allRecurringTransactions = this.recurringTransactionRepository
.findByLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(now);
.findByDeletedFalseAndLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(now);
//@formatter:off
return IterableUtils.toList(allRecurringTransactions).stream()
@@ -411,7 +411,11 @@ public class RecurringTransactionService {
}
try {
this.recurringTransactionRepository.deleteById(Long.valueOf(recurringTransactionId));
RecurringTransaction recurringTransaction = optionalRecurringTransaction.get();
recurringTransaction.setDeleted(true);
this.recurringTransactionRepository.save(recurringTransaction);
} catch (Exception e) {
LOGGER.error("Could not delete recurring transaction!", e);

View File

@@ -24,6 +24,7 @@ CREATE TABLE recurring_transaction (
first_occurrence DATE NOT NULL,
last_occurrence DATE,
holiday_weekend_type VARCHAR(255) NOT NULL,
deleted BOOLEAN DEFAULT FALSE NOT NULL,
CONSTRAINT fk_recurring_transaction_from_account FOREIGN KEY (from_account_id) REFERENCES account (id),
CONSTRAINT fk_recurring_transaction_to_account FOREIGN KEY (to_account_id) REFERENCES account (id)

View File

@@ -24,6 +24,7 @@ CREATE TABLE recurring_transaction (
first_occurrence DATE NOT NULL,
last_occurrence DATE,
holiday_weekend_type VARCHAR(255) NOT NULL,
deleted BOOLEAN DEFAULT 'TRUE' NOT NULL,
CONSTRAINT fk_recurring_transaction_from_account FOREIGN KEY (from_account_id) REFERENCES account (id),
CONSTRAINT fk_recurring_transaction_to_account FOREIGN KEY (to_account_id) REFERENCES account (id)