[950fa0d] | 1 | package edu.gjoko.schedlr.services;
|
---|
| 2 |
|
---|
[77205be] | 3 | import edu.gjoko.schedlr.dto.AppointmentInfoDto;
|
---|
| 4 | import edu.gjoko.schedlr.entity.*;
|
---|
[950fa0d] | 5 | import edu.gjoko.schedlr.exceptions.BlockingTimeException;
|
---|
[77205be] | 6 | import edu.gjoko.schedlr.mappers.AppointmentInfoDtoBusinessMapper;
|
---|
| 7 | import edu.gjoko.schedlr.mappers.AppointmentInfoDtoCustomerMapper;
|
---|
[950fa0d] | 8 | import edu.gjoko.schedlr.repositories.AppointmentRepository;
|
---|
| 9 | import edu.gjoko.schedlr.repositories.ServiceRepository;
|
---|
[77205be] | 10 | import edu.gjoko.schedlr.repositories.StakeholderRepository;
|
---|
[950fa0d] | 11 | import lombok.AllArgsConstructor;
|
---|
| 12 | import org.springframework.stereotype.Service;
|
---|
| 13 |
|
---|
[77205be] | 14 | import javax.persistence.EntityNotFoundException;
|
---|
| 15 | import javax.security.sasl.AuthenticationException;
|
---|
| 16 | import java.time.LocalDateTime;
|
---|
| 17 | import java.util.ArrayList;
|
---|
[950fa0d] | 18 | import java.util.List;
|
---|
[77205be] | 19 | import java.util.Optional;
|
---|
[950fa0d] | 20 |
|
---|
| 21 | @Service
|
---|
| 22 | @AllArgsConstructor
|
---|
| 23 | public class AppointmentsService {
|
---|
| 24 |
|
---|
| 25 | private final AppointmentRepository appointmentRepository;
|
---|
[77205be] | 26 | private final StakeholderRepository stakeholderRepository;
|
---|
[950fa0d] | 27 | private final ServiceRepository serviceRepository;
|
---|
[77205be] | 28 | private final AppointmentInfoDtoBusinessMapper appointmentInfoDtoBusinessMapper;
|
---|
| 29 | private final AppointmentInfoDtoCustomerMapper appointmentInfoDtoCustomerMapper;
|
---|
| 30 |
|
---|
[950fa0d] | 31 |
|
---|
| 32 | public void saveAppointment(Appointment appointment) {
|
---|
| 33 | var service = serviceRepository.findById(appointment.getService().getId()).get();
|
---|
[77205be] | 34 |
|
---|
| 35 | // remove 1 minute in order to prevent overlapping
|
---|
| 36 | appointment.setEndTime(appointment.getStartTime().plusMinutes(service.getDuration() - 1));
|
---|
[950fa0d] | 37 | List<Appointment> blockingAppointments = appointmentRepository.
|
---|
| 38 | findBlockingAppointments(
|
---|
[77205be] | 39 | appointment.getService().getBusiness().getId(), appointment.getStartTime(), appointment.getEndTime());
|
---|
[950fa0d] | 40 |
|
---|
| 41 | // check to see if there are blocking exceptions
|
---|
| 42 | if (blockingAppointments != null && !blockingAppointments.isEmpty()) {
|
---|
| 43 | throw new BlockingTimeException();
|
---|
| 44 | }
|
---|
| 45 | appointmentRepository.save(appointment);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[77205be] | 48 | public List<AppointmentInfoDto> getFutureAppointmentInfoList(Long stakeholderId) {
|
---|
| 49 | StakeholderType type = stakeholderRepository.findById(stakeholderId).get().getStakeholderType();
|
---|
| 50 | List<AppointmentInfoDto> appointmentInfoDtoList = new ArrayList<>();
|
---|
| 51 |
|
---|
| 52 | switch (type) {
|
---|
| 53 | case BUSINESS_OWNER -> appointmentInfoDtoList = appointmentRepository.findFutureAppointmentsByBusinessOwnerId(stakeholderId, LocalDateTime.now())
|
---|
| 54 | .stream()
|
---|
| 55 | .map(appointmentInfoDtoBusinessMapper::appointmentToAppointmentInfoDto)
|
---|
| 56 | .toList();
|
---|
| 57 | case CUSTOMER -> appointmentInfoDtoList = appointmentRepository.findFutureAppointmentsByCustomerId(stakeholderId, LocalDateTime.now())
|
---|
| 58 | .stream()
|
---|
| 59 | .map(appointmentInfoDtoCustomerMapper::appointmentToAppointmentInfoDto)
|
---|
| 60 | .toList();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | return appointmentInfoDtoList;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public List<AppointmentInfoDto> getPastAppointmentInfoList(Long stakeholderId) {
|
---|
| 67 | StakeholderType type = stakeholderRepository.findById(stakeholderId).get().getStakeholderType();
|
---|
| 68 | List<Appointment> appointmentInfoDtoList = new ArrayList<>();
|
---|
| 69 |
|
---|
| 70 | switch (type) {
|
---|
| 71 | case BUSINESS_OWNER -> appointmentInfoDtoList = appointmentRepository.findPastAppointmentsByBusinessOwnerId(stakeholderId, LocalDateTime.now());
|
---|
| 72 | case CUSTOMER -> appointmentInfoDtoList = appointmentRepository.findPastAppointmentsByCustomerId(stakeholderId, LocalDateTime.now());
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return appointmentInfoDtoList
|
---|
| 76 | .stream()
|
---|
| 77 | .map(appointmentInfoDtoBusinessMapper::appointmentToAppointmentInfoDto)
|
---|
| 78 | .toList();
|
---|
| 79 |
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public List<Appointment> getActiveAppointmentsByBusiness(Long businessId) {
|
---|
| 83 | return appointmentRepository.getActiveAppointmentsByBusiness(businessId);
|
---|
[950fa0d] | 84 | }
|
---|
| 85 |
|
---|
[77205be] | 86 | public void deleteAppointment(Long appointmentId, Long stakeholderId) throws AuthenticationException {
|
---|
| 87 | Optional<Stakeholder> stakeholderOptional = stakeholderRepository.findById(stakeholderId);
|
---|
| 88 | if (stakeholderOptional.isPresent()) {
|
---|
| 89 | Optional<Appointment> optional = appointmentRepository.findAppointmentByIdAndCustomer_Id(appointmentId, stakeholderId);
|
---|
| 90 | if (optional.isEmpty()) {
|
---|
| 91 | optional = appointmentRepository.findAppointmentByIdAndService_Business_Owner_Id(appointmentId, stakeholderId);
|
---|
| 92 | if (optional.isEmpty()) {
|
---|
| 93 | throw new EntityNotFoundException("No appointment was found for the give stakeholder and appointment id.");
|
---|
| 94 | } else {
|
---|
| 95 | softDeleteAppointment(optional.get(), AppointmentStatus.CANCELLED_BY_BUSINESS_OWNER);
|
---|
| 96 | }
|
---|
| 97 | } else {
|
---|
| 98 | softDeleteAppointment(optional.get(), AppointmentStatus.CANCELLED_BY_CUSTOMER);
|
---|
| 99 | }
|
---|
| 100 | } else {
|
---|
| 101 | throw new AuthenticationException();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | public void softDeleteAppointment(Appointment appointment, AppointmentStatus appointmentStatus) {
|
---|
| 106 | appointment.setAppointmentStatus(appointmentStatus);
|
---|
| 107 | appointmentRepository.save(appointment);
|
---|
[950fa0d] | 108 | }
|
---|
| 109 | }
|
---|