updated backend so booked appointment automatically changes to completed

This commit is contained in:
Alex
2026-04-08 19:16:18 -06:00
parent ccb0d0dc14
commit 9998b31ab4
6 changed files with 135 additions and 0 deletions

View File

@@ -4,8 +4,10 @@ import com.petshop.backend.config.FlywayContextInitializer;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO)
public class BackendApplication {
public static void main(String[] args) {

View File

@@ -0,0 +1,22 @@
package com.petshop.backend.config;
import com.petshop.backend.service.AppointmentService;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class ApplicationStartupListener implements ApplicationListener<ContextRefreshedEvent> {
private final AppointmentService appointmentService;
public ApplicationStartupListener(AppointmentService appointmentService) {
this.appointmentService = appointmentService;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//update booked appointments to complete on startup
appointmentService.updatePastAppointmentsStatus();
}
}

View File

@@ -47,4 +47,7 @@ public interface AppointmentRepository extends JpaRepository<Appointment, Long>
@Query("SELECT a FROM Appointment a JOIN FETCH a.service WHERE a.employee.id IN :employeeIds AND a.appointmentDate = :date AND LOWER(a.appointmentStatus) NOT IN ('cancelled', 'missed')")
List<Appointment> findByEmployeeIdInAndAppointmentDate(@Param("employeeIds") List<Long> employeeIds, @Param("date") LocalDate date);
@Query("SELECT a FROM Appointment a WHERE (a.appointmentDate < :currentDate OR (a.appointmentDate = :currentDate AND a.appointmentTime < :currentTime)) AND LOWER(a.appointmentStatus) = 'booked'")
List<Appointment> findPastBookedAppointments(@Param("currentDate") LocalDate currentDate, @Param("currentTime") LocalTime currentTime);
}

View File

@@ -16,6 +16,7 @@ import com.petshop.backend.repository.UserRepository;
import com.petshop.backend.util.AuthenticationHelper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -209,6 +210,20 @@ public class AppointmentService {
return availableSlots;
}
//Update booked status to completed at every midnight
@Scheduled(cron = "0 0 0 * * ?")
@Transactional
public void updatePastAppointmentsStatus() {
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
List<Appointment> pastBookedAppointments = appointmentRepository.findPastBookedAppointments(currentDate, currentTime);
for (Appointment appointment : pastBookedAppointments) {
appointment.setAppointmentStatus("COMPLETED");
appointmentRepository.save(appointment);
}
}
private String normalizeFilter(String value) {
if (value == null) {
return null;