GetNotificationsCommand.php
1 year ago
GetNotificationsCommandHandler.php
6 months ago
GetSMSNotificationsHistoryCommand.php
1 year ago
GetSMSNotificationsHistoryCommandHandler.php
6 months ago
SendAmeliaSmsApiRequestCommand.php
1 year ago
SendAmeliaSmsApiRequestCommandHandler.php
2 years ago
SendTestEmailCommand.php
1 year ago
SendTestEmailCommandHandler.php
6 months ago
SendUndeliveredNotificationsCommand.php
1 year ago
SendUndeliveredNotificationsCommandHandler.php
2 years ago
UpdateNotificationCommand.php
1 year ago
UpdateNotificationCommandHandler.php
2 weeks ago
UpdateNotificationStatusCommand.php
1 year ago
UpdateNotificationStatusCommandHandler.php
6 months ago
UpdateSMSNotificationHistoryCommand.php
1 year ago
UpdateSMSNotificationHistoryCommandHandler.php
6 months ago
UpdateSMSNotificationHistoryDirectlyCommand.php
6 months ago
UpdateSMSNotificationHistoryDirectlyCommandHandler.php
6 months ago
ValidateSMTPCredentialsCommand.php
4 months ago
ValidateSMTPCredentialsCommandHandler.php
4 months ago
UpdateNotificationCommandHandler.php
165 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Notification; |
| 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\Entity\EntityApplicationService; |
| 9 | use AmeliaBooking\Application\Services\Notification\NotificationHelperService; |
| 10 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 11 | use AmeliaBooking\Domain\Entity\Entities; |
| 12 | use AmeliaBooking\Domain\Entity\Notification\Notification; |
| 13 | use AmeliaBooking\Domain\Factory\Notification\NotificationFactory; |
| 14 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 15 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 16 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 17 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 18 | use AmeliaBooking\Infrastructure\Repository\Notification\NotificationRepository; |
| 19 | use AmeliaBooking\Infrastructure\Repository\Notification\NotificationsToEntitiesRepository; |
| 20 | use Interop\Container\Exception\ContainerException; |
| 21 | use Slim\Exception\ContainerValueNotFoundException; |
| 22 | |
| 23 | /** |
| 24 | * Class UpdateNotificationCommandHandler |
| 25 | * |
| 26 | * @package AmeliaBooking\Application\Commands\Notification |
| 27 | */ |
| 28 | class UpdateNotificationCommandHandler extends CommandHandler |
| 29 | { |
| 30 | public $mandatoryFields = [ |
| 31 | 'subject', |
| 32 | 'content' |
| 33 | ]; |
| 34 | |
| 35 | /** |
| 36 | * @param UpdateNotificationCommand $command |
| 37 | * |
| 38 | * @return CommandResult |
| 39 | * @throws ContainerValueNotFoundException |
| 40 | * @throws QueryExecutionException |
| 41 | * @throws NotFoundException |
| 42 | * @throws InvalidArgumentException |
| 43 | * @throws AccessDeniedException |
| 44 | * @throws ContainerException |
| 45 | */ |
| 46 | public function handle(UpdateNotificationCommand $command) |
| 47 | { |
| 48 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::NOTIFICATIONS)) { |
| 49 | throw new AccessDeniedException('You are not allowed to update notification'); |
| 50 | } |
| 51 | |
| 52 | $notificationId = (int)$command->getArg('id'); |
| 53 | |
| 54 | $result = new CommandResult(); |
| 55 | |
| 56 | $this->checkMandatoryFields($command); |
| 57 | |
| 58 | $notificationData = $command->getFields(); |
| 59 | |
| 60 | if (empty($notificationData['entityIds'])) { |
| 61 | $notificationData['entityIds'] = []; |
| 62 | } |
| 63 | |
| 64 | /** @var EntityApplicationService $entityService */ |
| 65 | $entityService = $this->container->get('application.entity.service'); |
| 66 | |
| 67 | $entityService->removeMissingEntitiesForNotification($notificationData); |
| 68 | |
| 69 | /** @var NotificationRepository $notificationRepo */ |
| 70 | $notificationRepo = $this->container->get('domain.notification.repository'); |
| 71 | /** @var NotificationsToEntitiesRepository $notificationEntitiesRepo */ |
| 72 | $notificationEntitiesRepo = $this->container->get('domain.notificationEntities.repository'); |
| 73 | /** @var EventRepository $eventRepo */ |
| 74 | $eventRepo = $this->container->get('domain.booking.event.repository'); |
| 75 | /** @var NotificationHelperService $notificationHelper */ |
| 76 | $notificationHelper = $this->container->get('application.notificationHelper.service'); |
| 77 | |
| 78 | /** @var Notification $currentNotification */ |
| 79 | $currentNotification = $notificationRepo->getById($notificationId); |
| 80 | $currentEntityList = $notificationEntitiesRepo->getEntities($notificationId); |
| 81 | |
| 82 | $content = $command->getField('content'); |
| 83 | |
| 84 | if ($command->getField('type') === 'email') { |
| 85 | // If content has paragraph tags, only remove newlines between HTML tags (formatting) |
| 86 | // Otherwise convert newlines to <br> tags for plain text content |
| 87 | if (strpos($content, '<p>') !== false || strpos($content, '<P>') !== false) { |
| 88 | // WYSIWYG mode: strip line breaks/indentation between tags only; keep spaces (e.g. between inline elements) |
| 89 | $content = preg_replace('/>(?:\s*\R\s*)+</', '><', $content); |
| 90 | } else { |
| 91 | // Plain text/HTML mode: convert newlines to <br> tags |
| 92 | $content = nl2br($content); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if ($command->getField('type') !== 'whatsapp') { |
| 97 | $contentRes = $notificationHelper->parseAndReplace($content); |
| 98 | $parsedContent = $contentRes[0]; |
| 99 | $content = $contentRes[1]; |
| 100 | } |
| 101 | |
| 102 | $isCustom = $command->getField('customName') !== null; |
| 103 | |
| 104 | $notificationData['id'] = $notificationId; |
| 105 | $notificationData['name'] = $isCustom ? $command->getField('name') : $currentNotification->getName()->getValue(); |
| 106 | $notificationData['status'] = $command->getField('status') ?: $currentNotification->getStatus()->getValue(); |
| 107 | $notificationData['type'] = $currentNotification->getType()->getValue(); |
| 108 | $notificationData['sendTo'] = $currentNotification->getSendTo()->getValue(); |
| 109 | $notificationData['content'] = $content; |
| 110 | |
| 111 | $notificationData = apply_filters('amelia_before_notification_updated_filter', $notificationData); |
| 112 | |
| 113 | do_action('amelia_before_notification_updated', $notificationData); |
| 114 | |
| 115 | /** @var Notification $notification */ |
| 116 | $notification = NotificationFactory::create($notificationData); |
| 117 | |
| 118 | $minimumTime = $command->getField('minimumTimeBeforeBooking'); |
| 119 | if (!empty($minimumTime) && json_encode($minimumTime)) { |
| 120 | $notification->setMinimumTimeBeforeBooking(new Json(json_encode($minimumTime))); |
| 121 | } |
| 122 | |
| 123 | if (!$notification instanceof Notification) { |
| 124 | $result->setResult(CommandResult::RESULT_ERROR); |
| 125 | $result->setMessage('Could not update notification entity.'); |
| 126 | |
| 127 | return $result; |
| 128 | } |
| 129 | |
| 130 | if ($notificationRepo->update($notificationId, $notification)) { |
| 131 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 132 | $result->setMessage('Successfully updated notification.'); |
| 133 | $result->setData( |
| 134 | [ |
| 135 | Entities::NOTIFICATION => $notification->toArray(), |
| 136 | 'update' => !empty($parsedContent) |
| 137 | ] |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | if ($notification->getCustomName()) { |
| 142 | $removeEntities = array_diff($currentEntityList, $notification->getEntityIds()); |
| 143 | $addEntities = array_diff($notification->getEntityIds(), $currentEntityList); |
| 144 | |
| 145 | foreach ($removeEntities as $removeEntity) { |
| 146 | $notificationEntitiesRepo->removeEntity($notificationId, $removeEntity, $notification->getEntity()->getValue()); |
| 147 | } |
| 148 | foreach ($addEntities as $addEntity) { |
| 149 | $recurringMain = null; |
| 150 | if ($notification->getEntity()->getValue() === Entities::EVENT) { |
| 151 | $recurring = $eventRepo->isRecurring($addEntity); |
| 152 | if ($recurring['event_recurringOrder'] !== null) { |
| 153 | $recurringMain = $recurring['event_recurringOrder'] === 1 ? $addEntity : $recurring['event_parentId']; |
| 154 | } |
| 155 | } |
| 156 | $notificationEntitiesRepo->addEntity($notificationId, $recurringMain ?: $addEntity, $notification->getEntity()->getValue()); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | do_action('amelia_after_notification_updated', $notification->toArray()); |
| 161 | |
| 162 | return $result; |
| 163 | } |
| 164 | } |
| 165 |