InvalidateExpiredSchedules.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Schedule;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. class InvalidateExpiredSchedules implements ShouldQueue
  10. {
  11. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  12. public function handle(): void
  13. {
  14. $today = today();
  15. $now = now();
  16. // Agendamentos aceitos ou pendente cujo a data já passou.
  17. Schedule::query()
  18. ->whereIn('status', ['accepted', 'pending'])
  19. ->whereDate('date', '<', $today)
  20. ->update([
  21. 'status' => 'cancelled',
  22. 'cancel_text' => 'Agendamento expirado',
  23. ]);
  24. // Agendamentos pendentes de hoje cujo horário já terminou.
  25. Schedule::query()
  26. ->where('status', 'pending')
  27. ->whereDate('date', $today)
  28. ->whereNotNull('end_time')
  29. ->whereRaw("CAST(end_time AS time) < ?", [$now->format('H:i:s')])
  30. ->update([
  31. 'status' => 'cancelled',
  32. 'cancel_text' => 'Agendamento expirado',
  33. ]);
  34. }
  35. }