Tag
2 months ago
AddEventCommand.php
1 year ago
AddEventCommandHandler.php
2 weeks ago
DeleteEventBookingCommand.php
7 years ago
DeleteEventBookingCommandHandler.php
3 months ago
DeleteEventCommand.php
1 year ago
DeleteEventCommandHandler.php
6 months ago
DeleteEventsCommand.php
6 months ago
DeleteEventsCommandHandler.php
3 months ago
GetCalendarEventsCommand.php
1 year ago
GetCalendarEventsCommandHandler.php
1 year ago
GetEventBookingCommand.php
6 months ago
GetEventBookingCommandHandler.php
2 weeks ago
GetEventBookingsCommand.php
1 year ago
GetEventBookingsCommandHandler.php
3 months ago
GetEventCommand.php
7 years ago
GetEventCommandHandler.php
3 months ago
GetEventsCommand.php
1 year ago
GetEventsCommandHandler.php
2 weeks ago
UpdateEventBookingCommand.php
7 years ago
UpdateEventBookingCommandHandler.php
6 months ago
UpdateEventCommand.php
1 year ago
UpdateEventCommandHandler.php
3 months ago
UpdateEventStatusCommand.php
1 year ago
UpdateEventStatusCommandHandler.php
6 months ago
UpdateEventVisibilityCommand.php
6 months ago
UpdateEventVisibilityCommandHandler.php
6 months ago
UpdateEventCommandHandler.php
301 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Booking\Event; |
| 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\EventApplicationService; |
| 9 | use AmeliaBooking\Application\Services\Entity\EntityApplicationService; |
| 10 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 11 | use AmeliaBooking\Application\Services\Zoom\AbstractZoomApplicationService; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 13 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 14 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 15 | use AmeliaBooking\Domain\Entity\Booking\Event\EventPeriod; |
| 16 | use AmeliaBooking\Domain\Entity\Entities; |
| 17 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 18 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 19 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 20 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 21 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 22 | use Exception; |
| 23 | use Interop\Container\Exception\ContainerException; |
| 24 | use Slim\Exception\ContainerValueNotFoundException; |
| 25 | |
| 26 | /** |
| 27 | * Class UpdateEventCommandHandler |
| 28 | * |
| 29 | * @package AmeliaBooking\Application\Commands\Booking\Event |
| 30 | */ |
| 31 | class UpdateEventCommandHandler extends CommandHandler |
| 32 | { |
| 33 | /** |
| 34 | * @var array |
| 35 | */ |
| 36 | public $mandatoryFields = [ |
| 37 | 'id', |
| 38 | 'name', |
| 39 | 'periods' |
| 40 | ]; |
| 41 | |
| 42 | /** |
| 43 | * @param UpdateEventCommand $command |
| 44 | * |
| 45 | * @return CommandResult |
| 46 | * @throws ContainerValueNotFoundException |
| 47 | * @throws QueryExecutionException |
| 48 | * @throws InvalidArgumentException |
| 49 | * @throws AccessDeniedException |
| 50 | * @throws ContainerException |
| 51 | * @throws Exception |
| 52 | */ |
| 53 | public function handle(UpdateEventCommand $command) |
| 54 | { |
| 55 | $result = new CommandResult(); |
| 56 | |
| 57 | $this->checkMandatoryFields($command); |
| 58 | |
| 59 | $eventData = $command->getFields(); |
| 60 | |
| 61 | /** @var EventRepository $eventRepository */ |
| 62 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 63 | /** @var EventApplicationService $eventApplicationService */ |
| 64 | $eventApplicationService = $this->container->get('application.booking.event.service'); |
| 65 | /** @var UserApplicationService $userAS */ |
| 66 | $userAS = $this->getContainer()->get('application.user.service'); |
| 67 | /** @var SettingsService $settingsDS */ |
| 68 | $settingsDS = $this->container->get('domain.settings.service'); |
| 69 | /** @var EntityApplicationService $entityService */ |
| 70 | $entityService = $this->container->get('application.entity.service'); |
| 71 | |
| 72 | try { |
| 73 | /** @var AbstractUser $user */ |
| 74 | $user = $command->getUserApplicationService()->authorization( |
| 75 | $command->getPage() === 'cabinet' ? $command->getToken() : null, |
| 76 | $command->getCabinetType() |
| 77 | ); |
| 78 | } catch (AuthorizationException $e) { |
| 79 | $result->setResult(CommandResult::RESULT_ERROR); |
| 80 | $result->setData( |
| 81 | [ |
| 82 | 'reauthorize' => true |
| 83 | ] |
| 84 | ); |
| 85 | |
| 86 | return $result; |
| 87 | } |
| 88 | |
| 89 | if ( |
| 90 | $userAS->isCustomer($user) || |
| 91 | ( |
| 92 | $userAS->isProvider($user) && !$settingsDS->getSetting('roles', 'allowWriteEvents') |
| 93 | ) |
| 94 | ) { |
| 95 | throw new AccessDeniedException('You are not allowed to update an event'); |
| 96 | } |
| 97 | |
| 98 | $entityService->removeMissingEntitiesForEvent($eventData); |
| 99 | |
| 100 | |
| 101 | /** @var Event $oldEvent */ |
| 102 | $oldEvent = $eventApplicationService->getEventById( |
| 103 | $eventData['id'], |
| 104 | [ |
| 105 | 'fetchEventsPeriods' => true, |
| 106 | 'fetchEventsTickets' => true, |
| 107 | 'fetchEventsTags' => true, |
| 108 | 'fetchEventsProviders' => true, |
| 109 | 'fetchEventsImages' => true, |
| 110 | 'fetchBookings' => true, |
| 111 | 'fetchBookingsTickets' => true, |
| 112 | 'fetchBookingsUsers' => true, |
| 113 | 'fetchBookingsPayments' => true, |
| 114 | ] |
| 115 | ); |
| 116 | |
| 117 | $eventData = |
| 118 | apply_filters('amelia_before_event_updated_filter', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally')); |
| 119 | |
| 120 | do_action('amelia_before_event_updated', $eventData, $oldEvent ? $oldEvent->toArray() : null, $command->getField('applyGlobally')); |
| 121 | |
| 122 | try { |
| 123 | /** @var Event $event */ |
| 124 | $event = $eventApplicationService->build($eventData); |
| 125 | } catch (Exception $e) { |
| 126 | $result->setResult(CommandResult::RESULT_ERROR); |
| 127 | $result->setMessage($e->getMessage()); |
| 128 | $result->setData(['message' => $e->getMessage()]); |
| 129 | return $result; |
| 130 | } |
| 131 | |
| 132 | if (!empty($oldEvent->getPicture()) && $command->getPage() === 'cabinet') { |
| 133 | $event->setPicture($oldEvent->getPicture()); |
| 134 | } |
| 135 | |
| 136 | /** @var DateTimeValue $newUntil */ |
| 137 | $newUntil = $event->getRecurring() |
| 138 | ? $event->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0) |
| 139 | : null; |
| 140 | |
| 141 | /** @var DateTimeValue $oldUntil */ |
| 142 | $oldUntil = $oldEvent->getRecurring() |
| 143 | ? $oldEvent->getRecurring()->getUntil()->getValue()->setTime(0, 0, 0) |
| 144 | : null; |
| 145 | |
| 146 | if ( |
| 147 | $oldEvent->getRecurring() && |
| 148 | $event->getRecurring() && |
| 149 | ( |
| 150 | $newUntil < $oldUntil || |
| 151 | $event->getRecurring()->getCycle()->getValue() !== $oldEvent->getRecurring()->getCycle()->getValue() |
| 152 | ) |
| 153 | ) { |
| 154 | $result->setResult(CommandResult::RESULT_ERROR); |
| 155 | $result->setMessage('Could not update event'); |
| 156 | |
| 157 | return $result; |
| 158 | } |
| 159 | |
| 160 | $event->setBookings($oldEvent->getBookings()); |
| 161 | |
| 162 | /** @var EventPeriod $oldEventPeriod */ |
| 163 | foreach ($oldEvent->getPeriods()->getItems() as $oldEventPeriod) { |
| 164 | /** @var EventPeriod $eventPeriod */ |
| 165 | foreach ($event->getPeriods()->getItems() as $eventPeriod) { |
| 166 | if ( |
| 167 | $eventPeriod->getId() && |
| 168 | $oldEventPeriod->getId()->getValue() === $eventPeriod->getId()->getValue() |
| 169 | ) { |
| 170 | if ($oldEventPeriod->getZoomMeeting()) { |
| 171 | $eventPeriod->setZoomMeeting($oldEventPeriod->getZoomMeeting()); |
| 172 | } |
| 173 | if ($oldEventPeriod->getLessonSpace()) { |
| 174 | $eventPeriod->setLessonSpace($oldEventPeriod->getLessonSpace()); |
| 175 | } |
| 176 | if ($oldEventPeriod->getGoogleCalendarEventId()) { |
| 177 | $eventPeriod->setGoogleCalendarEventId($oldEventPeriod->getGoogleCalendarEventId()); |
| 178 | } |
| 179 | if ($oldEventPeriod->getGoogleMeetUrl()) { |
| 180 | $eventPeriod->setGoogleMeetUrl($oldEventPeriod->getGoogleMeetUrl()); |
| 181 | } |
| 182 | if ($oldEventPeriod->getOutlookCalendarEventId()) { |
| 183 | $eventPeriod->setOutlookCalendarEventId($oldEventPeriod->getOutlookCalendarEventId()); |
| 184 | } |
| 185 | if ($oldEventPeriod->getMicrosoftTeamsUrl()) { |
| 186 | $eventPeriod->setMicrosoftTeamsUrl($oldEventPeriod->getMicrosoftTeamsUrl()); |
| 187 | } |
| 188 | if ($oldEventPeriod->getAppleCalendarEventId()) { |
| 189 | $eventPeriod->setAppleCalendarEventId($oldEventPeriod->getAppleCalendarEventId()); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | $eventRepository->beginTransaction(); |
| 196 | |
| 197 | try { |
| 198 | $parsedEvents = $eventApplicationService->update( |
| 199 | $oldEvent, |
| 200 | $event, |
| 201 | $command->getField('applyGlobally') |
| 202 | ); |
| 203 | } catch (QueryExecutionException $e) { |
| 204 | $eventRepository->rollback(); |
| 205 | throw $e; |
| 206 | } |
| 207 | |
| 208 | $eventRepository->commit(); |
| 209 | |
| 210 | do_action( |
| 211 | 'amelia_after_event_updated', |
| 212 | $event ? $event->toArray() : null, |
| 213 | $oldEvent ? $oldEvent->toArray() : null, |
| 214 | $command->getField('applyGlobally') |
| 215 | ); |
| 216 | |
| 217 | $providersRemoved = array_udiff( |
| 218 | $oldEvent->getProviders()->getItems(), |
| 219 | $event->getProviders()->getItems(), |
| 220 | function ($a, $b) { |
| 221 | if ($a->getId()->getValue() == $b->getId()->getValue()) { |
| 222 | return 0; |
| 223 | } else { |
| 224 | return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1); |
| 225 | } |
| 226 | } |
| 227 | ); |
| 228 | |
| 229 | $providersAdded = array_udiff( |
| 230 | $event->getProviders()->getItems(), |
| 231 | $oldEvent->getProviders()->getItems(), |
| 232 | function ($a, $b) { |
| 233 | if ($a->getId()->getValue() == $b->getId()->getValue()) { |
| 234 | return 0; |
| 235 | } else { |
| 236 | return ($a->getId()->getValue() < $b->getId()->getValue() ? -1 : 1); |
| 237 | } |
| 238 | } |
| 239 | ); |
| 240 | |
| 241 | $newInfo = ($event->getDescription() ? $event->getDescription()->getValue() : null) !== |
| 242 | ($oldEvent->getDescription() ? $oldEvent->getDescription()->getValue() : null) || |
| 243 | $event->getName()->getValue() !== $oldEvent->getName()->getValue(); |
| 244 | |
| 245 | $zoomUserChanged = ($event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null) !== |
| 246 | ($oldEvent->getZoomUserId() ? $oldEvent->getZoomUserId()->getValue() : null); |
| 247 | |
| 248 | $zoomUsersLicenced = false; |
| 249 | |
| 250 | if ($oldEvent->getZoomUserId() && $event->getZoomUserId() && $zoomUserChanged) { |
| 251 | /** @var AbstractZoomApplicationService $zoomService */ |
| 252 | $zoomService = $this->container->get('application.zoom.service'); |
| 253 | |
| 254 | $zoomUserType = 0; |
| 255 | $zoomOldUserType = 0; |
| 256 | $zoomResult = $zoomService->getUsers(); |
| 257 | if ( |
| 258 | !(isset($zoomResult['code']) && $zoomResult['code'] === 124) && |
| 259 | !($zoomResult['users'] === null && isset($zoomResult['message'])) |
| 260 | ) { |
| 261 | $zoomUsers = $zoomResult['users']; |
| 262 | foreach ($zoomUsers as $key => $val) { |
| 263 | if ($val['id'] === $event->getZoomUserId()->getValue()) { |
| 264 | $zoomUserType = $val['type']; |
| 265 | } |
| 266 | if ($val['id'] === $oldEvent->getZoomUserId()->getValue()) { |
| 267 | $zoomOldUserType = $val['type']; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | if ($zoomOldUserType > 1 && $zoomUserType > 1) { |
| 272 | $zoomUsersLicenced = true; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | $organizerChanged = ($event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null) |
| 277 | !== ($oldEvent->getOrganizerId() ? $oldEvent->getOrganizerId()->getValue() : null); |
| 278 | |
| 279 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 280 | $result->setMessage('Successfully updated event.'); |
| 281 | $result->setData( |
| 282 | [ |
| 283 | Entities::EVENTS => $parsedEvents, |
| 284 | 'zoomUserChanged' => $zoomUserChanged && $event->getZoomUserId() ? $event->getZoomUserId()->getValue() : null, |
| 285 | 'zoomUsersLicenced' => $zoomUsersLicenced, |
| 286 | 'newInfo' => $newInfo ? [ |
| 287 | 'name' => $event->getName(), |
| 288 | 'description' => $event->getDescription() |
| 289 | ] : null, |
| 290 | 'newProviders' => $providersAdded, |
| 291 | 'removeProviders' => $providersRemoved, |
| 292 | 'organizerChanged' => $organizerChanged, |
| 293 | 'newOrganizer' => $event->getOrganizerId() ? $event->getOrganizerId()->getValue() : null, |
| 294 | 'notifyParticipants' => $command->getField('notifyParticipants') |
| 295 | ] |
| 296 | ); |
| 297 | |
| 298 | return $result; |
| 299 | } |
| 300 | } |
| 301 |