user(); if ($user->hasRole('Admin')) { return $this->adminDashboard(); } if ($user->hasRole('RH')) { return $this->rhDashboard(); } // If user has a service role $serviceRoles = ['DSI', 'Batiment', 'ParcAuto']; foreach ($serviceRoles as $role) { if ($user->hasRole($role)) { return $this->serviceDashboard($role); } } if ($user->hasRole('Prescripteur')) { return $this->prescriberDashboard(); } return Inertia::render('Dashboard'); } protected function adminDashboard() { $avgCompletionTime = IntegrationRequest::where('status', IntegrationStatus::Completed) ->whereNotNull('completed_at') ->selectRaw('AVG(TIMESTAMPDIFF(DAY, created_at, completed_at)) as avg_days') ->value('avg_days') ?? 0; $serviceDistribution = \App\Models\Service::withCount('serviceTasks')->get()->map(function ($service) { return [ 'name' => $service->name, 'tasks_count' => $service->service_tasks_count, ]; }); return Inertia::render('Dashboard/Admin', [ 'stats' => [ 'total_integrations' => IntegrationRequest::count(), 'active_integrations' => IntegrationRequest::whereNotIn('status', [IntegrationStatus::Completed, IntegrationStatus::Cancelled])->count(), 'completed_integrations' => IntegrationRequest::where('status', IntegrationStatus::Completed)->count(), 'overdue_tasks' => ServiceTask::where('status', '!=', ServiceTaskStatus::Completed) ->where('sla_deadline', '<', now()) ->count(), 'avg_completion_days' => round($avgCompletionTime, 1), 'service_distribution' => $serviceDistribution, ], 'recent_requests' => IntegrationRequest::with(['agent', 'template'])->latest()->take(5)->get(), ]); } protected function rhDashboard() { return Inertia::render('Dashboard/RH', [ 'pending_validation' => IntegrationRequest::where('status', IntegrationStatus::PendingRHValidation) ->with(['agent', 'template']) ->get(), 'active_integrations' => IntegrationRequest::where('status', IntegrationStatus::InProgress) ->with(['agent', 'template', 'serviceTasks']) ->get(), 'completed_integrations' => IntegrationRequest::where('status', IntegrationStatus::Completed) ->with(['agent', 'template', 'serviceTasks']) ->latest('completed_at') ->get(), 'stats' => [ 'in_progress' => IntegrationRequest::where('status', IntegrationStatus::InProgress)->count(), 'completed' => IntegrationRequest::where('status', IntegrationStatus::Completed)->count(), // Added completed count 'overdue' => ServiceTask::where('status', '!=', ServiceTaskStatus::Completed) ->where('sla_deadline', '<', now()) ->count(), ], ]); } protected function serviceDashboard(string $role) { return Inertia::render('Dashboard/Service', [ 'role' => $role, 'my_tasks' => ServiceTask::where('status', '!=', ServiceTaskStatus::Completed) ->whereHas('service', function ($query) use ($role) { $query->where('name', $role); }) ->whereHas('integrationRequest', function ($query) { $query->whereNotIn('status', [IntegrationStatus::Draft, IntegrationStatus::PendingRHValidation]); }) ->with(['integrationRequest.agent', 'taskItems']) ->get(), 'completed_tasks' => ServiceTask::where('status', ServiceTaskStatus::Completed) ->whereHas('service', function ($query) use ($role) { $query->where('name', $role); }) ->latest() ->take(5) ->get(), ]); } protected function prescriberDashboard() { return Inertia::render('Dashboard/Prescriber', [ 'my_requests' => IntegrationRequest::whereHas('agent', function ($query) { $query->where('created_by', auth()->id()); }) ->with(['agent', 'template']) ->latest() ->get(), ]); } }