SendOpportunityPushJob.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\User;
  4. use App\Notifications\Push\Prestador\Agendamento\NovaOportunidadePush;
  5. use App\Services\PushNotificationService;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Illuminate\Support\Facades\Log;
  12. class SendOpportunityPushJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. public int $tries = 2;
  16. public int $timeout = 30;
  17. public array $backoff = [15, 60];
  18. public function __construct(
  19. public readonly int $userId,
  20. public readonly int $scheduleId,
  21. public readonly ?string $district = null,
  22. public readonly ?string $dateLabel = null,
  23. public readonly int $quantity = 1,
  24. ) {}
  25. public function handle(PushNotificationService $pushNotificationService): void
  26. {
  27. $user = User::find($this->userId);
  28. Log::info('----------entrou disparo-------------');
  29. if (! $user) {
  30. return;
  31. }
  32. Log::info('tem user');
  33. try {
  34. $pushNotificationService->sendToUser(
  35. $user,
  36. new NovaOportunidadePush($this->district, $this->dateLabel, $this->quantity)
  37. );
  38. } catch (\Throwable $exception) {
  39. Log::error('Falha ao enviar push de nova oportunidade', [
  40. 'user_id' => $this->userId,
  41. 'schedule_id' => $this->scheduleId,
  42. 'error' => $exception->getMessage(),
  43. ]);
  44. }
  45. }
  46. }