54 lines
1.3 KiB
PHP
54 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\TaskStatus;
|
|
use App\Models\Task;
|
|
use App\Notifications\TaskOverdueNotification;
|
|
use Illuminate\Console\Command;
|
|
|
|
final class NotifyOverdueTasks extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'tasks:notify-overdue';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Command description';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): int
|
|
{
|
|
Task::query()
|
|
->whereNotNull('due_date')
|
|
->where('due_date', '<', now()->toDateString())
|
|
->where('status', '!=', TaskStatus::COMPLETED)
|
|
->whereNull('notified_at')
|
|
->with('user')
|
|
->chunkById(100, function ($tasks) {
|
|
foreach ($tasks as $task) {
|
|
$task->user->notify(
|
|
new TaskOverdueNotification($task)
|
|
);
|
|
|
|
$task->update([
|
|
'notified_at' => now(),
|
|
]);
|
|
}
|
|
});
|
|
$this->info('Tasks overdue notification send)');
|
|
return self::SUCCESS;
|
|
}
|
|
}
|