77 lines
2.5 KiB
PHP
77 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ServiceTask;
|
|
use App\Models\TaskItem;
|
|
use App\Enums\ServiceTaskStatus;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ServiceTaskManager
|
|
{
|
|
public function startTask(ServiceTask $task): void
|
|
{
|
|
$task->update([
|
|
'status' => ServiceTaskStatus::InProgress,
|
|
'started_at' => now(),
|
|
]);
|
|
|
|
// Log activity
|
|
}
|
|
|
|
public function completeTaskItem(TaskItem $item, array $data = []): void
|
|
{
|
|
$item->update([
|
|
'is_completed' => true,
|
|
'completed_at' => now(),
|
|
'completed_by' => auth()->id(),
|
|
'data' => $data,
|
|
]);
|
|
|
|
$this->checkIfTaskCanBeValidated($item->serviceTask);
|
|
}
|
|
|
|
protected function checkIfTaskCanBeValidated(ServiceTask $task): void
|
|
{
|
|
$allMandatoryCompleted = $task->taskItems()
|
|
->where('is_mandatory', true)
|
|
->where('is_completed', false)
|
|
->count() === 0;
|
|
|
|
if ($allMandatoryCompleted && $task->status !== ServiceTaskStatus::WaitingValidation && $task->status !== ServiceTaskStatus::Completed) {
|
|
$task->update(['status' => ServiceTaskStatus::WaitingValidation]);
|
|
|
|
// Notify service manager
|
|
$users = \App\Models\User::role($task->service->name)->get();
|
|
\Illuminate\Support\Facades\Notification::send($users, new \App\Notifications\ServiceTaskReadyForValidationNotification($task));
|
|
}
|
|
}
|
|
|
|
public function validateTask(ServiceTask $task): void
|
|
{
|
|
$task->update([
|
|
'status' => ServiceTaskStatus::Completed,
|
|
'completed_at' => now(),
|
|
'validated_by' => auth()->id(),
|
|
]);
|
|
|
|
// Notify RH that a service has completed their task
|
|
$rhUsers = \App\Models\User::role('RH')->get();
|
|
\Illuminate\Support\Facades\Notification::send($rhUsers, new \App\Notifications\ServiceTaskValidatedNotification($task));
|
|
|
|
// Trigger check on the parent integration request
|
|
app(IntegrationService::class)->checkCompletion($task->integrationRequest);
|
|
}
|
|
|
|
public function rejectTask(ServiceTask $task, string $reason): void
|
|
{
|
|
$task->update([
|
|
'status' => ServiceTaskStatus::Rejected,
|
|
]);
|
|
|
|
// Notify service users
|
|
$users = \App\Models\User::role($task->service->name)->get();
|
|
\Illuminate\Support\Facades\Notification::send($users, new \App\Notifications\ServiceTaskRejectedNotification($task, $reason));
|
|
}
|
|
}
|