fix: other major changes
This commit is contained in:
parent
c716570bc2
commit
f1974a18e7
5 changed files with 51 additions and 35 deletions
|
@ -4,11 +4,11 @@ import lombok.Getter;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public enum BadgeMessage {
|
public enum BadgeMessage {
|
||||||
PAID_TO_SOMEONE("Paid to someone", "badge success"),
|
PAID_TO_ME("Paid to me", "badge success"),
|
||||||
PAID_TO_YOU("Paid to you", "badge success"),
|
PAID_BY_ME("Paid by me", "badge success"),
|
||||||
PAID("Paid", "badge success"),
|
PAID("Paid", "badge success"),
|
||||||
OWED_BY_SOMEONE("Owed by someone", "badge warning"),
|
OWED_TO_ME("Owed to me", "badge warning"),
|
||||||
OWED_TO_YOU("Owed to you", "badge warning"),
|
OWED_BY_ME("Owed by me", "badge warning"),
|
||||||
NOT_PAID("Not paid", "badge warning"),
|
NOT_PAID("Not paid", "badge warning"),
|
||||||
UNKNOWN("Unknown status", "badge error");
|
UNKNOWN("Unknown status", "badge error");
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,22 @@
|
||||||
package com.application.munera.services;
|
package com.application.munera.services;
|
||||||
|
|
||||||
|
import com.application.munera.SecurityUtils;
|
||||||
import com.application.munera.data.Expense;
|
import com.application.munera.data.Expense;
|
||||||
import com.application.munera.data.ExpenseType;
|
import com.application.munera.data.ExpenseType;
|
||||||
import com.application.munera.data.Person;
|
import com.application.munera.data.Person;
|
||||||
import com.application.munera.repositories.ExpenseRepository;
|
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.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@ -21,9 +24,13 @@ import java.util.stream.Stream;
|
||||||
public class ExpenseService {
|
public class ExpenseService {
|
||||||
|
|
||||||
private final ExpenseRepository expenseRepository;
|
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.expenseRepository = expenseRepository;
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
this.personRepository = personRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -134,9 +141,7 @@ public class ExpenseService {
|
||||||
* @param entity the expense to update
|
* @param entity the expense to update
|
||||||
*/
|
*/
|
||||||
public void update(Expense entity) {
|
public void update(Expense entity) {
|
||||||
if (Boolean.TRUE.equals(entity.getIsPaid())) {
|
if (Boolean.TRUE.equals(entity.getIsPaid())) entity.setPaymentDate(LocalDateTime.now());
|
||||||
entity.setPaymentDate(LocalDateTime.now());
|
|
||||||
}
|
|
||||||
this.setExpenseType(entity);
|
this.setExpenseType(entity);
|
||||||
expenseRepository.save(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.
|
* 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
|
* @param expense the expense to set the type of
|
||||||
*/
|
*/
|
||||||
private void setExpenseType(final @Nonnull Expense expense) {
|
private void setExpenseType(final @Nonnull Expense expense) {
|
||||||
// Check if the payer is present
|
// Get the currently logged-in user
|
||||||
if (Objects.nonNull(expense.getPayer())) {
|
UserDetails userDetails = SecurityUtils.getLoggedInUserDetails();
|
||||||
// If payer is present, set type to CREDIT
|
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);
|
expense.setExpenseType(ExpenseType.CREDIT);
|
||||||
}
|
} else if (!payer.equals(loggedInPerson) && beneficiary.equals(loggedInPerson)) {
|
||||||
// Check if the beneficiary is present and no payer
|
// Logged-in user is the beneficiary, and the payer is someone else
|
||||||
else if (Objects.nonNull(expense.getBeneficiary())) {
|
|
||||||
// If beneficiary is present and no payer, set type to DEBIT
|
|
||||||
expense.setExpenseType(ExpenseType.DEBIT);
|
expense.setExpenseType(ExpenseType.DEBIT);
|
||||||
}
|
} else if (payer.equals(loggedInPerson) && beneficiary.equals(loggedInPerson)) {
|
||||||
// If neither payer nor beneficiary is present
|
// Both payer and beneficiary are the logged-in user
|
||||||
else {
|
|
||||||
// Set type to NONE
|
|
||||||
expense.setExpenseType(ExpenseType.NONE);
|
expense.setExpenseType(ExpenseType.NONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,11 +9,11 @@ import org.springframework.stereotype.Service;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ViewService {
|
public class ServiceView {
|
||||||
|
|
||||||
private final ExpenseService expenseService;
|
private final ExpenseService expenseService;
|
||||||
|
|
||||||
public ViewService(ExpenseService expenseService) {
|
public ServiceView(ExpenseService expenseService) {
|
||||||
this.expenseService = expenseService;
|
this.expenseService = expenseService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,8 +45,8 @@ public class ViewService {
|
||||||
|
|
||||||
private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) {
|
private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) {
|
||||||
return switch (type) {
|
return switch (type) {
|
||||||
case CREDIT -> isPaid ? BadgeMessage.PAID_TO_SOMEONE : BadgeMessage.OWED_BY_SOMEONE;
|
case CREDIT -> isPaid ? BadgeMessage.PAID_TO_ME : BadgeMessage.OWED_TO_ME;
|
||||||
case DEBIT -> isPaid ? BadgeMessage.PAID_TO_YOU : BadgeMessage.OWED_TO_YOU;
|
case DEBIT -> isPaid ? BadgeMessage.PAID_BY_ME : BadgeMessage.OWED_BY_ME;
|
||||||
case NONE -> isPaid ? BadgeMessage.PAID : BadgeMessage.NOT_PAID;
|
case NONE -> isPaid ? BadgeMessage.PAID : BadgeMessage.NOT_PAID;
|
||||||
default -> BadgeMessage.UNKNOWN;
|
default -> BadgeMessage.UNKNOWN;
|
||||||
};
|
};
|
|
@ -59,7 +59,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
|
||||||
private final CategoryService categoryService;
|
private final CategoryService categoryService;
|
||||||
private final PersonService personService;
|
private final PersonService personService;
|
||||||
private final EventService eventService;
|
private final EventService eventService;
|
||||||
private final ViewService viewService;
|
private final ServiceView serviceView;
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private TextField name;
|
private TextField name;
|
||||||
private TextField cost;
|
private TextField cost;
|
||||||
|
@ -75,12 +75,12 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
|
||||||
private ComboBox<Event> event;
|
private ComboBox<Event> event;
|
||||||
|
|
||||||
@Autowired
|
@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.expenseService = expenseService;
|
||||||
this.categoryService = categoryService;
|
this.categoryService = categoryService;
|
||||||
this.personService = personService;
|
this.personService = personService;
|
||||||
this.eventService = eventService;
|
this.eventService = eventService;
|
||||||
this.viewService = viewService;
|
this.serviceView = serviceView;
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
addClassNames("expenses-view");
|
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::getPeriodUnit).setHeader("Period Unit").setSortable(true);
|
||||||
grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date");
|
grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date");
|
||||||
// grid.addColumn(expenseEvent -> expenseEvent.getEvent().getName()).setHeader("Event").setSortable(true);
|
// 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.getColumns().forEach(col -> col.setAutoWidth(true));
|
||||||
|
|
||||||
grid.setItems(this.expenseService.findAllOrderByDateDescending());
|
grid.setItems(this.expenseService.findAllOrderByDateDescending());
|
||||||
|
|
|
@ -4,7 +4,7 @@ import com.application.munera.data.Expense;
|
||||||
import com.application.munera.data.Person;
|
import com.application.munera.data.Person;
|
||||||
import com.application.munera.services.ExpenseService;
|
import com.application.munera.services.ExpenseService;
|
||||||
import com.application.munera.services.PersonService;
|
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.application.munera.views.MainLayout;
|
||||||
import com.vaadin.flow.component.UI;
|
import com.vaadin.flow.component.UI;
|
||||||
import com.vaadin.flow.component.button.Button;
|
import com.vaadin.flow.component.button.Button;
|
||||||
|
@ -56,15 +56,15 @@ public class PeopleView extends Div implements BeforeEnterObserver {
|
||||||
private Person person;
|
private Person person;
|
||||||
private final PersonService personService;
|
private final PersonService personService;
|
||||||
private final ExpenseService expenseService;
|
private final ExpenseService expenseService;
|
||||||
private final ViewService viewService;
|
private final ServiceView serviceView;
|
||||||
private TextField firstName;
|
private TextField firstName;
|
||||||
private TextField lastName;
|
private TextField lastName;
|
||||||
private EmailField email;
|
private EmailField email;
|
||||||
|
|
||||||
public PeopleView(PersonService personService, ExpenseService expenseService, ViewService viewService) {
|
public PeopleView(PersonService personService, ExpenseService expenseService, ServiceView serviceView) {
|
||||||
this.personService = personService;
|
this.personService = personService;
|
||||||
this.expenseService = expenseService;
|
this.expenseService = expenseService;
|
||||||
this.viewService = viewService;
|
this.serviceView = serviceView;
|
||||||
addClassNames("expenses-view");
|
addClassNames("expenses-view");
|
||||||
|
|
||||||
// Create UI
|
// Create UI
|
||||||
|
@ -79,8 +79,8 @@ public class PeopleView extends Div implements BeforeEnterObserver {
|
||||||
grid.addHierarchyColumn(this::getNodeName).setHeader("Name");
|
grid.addHierarchyColumn(this::getNodeName).setHeader("Name");
|
||||||
grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true);
|
grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true);
|
||||||
grid.addColumn(new ComponentRenderer<>(persona -> {
|
grid.addColumn(new ComponentRenderer<>(persona -> {
|
||||||
if (persona instanceof Person) return this.viewService.createPersonBadge(personService.calculateNetBalance((Person) persona));
|
if (persona instanceof Person) return this.serviceView.createPersonBadge(personService.calculateNetBalance((Person) persona));
|
||||||
else return this.viewService.createExpenseBadge(((Expense) persona));
|
else return this.serviceView.createExpenseBadge(((Expense) persona));
|
||||||
})).setHeader("Balance Status");
|
})).setHeader("Balance Status");
|
||||||
|
|
||||||
grid.addColumn(new ComponentRenderer<>(persona -> {
|
grid.addColumn(new ComponentRenderer<>(persona -> {
|
||||||
|
|
Loading…
Reference in a new issue