InvalidateExpiredSchedules.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 cuja data já passou.
  17. Schedule::query()
  18. ->where('status', 'accepted')
  19. ->whereDate('date', '<', $today)
  20. ->update([
  21. 'status' => 'cancelled',
  22. ]);
  23. // Agendamentos pendentes cuja data já passou.
  24. Schedule::query()
  25. ->where('status', 'pending')
  26. ->whereDate('date', '<', $today)
  27. ->update([
  28. 'status' => 'cancelled',
  29. ]);
  30. // Agendamentos pendentes de hoje cujo horário já terminou.
  31. Schedule::query()
  32. ->where('status', 'pending')
  33. ->whereDate('date', $today)
  34. ->whereNotNull('end_time')
  35. ->whereRaw("CAST(end_time AS time) < ?", [$now->format('H:i:s')])
  36. ->update([
  37. 'status' => 'cancelled',
  38. ]);
  39. }
  40. }