AbstractNotificationService.php
2 months ago
AbstractWhatsAppNotificationService.php
3 months ago
ApplicationNotificationService.php
2 months ago
AppointmentNotificationService.php
2 weeks ago
BasicWhatsAppNotificationService.php
6 months ago
EmailNotificationService.php
1 month ago
NotificationHelperService.php
6 months ago
SMSAPIService.php
2 months ago
SMSNotificationService.php
1 month ago
ApplicationNotificationService.php
380 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Services\Notification; |
| 9 | |
| 10 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 11 | use AmeliaBooking\Domain\Collection\Collection; |
| 12 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 13 | use AmeliaBooking\Domain\Entity\Booking\Appointment\Appointment; |
| 14 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 15 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 16 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 17 | use AmeliaBooking\Infrastructure\Common\Container; |
| 18 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 19 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 20 | |
| 21 | /** |
| 22 | * Class ApplicationNotificationService |
| 23 | * |
| 24 | * @package AmeliaBooking\Application\Services\Notification |
| 25 | */ |
| 26 | class ApplicationNotificationService |
| 27 | { |
| 28 | protected $container; |
| 29 | |
| 30 | /** |
| 31 | * ApplicationNotificationService constructor. |
| 32 | * |
| 33 | * @param Container $container |
| 34 | */ |
| 35 | public function __construct(Container $container) |
| 36 | { |
| 37 | $this->container = $container; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param Appointment $appointment |
| 42 | * @param bool $logNotification |
| 43 | * |
| 44 | * @throws InvalidArgumentException |
| 45 | * @throws QueryExecutionException |
| 46 | */ |
| 47 | public function sendAppointmentProviderStatusNotifications( |
| 48 | $appointment, |
| 49 | $logNotification = true |
| 50 | ) { |
| 51 | /** @var SettingsService $settingsService */ |
| 52 | $settingsService = $this->container->get('domain.settings.service'); |
| 53 | |
| 54 | /** @var AppointmentNotificationService $appointmentNotificationService */ |
| 55 | $appointmentNotificationService = $this->container->get('application.notification.appointment.service'); |
| 56 | |
| 57 | /** @var EmailNotificationService $emailNotificationService */ |
| 58 | $emailNotificationService = $this->container->get('application.emailNotification.service'); |
| 59 | |
| 60 | /** @var SMSNotificationService $smsNotificationService */ |
| 61 | $smsNotificationService = $this->container->get('application.smsNotification.service'); |
| 62 | |
| 63 | /** @var AbstractWhatsAppNotificationService $whatsAppNotificationService */ |
| 64 | $whatsAppNotificationService = $this->container->get('application.whatsAppNotification.service'); |
| 65 | |
| 66 | $appointmentNotificationService->sendProviderStatusNotifications( |
| 67 | $emailNotificationService, |
| 68 | $appointment, |
| 69 | $logNotification |
| 70 | ); |
| 71 | |
| 72 | if ($settingsService->getSetting('notifications', 'smsSignedIn') === true) { |
| 73 | $appointmentNotificationService->sendProviderStatusNotifications( |
| 74 | $smsNotificationService, |
| 75 | $appointment, |
| 76 | $logNotification |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | if ($whatsAppNotificationService->checkRequiredFields()) { |
| 81 | $appointmentNotificationService->sendProviderStatusNotifications( |
| 82 | $whatsAppNotificationService, |
| 83 | $appointment, |
| 84 | $logNotification |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param Appointment $appointment |
| 91 | * @param Collection $bookings |
| 92 | * @param bool $logNotification |
| 93 | * @param bool $isBackend |
| 94 | * @param bool $sendInvoice |
| 95 | * |
| 96 | * @throws InvalidArgumentException |
| 97 | * @throws QueryExecutionException |
| 98 | * @throws NotFoundException |
| 99 | * @throws AccessDeniedException |
| 100 | */ |
| 101 | public function sendAppointmentCustomersStatusNotifications( |
| 102 | $appointment, |
| 103 | $bookings, |
| 104 | $logNotification = true, |
| 105 | $isBackend = true, |
| 106 | $sendInvoice = false, |
| 107 | $notifyCustomers = true |
| 108 | ) { |
| 109 | /** @var SettingsService $settingsService */ |
| 110 | $settingsService = $this->container->get('domain.settings.service'); |
| 111 | |
| 112 | /** @var AppointmentNotificationService $appointmentNotificationService */ |
| 113 | $appointmentNotificationService = $this->container->get('application.notification.appointment.service'); |
| 114 | |
| 115 | /** @var EmailNotificationService $emailNotificationService */ |
| 116 | $emailNotificationService = $this->container->get('application.emailNotification.service'); |
| 117 | |
| 118 | /** @var SMSNotificationService $smsNotificationService */ |
| 119 | $smsNotificationService = $this->container->get('application.smsNotification.service'); |
| 120 | |
| 121 | /** @var AbstractWhatsAppNotificationService $whatsAppNotificationService */ |
| 122 | $whatsAppNotificationService = $this->container->get('application.whatsAppNotification.service'); |
| 123 | |
| 124 | $appointmentNotificationService->sendCustomersStatusNotifications( |
| 125 | $emailNotificationService, |
| 126 | $appointment, |
| 127 | $bookings, |
| 128 | $logNotification, |
| 129 | $isBackend, |
| 130 | $sendInvoice, |
| 131 | $notifyCustomers |
| 132 | ); |
| 133 | |
| 134 | if ($settingsService->getSetting('notifications', 'smsSignedIn') === true) { |
| 135 | $appointmentNotificationService->sendCustomersStatusNotifications( |
| 136 | $smsNotificationService, |
| 137 | $appointment, |
| 138 | $bookings, |
| 139 | $logNotification, |
| 140 | $isBackend, |
| 141 | $sendInvoice |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | if ($whatsAppNotificationService->checkRequiredFields()) { |
| 146 | $appointmentNotificationService->sendCustomersStatusNotifications( |
| 147 | $whatsAppNotificationService, |
| 148 | $appointment, |
| 149 | $bookings, |
| 150 | $logNotification, |
| 151 | $isBackend, |
| 152 | $sendInvoice |
| 153 | ); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @param Appointment $appointment |
| 159 | * @param bool $notifyProvider |
| 160 | * @param bool $notifyCustomers |
| 161 | * |
| 162 | * @throws QueryExecutionException |
| 163 | * @throws InvalidArgumentException |
| 164 | * @throws NotFoundException |
| 165 | */ |
| 166 | public function sendAppointmentRescheduleNotifications( |
| 167 | $appointment, |
| 168 | $notifyProvider = true, |
| 169 | $notifyCustomers = true |
| 170 | ) { |
| 171 | /** @var SettingsService $settingsService */ |
| 172 | $settingsService = $this->container->get('domain.settings.service'); |
| 173 | |
| 174 | /** @var AppointmentNotificationService $appointmentNotificationService */ |
| 175 | $appointmentNotificationService = $this->container->get('application.notification.appointment.service'); |
| 176 | |
| 177 | /** @var EmailNotificationService $emailNotificationService */ |
| 178 | $emailNotificationService = $this->container->get('application.emailNotification.service'); |
| 179 | |
| 180 | /** @var SMSNotificationService $smsNotificationService */ |
| 181 | $smsNotificationService = $this->container->get('application.smsNotification.service'); |
| 182 | |
| 183 | /** @var AbstractWhatsAppNotificationService $whatsAppNotificationService */ |
| 184 | $whatsAppNotificationService = $this->container->get('application.whatsAppNotification.service'); |
| 185 | |
| 186 | $appointmentNotificationService->sendRescheduledNotifications( |
| 187 | $emailNotificationService, |
| 188 | $appointment, |
| 189 | $notifyProvider, |
| 190 | $notifyCustomers |
| 191 | ); |
| 192 | |
| 193 | if ($settingsService->getSetting('notifications', 'smsSignedIn') === true) { |
| 194 | $appointmentNotificationService->sendRescheduledNotifications( |
| 195 | $smsNotificationService, |
| 196 | $appointment, |
| 197 | $notifyProvider, |
| 198 | $notifyCustomers |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | if ($whatsAppNotificationService->checkRequiredFields()) { |
| 203 | $appointmentNotificationService->sendRescheduledNotifications( |
| 204 | $whatsAppNotificationService, |
| 205 | $appointment, |
| 206 | $notifyProvider, |
| 207 | $notifyCustomers |
| 208 | ); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Wrapper for sending waiting list available spot notifications through all active channels. |
| 214 | * |
| 215 | * @param Appointment $appointment |
| 216 | * @param Collection $waitingBookings |
| 217 | * |
| 218 | * @throws InvalidArgumentException |
| 219 | * @throws QueryExecutionException |
| 220 | */ |
| 221 | public function sendWaitingListAvailableSpotNotifications( |
| 222 | $appointment, |
| 223 | $waitingBookings |
| 224 | ) { |
| 225 | /** @var SettingsService $settingsService */ |
| 226 | $settingsService = $this->container->get('domain.settings.service'); |
| 227 | |
| 228 | /** @var AppointmentNotificationService $appointmentNotificationService */ |
| 229 | $appointmentNotificationService = $this->container->get('application.notification.appointment.service'); |
| 230 | |
| 231 | /** @var EmailNotificationService $emailNotificationService */ |
| 232 | $emailNotificationService = $this->container->get('application.emailNotification.service'); |
| 233 | |
| 234 | /** @var SMSNotificationService $smsNotificationService */ |
| 235 | $smsNotificationService = $this->container->get('application.smsNotification.service'); |
| 236 | |
| 237 | /** @var AbstractWhatsAppNotificationService $whatsAppNotificationService */ |
| 238 | $whatsAppNotificationService = $this->container->get('application.whatsAppNotification.service'); |
| 239 | |
| 240 | $appointmentNotificationService->sendWaitingListAvailableSpotNotification( |
| 241 | $emailNotificationService, |
| 242 | $appointment, |
| 243 | $waitingBookings |
| 244 | ); |
| 245 | |
| 246 | if ($settingsService->getSetting('notifications', 'smsSignedIn') === true) { |
| 247 | $appointmentNotificationService->sendWaitingListAvailableSpotNotification( |
| 248 | $smsNotificationService, |
| 249 | $appointment, |
| 250 | $waitingBookings |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | if ($whatsAppNotificationService->checkRequiredFields()) { |
| 255 | $appointmentNotificationService->sendWaitingListAvailableSpotNotification( |
| 256 | $whatsAppNotificationService, |
| 257 | $appointment, |
| 258 | $waitingBookings |
| 259 | ); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @param Appointment $appointment |
| 265 | * @param int|null $changedProviderId |
| 266 | * @param bool $notifyProvider |
| 267 | * @param bool $notifyCustomers |
| 268 | * |
| 269 | * @throws QueryExecutionException |
| 270 | * @throws InvalidArgumentException |
| 271 | */ |
| 272 | public function sendAppointmentUpdatedNotifications( |
| 273 | $appointment, |
| 274 | $changedProviderId, |
| 275 | $notifyProvider = true, |
| 276 | $notifyCustomers = true |
| 277 | ) { |
| 278 | $appointment->setAssignedEmployeeId(new Id($appointment->getProviderId()->getValue())); |
| 279 | |
| 280 | $this->sendAppointmentUpdatedNotificationsForUserType( |
| 281 | $appointment, |
| 282 | false, |
| 283 | $notifyCustomers |
| 284 | ); |
| 285 | |
| 286 | if ($changedProviderId) { |
| 287 | $newProviderId = $appointment->getProviderId()->getValue(); |
| 288 | |
| 289 | $appointment->setProviderId(new Id($changedProviderId)); |
| 290 | } |
| 291 | |
| 292 | $this->sendAppointmentUpdatedNotificationsForUserType( |
| 293 | $appointment, |
| 294 | $notifyProvider, |
| 295 | false |
| 296 | ); |
| 297 | |
| 298 | if ($changedProviderId) { |
| 299 | $appointment->setProviderId(new Id($newProviderId)); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @param Appointment $appointment |
| 305 | * @param bool $notifyProvider |
| 306 | * @param bool $notifyCustomers |
| 307 | * |
| 308 | * @throws QueryExecutionException |
| 309 | * @throws InvalidArgumentException |
| 310 | */ |
| 311 | private function sendAppointmentUpdatedNotificationsForUserType( |
| 312 | $appointment, |
| 313 | $notifyProvider = true, |
| 314 | $notifyCustomers = true |
| 315 | ) { |
| 316 | /** @var SettingsService $settingsService */ |
| 317 | $settingsService = $this->container->get('domain.settings.service'); |
| 318 | |
| 319 | /** @var AppointmentNotificationService $appointmentNotificationService */ |
| 320 | $appointmentNotificationService = $this->container->get('application.notification.appointment.service'); |
| 321 | |
| 322 | /** @var EmailNotificationService $emailNotificationService */ |
| 323 | $emailNotificationService = $this->container->get('application.emailNotification.service'); |
| 324 | |
| 325 | /** @var SMSNotificationService $smsNotificationService */ |
| 326 | $smsNotificationService = $this->container->get('application.smsNotification.service'); |
| 327 | |
| 328 | /** @var AbstractWhatsAppNotificationService $whatsAppNotificationService */ |
| 329 | $whatsAppNotificationService = $this->container->get('application.whatsAppNotification.service'); |
| 330 | |
| 331 | $appointmentNotificationService->sendUpdatedNotifications( |
| 332 | $emailNotificationService, |
| 333 | $appointment, |
| 334 | $notifyProvider, |
| 335 | $notifyCustomers |
| 336 | ); |
| 337 | |
| 338 | if ($settingsService->getSetting('notifications', 'smsSignedIn') === true) { |
| 339 | $appointmentNotificationService->sendUpdatedNotifications( |
| 340 | $smsNotificationService, |
| 341 | $appointment, |
| 342 | $notifyProvider, |
| 343 | $notifyCustomers |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | if ($whatsAppNotificationService->checkRequiredFields()) { |
| 348 | $appointmentNotificationService->sendUpdatedNotifications( |
| 349 | $whatsAppNotificationService, |
| 350 | $appointment, |
| 351 | $notifyProvider, |
| 352 | $notifyCustomers |
| 353 | ); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * @param Event $event |
| 359 | * @param string $bookingKey |
| 360 | * |
| 361 | * @throws InvalidArgumentException |
| 362 | * @throws QueryExecutionException |
| 363 | */ |
| 364 | public function sendEventQrNotification($event, $bookingKey) |
| 365 | { |
| 366 | /** @var AppointmentNotificationService $appointmentNotificationService */ |
| 367 | $appointmentNotificationService = $this->container->get('application.notification.appointment.service'); |
| 368 | |
| 369 | /** @var EmailNotificationService $emailNotificationService */ |
| 370 | $emailNotificationService = $this->container->get('application.emailNotification.service'); |
| 371 | |
| 372 | $appointmentNotificationService->sendQrNotifications( |
| 373 | $emailNotificationService, |
| 374 | $event, |
| 375 | $bookingKey, |
| 376 | true |
| 377 | ); |
| 378 | } |
| 379 | } |
| 380 |