fix: sync employee photo to user profile avatar, topbar, and dashboard

This commit is contained in:
Ray1111-aa
2026-10-20 19:22:28 +07:00
parent 7126648c1c
commit cd3ce0c191
10 changed files with 424 additions and 49 deletions
+23 -2
View File
@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use App\Models\AdminActivityLog; use App\Models\AdminActivityLog;
use App\Models\Gate; use App\Models\Gate;
use App\Models\User; use App\Models\User;
use App\Services\ImageCompressionService;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
@@ -73,6 +74,7 @@ class UserController extends Controller
'password' => ['required', 'string', 'min:6'], 'password' => ['required', 'string', 'min:6'],
'role' => ['required', 'string', 'exists:roles,name'], 'role' => ['required', 'string', 'exists:roles,name'],
'gate_id' => ['nullable', 'exists:gates,id'], 'gate_id' => ['nullable', 'exists:gates,id'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
], [ ], [
'name.required' => 'Nama lengkap wajib diisi.', 'name.required' => 'Nama lengkap wajib diisi.',
'username.required' => 'Username wajib diisi.', 'username.required' => 'Username wajib diisi.',
@@ -85,6 +87,11 @@ class UserController extends Controller
'role.exists' => 'Role yang dipilih tidak valid.', 'role.exists' => 'Role yang dipilih tidak valid.',
]); ]);
$photoPath = null;
if ($request->hasFile('photo')) {
$photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80);
}
$user = User::create([ $user = User::create([
'name' => $validated['name'], 'name' => $validated['name'],
'username' => $validated['username'], 'username' => $validated['username'],
@@ -92,6 +99,7 @@ class UserController extends Controller
'password' => Hash::make($validated['password']), 'password' => Hash::make($validated['password']),
'role' => $validated['role'], // Simpan ke kolom legacy untuk backwards-compat 'role' => $validated['role'], // Simpan ke kolom legacy untuk backwards-compat
'gate_id' => $validated['gate_id'] ?? null, 'gate_id' => $validated['gate_id'] ?? null,
'photo_path' => $photoPath,
]); ]);
// Assign Spatie Role // Assign Spatie Role
@@ -110,7 +118,10 @@ class UserController extends Controller
// Jika user berstatus employee (role employee atau memiliki wewenang operasional karyawan), pastikan profil master Employee ada // Jika user berstatus employee (role employee atau memiliki wewenang operasional karyawan), pastikan profil master Employee ada
if ($user->isEmployee()) { if ($user->isEmployee()) {
$user->getOrCreateEmployee(); $emp = $user->getOrCreateEmployee();
if ($emp && $photoPath && empty($emp->photo_path)) {
$emp->update(['photo_path' => $photoPath]);
}
} }
// Rekam ke Log Riwayat Aktivitas Super Admin // Rekam ke Log Riwayat Aktivitas Super Admin
@@ -149,6 +160,7 @@ class UserController extends Controller
'password' => ['nullable', 'string', 'min:6'], 'password' => ['nullable', 'string', 'min:6'],
'role' => ['required', 'string', 'exists:roles,name'], 'role' => ['required', 'string', 'exists:roles,name'],
'gate_id' => ['nullable', 'exists:gates,id'], 'gate_id' => ['nullable', 'exists:gates,id'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
], [ ], [
'name.required' => 'Nama lengkap wajib diisi.', 'name.required' => 'Nama lengkap wajib diisi.',
'username.required' => 'Username wajib diisi.', 'username.required' => 'Username wajib diisi.',
@@ -180,6 +192,11 @@ class UserController extends Controller
$passwordUpdated = true; $passwordUpdated = true;
} }
if ($request->hasFile('photo')) {
ImageCompressionService::deleteIfExists($user->photo_path);
$user->photo_path = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80);
}
$user->save(); $user->save();
// Sync Spatie Role // Sync Spatie Role
@@ -199,9 +216,13 @@ class UserController extends Controller
$user->employee->update([ $user->employee->update([
'name' => $user->name, 'name' => $user->name,
'nik' => $user->username, 'nik' => $user->username,
'photo_path' => $user->photo_path ?: $user->employee->photo_path,
]); ]);
} elseif ($user->isEmployee()) { } elseif ($user->isEmployee()) {
$user->getOrCreateEmployee(); $emp = $user->getOrCreateEmployee();
if ($emp && $user->photo_path) {
$emp->update(['photo_path' => $user->photo_path]);
}
} }
// Susun deskripsi perubahan yang mudah dipahami non-IT // Susun deskripsi perubahan yang mudah dipahami non-IT
@@ -568,6 +568,7 @@ class EmployeeController extends Controller
$photoPath = null; $photoPath = null;
if ($request->hasFile('photo')) { if ($request->hasFile('photo')) {
$photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80); $photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80);
$user->update(['photo_path' => $photoPath]);
} }
Employee::create([ Employee::create([
@@ -627,6 +628,18 @@ class EmployeeController extends Controller
'photo_path' => $photoPath, 'photo_path' => $photoPath,
]); ]);
// Sinkronkan akun login User terkait
$linkedUser = $employee->user ?: User::where('username', $employee->nik)->first();
if ($linkedUser) {
if (!$employee->user_id) {
$employee->update(['user_id' => $linkedUser->id]);
}
$linkedUser->update([
'name' => $employee->name,
'photo_path' => $photoPath,
]);
}
return back()->with('success', "Data SIMPER untuk {$employee->name} berhasil diperbarui."); return back()->with('success', "Data SIMPER untuk {$employee->name} berhasil diperbarui.");
} }
} }
+9 -4
View File
@@ -48,15 +48,20 @@ class Employee extends Model
public function getPhotoUrlAttribute(): ?string public function getPhotoUrlAttribute(): ?string
{ {
if (!$this->photo_path) { $path = $this->photo_path;
if (empty($path) && $this->user_id) {
$path = $this->user?->photo_path;
}
if (!$path) {
return null; return null;
} }
if (str_starts_with($this->photo_path, 'http://') || str_starts_with($this->photo_path, 'https://')) { if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {
return $this->photo_path; return $path;
} }
return asset('storage/' . ltrim($this->photo_path, '/')); return asset('storage/' . ltrim($path, '/'));
} }
/** /**
+46
View File
@@ -58,6 +58,16 @@ class User extends Authenticatable
'password', 'password',
'role', 'role',
'gate_id', 'gate_id',
'photo_path',
];
/**
* The accessors to append to the model's array form.
*
* @var list<string>
*/
protected $appends = [
'photo_url',
]; ];
/** /**
@@ -98,6 +108,42 @@ class User extends Authenticatable
return $this->hasOne(Employee::class); return $this->hasOne(Employee::class);
} }
/**
* Dapatkan URL foto profil pengguna (mendukung delegasi otomatis ke foto master Employee).
*/
public function getPhotoUrlAttribute(): ?string
{
if (!empty($this->photo_path)) {
if (str_starts_with($this->photo_path, 'http://') || str_starts_with($this->photo_path, 'https://')) {
return $this->photo_path;
}
return asset('storage/' . ltrim($this->photo_path, '/'));
}
// Delegasi ke profil employee tertaut
if ($this->relationLoaded('employee') && $this->employee) {
return $this->employee->photo_url;
}
$employee = $this->employee()->first();
if ($employee && $employee->photo_url) {
return $employee->photo_url;
}
// Fallback cari employee dengan NIK sama dengan username
if (!empty($this->username)) {
$employeeByNik = Employee::where('nik', $this->username)->first();
if ($employeeByNik) {
if (empty($employeeByNik->user_id)) {
$employeeByNik->update(['user_id' => $this->id]);
}
return $employeeByNik->photo_url;
}
}
return null;
}
/** /**
* Dapatkan profil Employee yang tertaut, atau cari berdasarkan NIK (username), * Dapatkan profil Employee yang tertaut, atau cari berdasarkan NIK (username),
* atau buat profil Employee default jika akun berstatus employee / super admin penguji. * atau buat profil Employee default jika akun berstatus employee / super admin penguji.
@@ -0,0 +1,52 @@
<?php
use App\Models\Employee;
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if (!Schema::hasColumn('users', 'photo_path')) {
Schema::table('users', function (Blueprint $table) {
$table->string('photo_path')->nullable()->after('role');
});
}
// Sinkronkan foto yang sudah ada dari master Employee ke akun User terkait
try {
$employees = Employee::whereNotNull('photo_path')->get();
foreach ($employees as $emp) {
if ($emp->user_id) {
User::where('id', $emp->user_id)->update(['photo_path' => $emp->photo_path]);
} elseif (!empty($emp->nik)) {
$user = User::where('username', $emp->nik)->first();
if ($user) {
$user->update(['photo_path' => $emp->photo_path]);
$emp->update(['user_id' => $user->id]);
}
}
}
} catch (\Throwable $e) {
// Ignore if tables are being set up in test environment
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
if (Schema::hasColumn('users', 'photo_path')) {
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('photo_path');
});
}
}
};
+102 -2
View File
@@ -210,9 +210,13 @@
<tr class="hover:bg-slate-50/70 transition"> <tr class="hover:bg-slate-50/70 transition">
<td class="py-3 px-4"> <td class="py-3 px-4">
<div class="flex items-center gap-3"> <div class="flex items-center gap-3">
@if($user->photo_url)
<img src="{{ $user->photo_url }}" alt="{{ $user->name }}" class="w-8 h-8 rounded-full object-cover border border-slate-200 shadow-2xs flex-shrink-0">
@else
<div class="w-8 h-8 rounded-full bg-indigo-50 border border-indigo-100 text-indigo-700 font-black flex items-center justify-center text-xs flex-shrink-0"> <div class="w-8 h-8 rounded-full bg-indigo-50 border border-indigo-100 text-indigo-700 font-black flex items-center justify-center text-xs flex-shrink-0">
{{ strtoupper(substr($user->name, 0, 1)) }} {{ strtoupper(substr($user->name, 0, 1)) }}
</div> </div>
@endif
<div> <div>
<p class="font-bold text-slate-900 text-xs">{{ $user->name }}</p> <p class="font-bold text-slate-900 text-xs">{{ $user->name }}</p>
<p class="text-[11px] text-slate-400 font-mono">@<span>{{ $user->username }}</span></p> <p class="text-[11px] text-slate-400 font-mono">@<span>{{ $user->username }}</span></p>
@@ -303,7 +307,7 @@
</button> </button>
</div> </div>
<form action="{{ route('admin.users.store') }}" method="POST" class="p-6 space-y-4"> <form action="{{ route('admin.users.store') }}" method="POST" enctype="multipart/form-data" class="p-6 space-y-4">
@csrf @csrf
<div> <div>
<label class="block text-xs font-bold text-slate-700 mb-1">Nama Lengkap *</label> <label class="block text-xs font-bold text-slate-700 mb-1">Nama Lengkap *</label>
@@ -326,6 +330,26 @@
<input type="password" name="password" required minlength="6" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white" placeholder="Minimal 6 karakter"> <input type="password" name="password" required minlength="6" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white" placeholder="Minimal 6 karakter">
</div> </div>
<!-- Upload Foto Profil (Opsional) -->
<div>
<label class="block text-xs font-bold text-slate-700 mb-1">Foto Profil Pengguna (Opsional)</label>
<div class="flex items-center space-x-3">
<div id="addUserPhotoPreviewContainer" class="w-12 h-12 rounded-full border-2 border-dashed border-slate-300 bg-slate-50 flex items-center justify-center overflow-hidden flex-shrink-0 shadow-2xs">
<img id="addUserPhotoPreview" src="" alt="Pratinjau" class="w-full h-full object-cover hidden">
<i id="addUserPhotoPlaceholder" class="fa-solid fa-camera text-slate-400 text-sm"></i>
</div>
<div class="flex-1">
<input type="file"
name="photo"
id="addUserPhotoInput"
accept="image/jpeg,image/png,image/jpg,image/webp"
onchange="previewUserImage(this, 'addUserPhotoPreview', 'addUserPhotoPlaceholder')"
class="block w-full text-xs text-slate-500 file:mr-2.5 file:py-1.5 file:px-3 file:rounded-lg file:border-0 file:text-[11px] file:font-bold file:bg-indigo-50 file:text-indigo-700 hover:file:bg-indigo-100 cursor-pointer">
<p class="text-[10px] text-slate-400 mt-0.5">Otomatis dikompresi hemat storage (Maks. 10MB).</p>
</div>
</div>
</div>
<div class="grid grid-cols-2 gap-3"> <div class="grid grid-cols-2 gap-3">
<div> <div>
<label class="block text-xs font-bold text-slate-700 mb-1">Role RBAC *</label> <label class="block text-xs font-bold text-slate-700 mb-1">Role RBAC *</label>
@@ -374,7 +398,7 @@
</button> </button>
</div> </div>
<form id="editUserForm" method="POST" class="p-6 space-y-4"> <form id="editUserForm" method="POST" enctype="multipart/form-data" class="p-6 space-y-4">
@csrf @csrf
<div> <div>
<label class="block text-xs font-bold text-slate-700 mb-1">Nama Lengkap *</label> <label class="block text-xs font-bold text-slate-700 mb-1">Nama Lengkap *</label>
@@ -399,6 +423,26 @@
<input type="password" name="password" minlength="6" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white" placeholder="Masukkan password baru"> <input type="password" name="password" minlength="6" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white" placeholder="Masukkan password baru">
</div> </div>
<!-- Upload / Ganti Foto Profil -->
<div>
<label class="block text-xs font-bold text-slate-700 mb-1">Foto Profil Pengguna</label>
<div class="flex items-center space-x-3">
<div id="editUserPhotoPreviewContainer" class="w-12 h-12 rounded-full border border-slate-200 bg-slate-50 flex items-center justify-center overflow-hidden flex-shrink-0 shadow-2xs">
<img id="editUserPhotoPreview" src="" alt="Foto" class="w-full h-full object-cover hidden">
<i id="editUserPhotoPlaceholder" class="fa-solid fa-user text-slate-400 text-sm"></i>
</div>
<div class="flex-1">
<input type="file"
name="photo"
id="editUserPhotoInput"
accept="image/jpeg,image/png,image/jpg,image/webp"
onchange="previewUserImage(this, 'editUserPhotoPreview', 'editUserPhotoPlaceholder')"
class="block w-full text-xs text-slate-500 file:mr-2.5 file:py-1.5 file:px-3 file:rounded-lg file:border-0 file:text-[11px] file:font-bold file:bg-indigo-50 file:text-indigo-700 hover:file:bg-indigo-100 cursor-pointer">
<p class="text-[10px] text-slate-400 mt-0.5">Pilih file baru jika ingin mengganti foto profil.</p>
</div>
</div>
</div>
<div class="grid grid-cols-2 gap-3"> <div class="grid grid-cols-2 gap-3">
<div> <div>
<label class="block text-xs font-bold text-slate-700 mb-1">Role RBAC *</label> <label class="block text-xs font-bold text-slate-700 mb-1">Role RBAC *</label>
@@ -519,6 +563,24 @@
const rolePermissionsMap = @json($rolePermissionsMap ?? []); const rolePermissionsMap = @json($rolePermissionsMap ?? []);
let currentUserDirectPerms = []; let currentUserDirectPerms = [];
function previewUserImage(input, previewId, placeholderId) {
const preview = document.getElementById(previewId);
const placeholder = document.getElementById(placeholderId);
if (input.files && input.files[0]) {
const reader = new FileReader();
reader.onload = function(e) {
if (preview) {
preview.src = e.target.result;
preview.classList.remove('hidden');
}
if (placeholder) {
placeholder.classList.add('hidden');
}
};
reader.readAsDataURL(input.files[0]);
}
}
function openCreateUserModal() { function openCreateUserModal() {
document.getElementById('createUserModal').classList.remove('hidden'); document.getElementById('createUserModal').classList.remove('hidden');
document.documentElement.classList.add('modal-open'); document.documentElement.classList.add('modal-open');
@@ -527,6 +589,17 @@
function closeCreateUserModal() { function closeCreateUserModal() {
document.getElementById('createUserModal').classList.add('hidden'); document.getElementById('createUserModal').classList.add('hidden');
document.documentElement.classList.remove('modal-open'); document.documentElement.classList.remove('modal-open');
const input = document.getElementById('addUserPhotoInput');
const preview = document.getElementById('addUserPhotoPreview');
const placeholder = document.getElementById('addUserPhotoPlaceholder');
if (input) input.value = '';
if (preview) {
preview.src = '';
preview.classList.add('hidden');
}
if (placeholder) {
placeholder.classList.remove('hidden');
}
} }
function toggleDirectPermsAccordion() { function toggleDirectPermsAccordion() {
@@ -633,6 +706,24 @@
document.getElementById('edit_role').value = primaryRole; document.getElementById('edit_role').value = primaryRole;
document.getElementById('edit_gate_id').value = user.gate_id || ''; document.getElementById('edit_gate_id').value = user.gate_id || '';
const editPhotoPreview = document.getElementById('editUserPhotoPreview');
const editPhotoPlaceholder = document.getElementById('editUserPhotoPlaceholder');
const editPhotoInput = document.getElementById('editUserPhotoInput');
if (editPhotoInput) editPhotoInput.value = '';
if (user.photo_url) {
if (editPhotoPreview) {
editPhotoPreview.src = user.photo_url;
editPhotoPreview.classList.remove('hidden');
}
if (editPhotoPlaceholder) editPhotoPlaceholder.classList.add('hidden');
} else {
if (editPhotoPreview) {
editPhotoPreview.src = '';
editPhotoPreview.classList.add('hidden');
}
if (editPhotoPlaceholder) editPhotoPlaceholder.classList.remove('hidden');
}
currentUserDirectPerms = Array.isArray(userDirectPerms) ? [...userDirectPerms] : []; currentUserDirectPerms = Array.isArray(userDirectPerms) ? [...userDirectPerms] : [];
updatePermissionCheckboxes(); updatePermissionCheckboxes();
@@ -643,6 +734,15 @@
function closeEditUserModal() { function closeEditUserModal() {
document.getElementById('editUserModal').classList.add('hidden'); document.getElementById('editUserModal').classList.add('hidden');
document.documentElement.classList.remove('modal-open'); document.documentElement.classList.remove('modal-open');
const editPhotoInput = document.getElementById('editUserPhotoInput');
const editPhotoPreview = document.getElementById('editUserPhotoPreview');
const editPhotoPlaceholder = document.getElementById('editUserPhotoPlaceholder');
if (editPhotoInput) editPhotoInput.value = '';
if (editPhotoPreview) {
editPhotoPreview.src = '';
editPhotoPreview.classList.add('hidden');
}
if (editPhotoPlaceholder) editPhotoPlaceholder.classList.remove('hidden');
} }
function openDeleteUserModal(userId, userName) { function openDeleteUserModal(userId, userName) {
+26 -10
View File
@@ -6,36 +6,52 @@
<div class="w-full space-y-6"> <div class="w-full space-y-6">
<!-- Header Halaman Simple Kayak Satpam (Seamless, Tanpa Card Wrapper) --> <!-- Header Halaman Simple Kayak Satpam (Seamless, Tanpa Card Wrapper) -->
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-3 border-b border-slate-200/80"> <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4 pb-4 border-b border-slate-200/80">
<div class="flex items-center space-x-3.5 min-w-0">
@php
$empPhoto = $employee?->photo_url ?: auth()->user()->photo_url;
$empName = $employee ? $employee->name : auth()->user()->name;
@endphp
@if($empPhoto)
<img src="{{ $empPhoto }}" alt="{{ $empName }}" class="w-12 h-12 rounded-2xl object-cover ring-2 ring-emerald-500/20 shadow-sm flex-shrink-0">
@else
<div class="w-12 h-12 rounded-2xl bg-gradient-to-br from-emerald-700 to-[#1b5e20] text-white font-black text-lg flex items-center justify-center ring-2 ring-emerald-500/20 shadow-sm flex-shrink-0">
{{ strtoupper(substr($empName, 0, 1)) }}
</div>
@endif
<div class="min-w-0"> <div class="min-w-0">
<div class="flex items-center flex-wrap gap-2.5"> <div class="flex items-center flex-wrap gap-2.5">
<h1 class="text-lg sm:text-xl font-black text-slate-900 tracking-tight"> <h1 class="text-lg sm:text-xl font-black text-slate-900 tracking-tight">
Dashboard Karyawan {{ $empName }}
</h1> </h1>
@if($employee && $employee->hasValidSimper()) @if($employee && $employee->hasValidSimper())
<span class="px-2.5 py-1 rounded-lg text-xs font-black bg-emerald-50 text-emerald-700 border border-emerald-200"> <span class="px-2.5 py-0.5 rounded-lg text-xs font-black bg-emerald-50 text-emerald-700 border border-emerald-200">
SIMPER Aktif SIMPER Aktif
</span> </span>
@elseif($employee) @elseif($employee)
<span class="px-2.5 py-1 rounded-lg text-xs font-black bg-rose-50 text-rose-700 border border-rose-200"> <span class="px-2.5 py-0.5 rounded-lg text-xs font-black bg-rose-50 text-rose-700 border border-rose-200">
SIMPER Exp SIMPER Exp
</span> </span>
@endif @endif
</div> </div>
<div class="text-sm sm:text-base text-slate-600 font-medium truncate mt-1 flex items-center flex-wrap gap-1.5"> <div class="text-xs sm:text-sm text-slate-500 font-medium truncate mt-0.5 flex items-center flex-wrap gap-1.5">
<span>Karyawan: <strong class="text-slate-900 font-bold">{{ $employee ? $employee->name : auth()->user()->name }}</strong></span>
@if($employee && $employee->nik) @if($employee && $employee->nik)
<span class="text-slate-500 font-mono text-xs sm:text-sm font-semibold">({{ $employee->nik }})</span> <span class="text-slate-700 font-mono font-semibold">NIK: {{ $employee->nik }}</span>
@endif @endif
@if($employee && $employee->division) @if($employee && $employee->division)
<span class="text-slate-400">&bull;</span> <span class="text-slate-300">&bull;</span>
<span class="text-slate-500 text-xs sm:text-sm">{{ $employee->division }}</span> <span class="text-slate-600">{{ $employee->division }}</span>
@endif @endif
@if($employee && $employee->position)
<span class="text-slate-300">&bull;</span>
<span class="text-slate-600 font-semibold">{{ $employee->position }}</span>
@endif
</div>
</div> </div>
</div> </div>
@if($employee && $employee->simper_expired_at) @if($employee && $employee->simper_expired_at)
<div class="text-xs text-slate-500 font-medium self-start sm:self-center"> <div class="text-xs text-slate-500 font-medium self-start sm:self-center bg-slate-50 px-3 py-1.5 rounded-xl border border-slate-200/60">
Masa Berlaku SIMPER: <span class="font-bold text-slate-800">{{ $employee->simper_expired_at->format('d/m/Y') }}</span> Masa Berlaku SIMPER: <span class="font-bold text-slate-800">{{ $employee->simper_expired_at->format('d/m/Y') }}</span>
</div> </div>
@endif @endif
+12
View File
@@ -449,9 +449,13 @@
<!-- Footer Bawah Sidebar: Profil Pengguna & Tombol Logout (Persis Gambar IT All Role) --> <!-- Footer Bawah Sidebar: Profil Pengguna & Tombol Logout (Persis Gambar IT All Role) -->
<div class="p-4 border-t border-slate-100 flex items-center justify-between bg-slate-50/50"> <div class="p-4 border-t border-slate-100 flex items-center justify-between bg-slate-50/50">
<a href="{{ route('profile.index') }}" class="flex items-center space-x-2.5 min-w-0 hover:opacity-80 transition group" title="Buka Profil Saya"> <a href="{{ route('profile.index') }}" class="flex items-center space-x-2.5 min-w-0 hover:opacity-80 transition group" title="Buka Profil Saya">
@if(auth()->user()->photo_url)
<img src="{{ auth()->user()->photo_url }}" alt="{{ auth()->user()->name }}" class="w-8 h-8 rounded-full object-cover flex-shrink-0 shadow-sm group-hover:ring-2 ring-emerald-500/30">
@else
<div class="w-8 h-8 rounded-full bg-[#1b5e20] text-white font-black text-xs flex items-center justify-center flex-shrink-0 shadow-sm group-hover:ring-2 ring-emerald-500/30"> <div class="w-8 h-8 rounded-full bg-[#1b5e20] text-white font-black text-xs flex items-center justify-center flex-shrink-0 shadow-sm group-hover:ring-2 ring-emerald-500/30">
{{ strtoupper(substr(auth()->user()->name, 0, 1)) }} {{ strtoupper(substr(auth()->user()->name, 0, 1)) }}
</div> </div>
@endif
<div class="truncate"> <div class="truncate">
<p class="text-xs font-bold text-slate-800 truncate leading-tight group-hover:text-[#1b5e20]">{{ auth()->user()->name }}</p> <p class="text-xs font-bold text-slate-800 truncate leading-tight group-hover:text-[#1b5e20]">{{ auth()->user()->name }}</p>
<p class="text-[10px] text-slate-400 capitalize truncate">{{ str_replace('_', ' ', auth()->user()->role) }}</p> <p class="text-[10px] text-slate-400 capitalize truncate">{{ str_replace('_', ' ', auth()->user()->role) }}</p>
@@ -740,9 +744,13 @@
<div class="p-4 border-t border-slate-100 flex items-center justify-between bg-slate-50/50"> <div class="p-4 border-t border-slate-100 flex items-center justify-between bg-slate-50/50">
<a href="{{ route('profile.index') }}" class="flex items-center space-x-2.5 min-w-0 hover:opacity-80 transition group" title="Buka Profil Saya"> <a href="{{ route('profile.index') }}" class="flex items-center space-x-2.5 min-w-0 hover:opacity-80 transition group" title="Buka Profil Saya">
@if(auth()->user()->photo_url)
<img src="{{ auth()->user()->photo_url }}" alt="{{ auth()->user()->name }}" class="w-8 h-8 rounded-full object-cover flex-shrink-0 shadow-sm group-hover:ring-2 ring-emerald-500/30">
@else
<div class="w-8 h-8 rounded-full bg-[#1b5e20] text-white font-black text-xs flex items-center justify-center flex-shrink-0 shadow-sm group-hover:ring-2 ring-emerald-500/30"> <div class="w-8 h-8 rounded-full bg-[#1b5e20] text-white font-black text-xs flex items-center justify-center flex-shrink-0 shadow-sm group-hover:ring-2 ring-emerald-500/30">
{{ strtoupper(substr(auth()->user()->name, 0, 1)) }} {{ strtoupper(substr(auth()->user()->name, 0, 1)) }}
</div> </div>
@endif
<div class="truncate"> <div class="truncate">
<p class="text-xs font-bold text-slate-800 truncate leading-tight group-hover:text-[#1b5e20]">{{ auth()->user()->name }}</p> <p class="text-xs font-bold text-slate-800 truncate leading-tight group-hover:text-[#1b5e20]">{{ auth()->user()->name }}</p>
<p class="text-[10px] text-slate-400 capitalize truncate">{{ str_replace('_', ' ', auth()->user()->role) }}</p> <p class="text-[10px] text-slate-400 capitalize truncate">{{ str_replace('_', ' ', auth()->user()->role) }}</p>
@@ -824,9 +832,13 @@
class="flex items-center space-x-2.5 p-1 sm:px-2.5 sm:py-1.5 rounded-xl hover:bg-slate-50 transition group focus:outline-none cursor-pointer" class="flex items-center space-x-2.5 p-1 sm:px-2.5 sm:py-1.5 rounded-xl hover:bg-slate-50 transition group focus:outline-none cursor-pointer"
aria-expanded="false" aria-expanded="false"
aria-haspopup="true"> aria-haspopup="true">
@if(auth()->user()->photo_url)
<img src="{{ auth()->user()->photo_url }}" alt="{{ auth()->user()->name }}" class="w-9 h-9 rounded-full object-cover shadow-sm group-hover:ring-2 ring-emerald-500/30 flex-shrink-0">
@else
<div class="w-9 h-9 rounded-full bg-[#1b5e20] text-white font-bold text-sm flex items-center justify-center shadow-sm group-hover:ring-2 ring-emerald-500/30 flex-shrink-0"> <div class="w-9 h-9 rounded-full bg-[#1b5e20] text-white font-bold text-sm flex items-center justify-center shadow-sm group-hover:ring-2 ring-emerald-500/30 flex-shrink-0">
{{ strtoupper(substr(auth()->user()->name, 0, 1)) }} {{ strtoupper(substr(auth()->user()->name, 0, 1)) }}
</div> </div>
@endif
<div class="text-left hidden sm:block"> <div class="text-left hidden sm:block">
<p class="text-xs font-bold text-slate-800 leading-tight group-hover:text-[#1b5e20]">{{ auth()->user()->name }}</p> <p class="text-xs font-bold text-slate-800 leading-tight group-hover:text-[#1b5e20]">{{ auth()->user()->name }}</p>
<p class="text-[10px] font-semibold text-slate-400 uppercase tracking-wider mt-0.5"> <p class="text-[10px] font-semibold text-slate-400 uppercase tracking-wider mt-0.5">
+33
View File
@@ -35,9 +35,13 @@
<!-- Kolom Kiri: Kartu Ringkasan Pengguna --> <!-- Kolom Kiri: Kartu Ringkasan Pengguna -->
<div class="bg-white rounded-2xl border border-slate-200 p-6 shadow-sm flex flex-col items-center text-center"> <div class="bg-white rounded-2xl border border-slate-200 p-6 shadow-sm flex flex-col items-center text-center">
<!-- Avatar Besar --> <!-- Avatar Besar -->
@if($user->photo_url)
<img src="{{ $user->photo_url }}" alt="{{ $user->name }}" class="w-24 h-24 rounded-full object-cover shadow-lg mb-4 ring-4 ring-emerald-50">
@else
<div class="w-24 h-24 rounded-full bg-[#1b5e20] text-white text-3xl font-black flex items-center justify-center shadow-lg mb-4 ring-4 ring-emerald-50"> <div class="w-24 h-24 rounded-full bg-[#1b5e20] text-white text-3xl font-black flex items-center justify-center shadow-lg mb-4 ring-4 ring-emerald-50">
{{ strtoupper(substr($user->name, 0, 1)) }} {{ strtoupper(substr($user->name, 0, 1)) }}
</div> </div>
@endif
<h3 class="text-lg font-bold text-slate-900">{{ $user->name }}</h3> <h3 class="text-lg font-bold text-slate-900">{{ $user->name }}</h3>
<p class="text-xs font-mono text-slate-500 mt-0.5">NIK / Username: {{ $user->username ?? '-' }}</p> <p class="text-xs font-mono text-slate-500 mt-0.5">NIK / Username: {{ $user->username ?? '-' }}</p>
@@ -62,6 +66,35 @@
<span class="text-slate-500">Unit Operasional</span> <span class="text-slate-500">Unit Operasional</span>
<span class="font-semibold text-slate-800">PLTA Merangin 350 MW</span> <span class="font-semibold text-slate-800">PLTA Merangin 350 MW</span>
</div> </div>
@if($user->employee || ($user->isEmployee() && $user->getOrCreateEmployee()))
@php $emp = $user->employee ?: $user->getOrCreateEmployee(); @endphp
@if($emp && $emp->division)
<div class="flex justify-between items-center py-1">
<span class="text-slate-500">Divisi</span>
<span class="font-semibold text-slate-800">{{ $emp->division }}</span>
</div>
@endif
@if($emp && $emp->position)
<div class="flex justify-between items-center py-1">
<span class="text-slate-500">Jabatan</span>
<span class="font-semibold text-slate-800">{{ $emp->position }}</span>
</div>
@endif
@if($emp && $emp->is_operator)
<div class="flex justify-between items-center py-1">
<span class="text-slate-500">Status SIMPER</span>
@if($emp->hasValidSimper())
<span class="inline-flex items-center px-2 py-0.5 rounded text-[11px] font-bold bg-emerald-100 text-emerald-800">
Aktif (s/d {{ $emp->simper_expired_at?->format('d/m/Y') }})
</span>
@else
<span class="inline-flex items-center px-2 py-0.5 rounded text-[11px] font-bold bg-rose-100 text-rose-800">
Kedaluwarsa
</span>
@endif
</div>
@endif
@endif
<div class="flex justify-between items-center py-1"> <div class="flex justify-between items-center py-1">
<span class="text-slate-500">Terdaftar Sejak</span> <span class="text-slate-500">Terdaftar Sejak</span>
<span class="font-semibold text-slate-800">{{ $user->created_at ? $user->created_at->format('d M Y') : '12 Okt 2026' }}</span> <span class="font-semibold text-slate-800">{{ $user->created_at ? $user->created_at->format('d M Y') : '12 Okt 2026' }}</span>
@@ -253,4 +253,81 @@ class PhotoUploadAndCompressionTest extends TestCase
Storage::disk('public')->assertMissing($oldPath); Storage::disk('public')->assertMissing($oldPath);
Storage::disk('public')->assertExists($newPath); Storage::disk('public')->assertExists($newPath);
} }
public function test_uploaded_employee_photo_syncs_to_user_and_displays_on_screens(): void
{
Storage::fake('public');
$avatar = UploadedFile::fake()->image('ahmad_avatar.png', 400, 400);
// HSE menambahkan karyawan Ahmad dengan foto
$this->actingAs($this->adminHse)->post(route('hse.employees.store'), [
'nik' => 'ahmad',
'name' => 'Ahmad Sukaya',
'unit_kerja' => 'Umum',
'division' => 'Operasional',
'position' => 'Driver Operasional',
'is_operator' => '1',
'simper_expired_at' => now()->addYear()->toDateString(),
'photo' => $avatar,
]);
$user = User::where('username', 'ahmad')->firstOrFail();
$employee = Employee::where('nik', 'ahmad')->firstOrFail();
// 1. Verifikasi sinkronisasi photo_url antara User dan Employee
$this->assertNotNull($user->photo_url);
$this->assertNotNull($employee->photo_url);
$this->assertEquals($employee->photo_url, $user->photo_url);
// 2. Login sebagai Ahmad dan buka Dashboard Karyawan
$dashboardResponse = $this->actingAs($user)->get(route('employee.dashboard'));
$dashboardResponse->assertOk();
$dashboardResponse->assertSee($user->photo_url, false);
// 3. Buka Halaman Profil Saya
$profileResponse = $this->actingAs($user)->get(route('profile.index'));
$profileResponse->assertOk();
$profileResponse->assertSee($user->photo_url, false);
}
public function test_superadmin_can_upload_photo_and_sync_with_employee(): void
{
Storage::fake('public');
$superAdmin = User::factory()->create([
'username' => 'superadmin_test',
'role' => 'super_admin',
]);
$superAdmin->assignRole('super_admin');
$avatar = UploadedFile::fake()->image('user_avatar.png', 500, 500);
// Super Admin membuat user baru dengan role employee dan menyertakan foto
$response = $this->actingAs($superAdmin)->post(route('admin.users.store'), [
'name' => 'Karyawan Baru',
'username' => 'karyawan_baru',
'email' => '[email protected]',
'password' => 'password123',
'role' => 'employee',
'photo' => $avatar,
]);
$response->assertRedirect(route('admin.users.index'));
$newUser = User::where('username', 'karyawan_baru')->firstOrFail();
$this->assertNotNull($newUser->photo_path);
Storage::disk('public')->assertExists($newUser->photo_path);
// Verifikasi otomatis sinkron ke profil Employee
$newEmployee = $newUser->employee;
$this->assertNotNull($newEmployee);
$this->assertEquals($newUser->photo_path, $newEmployee->photo_path);
$this->assertEquals($newUser->photo_url, $newEmployee->photo_url);
// Verifikasi foto muncul di tabel admin/users
$indexResponse = $this->actingAs($superAdmin)->get(route('admin.users.index'));
$indexResponse->assertOk();
$indexResponse->assertSee($newUser->photo_url, false);
}
} }