SendActionBrowserNotification.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\ObjectActionCreate;
  4. use App\Models\Enums\ActionObjectType;
  5. use App\Models\Enums\NotificationObjectType;
  6. use App\Models\Enums\ObjectAction;
  7. use App\Models\Notification;
  8. use App\Models\NotificationRecord;
  9. use App\Repositories\ConfigRepository;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. class SendActionBrowserNotification implements ShouldQueue
  13. {
  14. /**
  15. * Create the event listener.
  16. */
  17. public function __construct()
  18. {
  19. //
  20. }
  21. /**
  22. * Handle the event.
  23. */
  24. public function handle(ObjectActionCreate $event): void
  25. {
  26. if(! ConfigRepository::openBrowserNotification()){
  27. return;
  28. }
  29. $actionObjectType = ActionObjectType::tryFrom($event->action->object_type);
  30. $object = $actionObjectType->modelBuilder()->find($event->action->object_id);
  31. if (! $object) {
  32. return;
  33. }
  34. $userIds = match ($actionObjectType) {
  35. ActionObjectType::TASK => $object->assign > 0 ? [$object->assign] : [],
  36. default => [],
  37. };
  38. if (! $userIds) {
  39. return;
  40. }
  41. $notification = Notification::query()->create([
  42. 'object_type' => NotificationObjectType::ACTION->value,
  43. 'object_id' => $event->action->id,
  44. ]);
  45. foreach ($userIds as $userId) {
  46. NotificationRecord::query()->create([
  47. 'notification_id' => $notification->id,
  48. 'user_id' => $userId,
  49. ]);
  50. }
  51. }
  52. public function shouldQueue(ObjectActionCreate $event): bool
  53. {
  54. $actionObjectType = ActionObjectType::tryFrom($event->action->object_type);
  55. $objectAction = ObjectAction::tryFrom($event->action->action);
  56. if (! $actionObjectType || !$objectAction) {
  57. return false;
  58. }
  59. return in_array($objectAction->value, $event->notificationSetting[$actionObjectType->value]['email'] ?? []);
  60. }
  61. }