diff --git a/src/main/java/com/primefactorsolutions/views/MainView.java b/src/main/java/com/primefactorsolutions/views/MainView.java index 9a9fb8f..ebe4a5b 100644 --- a/src/main/java/com/primefactorsolutions/views/MainView.java +++ b/src/main/java/com/primefactorsolutions/views/MainView.java @@ -10,7 +10,35 @@ import jakarta.annotation.security.PermitAll; @Route(value = "", layout = MainLayout.class) @PermitAll public class MainView extends Main { - public MainView() { - add(new Text("Welcome to PFS Intra.")); + + private final TimeOffRequestService requestService; + + public MainView(final TimeOffRequestService requestService) { + this.requestService = requestService; + add(new Text("Welcome")); + updateRequestStatuses(); + } + + private void updateRequestStatuses() { + List requests = requestService.findAllTimeOffRequests(); + LocalDate now = LocalDate.now(); + + for (TimeOffRequest request : requests) { + if (request.getState() == TimeOffRequestStatus.APROBADO) { + LocalDate expirationDate = request.getExpiration(); + LocalDate startDate = request.getStartDate(); + LocalDate endDate = request.getEndDate(); + + if (now.isAfter(expirationDate)) { + request.setState(TimeOffRequestStatus.VENCIDO); + } else if (now.isAfter(endDate) && now.isBefore(expirationDate)) { + request.setState(TimeOffRequestStatus.TOMADO); + } else if (now.isEqual(startDate) || now.isAfter(startDate) && now.isBefore(endDate)) { + request.setState(TimeOffRequestStatus.EN_USO); + } + } + } + + requestService.saveAll(requests); } }