#23 Transaction import: new period

This commit is contained in:
2021-09-01 22:41:18 +02:00
parent 6a3359ea5c
commit 70218ad7dc
14 changed files with 235 additions and 90 deletions

View File

@@ -10,7 +10,9 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("periods")
@@ -26,7 +28,8 @@ public class PeriodController {
LOGGER.debug("/periods/closeCurrentExpensePeriod called");
}
final ResponseEntity responseEntity = this.periodService.closeCurrentExpensePeriod().toResponseEntity();
final ResponseEntity responseEntity = this.periodService.closeCurrentExpensePeriod(Optional.empty())
.toResponseEntity();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("/periods/closeCurrentExpensePeriod returns with %s", responseEntity));

View File

@@ -168,12 +168,12 @@ public class TransactionController {
}
@PostMapping(value = "/transactions/upload")
public ResponseEntity createTransaction(@RequestBody Collection<CreateUploadedTransactionsRequestDto> requestDtos) {
public ResponseEntity createTransaction(@RequestBody CreateUploadedTransactionsRequestDto requestDto) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("POST /transactions/upload got parameters: %s", requestDtos));
LOGGER.debug(String.format("POST /transactions/upload got parameters: %s", requestDto));
}
final ResponseReason responseReason = this.transactionService.createTransactions(requestDtos);
final ResponseReason responseReason = this.transactionService.createTransactions(requestDto);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("POST /transactions/upload returns with %s", responseReason.name()));

View File

@@ -45,14 +45,17 @@ public class PeriodService {
/**
* This method closes the current expense period and opens a new one.
*
* @param endDate an optional end date timestamp for the currently open expense period. If none is given the current
* timestamp is used
*
* @return {@link ResponseReason#OK} if the operation succeeded, {@link ResponseReason#UNKNOWN_ERROR} if an
* unexpected exception occurred.
*/
@Transactional(propagation = Propagation.REQUIRED)
public ResponseReason closeCurrentExpensePeriod() {
public ResponseReason closeCurrentExpensePeriod(Optional<LocalDateTime> endDate) {
final Period currentPeriod = this.getCurrentExpensePeriod();
final Period nextPeriod = new Period();
final LocalDateTime now = LocalDateTime.now();
final LocalDateTime now = endDate.orElse(LocalDateTime.now());
ResponseReason response;
currentPeriod.setEnd(now);

View File

@@ -22,6 +22,8 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
@@ -418,15 +420,16 @@ public class TransactionService {
/**
* This method creates multiple transactions in one batch based on the given parameters. In case of invalid
* parameters the creation aborts and the first issue found is returned.
* parameters the creation aborts and the first issue found is returned. If requested via the given parameter
* the method will also close and open expense periods.
*
* @param requestDtos parameters to create the transactions for
* @param requestDto parameter to create the transactions for
* @return {@link ResponseReason#CREATED CREATED} in case of success, another instance of {@link ResponseReason}
* otherwise. Never <code>null</code>
*/
@Transactional(propagation = Propagation.REQUIRED)
public ResponseReason createTransactions(Collection<CreateUploadedTransactionsRequestDto> requestDtos) {
requestDtos.stream().forEach(dto -> {
public ResponseReason createTransactions(CreateUploadedTransactionsRequestDto requestDto) {
requestDto.getEntries().stream().forEach(dto -> {
final ResponseReason responseReason;
if(StringUtils.isNotEmpty(dto.getRecurringTransactionId())) {
@@ -435,6 +438,15 @@ public class TransactionService {
.orElseThrow(() -> new FinancerServiceException(
ResponseReason.RECURRING_TRANSACTION_NOT_FOUND));
if(recurringTransaction.getId().equals(requestDto.getNewPeriodOnRecurringTransactionId())) {
this.periodService
.closeCurrentExpensePeriod(Optional.of(
LocalDateTime.of(
LocalDate.parse(dto.getDate(),
DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat())),
LocalTime.now())));
}
responseReason = this.createTransaction(dto.getFromAccountKey(),
dto.getToAccountKey(),
Long.valueOf(dto.getAmount()),