ameliabooking
/
src
/
Application
/
Commands
/
Booking
/
Appointment
/
GetAppointmentCommandHandler.php
AddAppointmentCommand.php
7 years ago
AddAppointmentCommandHandler.php
1 year ago
AddBookingCommand.php
7 years ago
AddBookingCommandHandler.php
1 year ago
ApproveBookingRemotelyCommand.php
2 years ago
ApproveBookingRemotelyCommandHandler.php
1 year ago
CancelBookingCommand.php
7 years ago
CancelBookingCommandHandler.php
2 years ago
CancelBookingRemotelyCommand.php
7 years ago
CancelBookingRemotelyCommandHandler.php
2 years ago
DeleteAppointmentCommand.php
7 years ago
DeleteAppointmentCommandHandler.php
2 years ago
DeleteBookingCommand.php
4 years ago
DeleteBookingCommandHandler.php
2 years ago
GetAppointmentCommand.php
7 years ago
GetAppointmentCommandHandler.php
1 year ago
GetAppointmentsCommand.php
7 years ago
GetAppointmentsCommandHandler.php
1 year ago
GetIcsCommand.php
5 years ago
GetIcsCommandHandler.php
4 years ago
GetPackageAppointmentsCommand.php
1 year ago
GetPackageAppointmentsCommandHandler.php
1 year ago
GetTimeSlotsCommand.php
7 years ago
GetTimeSlotsCommandHandler.php
1 year ago
ReassignBookingCommand.php
5 years ago
ReassignBookingCommandHandler.php
1 year ago
RejectBookingRemotelyCommand.php
2 years ago
RejectBookingRemotelyCommandHandler.php
1 year ago
SuccessfulBookingCommand.php
7 years ago
SuccessfulBookingCommandHandler.php
1 year ago
UpdateAppointmentCommand.php
7 years ago
UpdateAppointmentCommandHandler.php
1 year ago
UpdateAppointmentStatusCommand.php
7 years ago
UpdateAppointmentStatusCommandHandler.php
1 year ago
UpdateAppointmentTimeCommand.php
7 years ago
UpdateAppointmentTimeCommandHandler.php
1 year ago
UpdateBookingStatusCommand.php
1 year ago
UpdateBookingStatusCommandHandler.php
1 year ago
GetAppointmentCommandHandler.php
204 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\User\CustomerApplicationService; |
| 9 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 10 | use AmeliaBooking\Domain\Collection\Collection; |
| 11 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 13 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 14 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 15 | use AmeliaBooking\Domain\Entity\Entities; |
| 16 | use AmeliaBooking\Domain\Entity\Payment\Payment; |
| 17 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 18 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 19 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 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\Payment\PaymentRepository; |
| 24 | use AmeliaBooking\Infrastructure\Services\LessonSpace\AbstractLessonSpaceService; |
| 25 | use Slim\Exception\ContainerValueNotFoundException; |
| 26 | |
| 27 | /** |
| 28 | * Class GetAppointmentCommandHandler |
| 29 | * |
| 30 | * @package AmeliaBooking\Application\Commands\Booking\Appointment |
| 31 | */ |
| 32 | class GetAppointmentCommandHandler extends CommandHandler |
| 33 | { |
| 34 | /** |
| 35 | * @param GetAppointmentCommand $command |
| 36 | * |
| 37 | * @return CommandResult |
| 38 | * @throws ContainerValueNotFoundException |
| 39 | * @throws AccessDeniedException |
| 40 | * @throws QueryExecutionException |
| 41 | * @throws InvalidArgumentException |
| 42 | * @throws NotFoundException |
| 43 | */ |
| 44 | public function handle(GetAppointmentCommand $command) |
| 45 | { |
| 46 | $result = new CommandResult(); |
| 47 | |
| 48 | /** @var UserApplicationService $userAS */ |
| 49 | $userAS = $this->container->get('application.user.service'); |
| 50 | |
| 51 | try { |
| 52 | /** @var AbstractUser $user */ |
| 53 | $user = $command->getUserApplicationService()->authorization( |
| 54 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 55 | $command->getCabinetType() |
| 56 | ); |
| 57 | } catch (AuthorizationException $e) { |
| 58 | $result->setResult(CommandResult::RESULT_ERROR); |
| 59 | $result->setData( |
| 60 | [ |
| 61 | 'reauthorize' => true |
| 62 | ] |
| 63 | ); |
| 64 | |
| 65 | return $result; |
| 66 | } |
| 67 | |
| 68 | /** @var AppointmentRepository $appointmentRepo */ |
| 69 | $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); |
| 70 | |
| 71 | /** @var CustomerApplicationService $customerAS */ |
| 72 | $customerAS = $this->container->get('application.user.customer.service'); |
| 73 | |
| 74 | /** @var Appointment $appointment */ |
| 75 | $appointment = $appointmentRepo->getById((int)$command->getField('id')); |
| 76 | |
| 77 | if ($userAS->isCustomer($user) && !$customerAS->hasCustomerBooking($appointment->getBookings(), $user)) { |
| 78 | throw new AccessDeniedException('You are not allowed to read appointment'); |
| 79 | } |
| 80 | |
| 81 | /** @var PaymentRepository $paymentRepository */ |
| 82 | $paymentRepository = $this->container->get('domain.payment.repository'); |
| 83 | |
| 84 | $bookingIds = []; |
| 85 | |
| 86 | /** @var CustomerBooking $booking */ |
| 87 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 88 | /** @var Payment $payment */ |
| 89 | foreach ($booking->getPayments()->getItems() as $payment) { |
| 90 | if ($payment->getParentId() && $payment->getParentId()->getValue()) { |
| 91 | try { |
| 92 | /** @var Payment $parentPayment */ |
| 93 | $parentPayment = $paymentRepository->getById($payment->getParentId()->getValue()); |
| 94 | |
| 95 | $bookingIds[] = $parentPayment->getCustomerBookingId()->getValue(); |
| 96 | } catch (\Exception $e) { |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** @var Collection $relatedPayments */ |
| 101 | $relatedPayments = $paymentRepository->getByEntityId( |
| 102 | $payment->getParentId() ? $payment->getParentId()->getValue() : $payment->getId()->getValue(), |
| 103 | 'parentId' |
| 104 | ); |
| 105 | |
| 106 | /** @var Payment $relatedPayment */ |
| 107 | foreach ($relatedPayments->getItems() as $relatedPayment) { |
| 108 | $bookingIds[] = $relatedPayment->getCustomerBookingId()->getValue(); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** @var Collection $recurringAppointments */ |
| 114 | $recurringAppointments = $bookingIds ? $appointmentRepo->getFiltered( |
| 115 | [ |
| 116 | 'bookingIds' => array_unique($bookingIds), |
| 117 | 'customerId' => !empty($command->getField('params')['customerId']) ? $command->getField('params')['customerId'] : null |
| 118 | ] |
| 119 | ) : $appointmentRepo->getFiltered( |
| 120 | [ |
| 121 | 'parentId' => $appointment->getParentId() ? |
| 122 | $appointment->getParentId()->getValue() : $appointment->getId()->getValue() |
| 123 | ] |
| 124 | ); |
| 125 | |
| 126 | if ($recurringAppointments->keyExists($appointment->getId()->getValue())) { |
| 127 | $recurringAppointments->deleteItem($appointment->getId()->getValue()); |
| 128 | } |
| 129 | |
| 130 | $customerAS->removeBookingsForOtherCustomers($user, new Collection([$appointment])); |
| 131 | |
| 132 | /** @var CustomerBooking $booking */ |
| 133 | foreach ($appointment->getBookings()->getItems() as $booking) { |
| 134 | $customFields = []; |
| 135 | |
| 136 | if ($booking->getCustomFields() && |
| 137 | ($customFields = json_decode($booking->getCustomFields()->getValue(), true)) === null |
| 138 | ) { |
| 139 | $booking->setCustomFields(null); |
| 140 | } |
| 141 | |
| 142 | if ($customFields) { |
| 143 | $parsedCustomFields = []; |
| 144 | |
| 145 | foreach ((array)$customFields as $key => $customField) { |
| 146 | if ($customField) { |
| 147 | $parsedCustomFields[$key] = $customField; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | $booking->setCustomFields(new Json(json_encode($parsedCustomFields))); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (!empty($command->getField('params')['timeZone'])) { |
| 156 | $appointment->getBookingStart()->getValue()->setTimezone( |
| 157 | new \DateTimeZone($command->getField('params')['timeZone']) |
| 158 | ); |
| 159 | |
| 160 | $appointment->getBookingEnd()->getValue()->setTimezone( |
| 161 | new \DateTimeZone($command->getField('params')['timeZone']) |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | $appointmentArray = $appointment->toArray(); |
| 166 | if (!empty($appointmentArray['lessonSpace'])) { |
| 167 | /** @var SettingsService $settingsDS */ |
| 168 | $settingsDS = $this->container->get('domain.settings.service'); |
| 169 | |
| 170 | $lessonSpaceApiKey = $settingsDS->getSetting('lessonSpace', 'apiKey'); |
| 171 | $lessonSpaceEnabled = $settingsDS->getSetting('lessonSpace', 'enabled'); |
| 172 | $lessonSpaceCompanyId = $settingsDS->getSetting('lessonSpace', 'companyId'); |
| 173 | if ($lessonSpaceEnabled && $lessonSpaceApiKey && $lessonSpaceCompanyId) { |
| 174 | /** @var AbstractLessonSpaceService $lessonSpaceService */ |
| 175 | $lessonSpaceService = $this->container->get('infrastructure.lesson.space.service'); |
| 176 | $spaceId = explode("https://www.thelessonspace.com/space/", $appointmentArray['lessonSpace']); |
| 177 | if ($spaceId && count($spaceId) > 1) { |
| 178 | $appointmentArray['lessonSpaceDetails'] = $lessonSpaceService->getSpace($lessonSpaceApiKey, $lessonSpaceCompanyId, $spaceId[1]); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (isset($appointmentArray['notifyParticipants'])) { |
| 184 | $appointmentArray['notifyParticipants'] = intval($appointmentArray['notifyParticipants']); |
| 185 | } |
| 186 | |
| 187 | $appointmentArray = apply_filters('amelia_get_appointment_filter', $appointmentArray); |
| 188 | |
| 189 | do_action('amelia_get_appointment', $appointmentArray); |
| 190 | |
| 191 | |
| 192 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 193 | $result->setMessage('Successfully retrieved appointment'); |
| 194 | $result->setData( |
| 195 | [ |
| 196 | Entities::APPOINTMENT => $appointmentArray, |
| 197 | 'recurring' => $recurringAppointments->toArray() |
| 198 | ] |
| 199 | ); |
| 200 | |
| 201 | return $result; |
| 202 | } |
| 203 | } |
| 204 |