SendActionBrowserNotification.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. ActionObjectType::REQUIREMENT=>$object->reviewed_by > 0 ? [$object->reviewed_by ] :[],
  37. default => [],
  38. };
  39. if (! $userIds) {
  40. return;
  41. }
  42. $notification = Notification::query()->create([
  43. 'object_type' => NotificationObjectType::ACTION->value,
  44. 'object_id' => $event->action->id,
  45. ]);
  46. foreach ($userIds as $userId) {
  47. NotificationRecord::query()->create([
  48. 'notification_id' => $notification->id,
  49. 'user_id' => $userId,
  50. ]);
  51. }
  52. }
  53. public function shouldQueue(ObjectActionCreate $event): bool
  54. {
  55. $actionObjectType = ActionObjectType::tryFrom($event->action->object_type);
  56. $objectAction = ObjectAction::tryFrom($event->action->action);
  57. if (! $actionObjectType || !$objectAction) {
  58. return false;
  59. }
  60. return in_array($objectAction->value, $event->notificationSetting[$actionObjectType->value]['browser'] ?? []);
  61. }
  62. }