fix(hse): allow employee import and guard creation to restore soft-deleted users and employees without duplicate key collision

This commit is contained in:
Ray1111-aa
2026-11-03 14:13:45 +07:00
parent a55ead4c5b
commit dc069fe0ec
3 changed files with 96 additions and 13 deletions
@@ -259,9 +259,23 @@ class SecurityGuardController extends Controller
]);
// 3. Otomatis buat / sinkronkan profil Employee untuk Satpam (Divisi: Private Guard, Jabatan: Anggota)
Employee::firstOrCreate(
['nik' => $badgeId],
[
$guardEmp = Employee::withTrashed()->where('nik', $badgeId)->first();
if ($guardEmp) {
if ($guardEmp->trashed()) {
$guardEmp->restore();
}
$guardEmp->update([
'user_id' => $user->id,
'name' => $request->input('name'),
'employee_type' => 'Karyawan',
'unit_kerja' => $guardEmp->unit_kerja ?: 'Private Guard',
'division' => ($guardEmp->division && $guardEmp->division !== '-') ? $guardEmp->division : 'Private Guard',
'position' => ($guardEmp->position && $guardEmp->position !== '-') ? $guardEmp->position : 'Anggota',
'is_operator' => false,
]);
} else {
Employee::create([
'nik' => $badgeId,
'user_id' => $user->id,
'name' => $request->input('name'),
'employee_type' => 'Karyawan',
@@ -269,8 +283,8 @@ class SecurityGuardController extends Controller
'division' => 'Private Guard',
'position' => 'Anggota',
'is_operator' => false,
]
);
]);
}
});
return back()->with('success', "Petugas Satpam {$request->input('name')} (ID: {$request->input('badge_id')}) berhasil didaftarkan.");