Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fun.trackmoney.transaction.controller;

import fun.trackmoney.transaction.dto.BillResponseDTO;
import fun.trackmoney.transaction.dto.CreateTransactionDTO;
import fun.trackmoney.transaction.dto.TransactionResponseDTO;
import fun.trackmoney.transaction.dto.TransactionUpdateDTO;
Expand Down Expand Up @@ -61,6 +62,12 @@ public ResponseEntity<ApiResponse<BigDecimal>> getExpense(@PathVariable Integer
new ApiResponse<>(true,"Get expense", transactionService.getExpense(id),null));
}

@GetMapping("/bill/{id}")
public ResponseEntity<ApiResponse<BillResponseDTO>> getBill(@PathVariable Integer id) {
return ResponseEntity.ok().body(
new ApiResponse<>(true,"Get expense", transactionService.getBill(id),null));
}

@PutMapping("/{id}")
public ResponseEntity<ApiResponse<TransactionResponseDTO>> updateTransaction(@PathVariable Integer id,
@RequestBody TransactionUpdateDTO dto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fun.trackmoney.transaction.dto;

import java.math.BigDecimal;

public record BillResponseDTO(BigDecimal bill,
BigDecimal totalUpcoming,
BigDecimal bueSoon) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

public interface TransactionRepository extends JpaRepository<TransactionEntity, Integer> {
@Query("SELECT a FROM TransactionEntity a WHERE a.account.accountId = :accountId")
List<TransactionEntity> findAllByUserEmail(@Param("accountId") Integer accountId);
List<TransactionEntity> findAllByAccountId(@Param("accountId") Integer accountId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fun.trackmoney.category.entity.CategoryEntity;
import fun.trackmoney.category.service.CategoryService;
import fun.trackmoney.enums.TransactionType;
import fun.trackmoney.transaction.dto.BillResponseDTO;
import fun.trackmoney.transaction.dto.CreateTransactionDTO;
import fun.trackmoney.transaction.dto.TransactionResponseDTO;
import fun.trackmoney.transaction.dto.TransactionUpdateDTO;
Expand All @@ -16,6 +17,7 @@
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -82,7 +84,7 @@ public void delete(Integer id) {
}

public BigDecimal getIncome(Integer accountId) {
return transactionRepository.findAllByUserEmail(accountId)
return transactionRepository.findAllByAccountId(accountId)
.stream()
.filter(t -> TransactionType.INCOME.equals(t.getTransactionType()))
.map(TransactionEntity::getAmount)
Expand All @@ -91,11 +93,49 @@ public BigDecimal getIncome(Integer accountId) {
}

public BigDecimal getExpense(Integer accountId) {
return transactionRepository.findAllByUserEmail(accountId)
return transactionRepository.findAllByAccountId(accountId)
.stream()
.filter(t -> TransactionType.EXPENSE.equals(t.getTransactionType()))
.map(TransactionEntity::getAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}

public BillResponseDTO getBill(Integer id) {
var result = transactionRepository.findAllByAccountId(id);

BigDecimal totalBillsBeforeToday = result.stream()
.filter(transaction ->
transaction.getCategory().getName().equalsIgnoreCase("bill") &&
transaction.getTransactionDate().toLocalDateTime().toLocalDate().isBefore(LocalDate.now())
)
.map(TransactionEntity::getAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);


BigDecimal totalUpComing = result.stream()
.filter(transaction ->
transaction.getCategory().getName().equalsIgnoreCase("bill") &&
transaction.getTransactionDate().toLocalDateTime().toLocalDate().isAfter(LocalDate.now())
)
.map(TransactionEntity::getAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);

BigDecimal totalBueSoon = result.stream()
.filter(transaction -> {
LocalDate date = transaction.getTransactionDate().toLocalDateTime().toLocalDate();
return transaction.getCategory().getName().equalsIgnoreCase("bill")
&& !date.isBefore(LocalDate.now())
&& !date.isAfter(LocalDate.now().plusDays(7));
})

.map(TransactionEntity::getAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);


return new BillResponseDTO(totalBillsBeforeToday, totalUpComing, totalBueSoon);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -151,7 +153,7 @@ void getIncome_shouldReturnTotalIncome() {
tr2.setTransactionType(TransactionType.INCOME);

var accountId = 1;
when(transactionRepository.findAllByUserEmail(accountId)).thenReturn(List.of(tr1, tr2));
when(transactionRepository.findAllByAccountId(accountId)).thenReturn(List.of(tr1, tr2));

var result = transactionService.getIncome(accountId);

Expand All @@ -169,10 +171,90 @@ void getExpense_shouldReturnTotalExpense() {
tr2.setTransactionType(TransactionType.EXPENSE);

var accountId = 1;
when(transactionRepository.findAllByUserEmail(accountId)).thenReturn(List.of(tr1, tr2));
when(transactionRepository.findAllByAccountId(accountId)).thenReturn(List.of(tr1, tr2));

var result = transactionService.getExpense(accountId);

assertEquals(totalIncome, result);
}

@Test
void getBill_shouldReturnBillResponseDTO() {
var accountId = 1;

var transaction1 = new TransactionEntity();
var transaction2 = new TransactionEntity();
var transaction3 = new TransactionEntity();

transaction1.setAmount(BigDecimal.valueOf(100));
transaction2.setAmount(BigDecimal.valueOf(100));
transaction3.setAmount(BigDecimal.valueOf(100));

LocalDateTime baseDate = LocalDate.now().atStartOfDay();


var day1 = Timestamp.valueOf(baseDate.minusDays(8));
transaction1.setTransactionDate(day1);

var day2 = Timestamp.valueOf(baseDate.plusDays(2));
transaction2.setTransactionDate(day2);

var day3 = Timestamp.valueOf(baseDate.plusDays(8));
transaction3.setTransactionDate(day3);

CategoryEntity category = new CategoryEntity(1, "bill", "anyColor");

transaction1.setCategory(category);
transaction2.setCategory(category);
transaction3.setCategory(category);

when(transactionRepository.findAllByAccountId(accountId)).thenReturn(List.of(transaction1, transaction2));

var result = transactionService.getBill(accountId);

assertNotNull(result);
assertEquals(BigDecimal.valueOf(100), result.totalUpcoming());
assertEquals(BigDecimal.valueOf(100), result.bill());
assertEquals(BigDecimal.valueOf(100), result.bueSoon());
}

@Test
void getBill_shouldReturnEmptyBillResponseDTOWhenNoBills() {
var accountId = 1;

var transaction1 = new TransactionEntity();
var transaction2 = new TransactionEntity();
var transaction3 = new TransactionEntity();

transaction1.setAmount(BigDecimal.valueOf(100));
transaction2.setAmount(BigDecimal.valueOf(100));
transaction3.setAmount(BigDecimal.valueOf(100));

LocalDateTime baseDate = LocalDate.now().atStartOfDay();


var day1 = Timestamp.valueOf(baseDate.minusDays(8));
transaction1.setTransactionDate(day1);

var day2 = Timestamp.valueOf(baseDate.plusDays(2));
transaction2.setTransactionDate(day2);

var day3 = Timestamp.valueOf(baseDate.plusDays(8));
transaction3.setTransactionDate(day3);

CategoryEntity category = new CategoryEntity(1, "otherCategory", "anyColor");

transaction1.setCategory(category);
transaction2.setCategory(category);
transaction3.setCategory(category);

when(transactionRepository.findAllByAccountId(accountId)).thenReturn(List.of(transaction1, transaction2));

var result = transactionService.getBill(accountId);

assertNotNull(result);
assertEquals(BigDecimal.valueOf(0), result.totalUpcoming());
assertEquals(BigDecimal.valueOf(0), result.bill());
assertEquals(BigDecimal.valueOf(0), result.bueSoon());
}
}