- PermissionSeeder: 29 permissions with role mapping (admin/manager/staff) - Policies updated to use hasPermissionTo() instead of hasRole() - ExportBackup command: JSON per-table backup with chunk processing - UserResource: CRUD users with role assignment (admin only) - RoleResource: CRUD roles with permission assignment (admin only) - UserPolicy + RolePolicy: admin-only access control - ImportCskh: detailed skip logging with color in dry-run mode - Widgets: permission-based visibility checks - Tests: 8/8 pass (21 assertions)
87 lines
2.8 KiB
PHP
87 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Models\Department;
|
|
use App\Models\Feedback;
|
|
use Filament\Widgets\ChartWidget;
|
|
|
|
class HandlerPerformanceWidget extends ChartWidget
|
|
{
|
|
protected ?string $heading = 'Avg Processing Time by Handler';
|
|
|
|
protected ?string $description = 'Average time from ticket creation to resolution, broken down by handler.';
|
|
|
|
protected int|string|array $columnSpan = 'full';
|
|
|
|
protected function getType(): string
|
|
{
|
|
return 'bar';
|
|
}
|
|
|
|
protected function getData(): array
|
|
{
|
|
$user = auth()->user();
|
|
|
|
$query = Feedback::query()
|
|
->whereIn('status', ['resolved', 'closed'])
|
|
->whereNotNull('assigned_to');
|
|
|
|
if ($user->hasRole('manager') && ! $user->hasRole('admin')) {
|
|
$deptIds = Department::where('manager_id', $user->id)->pluck('id');
|
|
$query->whereIn('current_department_id', $deptIds);
|
|
}
|
|
|
|
$handlers = (clone $query)
|
|
->select('assigned_to')
|
|
->selectRaw('AVG(strftime("%s", updated_at) - strftime("%s", created_at)) / 60 as avg_minutes')
|
|
->groupBy('assigned_to')
|
|
->with('assignedTo')
|
|
->get();
|
|
|
|
$labels = $handlers->map(fn ($h) => $h->assignedTo?->name ?? 'Unknown')->toArray();
|
|
$data = $handlers->map(fn ($h) => round($h->avg_minutes, 1))->toArray();
|
|
|
|
// Generate gradient colors based on performance
|
|
$maxMinutes = max($data) ?: 1;
|
|
$colors = collect($data)->map(function ($minutes) use ($maxMinutes) {
|
|
$ratio = $minutes / $maxMinutes;
|
|
if ($ratio > 0.8) {
|
|
return 'rgba(220, 38, 38, 0.8)'; // Red for slow
|
|
} elseif ($ratio > 0.5) {
|
|
return 'rgba(217, 119, 6, 0.8)'; // Amber for medium
|
|
}
|
|
return 'rgba(26, 86, 219, 0.8)'; // Blue for fast
|
|
})->toArray();
|
|
|
|
$borderColors = collect($data)->map(function ($minutes) use ($maxMinutes) {
|
|
$ratio = $minutes / $maxMinutes;
|
|
if ($ratio > 0.8) {
|
|
return 'rgb(220, 38, 38)';
|
|
} elseif ($ratio > 0.5) {
|
|
return 'rgb(217, 119, 6)';
|
|
}
|
|
return 'rgb(26, 86, 219)';
|
|
})->toArray();
|
|
|
|
return [
|
|
'datasets' => [
|
|
[
|
|
'label' => 'Avg Time (minutes)',
|
|
'data' => $data,
|
|
'backgroundColor' => $colors,
|
|
'borderColor' => $borderColors,
|
|
'borderWidth' => 1,
|
|
'borderRadius' => 6,
|
|
],
|
|
],
|
|
'labels' => $labels,
|
|
];
|
|
}
|
|
|
|
public static function canView(): bool
|
|
{
|
|
return auth()->user()?->hasPermissionTo('view-dashboard-widgets') ?? false;
|
|
}
|
|
}
|