65 خطوط
1.9 KiB
PHP
65 خطوط
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Team;
|
|
use App\Models\User;
|
|
use App\Services\DashboardService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function __construct(private DashboardService $dashboardService) {}
|
|
|
|
public function admin(): JsonResponse
|
|
{
|
|
Gate::authorize('view-admin-dashboard');
|
|
$stats = $this->dashboardService->admin();
|
|
|
|
// Agent ranking
|
|
$agents = User::role('agent')
|
|
->withCount(['assignedLeads', 'calls'])
|
|
->get()
|
|
->map(fn ($agent) => [
|
|
'id' => $agent->id,
|
|
'name' => $agent->name,
|
|
'leads_count' => $agent->assigned_leads_count,
|
|
'calls_count' => $agent->calls_count,
|
|
])
|
|
->sortByDesc('calls_count')
|
|
->values();
|
|
|
|
$stats['agent_ranking'] = $agents;
|
|
|
|
return response()->json($stats);
|
|
}
|
|
|
|
public function supervisor(): JsonResponse
|
|
{
|
|
Gate::authorize('view-supervisor-dashboard');
|
|
$teamId = auth()->user()->teams->first()?->id;
|
|
$stats = $this->dashboardService->supervisor($teamId);
|
|
|
|
// Team members
|
|
$team = Team::with('members')->find($teamId);
|
|
$stats['team_members'] = $team?->members->map(fn ($member) => [
|
|
'id' => $member->id,
|
|
'name' => $member->name,
|
|
'is_online' => $member->last_login_at && $member->last_login_at->gt(now()->subMinutes(15)),
|
|
'calls_today' => $member->calls()->whereDate('created_at', now()->today())->count(),
|
|
]);
|
|
|
|
return response()->json($stats);
|
|
}
|
|
|
|
public function agent(): JsonResponse
|
|
{
|
|
Gate::authorize('view-agent-dashboard');
|
|
$stats = $this->dashboardService->agent();
|
|
|
|
return response()->json($stats);
|
|
}
|
|
}
|