diff --git a/src/main/java/com/application/munera/data/BadgeMessage.java b/src/main/java/com/application/munera/data/BadgeMessage.java index 6e88abf..d8326d9 100644 --- a/src/main/java/com/application/munera/data/BadgeMessage.java +++ b/src/main/java/com/application/munera/data/BadgeMessage.java @@ -4,11 +4,11 @@ import lombok.Getter; @Getter public enum BadgeMessage { - PAID_TO_SOMEONE("Paid to someone", "badge success"), - PAID_TO_YOU("Paid to you", "badge success"), + PAID_TO_ME("Paid to me", "badge success"), + PAID_BY_ME("Paid by me", "badge success"), PAID("Paid", "badge success"), - OWED_BY_SOMEONE("Owed by someone", "badge warning"), - OWED_TO_YOU("Owed to you", "badge warning"), + OWED_TO_ME("Owed to me", "badge warning"), + OWED_BY_ME("Owed by me", "badge warning"), NOT_PAID("Not paid", "badge warning"), UNKNOWN("Unknown status", "badge error"); diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index 9a49ad1..82a291c 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -1,19 +1,22 @@ package com.application.munera.services; +import com.application.munera.SecurityUtils; import com.application.munera.data.Expense; import com.application.munera.data.ExpenseType; import com.application.munera.data.Person; import com.application.munera.repositories.ExpenseRepository; +import com.application.munera.repositories.PersonRepository; +import com.application.munera.repositories.UserRepository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; import javax.annotation.Nonnull; import java.time.LocalDateTime; import java.util.Collection; import java.util.List; -import java.util.Objects; import java.util.Optional; import java.util.stream.Stream; @@ -21,9 +24,13 @@ import java.util.stream.Stream; public class ExpenseService { private final ExpenseRepository expenseRepository; + private final UserRepository userRepository; + private final PersonRepository personRepository; - public ExpenseService(ExpenseRepository expenseRepository) { + public ExpenseService(ExpenseRepository expenseRepository, UserRepository userRepository, PersonRepository personRepository) { this.expenseRepository = expenseRepository; + this.userRepository = userRepository; + this.personRepository = personRepository; } /** @@ -134,9 +141,7 @@ public class ExpenseService { * @param entity the expense to update */ public void update(Expense entity) { - if (Boolean.TRUE.equals(entity.getIsPaid())) { - entity.setPaymentDate(LocalDateTime.now()); - } + if (Boolean.TRUE.equals(entity.getIsPaid())) entity.setPaymentDate(LocalDateTime.now()); this.setExpenseType(entity); expenseRepository.save(entity); } @@ -182,23 +187,34 @@ public class ExpenseService { /** * Sets the expense type depending on the presence or absence of a payer and beneficiary. - * This is used to filter expenses where the payer has been reimbursed. * @param expense the expense to set the type of */ private void setExpenseType(final @Nonnull Expense expense) { - // Check if the payer is present - if (Objects.nonNull(expense.getPayer())) { - // If payer is present, set type to CREDIT + // Get the currently logged-in user + UserDetails userDetails = SecurityUtils.getLoggedInUserDetails(); + if (userDetails == null) { + throw new IllegalStateException("No logged-in user found"); + } + + // Fetch the logged-in user + final var loggedInUserId = userRepository.findByUsername(userDetails.getUsername()).getId(); + Person loggedInPerson = this.personRepository.findByUserId(loggedInUserId).orElse(null); + + if (loggedInPerson == null) throw new IllegalStateException("No associated Person entity found for logged-in user"); + + // Check if the payer and beneficiary are present + Person payer = expense.getPayer(); + Person beneficiary = expense.getBeneficiary(); + + // Determine the expense type + if (payer.equals(loggedInPerson) && !beneficiary.equals(loggedInPerson)) { + // Logged-in user is the payer, and the beneficiary is someone else expense.setExpenseType(ExpenseType.CREDIT); - } - // Check if the beneficiary is present and no payer - else if (Objects.nonNull(expense.getBeneficiary())) { - // If beneficiary is present and no payer, set type to DEBIT + } else if (!payer.equals(loggedInPerson) && beneficiary.equals(loggedInPerson)) { + // Logged-in user is the beneficiary, and the payer is someone else expense.setExpenseType(ExpenseType.DEBIT); - } - // If neither payer nor beneficiary is present - else { - // Set type to NONE + } else if (payer.equals(loggedInPerson) && beneficiary.equals(loggedInPerson)) { + // Both payer and beneficiary are the logged-in user expense.setExpenseType(ExpenseType.NONE); } } diff --git a/src/main/java/com/application/munera/services/ViewService.java b/src/main/java/com/application/munera/services/ServiceView.java similarity index 86% rename from src/main/java/com/application/munera/services/ViewService.java rename to src/main/java/com/application/munera/services/ServiceView.java index 4cbc673..28c1d2b 100644 --- a/src/main/java/com/application/munera/services/ViewService.java +++ b/src/main/java/com/application/munera/services/ServiceView.java @@ -9,11 +9,11 @@ import org.springframework.stereotype.Service; import java.math.BigDecimal; @Service -public class ViewService { +public class ServiceView { private final ExpenseService expenseService; - public ViewService(ExpenseService expenseService) { + public ServiceView(ExpenseService expenseService) { this.expenseService = expenseService; } @@ -45,8 +45,8 @@ public class ViewService { private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) { return switch (type) { - case CREDIT -> isPaid ? BadgeMessage.PAID_TO_SOMEONE : BadgeMessage.OWED_BY_SOMEONE; - case DEBIT -> isPaid ? BadgeMessage.PAID_TO_YOU : BadgeMessage.OWED_TO_YOU; + case CREDIT -> isPaid ? BadgeMessage.PAID_TO_ME : BadgeMessage.OWED_TO_ME; + case DEBIT -> isPaid ? BadgeMessage.PAID_BY_ME : BadgeMessage.OWED_BY_ME; case NONE -> isPaid ? BadgeMessage.PAID : BadgeMessage.NOT_PAID; default -> BadgeMessage.UNKNOWN; }; diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index 63d8cd2..aa77ddc 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -59,7 +59,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { private final CategoryService categoryService; private final PersonService personService; private final EventService eventService; - private final ViewService viewService; + private final ServiceView serviceView; private final UserService userService; private TextField name; private TextField cost; @@ -75,12 +75,12 @@ public class ExpensesView extends Div implements BeforeEnterObserver { private ComboBox event; @Autowired - public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService, EventService eventService, ViewService viewService, UserService userService) { + public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService, EventService eventService, ServiceView serviceView, UserService userService) { this.expenseService = expenseService; this.categoryService = categoryService; this.personService = personService; this.eventService = eventService; - this.viewService = viewService; + this.serviceView = serviceView; this.userService = userService; addClassNames("expenses-view"); @@ -100,7 +100,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { grid.addColumn(Expense::getPeriodUnit).setHeader("Period Unit").setSortable(true); grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date"); // grid.addColumn(expenseEvent -> expenseEvent.getEvent().getName()).setHeader("Event").setSortable(true); - grid.addColumn(new ComponentRenderer<>(this.viewService::createExpenseBadge)).setHeader("Status").setSortable(true); + grid.addColumn(new ComponentRenderer<>(this.serviceView::createExpenseBadge)).setHeader("Status").setSortable(true); grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.setItems(this.expenseService.findAllOrderByDateDescending()); diff --git a/src/main/java/com/application/munera/views/people/PeopleView.java b/src/main/java/com/application/munera/views/people/PeopleView.java index 09aba16..72f95ec 100644 --- a/src/main/java/com/application/munera/views/people/PeopleView.java +++ b/src/main/java/com/application/munera/views/people/PeopleView.java @@ -4,7 +4,7 @@ import com.application.munera.data.Expense; import com.application.munera.data.Person; import com.application.munera.services.ExpenseService; import com.application.munera.services.PersonService; -import com.application.munera.services.ViewService; +import com.application.munera.services.ServiceView; import com.application.munera.views.MainLayout; import com.vaadin.flow.component.UI; import com.vaadin.flow.component.button.Button; @@ -56,15 +56,15 @@ public class PeopleView extends Div implements BeforeEnterObserver { private Person person; private final PersonService personService; private final ExpenseService expenseService; - private final ViewService viewService; + private final ServiceView serviceView; private TextField firstName; private TextField lastName; private EmailField email; - public PeopleView(PersonService personService, ExpenseService expenseService, ViewService viewService) { + public PeopleView(PersonService personService, ExpenseService expenseService, ServiceView serviceView) { this.personService = personService; this.expenseService = expenseService; - this.viewService = viewService; + this.serviceView = serviceView; addClassNames("expenses-view"); // Create UI @@ -79,8 +79,8 @@ public class PeopleView extends Div implements BeforeEnterObserver { grid.addHierarchyColumn(this::getNodeName).setHeader("Name"); grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true); grid.addColumn(new ComponentRenderer<>(persona -> { - if (persona instanceof Person) return this.viewService.createPersonBadge(personService.calculateNetBalance((Person) persona)); - else return this.viewService.createExpenseBadge(((Expense) persona)); + if (persona instanceof Person) return this.serviceView.createPersonBadge(personService.calculateNetBalance((Person) persona)); + else return this.serviceView.createExpenseBadge(((Expense) persona)); })).setHeader("Balance Status"); grid.addColumn(new ComponentRenderer<>(persona -> {