diff --git a/src/main/java/com/application/munera/repositories/ExpenseRepository.java b/src/main/java/com/application/munera/repositories/ExpenseRepository.java index 5da7e8d..508bd45 100644 --- a/src/main/java/com/application/munera/repositories/ExpenseRepository.java +++ b/src/main/java/com/application/munera/repositories/ExpenseRepository.java @@ -13,14 +13,29 @@ import java.util.Set; public interface ExpenseRepository extends JpaRepository, JpaSpecificationExecutor { - // 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 findExpensesByPayer(@Param("personId") Long personId); + List 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 findExpensesByBeneficiary(@Param("personId") Long personId); + List 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 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 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 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 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 findAllByYear(@Param("year") int year); @@ -31,7 +46,7 @@ public interface ExpenseRepository extends JpaRepository, 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 findUnapidExpensesByBeneficiary(@Param("personId") Long personId); + Set 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)") diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index 82a291c..9df0391 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -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 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 fetchExpensesForDashboard(Person loggedInPerson, Year year) { + List totalExpenses = new ArrayList<>(); + final var yearValue = year.getValue(); + + // Fetch expenses where you are the payer and beneficiary (self-expenses) for the given year + List 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 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 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 // ================================ diff --git a/src/main/java/com/application/munera/services/PersonService.java b/src/main/java/com/application/munera/services/PersonService.java index 198f95f..4e9a177 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -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 diff --git a/src/main/java/com/application/munera/services/UserService.java b/src/main/java/com/application/munera/services/UserService.java index 85f934c..b7433c2 100644 --- a/src/main/java/com/application/munera/services/UserService.java +++ b/src/main/java/com/application/munera/services/UserService.java @@ -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; + } + } diff --git a/src/main/java/com/application/munera/views/dashboard/DashboardView.java b/src/main/java/com/application/munera/views/dashboard/DashboardView.java index b1dc027..a510c18 100644 --- a/src/main/java/com/application/munera/views/dashboard/DashboardView.java +++ b/src/main/java/com/application/munera/views/dashboard/DashboardView.java @@ -105,7 +105,8 @@ public class DashboardView extends Div { } private String generateBarChartScript() { - List expenses = expenseService.findExpensesByYearExcludingCreditPaid(Year.now().getValue()); + final var loggedInPerson = this.personService.getLoggedInPerson(); + List expenses = expenseService.fetchExpensesForDashboard(loggedInPerson, Year.now()); // Prepare data for Highcharts Map monthlyData = new LinkedHashMap<>();