ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
ApproveBookingRemotelyCommandHandler.php
AddAppointmentCommand.php
1 year ago
AddAppointmentCommandHandler.php
2 months ago
AddBookingCommand.php
1 year ago
AddBookingCommandHandler.php
1 year ago
ApproveBookingRemotelyCommand.php
1 year ago
ApproveBookingRemotelyCommandHandler.php
2 months ago
CancelBookingCommand.php
1 year ago
CancelBookingCommandHandler.php
6 months ago
CancelBookingRemotelyCommand.php
1 year ago
CancelBookingRemotelyCommandHandler.php
1 month ago
DeleteAppointmentCommand.php
1 year ago
DeleteAppointmentCommandHandler.php
1 year ago
DeleteBookingCommand.php
1 year ago
DeleteBookingCommandHandler.php
10 months ago
DeleteBookingRemotelyCommand.php
10 months ago
DeleteBookingRemotelyCommandHandler.php
6 months ago
GetAppointmentBookingsCommand.php
6 months ago
GetAppointmentBookingsCommandHandler.php
2 months ago
GetAppointmentCommand.php
7 years ago
GetAppointmentCommandHandler.php
2 weeks ago
GetAppointmentsCommand.php
1 year ago
GetAppointmentsCommandHandler.php
2 weeks ago
GetIcsCommand.php
6 months ago
GetIcsCommandHandler.php
4 years ago
GetTimeSlotsCommand.php
1 year ago
GetTimeSlotsCommandHandler.php
1 month ago
ReassignBookingCommand.php
1 year ago
ReassignBookingCommandHandler.php
3 months ago
RejectBookingRemotelyCommand.php
1 year ago
RejectBookingRemotelyCommandHandler.php
4 months ago
SuccessfulBookingCommand.php
1 year ago
SuccessfulBookingCommandHandler.php
1 year ago
UpdateAppointmentCommand.php
1 year ago
UpdateAppointmentCommandHandler.php
3 months ago
UpdateAppointmentNoteCommand.php
3 months ago
UpdateAppointmentNoteCommandHandler.php
3 months ago
UpdateAppointmentStatusCommand.php
1 year ago
UpdateAppointmentStatusCommandHandler.php
2 months ago
UpdateAppointmentTimeCommand.php
1 year ago
UpdateAppointmentTimeCommandHandler.php
1 month ago
UpdateBookingStatusCommand.php
1 year ago
UpdateBookingStatusCommandHandler.php
5 months ago
ApproveBookingRemotelyCommandHandler.php
219 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Booking\Appointment; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Application\Services\Booking\BookingFallbackService; |
| 9 | use AmeliaBooking\Application\Services\User\CustomerApplicationService; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\BookingCancellationException; |
| 11 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 12 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 13 | use AmeliaBooking\Domain\Entity\Entities; |
| 14 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 15 | use AmeliaBooking\Domain\Services\Reservation\ReservationServiceInterface; |
| 16 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 17 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 18 | use AmeliaBooking\Domain\ValueObjects\String\BookingStatus; |
| 19 | use AmeliaBooking\Domain\ValueObjects\String\Token; |
| 20 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 21 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 22 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\AppointmentRepository; |
| 23 | use AmeliaBooking\Infrastructure\Repository\Booking\Appointment\CustomerBookingRepository; |
| 24 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 25 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 26 | use Slim\Exception\ContainerException; |
| 27 | use Slim\Exception\ContainerValueNotFoundException; |
| 28 | use UnexpectedValueException; |
| 29 | |
| 30 | /** |
| 31 | * Class ApproveBookingRemotelyCommandHandler |
| 32 | * |
| 33 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 34 | */ |
| 35 | class ApproveBookingRemotelyCommandHandler extends CommandHandler |
| 36 | { |
| 37 | /** |
| 38 | * @var array |
| 39 | */ |
| 40 | public $mandatoryFields = [ |
| 41 | 'token', |
| 42 | ]; |
| 43 | |
| 44 | /** |
| 45 | * @throws UnexpectedValueException |
| 46 | * @throws ContainerException |
| 47 | * @throws \InvalidArgumentException |
| 48 | * @throws ContainerValueNotFoundException |
| 49 | * @throws QueryExecutionException |
| 50 | * @throws InvalidArgumentException |
| 51 | * @throws AccessDeniedException |
| 52 | * @throws NotFoundException |
| 53 | * @throws BookingCancellationException |
| 54 | */ |
| 55 | public function handle(ApproveBookingRemotelyCommand $command): CommandResult |
| 56 | { |
| 57 | $this->checkMandatoryFields($command); |
| 58 | |
| 59 | $result = new CommandResult(); |
| 60 | |
| 61 | $type = Entities::APPOINTMENT; |
| 62 | |
| 63 | /** @var CustomerApplicationService $customerAS */ |
| 64 | $customerAS = $this->container->get('application.user.customer.service'); |
| 65 | |
| 66 | /** @var CustomerBookingRepository $bookingRepository */ |
| 67 | $bookingRepository = $this->container->get('domain.booking.customerBooking.repository'); |
| 68 | |
| 69 | /** @var AbstractUser $user */ |
| 70 | $user = $this->container->get('logged.in.user'); |
| 71 | |
| 72 | /** @var CustomerBooking $booking */ |
| 73 | $booking = $bookingRepository->getById((int)$command->getArg('id')); |
| 74 | |
| 75 | /** @var SettingsService $settingsService */ |
| 76 | $settingsService = $this->container->get('domain.settings.service'); |
| 77 | |
| 78 | $notificationSettings = $settingsService->getCategorySettings('notifications'); |
| 79 | |
| 80 | if ($booking === null) { |
| 81 | if (!empty($notificationSettings['approveErrorUrl'])) { |
| 82 | $result->setUrl($notificationSettings['approveErrorUrl']); |
| 83 | return $result; |
| 84 | } |
| 85 | |
| 86 | return $result->setHtml(BookingFallbackService::getFallbackHtml('failed')); |
| 87 | } |
| 88 | |
| 89 | $token = $bookingRepository->getToken((int)$command->getArg('id')); |
| 90 | |
| 91 | if (empty($token['token'])) { |
| 92 | throw new AccessDeniedException('You are not allowed to update booking status'); |
| 93 | } |
| 94 | |
| 95 | $booking->setToken(new Token($token['token'])); |
| 96 | |
| 97 | if (!$customerAS->isCustomerBooking($booking, $user, $command->getField('token'))) { |
| 98 | throw new AccessDeniedException('You are not allowed to update booking status'); |
| 99 | } |
| 100 | |
| 101 | if ($booking->getStatus()->getValue() === BookingStatus::WAITING) { |
| 102 | try { |
| 103 | $this->validateWaitingListCapacity($booking); |
| 104 | } catch (BookingCancellationException $e) { |
| 105 | $appointmentSettings = $settingsService->getCategorySettings('appointments'); |
| 106 | $waitingListDeniedUrl = !empty($appointmentSettings['waitingListAppointments']['redirectUrlDenied']) ? |
| 107 | $appointmentSettings['waitingListAppointments']['redirectUrlDenied'] : ''; |
| 108 | |
| 109 | if (!empty($waitingListDeniedUrl)) { |
| 110 | $result->setUrl($waitingListDeniedUrl); |
| 111 | return $result; |
| 112 | } |
| 113 | |
| 114 | if (!empty($notificationSettings['approveErrorUrl'])) { |
| 115 | $result->setUrl($notificationSettings['approveErrorUrl']); |
| 116 | return $result; |
| 117 | } |
| 118 | |
| 119 | return $result->setHtml(BookingFallbackService::getFallbackHtml('failed')); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** @var ReservationServiceInterface $reservationService */ |
| 124 | $reservationService = $this->container->get('application.reservation.service')->get($type); |
| 125 | |
| 126 | $status = BookingStatus::APPROVED; |
| 127 | |
| 128 | do_action('amelia_before_booking_approved_link', $booking ? $booking->toArray() : null); |
| 129 | |
| 130 | $bookingData = $reservationService->updateStatus($booking, $status); |
| 131 | |
| 132 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 133 | $result->setMessage('Successfully updated booking status'); |
| 134 | $result->setData( |
| 135 | array_merge( |
| 136 | $bookingData, |
| 137 | [ |
| 138 | 'type' => $type, |
| 139 | 'status' => $status, |
| 140 | 'message' => BackendStrings::get('appointment_status_changed') . strtolower(BackendStrings::get($status)) |
| 141 | ] |
| 142 | ) |
| 143 | ); |
| 144 | |
| 145 | if ($notificationSettings['approveSuccessUrl'] && $result->getResult() === CommandResult::RESULT_SUCCESS) { |
| 146 | $result->setUrl($notificationSettings['approveSuccessUrl']); |
| 147 | |
| 148 | do_action('amelia_after_booking_approved_link', $booking ? $booking->toArray() : null); |
| 149 | return $result; |
| 150 | } |
| 151 | |
| 152 | if ($notificationSettings['approveErrorUrl'] && $result->getResult() === CommandResult::RESULT_ERROR) { |
| 153 | $result->setUrl($notificationSettings['approveErrorUrl']); |
| 154 | |
| 155 | return $result; |
| 156 | } |
| 157 | // No redirect URL defined - show fallback page |
| 158 | if ($result->getResult() === CommandResult::RESULT_SUCCESS) { |
| 159 | return $result->setHtml(BookingFallbackService::getFallbackHtml('approved')); |
| 160 | } |
| 161 | return $result->setHtml(BookingFallbackService::getFallbackHtml('approved_with_issues')); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Validates waiting list appointment capacity |
| 166 | * |
| 167 | * @throws ContainerValueNotFoundException |
| 168 | * @throws ContainerException |
| 169 | * @throws BookingCancellationException |
| 170 | * @throws NotFoundException |
| 171 | */ |
| 172 | private function validateWaitingListCapacity(CustomerBooking $booking): void |
| 173 | { |
| 174 | /** @var AppointmentRepository $appointmentRepo */ |
| 175 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 176 | |
| 177 | /** @var Appointment $appointment */ |
| 178 | $appointment = $appointmentRepo->getById($booking->getAppointmentId()->getValue()); |
| 179 | |
| 180 | if ($appointment === null) { |
| 181 | throw new NotFoundException('This appointment does not exist!'); |
| 182 | } |
| 183 | |
| 184 | /** @var ProviderRepository $providerRepository */ |
| 185 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 186 | |
| 187 | $capacity = $providerRepository->getMaxCapacityByServiceId( |
| 188 | $appointment->getProviderId()->getValue(), |
| 189 | $appointment->getServiceId()->getValue() |
| 190 | ); |
| 191 | |
| 192 | $currentBookedPersons = $this->calculateCurrentBookedPersons($appointment); |
| 193 | $availableCapacity = $capacity - $currentBookedPersons; |
| 194 | |
| 195 | if ($availableCapacity < $booking->getPersons()->getValue()) { |
| 196 | throw new BookingCancellationException( |
| 197 | 'This slot is already taken! Available capacity: ' . $availableCapacity . ', requested: ' . $booking->getPersons()->getValue() |
| 198 | ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Calculates the total number of persons currently booked for an appointment |
| 204 | */ |
| 205 | private function calculateCurrentBookedPersons(Appointment $appointment): int |
| 206 | { |
| 207 | $persons = 0; |
| 208 | $approvedStatuses = [BookingStatus::APPROVED, BookingStatus::PENDING]; |
| 209 | |
| 210 | foreach ($appointment->getBookings()->getItems() as $existingBooking) { |
| 211 | if (in_array($existingBooking->getStatus()->getValue(), $approvedStatuses)) { |
| 212 | $persons += $existingBooking->getPersons()->getValue(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return $persons; |
| 217 | } |
| 218 | } |
| 219 |