Initial commit
This commit is contained in:
53
laravel/app/Console/Commands/NotifyOverdueTasks.php
Normal file
53
laravel/app/Console/Commands/NotifyOverdueTasks.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user