fix(pg): allow editing guard badge_id with user sync and fix verification logs filter js error
This commit is contained in:
@@ -13,6 +13,7 @@ use Illuminate\Http\RedirectResponse;
|
|||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
use Illuminate\View\View;
|
use Illuminate\View\View;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
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
|
public function update(Request $request, int $id): RedirectResponse
|
||||||
{
|
{
|
||||||
$guard = SecurityGuard::with('user')->findOrFail($id);
|
$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([
|
$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'],
|
'name' => ['required', 'string', 'max:100'],
|
||||||
'team' => ['required', 'integer', 'in:1,2,3'],
|
'team' => ['required', 'integer', 'in:1,2,3'],
|
||||||
'gate_id' => ['nullable', 'exists:gates,id'],
|
'gate_id' => ['nullable', 'exists:gates,id'],
|
||||||
'password' => ['nullable', 'string', 'min:4'],
|
'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.',
|
'name.required' => 'Nama lengkap Satpam wajib diisi.',
|
||||||
'team.required' => 'Silakan pilih Regu Satpam (1, 2, atau 3).',
|
'team.required' => 'Silakan pilih Regu Satpam (1, 2, atau 3).',
|
||||||
'password.min' => 'Password minimal terdiri dari 4 karakter.',
|
'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;
|
$gateId = $request->has('gate_id') ? ($request->input('gate_id') ?: null) : $guard->gate_id;
|
||||||
|
|
||||||
$guard->update([
|
$guard->update([
|
||||||
|
'badge_id' => $newBadgeId,
|
||||||
'name' => $request->input('name'),
|
'name' => $request->input('name'),
|
||||||
'team' => (int) $request->input('team'),
|
'team' => (int) $request->input('team'),
|
||||||
'gate_id' => $gateId,
|
'gate_id' => $gateId,
|
||||||
@@ -305,9 +323,14 @@ class SecurityGuardController extends Controller
|
|||||||
if ($guard->user) {
|
if ($guard->user) {
|
||||||
$userData = [
|
$userData = [
|
||||||
'name' => $request->input('name'),
|
'name' => $request->input('name'),
|
||||||
|
'username' => $newBadgeId,
|
||||||
'gate_id' => $gateId,
|
'gate_id' => $gateId,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ($guard->user->email === strtolower((string) $oldBadgeId) . '@kmh.co.id') {
|
||||||
|
$userData['email'] = strtolower($newBadgeId) . '@kmh.co.id';
|
||||||
|
}
|
||||||
|
|
||||||
if ($request->filled('password')) {
|
if ($request->filled('password')) {
|
||||||
$userData['password'] = Hash::make($request->input('password'));
|
$userData['password'] = Hash::make($request->input('password'));
|
||||||
}
|
}
|
||||||
@@ -316,9 +339,9 @@ class SecurityGuardController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sinkronkan nama dan profil Employee Satpam
|
// Sinkronkan nama dan profil Employee Satpam
|
||||||
$emp = Employee::where('user_id', $guard->user_id)->orWhere('nik', $guard->badge_id)->first();
|
|
||||||
if ($emp) {
|
if ($emp) {
|
||||||
$emp->update([
|
$emp->update([
|
||||||
|
'nik' => $newBadgeId,
|
||||||
'name' => $guard->name,
|
'name' => $guard->name,
|
||||||
'unit_kerja' => $emp->unit_kerja ?: 'Private Guard',
|
'unit_kerja' => $emp->unit_kerja ?: 'Private Guard',
|
||||||
'division' => ($emp->division && $emp->division !== '-') ? $emp->division : '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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -374,10 +374,14 @@
|
|||||||
<form id="editGuardForm" method="POST" action="" class="mt-5 space-y-4">
|
<form id="editGuardForm" method="POST" action="" class="mt-5 space-y-4">
|
||||||
@csrf
|
@csrf
|
||||||
|
|
||||||
<!-- ID Satpam (Read-only) -->
|
<!-- ID Satpam (Username Login) -->
|
||||||
<div>
|
<div>
|
||||||
<label class="block text-sm font-bold text-slate-800 mb-2">ID Satpam (Username Login)</label>
|
<label for="editBadgeId" class="block text-sm font-bold text-slate-800 mb-2">
|
||||||
<input type="text" id="editBadgeId" disabled class="w-full text-sm px-4 py-3 rounded-2xl border border-slate-200 bg-slate-100 text-slate-600 font-mono font-bold">
|
ID Satpam (Username Login) <span class="text-rose-500">*</span>
|
||||||
|
</label>
|
||||||
|
<input type="text" name="badge_id" id="editBadgeId" required placeholder="Contoh: SEC-001"
|
||||||
|
class="w-full text-sm px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-800 font-mono font-bold placeholder-slate-400 focus:outline-none focus:border-[#1b5e20] focus:ring-2 focus:ring-[#1b5e20]/10 transition">
|
||||||
|
<p class="text-[11px] text-slate-400 mt-1">Mengubah ID Satpam otomatis memperbarui username login dan NIK Karyawan.</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Nama Lengkap -->
|
<!-- Nama Lengkap -->
|
||||||
|
|||||||
@@ -658,7 +658,6 @@
|
|||||||
}, 500);
|
}, 500);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// Panduan Halaman Modal
|
// Panduan Halaman Modal
|
||||||
function openGuideModal() {
|
function openGuideModal() {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Tests\Feature;
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\Employee;
|
||||||
use App\Models\Gate;
|
use App\Models\Gate;
|
||||||
use App\Models\SecurityGuard;
|
use App\Models\SecurityGuard;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
@@ -201,4 +202,72 @@ class SecurityGuardFilterTest extends TestCase
|
|||||||
$middlewareResponse = $this->actingAs($user)->get(route('pg.dashboard'));
|
$middlewareResponse = $this->actingAs($user)->get(route('pg.dashboard'));
|
||||||
$middlewareResponse->assertStatus(403);
|
$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('[email protected]', $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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user