fix: things

This commit is contained in:
filippo-ferrari 2024-09-08 18:16:39 +02:00
parent 56138cd320
commit c243913b74
5 changed files with 76 additions and 8 deletions

View file

@ -13,14 +13,29 @@ import java.util.Set;
public interface ExpenseRepository extends JpaRepository<Expense, Long>, JpaSpecificationExecutor<Expense> {
// Find expenses where the creditor is a specific person
// Find expenses where the payer is a specific person
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId")
Set<Expense> findExpensesByPayer(@Param("personId") Long personId);
List<Expense> findExpensesByPayer(@Param("personId") Long personId);
// Find expenses where the debtor is a specific person
// Find expenses where the beneficiary is a specific person
@Query("SELECT e FROM Expense e WHERE e.beneficiary.id = :personId")
Set<Expense> findExpensesByBeneficiary(@Param("personId") Long personId);
List<Expense> findExpensesByBeneficiary(@Param("personId") Long personId);
// Find expenses where both payer and beneficiary are the same person
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId AND e.beneficiary.id = :personId")
List<Expense> findExpensesByPayerAndBeneficiary(@Param("personId") Long personId);
// Find expenses where the payer and beneficiary are the same person for a specific year
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId AND e.beneficiary.id = :personId AND YEAR(e.date) = :year")
List<Expense> findExpensesByPayerAndBeneficiaryAndYear(@Param("personId") Long personId, @Param("year") int year);
// Find expenses where the payer is a specific person for a specific year
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId AND YEAR(e.date) = :year")
List<Expense> findExpensesByPayerAndYear(@Param("personId") Long personId, @Param("year") int year);
// Find expenses where the beneficiary is a specific person for a specific year
@Query("SELECT e FROM Expense e WHERE e.beneficiary.id = :personId AND YEAR(e.date) = :year")
List<Expense> findExpensesByBeneficiaryAndYear(@Param("personId") Long personId, @Param("year") int year);
// Find all expenses for a given year
@Query("SELECT e FROM Expense e WHERE YEAR(e.date) = :year")
List<Expense> findAllByYear(@Param("year") int year);
@ -31,7 +46,7 @@ public interface ExpenseRepository extends JpaRepository<Expense, Long>, JpaSpec
// Find unpaid expenses where the debtor is a specific person
@Query("SELECT e FROM Expense e WHERE e.beneficiary.id = :personId AND e.isPaid = false")
Set<Expense> findUnapidExpensesByBeneficiary(@Param("personId") Long personId);
Set<Expense> findUnpaidExpensesByBeneficiary(@Param("personId") Long personId);
// Find expenses for a given year and filter by expense type and paid status
@Query("SELECT e FROM Expense e WHERE YEAR(e.date) = :year AND NOT (e.expenseType = :expenseType AND e.isPaid = true)")

View file

@ -15,6 +15,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Nonnull;
import java.time.LocalDateTime;
import java.time.Year;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@ -66,7 +68,7 @@ public class ExpenseService {
* @return the collection of unpaid expenses found
*/
public Collection<Expense> findUnpaidExpensesWhereBeneficiary(final Person person) {
return expenseRepository.findUnapidExpensesByBeneficiary(person.getId());
return expenseRepository.findUnpaidExpensesByBeneficiary(person.getId());
}
/**
@ -181,6 +183,26 @@ public class ExpenseService {
return (int) expenseRepository.count();
}
public List<Expense> fetchExpensesForDashboard(Person loggedInPerson, Year year) {
List<Expense> totalExpenses = new ArrayList<>();
final var yearValue = year.getValue();
// Fetch expenses where you are the payer and beneficiary (self-expenses) for the given year
List<Expense> bothExpenses = expenseRepository.findExpensesByPayerAndBeneficiaryAndYear(loggedInPerson.getId(), yearValue);
totalExpenses.addAll(bothExpenses); // Include these regardless of isPaid status
// Fetch expenses where you are the payer (you owe money), filtered by year
List<Expense> payerExpenses = expenseRepository.findExpensesByPayerAndYear(loggedInPerson.getId(), yearValue);
for (Expense expense : payerExpenses) {
if (!totalExpenses.contains(expense)) totalExpenses.add(expense);
}
// Fetch expenses where you are the beneficiary and not paid (amount owed to you), filtered by year
List<Expense> beneficiaryExpenses = expenseRepository.findExpensesByBeneficiaryAndYear(loggedInPerson.getId(), yearValue);
for (Expense expense : beneficiaryExpenses) {
if (Boolean.FALSE.equals(expense.getIsPaid()) && !totalExpenses.contains(expense)) totalExpenses.add(expense);
}
return totalExpenses;
}
// ================================
// Private methods
// ================================

View file

@ -10,17 +10,20 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@Service
public class PersonService {
private final UserService userService;
private final PersonRepository personRepository;
private final ExpenseService expenseService;
public PersonService(PersonRepository personRepository, ExpenseService expenseService) {
public PersonService(PersonRepository personRepository, ExpenseService expenseService, UserService userService) {
this.personRepository = personRepository;
this.expenseService = expenseService;
this.userService = userService;
}
/**
@ -79,6 +82,16 @@ public class PersonService {
return (int) this.personRepository.count();
}
/**
* Fetches the Person entity connected to the currently logged-in user.
*
* @return Person entity of the logged-in user, or null if not found.
*/
public Person getLoggedInPerson() {
final var user = userService.getLoggedInUser();
return Objects.requireNonNull(personRepository.findByUserId(user.getId()).orElse(null));
}
/**
* Updates a person in the repository.
* @param person the person to update

View file

@ -2,8 +2,11 @@ package com.application.munera.services;
import com.application.munera.data.User;
import com.application.munera.repositories.UserRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import static com.application.munera.SecurityUtils.getLoggedInUserDetails;
@Service
public class UserService {
@ -16,4 +19,18 @@ public class UserService {
public User findByUsername (String username) {
return this.userRepository.findByUsername(username);
}
/**
* Fetches the logged-in User entity.
* @return User entity of the logged-in user, or null if not found.
*/
public User getLoggedInUser() {
UserDetails userDetails = getLoggedInUserDetails();
if (userDetails != null) {
String username = userDetails.getUsername();
return userRepository.findByUsername(username);
}
return null;
}
}

View file

@ -105,7 +105,8 @@ public class DashboardView extends Div {
}
private String generateBarChartScript() {
List<Expense> expenses = expenseService.findExpensesByYearExcludingCreditPaid(Year.now().getValue());
final var loggedInPerson = this.personService.getLoggedInPerson();
List<Expense> expenses = expenseService.fetchExpensesForDashboard(loggedInPerson, Year.now());
// Prepare data for Highcharts
Map<String, Double> monthlyData = new LinkedHashMap<>();