CRM/backend/app/Notifications/TaskDueNotification.php

38 خطوط
1.1 KiB
PHP

<?php
namespace App\Notifications;
use App\Models\Notification;
use App\Models\Task;
final class TaskDueNotification
{
public static function send(Task $task, string $kind): void
{
if (! $task->assigned_to) {
return;
}
$moment = $kind === 'overdue' ? $task->due_at : $task->reminder_at;
if (! $moment) {
return;
}
Notification::firstOrCreate(
['idempotency_key' => "task:{$task->id}:{$kind}:{$moment->getTimestamp()}"],
[
'user_id' => $task->assigned_to,
'title' => $kind === 'overdue' ? 'کار عقب‌افتاده' : 'یادآوری موعد کار',
'message' => $task->subject,
'type' => $kind === 'overdue' ? 'task_overdue' : 'task_due',
'data' => [
'task_id' => $task->id,
'url' => "/tasks?task={$task->id}",
'due_at' => $task->due_at?->toISOString(),
],
'is_read' => false,
]
);
}
}