From 1d58c66a7a6f0088c1f79e895cffa20150c240bc Mon Sep 17 00:00:00 2001 From: Ariyutta Date: Sun, 27 Sep 2026 16:09:37 +0700 Subject: [PATCH] Perbaikan API WA --- .../VerifikasiAbsenController.php | 34 ++++++++++- app/Services/VerifikasiAbsenLinkService.php | 54 +++++++++++------- .../views/verifikasi-absen/halaman.blade.php | 56 ++++++++++++++----- routes/api.php | 1 + 4 files changed, 110 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/Api/VerifikasiAbsen/VerifikasiAbsenController.php b/app/Http/Controllers/Api/VerifikasiAbsen/VerifikasiAbsenController.php index 20a24675..cb0c08a3 100644 --- a/app/Http/Controllers/Api/VerifikasiAbsen/VerifikasiAbsenController.php +++ b/app/Http/Controllers/Api/VerifikasiAbsen/VerifikasiAbsenController.php @@ -34,7 +34,7 @@ class VerifikasiAbsenController extends Controller { $pengajuan = PengajuanPresensi::with(['pegawai', 'notes'])->findOrFail($id); - $otorisasi = $this->otorisasiAtasan($request, $pengajuan, renewSesi: true); + $otorisasi = $this->otorisasiAtasan($request, $pengajuan); if (! $otorisasi['ok']) { return $this->responseTidakBerwenang($otorisasi['reason']); @@ -44,7 +44,37 @@ class VerifikasiAbsenController extends Controller 'success' => true, 'state' => $this->resolveState($pengajuan), 'data' => $this->formatPengajuan($pengajuan), - 'new_token' => $otorisasi['new_token'], + ]); + } + + /** + * Terbitkan token baru untuk tautan yang sesinya sudah habis (dipicu tombol "Minta Token Baru"). + */ + public function perbaruiToken(Request $request, int $id): JsonResponse + { + $pengajuan = PengajuanPresensi::findOrFail($id); + + if ($pengajuan->status !== 'Diajukan') { + return response()->json([ + 'success' => false, + 'state' => 'already_processed', + 'message' => 'Pengajuan ini sudah diproses sebelumnya.', + ], 422); + } + + $tokenBaru = $this->linkService->regenerate($id, $request->input('token')); + + if (! $tokenBaru) { + return response()->json([ + 'success' => false, + 'state' => 'link_expired', + 'message' => 'Tautan verifikasi ini sudah tidak valid. Silakan minta tautan verifikasi baru.', + ], 403); + } + + return response()->json([ + 'success' => true, + 'new_token' => $tokenBaru, ]); } diff --git a/app/Services/VerifikasiAbsenLinkService.php b/app/Services/VerifikasiAbsenLinkService.php index dcbfeaff..21389315 100644 --- a/app/Services/VerifikasiAbsenLinkService.php +++ b/app/Services/VerifikasiAbsenLinkService.php @@ -44,15 +44,11 @@ class VerifikasiAbsenLinkService } /** - * @param bool $renewSesi Saat true dan sesi sudah lewat 5 menit, token lama akan digantikan dengan - * token baru (bukan langsung ditolak). Dipakai saat tautan dibuka ulang dari - * WhatsApp (klik tombol "Tinjau Pengajuan"), supaya tidak perlu membuat - * pengajuan baru hanya untuk mendapatkan token baru. - * @return array{ok: bool, nip: ?string, reason: ?string, new_token: ?string} + * @return array{ok: bool, nip: ?string, reason: ?string} */ - public function verify(int $pengajuanId, ?string $token, bool $renewSesi = false): array + public function verify(int $pengajuanId, ?string $token): array { - $gagal = fn (string $reason) => ['ok' => false, 'nip' => null, 'reason' => $reason, 'new_token' => null]; + $gagal = fn (string $reason) => ['ok' => false, 'nip' => null, 'reason' => $reason]; if (! $token) { return $gagal('invalid'); @@ -71,23 +67,43 @@ class VerifikasiAbsenLinkService return $gagal('link_expired'); } - $tokenBaru = null; - if ($row->first_accessed_at === null) { $row->update(['first_accessed_at' => now()]); } elseif (now()->greaterThan($row->first_accessed_at->copy()->addMinutes(self::SESI_MENIT))) { - if (! $renewSesi) { - return $gagal('session_expired'); - } - - $tokenBaru = Str::random(40); - $row->update([ - 'token' => $this->hash($tokenBaru), - 'first_accessed_at' => now(), - ]); + return $gagal('session_expired'); } - return ['ok' => true, 'nip' => $row->nip, 'reason' => null, 'new_token' => $tokenBaru]; + return ['ok' => true, 'nip' => $row->nip, 'reason' => null]; + } + + /** + * Terbitkan token baru untuk pengajuan yang sesinya sudah habis, dipicu lewat aksi eksplisit + * (tombol "Minta Token Baru" di halaman error), bukan otomatis saat tautan dibuka ulang. + * Token lama tetap harus dikenali (belum invalid) dan link belum lewat masa berlaku (5 hari). + */ + public function regenerate(int $pengajuanId, ?string $token): ?string + { + if (! $token) { + return null; + } + + $row = VerifikasiAbsenToken::where('pengajuan_id', $pengajuanId) + ->where('token', $this->hash($token)) + ->where('purpose', self::PURPOSE) + ->first(); + + if (! $row || now()->greaterThan($row->expires_at)) { + return null; + } + + $tokenBaru = Str::random(40); + + $row->update([ + 'token' => $this->hash($tokenBaru), + 'first_accessed_at' => null, + ]); + + return $tokenBaru; } public function markUsed(int $pengajuanId, string $token): void diff --git a/resources/views/verifikasi-absen/halaman.blade.php b/resources/views/verifikasi-absen/halaman.blade.php index b093738a..b100b2a8 100644 --- a/resources/views/verifikasi-absen/halaman.blade.php +++ b/resources/views/verifikasi-absen/halaman.blade.php @@ -124,6 +124,9 @@

+
@@ -229,26 +232,30 @@ }); } - function ambilPesanError(xhr) { + function ambilDataError(xhr) { try { - const data = JSON.parse(xhr.responseText); - return data.message || 'Terjadi kesalahan. Silakan coba lagi.'; + return JSON.parse(xhr.responseText); } catch (e) { - return 'Terjadi kesalahan. Silakan coba lagi.'; + return {}; } } + function ambilPesanError(xhr) { + return ambilDataError(xhr).message || 'Terjadi kesalahan. Silakan coba lagi.'; + } + const statusBadgeMap = { 'Diajukan': ['bg-warning text-dark', 'Menunggu Verifikasi'], 'Diterima': ['bg-success', 'Diterima'], 'Ditolak': ['bg-danger', 'Ditolak'], }; - function tampilkanPesan(icon, text, colorClass) { + function tampilkanPesan(icon, text, colorClass, bisaMintaTokenBaru = false) { $('#pesanState').removeClass('d-none'); $('#detailState').addClass('d-none'); $('#pesanIcon').attr('class', `fas ${icon} fa-3x mb-3 ${colorClass}`); $('#pesanText').text(text); + $('#btnMintaTokenBaru').toggleClass('d-none', !bisaMintaTokenBaru); } function tampilkanDetail(data, state) { @@ -288,24 +295,45 @@ url: `${apiBase}?token=${encodeURIComponent(token)}`, method: 'GET', success: function (res) { - if (res.new_token) { - token = res.new_token; - const url = new URL(window.location.href); - url.searchParams.set('token', token); - window.history.replaceState({}, '', url); - } - tampilkanDetail(res.data, res.state); selesaiLoading(); }, error: function (xhr) { - const pesan = ambilPesanError(xhr); - tampilkanPesan('fa-exclamation-triangle', pesan, 'text-danger'); + const data = ambilDataError(xhr); + tampilkanPesan('fa-exclamation-triangle', data.message || 'Terjadi kesalahan. Silakan coba lagi.', 'text-danger', data.state === 'session_expired'); selesaiLoading(); } }); } + $('#btnMintaTokenBaru').on('click', function () { + const $btn = $(this); + const originalText = $btn.html(); + $btn.prop('disabled', true).text('Memproses...'); + + $.ajax({ + url: `${apiBase}/perbarui-token?token=${encodeURIComponent(token)}`, + method: 'POST', + data: { token }, + success: function (res) { + token = res.new_token; + const url = new URL(window.location.href); + url.searchParams.set('token', token); + window.history.replaceState({}, '', url); + + $('#loadingState').removeClass('d-none'); + $('#mainCard').addClass('d-none'); + muatData(); + }, + error: function (xhr) { + showNotification('error', 'Gagal', ambilPesanError(xhr)); + }, + complete: function () { + $btn.prop('disabled', false).html(originalText); + } + }); + }); + function selesaiLoading() { $('#loadingState').addClass('d-none'); $('#mainCard').removeClass('d-none'); diff --git a/routes/api.php b/routes/api.php index 0e1132b0..4364071b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -116,6 +116,7 @@ Route::prefix('v1')->group(function () { Route::prefix('/verifikasi-absen')->name('verifikasi-absen.')->group(function () { Route::post('/link', [VerifikasiAbsenLinkController::class, 'generate'])->name('link'); Route::get('/{id}', [VerifikasiAbsenController::class, 'show'])->name('show'); + Route::post('/{id}/perbarui-token', [VerifikasiAbsenController::class, 'perbaruiToken'])->name('perbarui-token'); Route::post('/{id}/setujui', [VerifikasiAbsenController::class, 'setujui'])->name('setujui'); Route::post('/{id}/tolak', [VerifikasiAbsenController::class, 'tolak'])->name('tolak'); });