| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace App\Jobs;
- use App\Enums\PushNotificationTargetEnum;
- use App\Models\User;
- use App\Notifications\Push\Manual\ManualPush;
- use App\Services\PushNotificationService;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\Log;
- class SendManualPushJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public int $tries = 2;
- public int $timeout = 120;
- public array $backoff = [15, 60];
- /**
- * @param int[] $userIds
- */
- public function __construct(
- public readonly array $userIds,
- public readonly string $title,
- public readonly string $body,
- public readonly PushNotificationTargetEnum $target,
- ) {}
- public function handle(): void
- {
- try {
- $pushService = app(PushNotificationService::class);
- } catch (\Throwable $exception) {
- Log::error('Falha ao resolver PushNotificationService na push manual', [
- 'error' => $exception->getMessage(),
- ]);
- return;
- }
- $notification = new ManualPush($this->title, $this->body, $this->target);
- foreach (User::whereIn('id', $this->userIds)->get() as $user) {
- try {
- $pushService->sendToUser($user, $notification);
- } catch (\Throwable $exception) {
- Log::error('Falha ao enviar push manual', [
- 'user_id' => $user->id,
- 'target' => $this->target->value,
- 'error' => $exception->getMessage(),
- ]);
- }
- }
- }
- }
|