PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.31
Booking for Appointments and Events Calendar – Amelia v1.2.31
2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Booking / Appointment / GetAppointmentCommandHandler.php
ameliabooking / src / Application / Commands / Booking / Appointment Last commit date
AddAppointmentCommand.php 1 year ago AddAppointmentCommandHandler.php 1 year ago AddBookingCommand.php 1 year ago AddBookingCommandHandler.php 1 year ago ApproveBookingRemotelyCommand.php 1 year ago ApproveBookingRemotelyCommandHandler.php 1 year ago CancelBookingCommand.php 1 year ago CancelBookingCommandHandler.php 1 year ago CancelBookingRemotelyCommand.php 1 year ago CancelBookingRemotelyCommandHandler.php 2 years ago DeleteAppointmentCommand.php 1 year ago DeleteAppointmentCommandHandler.php 1 year ago DeleteBookingCommand.php 1 year ago DeleteBookingCommandHandler.php 1 year ago GetAppointmentCommand.php 7 years ago GetAppointmentCommandHandler.php 1 year ago GetAppointmentsCommand.php 1 year ago GetAppointmentsCommandHandler.php 1 year ago GetIcsCommand.php 1 year ago GetIcsCommandHandler.php 4 years ago GetPackageAppointmentsCommand.php 1 year ago GetPackageAppointmentsCommandHandler.php 1 year ago GetTimeSlotsCommand.php 1 year ago GetTimeSlotsCommandHandler.php 1 year ago ReassignBookingCommand.php 1 year ago ReassignBookingCommandHandler.php 1 year ago RejectBookingRemotelyCommand.php 1 year ago RejectBookingRemotelyCommandHandler.php 1 year ago SuccessfulBookingCommand.php 1 year ago SuccessfulBookingCommandHandler.php 1 year ago UpdateAppointmentCommand.php 1 year ago UpdateAppointmentCommandHandler.php 1 year ago UpdateAppointmentStatusCommand.php 1 year ago UpdateAppointmentStatusCommandHandler.php 1 year ago UpdateAppointmentTimeCommand.php 1 year ago UpdateAppointmentTimeCommandHandler.php 1 year ago UpdateBookingStatusCommand.php 1 year ago UpdateBookingStatusCommandHandler.php 1 year ago
GetAppointmentCommandHandler.php
208 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 (
137 $booking->getCustomFields() &&
138 ($customFields = json_decode($booking->getCustomFields()->getValue(), true)) === null
139 ) {
140 $booking->setCustomFields(null);
141 }
142
143 if ($customFields) {
144 $parsedCustomFields = [];
145
146 foreach ((array)$customFields as $key => $customField) {
147 if ($customField) {
148 $parsedCustomFields[$key] = $customField;
149 }
150 }
151
152 $booking->setCustomFields(new Json(json_encode($parsedCustomFields)));
153 }
154 }
155
156 if (!empty($command->getField('params')['timeZone'])) {
157 $appointment->getBookingStart()->getValue()->setTimezone(
158 new \DateTimeZone($command->getField('params')['timeZone'])
159 );
160
161 $appointment->getBookingEnd()->getValue()->setTimezone(
162 new \DateTimeZone($command->getField('params')['timeZone'])
163 );
164 }
165
166 $appointmentArray = $appointment->toArray();
167 if (!empty($appointmentArray['lessonSpace'])) {
168 /** @var SettingsService $settingsDS */
169 $settingsDS = $this->container->get('domain.settings.service');
170
171 $lessonSpaceApiKey = $settingsDS->getSetting('lessonSpace', 'apiKey');
172 $lessonSpaceEnabled = $settingsDS->getSetting('lessonSpace', 'enabled');
173 $lessonSpaceCompanyId = $settingsDS->getSetting('lessonSpace', 'companyId');
174 if ($lessonSpaceEnabled && $lessonSpaceApiKey && $lessonSpaceCompanyId) {
175 /** @var AbstractLessonSpaceService $lessonSpaceService */
176 $lessonSpaceService = $this->container->get('infrastructure.lesson.space.service');
177 $spaceId = explode("https://www.thelessonspace.com/space/", $appointmentArray['lessonSpace']);
178 if ($spaceId && count($spaceId) > 1) {
179 $appointmentArray['lessonSpaceDetails'] = $lessonSpaceService->getSpace($lessonSpaceApiKey, $lessonSpaceCompanyId, $spaceId[1]);
180 }
181 }
182 }
183
184 if (isset($appointmentArray['notifyParticipants'])) {
185 $appointmentArray['notifyParticipants'] = intval($appointmentArray['notifyParticipants']);
186 }
187 if (isset($appointmentArray['createPaymentLinks'])) {
188 $appointmentArray['createPaymentLinks'] = intval($appointmentArray['createPaymentLinks']);
189 }
190
191 $appointmentArray = apply_filters('amelia_get_appointment_filter', $appointmentArray);
192
193 do_action('amelia_get_appointment', $appointmentArray);
194
195
196 $result->setResult(CommandResult::RESULT_SUCCESS);
197 $result->setMessage('Successfully retrieved appointment');
198 $result->setData(
199 [
200 Entities::APPOINTMENT => $appointmentArray,
201 'recurring' => $recurringAppointments->toArray()
202 ]
203 );
204
205 return $result;
206 }
207 }
208