From f73c22ee9eaf10af345c8a6fdd0c86a1ce3a0832 Mon Sep 17 00:00:00 2001 From: Ray1111-aa Date: Thu, 22 Oct 2026 08:55:01 +0700 Subject: [PATCH] feat(pg): add flexible gate selection pop-up and remove rigid guard gate assignment --- .../Pg/GateInspectionController.php | 64 +++++- .../Pg/SecurityGuardController.php | 17 +- resources/views/pg/gate_inspection.blade.php | 139 ++++++++++++- resources/views/pg/guards_index.blade.php | 119 +++-------- routes/web.php | 1 + tests/Feature/GateInspectionTest.php | 193 ++++++++++++++++++ tests/Feature/SecurityGuardFilterTest.php | 2 +- 7 files changed, 435 insertions(+), 100 deletions(-) create mode 100644 tests/Feature/GateInspectionTest.php diff --git a/app/Http/Controllers/Pg/GateInspectionController.php b/app/Http/Controllers/Pg/GateInspectionController.php index f394b06..9b3767b 100644 --- a/app/Http/Controllers/Pg/GateInspectionController.php +++ b/app/Http/Controllers/Pg/GateInspectionController.php @@ -21,13 +21,30 @@ class GateInspectionController extends Controller */ public function index(Request $request): View { - // Ambil gerbang yang terikat pada akun petugas pos jaga yang sedang login $user = $request->user(); $currentGuard = $user ? $user->securityGuard : null; - $gateId = $user ? $user->gate_id : $request->query('gate_id'); - // Jika tidak ada gate_id terikat, ambil gerbang pertama sebagai fallback - $gate = $gateId ? Gate::find($gateId) : Gate::first(); + $allGates = Gate::where('is_active', true)->orderBy('name')->get(); + + // Cek gate yang dipilih: query param > session 'active_gate_id' > user->gate_id + $selectedGateId = $request->query('gate_id') + ?: session('active_gate_id') + ?: ($user?->gate_id); + + $needsGateSelection = false; + $gate = null; + + if ($selectedGateId && $allGates->contains('id', (int) $selectedGateId)) { + $gate = $allGates->firstWhere('id', (int) $selectedGateId); + session(['active_gate_id' => $gate->id]); + if ($user && $user->gate_id !== $gate->id) { + $user->update(['gate_id' => $gate->id]); + } + } else { + // Belum ada pos yang dipilih di sesi + $gate = $allGates->first(); + $needsGateSelection = true; + } $pendingActivities = collect(); if ($gate) { @@ -38,24 +55,61 @@ class GateInspectionController extends Controller ->get(); } + // Hitung antrean pending di setiap gerbang untuk pop-up pemilihan + $gateQueueCounts = VehicleActivity::where('validation_status', 'pending') + ->select('gate_id', DB::raw('count(*) as count')) + ->groupBy('gate_id') + ->pluck('count', 'gate_id'); + // Daftar satpam aktif untuk fallback pilihan $guards = SecurityGuard::where('is_active', true)->orderBy('name')->get(); return view('pg.gate_inspection', compact( 'gate', + 'allGates', + 'gateQueueCounts', + 'needsGateSelection', 'pendingActivities', 'guards', 'currentGuard' )); } + /** + * Set / ganti pos gerbang aktif yang sedang dijaga oleh petugas saat ini. + */ + public function selectGate(Request $request): RedirectResponse + { + $request->validate([ + 'gate_id' => ['required', 'exists:gates,id'], + ], [ + 'gate_id.required' => 'Silakan pilih salah satu pos gerbang.', + 'gate_id.exists' => 'Pos gerbang yang dipilih tidak valid.', + ]); + + $gateId = (int) $request->input('gate_id'); + $gate = Gate::findOrFail($gateId); + + session(['active_gate_id' => $gate->id]); + + $user = $request->user(); + if ($user && $user->gate_id !== $gate->id) { + $user->update(['gate_id' => $gate->id]); + } + + return redirect()->route('pg.gate.inspection', ['gate_id' => $gate->id]) + ->with('success', "Pos jaga aktif berhasil dialihkan ke: {$gate->name}"); + } + /** * Endpoint sync antrean realtime (Fetch) tanpa reload halaman. */ public function liveQueue(Request $request): JsonResponse { $user = $request->user(); - $gateId = $user ? $user->gate_id : $request->query('gate_id'); + $gateId = $request->query('gate_id') + ?: session('active_gate_id') + ?: ($user ? $user->gate_id : null); $gate = $gateId ? Gate::find($gateId) : Gate::first(); $pendingActivities = collect(); diff --git a/app/Http/Controllers/Pg/SecurityGuardController.php b/app/Http/Controllers/Pg/SecurityGuardController.php index bea5fa2..d9e2940 100644 --- a/app/Http/Controllers/Pg/SecurityGuardController.php +++ b/app/Http/Controllers/Pg/SecurityGuardController.php @@ -215,7 +215,7 @@ class SecurityGuardController extends Controller 'badge_id' => ['required', 'string', 'max:50', 'unique:security_guards,badge_id', 'unique:users,username'], 'name' => ['required', 'string', 'max:100'], 'team' => ['required', 'integer', 'in:1,2,3'], - 'gate_id' => ['required', 'exists:gates,id'], + 'gate_id' => ['nullable', 'exists:gates,id'], 'password' => ['required', 'string', 'min:4'], ], [ 'badge_id.required' => 'ID Satpam wajib diisi.', @@ -223,13 +223,13 @@ class SecurityGuardController extends Controller 'name.required' => 'Nama lengkap Satpam wajib diisi.', 'team.required' => 'Silakan pilih Regu Satpam (1, 2, atau 3).', 'team.in' => 'Regu Satpam harus bernilai 1, 2, atau 3.', - 'gate_id.required' => 'Silakan tentukan penempatan Pos Gerbang.', 'password.required' => 'Password akun login Satpam wajib diisi.', 'password.min' => 'Password minimal terdiri dari 4 karakter.', ]); DB::transaction(function () use ($request) { $badgeId = strtoupper(trim($request->input('badge_id'))); + $gateId = $request->input('gate_id') ?: null; // 1. Buat akun User untuk login Satpam di pos jaga $user = User::create([ @@ -238,7 +238,7 @@ class SecurityGuardController extends Controller 'email' => strtolower($badgeId) . '@kmh.co.id', 'password' => Hash::make($request->input('password')), 'role' => 'gate_officer', - 'gate_id' => $request->input('gate_id'), + 'gate_id' => $gateId, ]); // 2. Simpan master data Satpam @@ -246,7 +246,7 @@ class SecurityGuardController extends Controller 'badge_id' => $badgeId, 'name' => $request->input('name'), 'team' => (int) $request->input('team'), - 'gate_id' => $request->input('gate_id'), + 'gate_id' => $gateId, 'user_id' => $user->id, 'is_active' => true, ]); @@ -265,26 +265,27 @@ class SecurityGuardController extends Controller $request->validate([ 'name' => ['required', 'string', 'max:100'], 'team' => ['required', 'integer', 'in:1,2,3'], - 'gate_id' => ['required', 'exists:gates,id'], + 'gate_id' => ['nullable', 'exists:gates,id'], 'password' => ['nullable', 'string', 'min:4'], ], [ 'name.required' => 'Nama lengkap Satpam wajib diisi.', 'team.required' => 'Silakan pilih Regu Satpam (1, 2, atau 3).', - 'gate_id.required' => 'Silakan tentukan penempatan Pos Gerbang.', 'password.min' => 'Password minimal terdiri dari 4 karakter.', ]); DB::transaction(function () use ($request, $guard) { + $gateId = $request->has('gate_id') ? ($request->input('gate_id') ?: null) : $guard->gate_id; + $guard->update([ 'name' => $request->input('name'), 'team' => (int) $request->input('team'), - 'gate_id' => $request->input('gate_id'), + 'gate_id' => $gateId, ]); if ($guard->user) { $userData = [ 'name' => $request->input('name'), - 'gate_id' => $request->input('gate_id'), + 'gate_id' => $gateId, ]; if ($request->filled('password')) { diff --git a/resources/views/pg/gate_inspection.blade.php b/resources/views/pg/gate_inspection.blade.php index 074df9e..b6a5442 100644 --- a/resources/views/pg/gate_inspection.blade.php +++ b/resources/views/pg/gate_inspection.blade.php @@ -36,6 +36,14 @@ Regu {{ $currentGuard->team ?? '1' }} + +
Petugas: {{ $currentGuard ? $currentGuard->name : auth()->user()->name }} @@ -336,7 +344,7 @@ isSyncing = true; try { - const response = await fetch("{{ route('pg.gate.live_queue') }}", { + const response = await fetch("{{ route('pg.gate.live_queue') }}?gate_id={{ $gate ? $gate->id : '' }}", { headers: { 'X-Requested-With': 'XMLHttpRequest', 'Accept': 'application/json' @@ -427,6 +435,41 @@ const modal = document.getElementById('imageLightboxModal'); if (modal) modal.classList.add('hidden'); } + + // Modal Pemilihan Pos Gerbang Bertugas + let isGateModalForced = {{ $needsGateSelection ? 'true' : 'false' }}; + + function openGateSelectionModal(isForced = false) { + isGateModalForced = isForced; + const modal = document.getElementById('gateSelectionModal'); + if (modal) { + modal.classList.remove('hidden'); + modal.style.display = 'block'; + } + } + + function closeGateSelectionModal() { + if (isGateModalForced) { + return; + } + const modal = document.getElementById('gateSelectionModal'); + if (modal) { + modal.classList.add('hidden'); + modal.style.display = 'none'; + } + } + + function onGateModalBackdropClick() { + if (!isGateModalForced) { + closeGateSelectionModal(); + } + } + + @if($needsGateSelection) + document.addEventListener('DOMContentLoaded', function() { + openGateSelectionModal(true); + }); + @endif @@ -443,4 +486,98 @@
+ + + + +
+ +
+ +
+
+ +
+
+
+ +
+
+

+ Pilih Pos Gerbang Bertugas +

+

+ Tentukan gerbang tempat Anda berjaga saat ini untuk verifikasi perlintasan. +

+
+
+ @if(!$needsGateSelection) + + @endif +
+ + +
+ @csrf +
+ @foreach($allGates as $g) + @php + $isCurrent = $gate && $gate->id === $g->id; + $queueCount = $gateQueueCounts[$g->id] ?? 0; + @endphp + + @endforeach +
+ + @if(!$needsGateSelection) +
+ +
+ @endif +
+
+
+
@endsection diff --git a/resources/views/pg/guards_index.blade.php b/resources/views/pg/guards_index.blade.php index 964ab9e..a022328 100644 --- a/resources/views/pg/guards_index.blade.php +++ b/resources/views/pg/guards_index.blade.php @@ -78,30 +78,17 @@
-
+
- - -
@@ -129,7 +116,6 @@ ID SATPAM NAMA LENGKAP REGU - PENEMPATAN STATUS AKSI @@ -171,13 +157,6 @@ @endif - - - - {{ $g->gate->name ?? 'Belum Ditentukan' }} - - - @if($g->is_active) @@ -232,7 +211,7 @@ @empty - + @if(request('search'))

@@ -241,7 +220,7 @@

Pastikan ejaan ID Satpam atau nama petugas yang dicari sudah benar.

@elseif(request()->anyFilled(['team', 'gate_id']))

Belum ada data petugas Satpam yang sesuai kriteria filter.

-

Silakan pilih regu atau penempatan pos gerbang lain.

+

Silakan pilih regu atau ubah kata kunci pencarian.

@else

Belum ada data petugas Satpam terdaftar.

@@ -317,39 +296,21 @@ class="w-full text-sm px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-800 placeholder-slate-400 focus:outline-none focus:border-[#1b5e20] focus:ring-2 focus:ring-[#1b5e20]/10 transition">
- -
-
- - -
- -
- - -
+ +
+ + + Petugas dapat memilih pos gerbang jaga saat login/bertugas secara fleksibel.
@@ -414,31 +375,18 @@ class="w-full text-sm px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-800 placeholder-slate-400 focus:outline-none focus:border-[#1b5e20] focus:ring-2 focus:ring-[#1b5e20]/10 transition">
- -
-
- - -
- -
- - -
+ +
+ + + Petugas dapat memilih pos gerbang jaga saat login/bertugas secara fleksibel.
@@ -659,7 +607,8 @@ document.getElementById('editBadgeId').value = guard.badge_id || ('SEC-' + String(guard.id).padStart(3, '0')); document.getElementById('editName').value = guard.name || ''; document.getElementById('editTeam').value = guard.team || 1; - document.getElementById('editGateId').value = guard.gate_id || ''; + const editGateInput = document.getElementById('editGateId'); + if (editGateInput) editGateInput.value = guard.gate_id || ''; document.getElementById('editPassword').value = ''; modal.classList.remove('hidden'); @@ -710,7 +659,7 @@ const emptyTr = document.createElement('tr'); emptyTr.id = 'guardEmptySearchRow'; emptyTr.innerHTML = ` - +

Data "${escapeHtml(rawValue)}" tidak ditemukan di tabel diff --git a/routes/web.php b/routes/web.php index 6cc3dd1..012caf1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -161,6 +161,7 @@ Route::middleware('auth')->group(function () { // Verifikasi Pos Gerbang (Khusus Petugas Satpam Pos Gerbang) Route::get('/gate-inspection', [GateInspectionController::class, 'index'])->name('gate.inspection')->middleware('permission:gate.inspect'); + Route::post('/gate-inspection/select-gate', [GateInspectionController::class, 'selectGate'])->name('gate.select_gate')->middleware('permission:gate.inspect'); Route::get('/gate-inspection/live-queue', [GateInspectionController::class, 'liveQueue'])->name('gate.live_queue')->middleware('permission:gate.inspect'); Route::post('/gate-inspection/{id}/approve', [GateInspectionController::class, 'approve'])->name('gate.approve')->middleware('permission:gate.approve'); Route::post('/gate-inspection/{id}/reject', [GateInspectionController::class, 'reject'])->name('gate.reject')->middleware('permission:gate.reject'); diff --git a/tests/Feature/GateInspectionTest.php b/tests/Feature/GateInspectionTest.php new file mode 100644 index 0000000..df6ad65 --- /dev/null +++ b/tests/Feature/GateInspectionTest.php @@ -0,0 +1,193 @@ +seed(RolePermissionSeeder::class); + + $this->gate1 = Gate::create([ + 'name' => 'Pos Gerbang Utama', + 'code' => 'GATE-01', + 'location' => 'Main Entrance', + 'is_active' => true, + ]); + + $this->gate2 = Gate::create([ + 'name' => 'Pos Gerbang Bendungan', + 'code' => 'GATE-02', + 'location' => 'Dam Access', + 'is_active' => true, + ]); + + // Satpam dibuat TANPA gate_id (fleksibel) + $this->guardUser = User::factory()->create([ + 'name' => 'Petugas Satpam Uji', + 'username' => 'SEC999', + 'role' => 'gate_officer', + 'gate_id' => null, + ]); + $this->guardUser->assignRole('gate_officer'); + + $this->guard = SecurityGuard::create([ + 'badge_id' => 'SEC999', + 'name' => 'Petugas Satpam Uji', + 'team' => 1, + 'gate_id' => null, + 'user_id' => $this->guardUser->id, + 'is_active' => true, + ]); + } + + /** + * Saat satpam pertama kali login dan belum memilih gerbang di sesi, + * flag needsGateSelection = true dan modal pemilihan pos gerbang ditampilkan. + */ + public function test_guard_without_selected_gate_sees_selection_modal(): void + { + $response = $this->actingAs($this->guardUser) + ->get(route('pg.gate.inspection')); + + $response->assertOk(); + $response->assertViewHas('needsGateSelection', true); + $response->assertSee('Pilih Pos Gerbang Bertugas'); + $response->assertSee('Pos Gerbang Utama'); + $response->assertSee('Pos Gerbang Bendungan'); + $response->assertSee('Ganti Pos'); + } + + /** + * Satpam dapat memilih pos gerbang via POST select-gate. + * Sistem menyimpan ke session active_gate_id dan mengalihkan ke verifikasi gerbang terkait. + */ + public function test_guard_can_select_gate_and_stores_in_session(): void + { + $response = $this->actingAs($this->guardUser) + ->post(route('pg.gate.select_gate'), [ + 'gate_id' => $this->gate2->id, + ]); + + $response->assertRedirect(route('pg.gate.inspection', ['gate_id' => $this->gate2->id])); + $response->assertSessionHas('active_gate_id', $this->gate2->id); + + $this->guardUser->refresh(); + $this->assertEquals($this->gate2->id, $this->guardUser->gate_id); + + // Setelah tersimpan di sesi, kunjungan berikutnya tidak memunculkan needsGateSelection = true + $pageResponse = $this->actingAs($this->guardUser) + ->withSession(['active_gate_id' => $this->gate2->id]) + ->get(route('pg.gate.inspection')); + + $pageResponse->assertOk(); + $pageResponse->assertViewHas('needsGateSelection', false); + $pageResponse->assertSee('Pos Pos Gerbang Bendungan'); + } + + /** + * Live queue mengambil data antrean sesuai gerbang yang sedang aktif. + */ + public function test_live_queue_filters_by_active_gate(): void + { + $vehicle = Vehicle::create([ + 'code' => 'LV-99', + 'category' => 'light_vehicle', + 'brand_type' => 'Toyota Hilux 4x4', + 'current_km' => 1000, + ]); + + $driver = Employee::create([ + 'name' => 'Driver Uji', + 'nik' => 'KMH-999', + ]); + + // Buat aktivitas pending di Gate 2 + VehicleActivity::create([ + 'transaction_no' => 'TRX-999-01', + 'vehicle_id' => $vehicle->id, + 'driver_id' => $driver->id, + 'gate_id' => $this->gate2->id, + 'direction' => 'in', + 'previous_km' => 1000, + 'current_km' => 1050, + 'distance' => 50, + 'purpose' => 'Mobilisasi Pekerja Project', + 'validation_status' => 'pending', + 'recorded_at' => now(), + ]); + + // Request live queue dengan gate_id Gate 1 -> count = 0 + $resGate1 = $this->actingAs($this->guardUser) + ->getJson(route('pg.gate.live_queue', ['gate_id' => $this->gate1->id])); + + $resGate1->assertOk(); + $resGate1->assertJsonPath('count', 0); + + // Request live queue dengan gate_id Gate 2 -> count = 1 + $resGate2 = $this->actingAs($this->guardUser) + ->getJson(route('pg.gate.live_queue', ['gate_id' => $this->gate2->id])); + + $resGate2->assertOk(); + $resGate2->assertJsonPath('count', 1); + $this->assertStringContainsString('LV-99', $resGate2->json('html')); + } + + /** + * Admin PG dapat mendaftarkan Satpam baru tanpa mengisi gate_id (penempatan fleksibel). + */ + public function test_admin_pg_can_store_guard_without_gate_id(): void + { + $adminPg = User::factory()->create([ + 'name' => 'Admin PG Tester', + 'username' => 'admin_pg_test', + 'role' => 'pg_admin', + ]); + $adminPg->assignRole('pg_admin'); + + $data = [ + 'badge_id' => 'SEC-NEW-01', + 'name' => 'Satpam Baru Fleksibel', + 'team' => 2, + 'password' => 'secret123', + // gate_id dikosongkan + ]; + + $response = $this->actingAs($adminPg) + ->post(route('pg.guards.store'), $data); + + $response->assertRedirect(); + $response->assertSessionHas('success'); + + $this->assertDatabaseHas('security_guards', [ + 'badge_id' => 'SEC-NEW-01', + 'name' => 'Satpam Baru Fleksibel', + 'team' => 2, + 'gate_id' => null, + ]); + + $this->assertDatabaseHas('users', [ + 'username' => 'SEC-NEW-01', + 'role' => 'gate_officer', + 'gate_id' => null, + ]); + } +} diff --git a/tests/Feature/SecurityGuardFilterTest.php b/tests/Feature/SecurityGuardFilterTest.php index 9e1d9de..6d7f74f 100644 --- a/tests/Feature/SecurityGuardFilterTest.php +++ b/tests/Feature/SecurityGuardFilterTest.php @@ -33,7 +33,7 @@ class SecurityGuardFilterTest extends TestCase $response->assertSee('Data Satpam'); $response->assertSee('Cari ID Satpam atau nama petugas...'); $response->assertSee('Semua Regu'); - $response->assertSee('Semua Penempatan'); + $response->assertDontSee('Semua Penempatan'); $response->assertSee('Ahmad Fauzi'); }