77 lines
3.2 KiB
PHP
77 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Superuser;
|
|
|
|
use App\Enums\Role;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Legacy\Account;
|
|
use App\Support\Legacy\LoginHistory;
|
|
use App\Support\Legacy\SystemOverview;
|
|
use App\Support\Prototype\SuperuserData;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function __invoke(Request $request, SystemOverview $overview, LoginHistory $history): View
|
|
{
|
|
$role = Role::Superuser;
|
|
$user = $request->user()->loadMissing($role->profileRelation());
|
|
$profile = $user->{$role->profileRelation()};
|
|
|
|
// Label => nilai; kolom kosong dilewati.
|
|
$details = array_map(strval(...), array_filter([
|
|
'Username' => $user->username,
|
|
$role->identifierLabel() => $profile?->nip,
|
|
'Jabatan' => $profile?->jabatan,
|
|
'Unit kerja' => $profile?->unit_kerja,
|
|
], fn (mixed $value): bool => $value !== null));
|
|
|
|
// Akun DB lama: ringkasan asli akun, otoritas, dan riwayat masuk dari t_account, t_otoritasuser, dan t_historilogin.
|
|
if ($user instanceof Account) {
|
|
$authorities = $overview->authorities();
|
|
$logins = $overview->logins();
|
|
|
|
return view('superuser.dashboard-legacy', [
|
|
'user' => $user,
|
|
'details' => $details,
|
|
'accounts' => $overview->accounts(),
|
|
'authorityLabels' => array_keys($authorities),
|
|
'authorityData' => array_values($authorities),
|
|
'loginLabels' => array_map(strval(...), array_keys($logins['tahun'])),
|
|
'loginData' => array_values($logins['tahun']),
|
|
'lastLogin' => $logins['terakhir'],
|
|
'latest' => $history->latest(),
|
|
]);
|
|
}
|
|
|
|
$pengguna = SuperuserData::pengguna();
|
|
$aktivitas = SuperuserData::aktivitas();
|
|
$cadangan = SuperuserData::cadangan();
|
|
|
|
$perRole = collect(SuperuserData::ROLES)->mapWithKeys(fn (string $r): array => [$r => $pengguna->where('role', $r)->count()])->filter();
|
|
$perHari = $aktivitas
|
|
->groupBy(fn (array $a): string => substr($a['waktu'], 0, 10))
|
|
->map->count()
|
|
->sortKeys();
|
|
|
|
return view('superuser.dashboard', [
|
|
'user' => $user,
|
|
'details' => $details,
|
|
'totalAkun' => $pengguna->count(),
|
|
'akunAktif' => $pengguna->where('aktif', true)->count(),
|
|
'akunNonaktif' => $pengguna->where('aktif', false)->count(),
|
|
'totalAktivitas' => $aktivitas->count(),
|
|
'gagalMasuk' => $aktivitas->where('aksi', 'Gagal masuk')->count(),
|
|
'sesiAktif' => SuperuserData::sesiLain()->count() + 1,
|
|
'cadanganTerakhir' => $cadangan->firstWhere('status', 'Berhasil'),
|
|
'roleLabels' => $perRole->keys()->all(),
|
|
'roleData' => $perRole->values()->all(),
|
|
'hariLabels' => $perHari->keys()->map(fn (string $d): string => Carbon::parse($d)->locale('id')->isoFormat('D MMM'))->all(),
|
|
'hariData' => $perHari->values()->all(),
|
|
'aktivitasTerbaru' => $aktivitas->take(5),
|
|
]);
|
|
}
|
|
}
|