Adjust reference from transaction to recurring transaction

- Make the reference nullable
- Add the reference when a transaction gets created by a recurring transaction
- Add overload method createTransaction as the reference is optional
- Fix integration tests name so they get picked up by surefire
- Add a integration test for the recurringTransaction -> transaction creation
This commit is contained in:
2019-03-08 19:37:06 +01:00
parent b6318c65d4
commit 24e9dcda35
6 changed files with 91 additions and 7 deletions

View File

@@ -380,6 +380,7 @@ public class RecurringTransactionService {
recurringTransaction.getToAccount().getKey(),
recurringTransaction.getAmount(),
LocalDate.now().format(DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat())),
recurringTransaction.getDescription());
recurringTransaction.getDescription(),
recurringTransaction);
}
}

View File

@@ -4,6 +4,7 @@ import de.financer.ResponseReason;
import de.financer.config.FinancerConfig;
import de.financer.dba.TransactionRepository;
import de.financer.model.Account;
import de.financer.model.RecurringTransaction;
import de.financer.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -54,7 +55,16 @@ public class TransactionService {
}
@Transactional(propagation = Propagation.REQUIRED)
public ResponseReason createTransaction(String fromAccountKey, String toAccountKey, Long amount, String date, String description) {
public ResponseReason createTransaction(String fromAccountKey, String toAccountKey, Long amount, String date,
String description)
{
return this.createTransaction(fromAccountKey, toAccountKey, amount, date, description, null);
}
@Transactional(propagation = Propagation.REQUIRED)
public ResponseReason createTransaction(String fromAccountKey, String toAccountKey, Long amount, String date,
String description, RecurringTransaction recurringTransaction
) {
final Account fromAccount = this.accountService.getAccountByKey(fromAccountKey);
final Account toAccount = this.accountService.getAccountByKey(toAccountKey);
ResponseReason response = validateParameters(fromAccount, toAccount, amount, date);
@@ -65,7 +75,7 @@ public class TransactionService {
}
try {
final Transaction transaction = buildTransaction(fromAccount, toAccount, amount, description, date);
final Transaction transaction = buildTransaction(fromAccount, toAccount, amount, description, date, recurringTransaction);
fromAccount.setCurrentBalance(fromAccount.getCurrentBalance() + (this.ruleService
.getMultiplierFromAccount(fromAccount) * amount));
@@ -95,10 +105,14 @@ public class TransactionService {
* @param amount the transaction amount
* @param description the description of the transaction
* @param date the date of the transaction
* @param recurringTransaction the recurring transaction that caused the creation of this transaction, may be
* <code>null</code>
*
* @return the build {@link Transaction} instance
*/
private Transaction buildTransaction(Account fromAccount, Account toAccount, Long amount, String description, String date) {
private Transaction buildTransaction(Account fromAccount, Account toAccount, Long amount, String description,
String date, RecurringTransaction recurringTransaction
) {
final Transaction transaction = new Transaction();
transaction.setFromAccount(fromAccount);
@@ -106,6 +120,7 @@ public class TransactionService {
transaction.setAmount(amount);
transaction.setDescription(description);
transaction.setDate(LocalDate.parse(date, DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat())));
transaction.setRecurringTransaction(recurringTransaction);
return transaction;
}

View File

@@ -37,7 +37,7 @@ CREATE TABLE "transaction" ( --escape keyword "transaction"
"date" DATE NOT NULL, --escape keyword "date"
description VARCHAR(1000),
amount BIGINT NOT NULL,
recurring_transaction_id BIGINT NOT NULL,
recurring_transaction_id BIGINT,
CONSTRAINT fk_transaction_from_account FOREIGN KEY (from_account_id) REFERENCES account (id),
CONSTRAINT fk_transaction_to_account FOREIGN KEY (to_account_id) REFERENCES account (id),