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\Gate;
use App\Models\User;
use App\Services\ImageCompressionService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
@@ -73,6 +74,7 @@ class UserController extends Controller
'password' => ['required', 'string', 'min:6'],
'role' => ['required', 'string', 'exists:roles,name'],
'gate_id' => ['nullable', 'exists:gates,id'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
], [
'name.required' => 'Nama lengkap wajib diisi.',
'username.required' => 'Username wajib diisi.',
@@ -85,6 +87,11 @@ class UserController extends Controller
'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([
'name' => $validated['name'],
'username' => $validated['username'],
@@ -92,6 +99,7 @@ class UserController extends Controller
'password' => Hash::make($validated['password']),
'role' => $validated['role'], // Simpan ke kolom legacy untuk backwards-compat
'gate_id' => $validated['gate_id'] ?? null,
'photo_path' => $photoPath,
]);
// 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
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
@@ -149,6 +160,7 @@ class UserController extends Controller
'password' => ['nullable', 'string', 'min:6'],
'role' => ['required', 'string', 'exists:roles,name'],
'gate_id' => ['nullable', 'exists:gates,id'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
], [
'name.required' => 'Nama lengkap wajib diisi.',
'username.required' => 'Username wajib diisi.',
@@ -180,6 +192,11 @@ class UserController extends Controller
$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();
// Sync Spatie Role
@@ -199,9 +216,13 @@ class UserController extends Controller
$user->employee->update([
'name' => $user->name,
'nik' => $user->username,
'photo_path' => $user->photo_path ?: $user->employee->photo_path,
]);
} 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
@@ -568,6 +568,7 @@ class EmployeeController extends Controller
$photoPath = null;
if ($request->hasFile('photo')) {
$photoPath = ImageCompressionService::uploadAndCompress($request->file('photo'), 'employees', 500, 500, 80);
$user->update(['photo_path' => $photoPath]);
}
Employee::create([
@@ -627,6 +628,18 @@ class EmployeeController extends Controller
'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.");
}
}
+9 -4
View File
@@ -48,15 +48,20 @@ class Employee extends Model
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;
}
if (str_starts_with($this->photo_path, 'http://') || str_starts_with($this->photo_path, 'https://')) {
return $this->photo_path;
if (str_starts_with($path, 'http://') || str_starts_with($path, 'https://')) {
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',
'role',
'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);
}
/**
* 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),
* atau buat profil Employee default jika akun berstatus employee / super admin penguji.