From 61332f948ba7662799800b234fac94231d6fed78 Mon Sep 17 00:00:00 2001 From: Ray1111-aa Date: Tue, 3 Nov 2026 13:07:14 +0700 Subject: [PATCH] fix(pg): allow editing guard badge_id with user sync and fix verification logs filter js error --- .../Pg/SecurityGuardController.php | 31 +++++++-- resources/views/pg/guards_index.blade.php | 10 ++- .../views/pg/verification_logs.blade.php | 1 - tests/Feature/SecurityGuardFilterTest.php | 69 +++++++++++++++++++ 4 files changed, 103 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Pg/SecurityGuardController.php b/app/Http/Controllers/Pg/SecurityGuardController.php index 549e183..a8409a0 100644 --- a/app/Http/Controllers/Pg/SecurityGuardController.php +++ b/app/Http/Controllers/Pg/SecurityGuardController.php @@ -13,6 +13,7 @@ use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; +use Illuminate\Validation\Rule; use Illuminate\View\View; use Symfony\Component\HttpFoundation\Response; @@ -276,27 +277,44 @@ class SecurityGuardController extends Controller } /** - * Perbarui data Satpam (Nama, Regu, Penempatan Pos, dan Password opsional). + * Perbarui data Satpam (ID Satpam, Nama, Regu, Penempatan Pos, dan Password opsional). */ public function update(Request $request, int $id): RedirectResponse { $guard = SecurityGuard::with('user')->findOrFail($id); + $newBadgeId = strtoupper(trim((string) $request->input('badge_id'))); + $request->merge(['badge_id' => $newBadgeId]); + + $emp = Employee::where('user_id', $guard->user_id)->orWhere('nik', $guard->badge_id)->first(); + $request->validate([ + 'badge_id' => [ + 'required', + 'string', + 'max:50', + Rule::unique('security_guards', 'badge_id')->ignore($guard->id), + Rule::unique('users', 'username')->ignore($guard->user_id), + Rule::unique('employees', 'nik')->ignore($emp?->id), + ], 'name' => ['required', 'string', 'max:100'], 'team' => ['required', 'integer', 'in:1,2,3'], 'gate_id' => ['nullable', 'exists:gates,id'], 'password' => ['nullable', 'string', 'min:4'], ], [ + 'badge_id.required' => 'ID Satpam wajib diisi.', + 'badge_id.unique' => 'ID Satpam / NIK ini sudah digunakan oleh petugas/karyawan lain.', 'name.required' => 'Nama lengkap Satpam wajib diisi.', 'team.required' => 'Silakan pilih Regu Satpam (1, 2, atau 3).', 'password.min' => 'Password minimal terdiri dari 4 karakter.', ]); - DB::transaction(function () use ($request, $guard) { + DB::transaction(function () use ($request, $guard, $newBadgeId, $emp) { + $oldBadgeId = $guard->badge_id; $gateId = $request->has('gate_id') ? ($request->input('gate_id') ?: null) : $guard->gate_id; $guard->update([ + 'badge_id' => $newBadgeId, 'name' => $request->input('name'), 'team' => (int) $request->input('team'), 'gate_id' => $gateId, @@ -305,9 +323,14 @@ class SecurityGuardController extends Controller if ($guard->user) { $userData = [ 'name' => $request->input('name'), + 'username' => $newBadgeId, 'gate_id' => $gateId, ]; + if ($guard->user->email === strtolower((string) $oldBadgeId) . '@kmh.co.id') { + $userData['email'] = strtolower($newBadgeId) . '@kmh.co.id'; + } + if ($request->filled('password')) { $userData['password'] = Hash::make($request->input('password')); } @@ -316,9 +339,9 @@ class SecurityGuardController extends Controller } // Sinkronkan nama dan profil Employee Satpam - $emp = Employee::where('user_id', $guard->user_id)->orWhere('nik', $guard->badge_id)->first(); if ($emp) { $emp->update([ + 'nik' => $newBadgeId, 'name' => $guard->name, 'unit_kerja' => $emp->unit_kerja ?: 'Private Guard', 'division' => ($emp->division && $emp->division !== '-') ? $emp->division : 'Private Guard', @@ -327,7 +350,7 @@ class SecurityGuardController extends Controller } }); - return back()->with('success', "Data petugas Satpam {$guard->name} berhasil diperbarui."); + return back()->with('success', "Data petugas Satpam {$guard->name} ({$newBadgeId}) berhasil diperbarui."); } /** diff --git a/resources/views/pg/guards_index.blade.php b/resources/views/pg/guards_index.blade.php index 047795b..3200a49 100644 --- a/resources/views/pg/guards_index.blade.php +++ b/resources/views/pg/guards_index.blade.php @@ -374,10 +374,14 @@
@csrf - +
- - + + +

Mengubah ID Satpam otomatis memperbarui username login dan NIK Karyawan.

diff --git a/resources/views/pg/verification_logs.blade.php b/resources/views/pg/verification_logs.blade.php index 47e9fff..a4ae1df 100644 --- a/resources/views/pg/verification_logs.blade.php +++ b/resources/views/pg/verification_logs.blade.php @@ -658,7 +658,6 @@ }, 500); }); }); - }); // Panduan Halaman Modal function openGuideModal() { diff --git a/tests/Feature/SecurityGuardFilterTest.php b/tests/Feature/SecurityGuardFilterTest.php index aede0b1..80307ac 100644 --- a/tests/Feature/SecurityGuardFilterTest.php +++ b/tests/Feature/SecurityGuardFilterTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature; +use App\Models\Employee; use App\Models\Gate; use App\Models\SecurityGuard; use App\Models\User; @@ -201,4 +202,72 @@ class SecurityGuardFilterTest extends TestCase $middlewareResponse = $this->actingAs($user)->get(route('pg.dashboard')); $middlewareResponse->assertStatus(403); } + + public function test_admin_pg_can_edit_guard_badge_id_and_syncs_with_user_and_employee(): void + { + $guard = SecurityGuard::with('user')->where('badge_id', 'SEC-002')->first(); + $this->assertNotNull($guard); + $this->assertNotNull($guard->user); + + // Pastikan ada Employee dengan NIK lama + $emp = Employee::firstOrCreate( + ['nik' => 'SEC-002'], + [ + 'user_id' => $guard->user_id, + 'name' => $guard->name, + 'employee_type' => 'Karyawan', + 'unit_kerja' => 'Private Guard', + 'division' => 'Private Guard', + 'position' => 'Anggota', + ] + ); + + $response = $this->actingAs($this->adminPg)->post(route('pg.guards.update', $guard->id), [ + 'badge_id' => 'SEC-002-EDIT', + 'name' => 'Budi Santoso Diperbarui', + 'team' => 3, + 'password' => 'newpassword123', + ]); + + $response->assertRedirect(); + $response->assertSessionHasNoErrors(); + + // Verifikasi SecurityGuard + $guard->refresh(); + $this->assertEquals('SEC-002-EDIT', $guard->badge_id); + $this->assertEquals('Budi Santoso Diperbarui', $guard->name); + $this->assertEquals(3, $guard->team); + + // Verifikasi User login + $guard->user->refresh(); + $this->assertEquals('SEC-002-EDIT', $guard->user->username); + $this->assertEquals('sec-002-edit@kmh.co.id', $guard->user->email); + + // Verifikasi Employee NIK + $emp->refresh(); + $this->assertEquals('SEC-002-EDIT', $emp->nik); + $this->assertEquals('Budi Santoso Diperbarui', $emp->name); + + // Verifikasi login dengan ID baru dan password baru + $this->post('/logout'); + $loginResponse = $this->post('/login', [ + 'username' => 'SEC-002-EDIT', + 'password' => 'newpassword123', + ]); + $loginResponse->assertRedirect(); + $this->assertAuthenticatedAs($guard->user); + } + + public function test_editing_guard_with_duplicate_badge_id_is_rejected(): void + { + $guard = SecurityGuard::where('badge_id', 'SEC-002')->first(); + + $response = $this->actingAs($this->adminPg)->post(route('pg.guards.update', $guard->id), [ + 'badge_id' => 'SEC-001', // sudah dipakai oleh guard 1 + 'name' => 'Budi Duplicate', + 'team' => 2, + ]); + + $response->assertSessionHasErrors('badge_id'); + } }