12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Listeners;
- use App\Events\ObjectActionCreate;
- use App\Models\Enums\ActionObjectType;
- use App\Models\Enums\NotificationObjectType;
- use App\Models\Enums\ObjectAction;
- use App\Models\Notification;
- use App\Models\NotificationRecord;
- use App\Repositories\ConfigRepository;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Queue\InteractsWithQueue;
- class SendActionBrowserNotification implements ShouldQueue
- {
- /**
- * Create the event listener.
- */
- public function __construct()
- {
- //
- }
- /**
- * Handle the event.
- */
- public function handle(ObjectActionCreate $event): void
- {
- if(! ConfigRepository::openBrowserNotification()){
- return;
- }
- $actionObjectType = ActionObjectType::tryFrom($event->action->object_type);
- $object = $actionObjectType->modelBuilder()->find($event->action->object_id);
- if (! $object) {
- return;
- }
- $userIds = match ($actionObjectType) {
- ActionObjectType::TASK => $object->assign > 0 ? [$object->assign] : [],
- ActionObjectType::REQUIREMENT=>$object->reviewed_by > 0 ? [$object->reviewed_by ] :[],
- default => [],
- };
- if (! $userIds) {
- return;
- }
- $notification = Notification::query()->create([
- 'object_type' => NotificationObjectType::ACTION->value,
- 'object_id' => $event->action->id,
- ]);
- foreach ($userIds as $userId) {
- NotificationRecord::query()->create([
- 'notification_id' => $notification->id,
- 'user_id' => $userId,
- ]);
- }
- }
- public function shouldQueue(ObjectActionCreate $event): bool
- {
- $actionObjectType = ActionObjectType::tryFrom($event->action->object_type);
- $objectAction = ObjectAction::tryFrom($event->action->action);
- if (! $actionObjectType || !$objectAction) {
- return false;
- }
- return in_array($objectAction->value, $event->notificationSetting[$actionObjectType->value]['browser'] ?? []);
- }
- }
|