From 4615febec32a2145addc29f9dcaa68d9b4820f70 Mon Sep 17 00:00:00 2001 From: ricardo051199 Date: Mon, 4 Nov 2024 00:48:59 -0400 Subject: [PATCH] Actualizar BD - Actualizar el estado de los registros de solicitudes de vacaciones --- .../primefactorsolutions/views/MainView.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) 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); } }