Penambahan Generate Token WA Presensi
This commit is contained in:
@@ -105,14 +105,14 @@ class PengajuanAbsenController extends Controller
|
||||
|
||||
public function store(AbsenRequest $request)
|
||||
{
|
||||
if (! in_array($request->nip, ['19980513202401011', '19970812202101011'])) {
|
||||
return response()->json([
|
||||
'code' => 400,
|
||||
'status' => 'failed',
|
||||
'message' => 'Sedang proses Remunerasi, Tidak dapat mengajukan absen untuk saat ini',
|
||||
// 'message' => 'Sedang proses Maintenance, Tidak dapat mengajukan absen untuk saat ini',
|
||||
], 400);
|
||||
}
|
||||
// if (! in_array($request->nip, ['19980513202401011', '19970812202101011'])) {
|
||||
// return response()->json([
|
||||
// 'code' => 400,
|
||||
// 'status' => 'failed',
|
||||
// 'message' => 'Sedang proses Remunerasi, Tidak dapat mengajukan absen untuk saat ini',
|
||||
// // 'message' => 'Sedang proses Maintenance, Tidak dapat mengajukan absen untuk saat ini',
|
||||
// ], 400);
|
||||
// }
|
||||
|
||||
$batasBulanPengajuan = (int) date('m');
|
||||
if (date('j') < 2) {
|
||||
|
||||
@@ -43,10 +43,43 @@ class VerifikasiAbsenController extends Controller
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'state' => $this->resolveState($pengajuan),
|
||||
'sisa_detik' => $this->linkService->sisaSesiDetik($pengajuan->id, $request->input('token')),
|
||||
'data' => $this->formatPengajuan($pengajuan),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dipanggil saat tombol "Tinjau" di WhatsApp diklik lagi dengan token yang sesinya sudah habis.
|
||||
* Pengajuan yang sama tetap dipakai, hanya token sesinya diganti.
|
||||
*/
|
||||
public function perbaruiSesi(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$pengajuan = PengajuanPresensi::findOrFail($id);
|
||||
|
||||
if ($this->isExpired($pengajuan)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'state' => 'expired',
|
||||
'message' => 'Pengajuan ini sudah kadaluarsa dan tidak dapat diverifikasi lagi.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$tokenBaru = $this->linkService->perbaruiSesiJikaHabis($pengajuan->id, $request->input('token'));
|
||||
|
||||
if (! $tokenBaru) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'state' => 'unauthorized',
|
||||
'message' => 'Sesi tidak dapat diperbarui. Tautan tidak valid atau sudah kedaluwarsa.',
|
||||
], 403);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'url' => $this->linkService->url($pengajuan->id, $tokenBaru),
|
||||
]);
|
||||
}
|
||||
|
||||
public function setujui(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
@@ -231,7 +264,7 @@ class VerifikasiAbsenController extends Controller
|
||||
'session_expired' => response()->json([
|
||||
'success' => false,
|
||||
'state' => 'session_expired',
|
||||
'message' => 'Sesi verifikasi ini sudah lewat 5 menit sejak tautan dibuka. Silakan minta tautan verifikasi baru.',
|
||||
'message' => 'Sesi verifikasi ini sudah berakhir. Silakan klik kembali tombol "Tinjau Pengajuan" di WhatsApp untuk membuka sesi baru.',
|
||||
], 403),
|
||||
default => response()->json([
|
||||
'success' => false,
|
||||
|
||||
@@ -3,27 +3,21 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Verifikator\VerifikasiAbsenToken;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class VerifikasiAbsenLinkService
|
||||
{
|
||||
const TTL_MENIT_DEFAULT = 7200;
|
||||
|
||||
const SESI_MENIT = 5;
|
||||
// TODO: sementara 1 menit untuk testing, kembalikan ke 1440 (1 hari).
|
||||
const SESI_MENIT = 1;
|
||||
|
||||
const PURPOSE = 'verifikasi-presensi';
|
||||
|
||||
public function generate(int $pengajuanId, string $nip, int $ttlMenit = self::TTL_MENIT_DEFAULT): array
|
||||
{
|
||||
$tokenPlaintext = Str::random(40);
|
||||
|
||||
VerifikasiAbsenToken::create([
|
||||
'pengajuan_id' => $pengajuanId,
|
||||
'nip' => $nip,
|
||||
'token' => $this->hash($tokenPlaintext),
|
||||
'purpose' => self::PURPOSE,
|
||||
'expires_at' => now()->addMinutes($ttlMenit),
|
||||
]);
|
||||
$tokenPlaintext = $this->buatToken($pengajuanId, $nip, now()->addMinutes($ttlMenit));
|
||||
|
||||
return [
|
||||
'nip' => $nip,
|
||||
@@ -76,6 +70,57 @@ class VerifikasiAbsenLinkService
|
||||
return ['ok' => true, 'nip' => $row->nip, 'reason' => null];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sisa detik sesi untuk token ini (0 kalau sudah habis), null kalau token tidak dikenal atau belum pernah dibuka.
|
||||
*/
|
||||
public function sisaSesiDetik(int $pengajuanId, ?string $token): ?int
|
||||
{
|
||||
if (! $token) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = VerifikasiAbsenToken::where('pengajuan_id', $pengajuanId)
|
||||
->where('token', $this->hash($token))
|
||||
->where('purpose', self::PURPOSE)
|
||||
->first();
|
||||
|
||||
if (! $row || $row->first_accessed_at === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$berakhir = $row->first_accessed_at->copy()->addMinutes(self::SESI_MENIT);
|
||||
|
||||
return max(0, (int) now()->diffInSeconds($berakhir, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Kalau token lama sudah lewat sesi (tapi tautannya belum kedaluwarsa), buat token baru
|
||||
* untuk atasan & pengajuan yang sama. Masa berlaku tautan tidak diperpanjang.
|
||||
*
|
||||
* @return ?string Token plaintext baru, atau null kalau tidak perlu/tidak bisa diperbarui.
|
||||
*/
|
||||
public function perbaruiSesiJikaHabis(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) || $row->first_accessed_at === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! now()->greaterThan($row->first_accessed_at->copy()->addMinutes(self::SESI_MENIT))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->buatToken($pengajuanId, $row->nip, $row->expires_at);
|
||||
}
|
||||
|
||||
public function markUsed(int $pengajuanId, string $token): void
|
||||
{
|
||||
VerifikasiAbsenToken::where('pengajuan_id', $pengajuanId)
|
||||
@@ -84,6 +129,21 @@ class VerifikasiAbsenLinkService
|
||||
->update(['used_at' => now()]);
|
||||
}
|
||||
|
||||
private function buatToken(int $pengajuanId, string $nip, Carbon $expiresAt): string
|
||||
{
|
||||
$tokenPlaintext = Str::random(40);
|
||||
|
||||
VerifikasiAbsenToken::create([
|
||||
'pengajuan_id' => $pengajuanId,
|
||||
'nip' => $nip,
|
||||
'token' => $this->hash($tokenPlaintext),
|
||||
'purpose' => self::PURPOSE,
|
||||
'expires_at' => $expiresAt,
|
||||
]);
|
||||
|
||||
return $tokenPlaintext;
|
||||
}
|
||||
|
||||
private function hash(string $token): string
|
||||
{
|
||||
return hash('sha256', $token);
|
||||
|
||||
@@ -133,6 +133,10 @@
|
||||
<span id="statusBadge" class="badge status-badge"></span>
|
||||
</div>
|
||||
|
||||
<div id="timerSesi" class="alert alert-info py-2 small d-none">
|
||||
<i class="fas fa-clock"></i> Sesi berakhir dalam <strong id="timerSisa">--:--</strong>
|
||||
</div>
|
||||
|
||||
<dl class="row mb-0">
|
||||
<div class="col-6">
|
||||
<dt>Nama Pemohon</dt>
|
||||
@@ -251,7 +255,7 @@
|
||||
$('#pesanText').text(text);
|
||||
}
|
||||
|
||||
function tampilkanDetail(data, state) {
|
||||
function tampilkanDetail(data, state, sisaDetik) {
|
||||
$('#pesanState').addClass('d-none');
|
||||
$('#detailState').removeClass('d-none');
|
||||
|
||||
@@ -275,6 +279,76 @@
|
||||
} else {
|
||||
$('#aksiButtons').addClass('d-none');
|
||||
}
|
||||
|
||||
if (state === 'pending' && typeof sisaDetik === 'number') {
|
||||
mulaiTimer(sisaDetik);
|
||||
} else {
|
||||
hentikanTimer();
|
||||
}
|
||||
}
|
||||
|
||||
let timerInterval = null;
|
||||
|
||||
function formatWaktu(detik) {
|
||||
const m = String(Math.floor(detik / 60)).padStart(2, '0');
|
||||
const s = String(detik % 60).padStart(2, '0');
|
||||
return `${m}:${s}`;
|
||||
}
|
||||
|
||||
function hentikanTimer() {
|
||||
clearInterval(timerInterval);
|
||||
timerInterval = null;
|
||||
$('#timerSesi').addClass('d-none');
|
||||
}
|
||||
|
||||
function mulaiTimer(sisaDetik) {
|
||||
clearInterval(timerInterval);
|
||||
const berakhirPada = Date.now() + sisaDetik * 1000;
|
||||
|
||||
$('#timerSesi').removeClass('d-none alert-danger').addClass('alert-info');
|
||||
|
||||
const tick = function () {
|
||||
const sisa = Math.max(0, Math.ceil((berakhirPada - Date.now()) / 1000));
|
||||
$('#timerSisa').text(formatWaktu(sisa));
|
||||
$('#timerSesi').toggleClass('alert-danger', sisa <= 30).toggleClass('alert-info', sisa > 30);
|
||||
|
||||
if (sisa <= 0) {
|
||||
clearInterval(timerInterval);
|
||||
timerInterval = null;
|
||||
sesiHabis();
|
||||
}
|
||||
};
|
||||
|
||||
tick();
|
||||
timerInterval = setInterval(tick, 1000);
|
||||
}
|
||||
|
||||
function sesiHabis() {
|
||||
tampilkanPesan(
|
||||
'fa-clock',
|
||||
'Sesi verifikasi telah berakhir. Tutup halaman ini lalu klik kembali tombol "Tinjau Pengajuan" di WhatsApp untuk membuka sesi baru.',
|
||||
'text-warning'
|
||||
);
|
||||
}
|
||||
|
||||
const kunciDibuka = `verifikasi-absen-dibuka-${pengajuanId}-${token}`;
|
||||
|
||||
function sudahDibukaDiTabIni() {
|
||||
try { return sessionStorage.getItem(kunciDibuka) === '1'; } catch (e) { return true; }
|
||||
}
|
||||
|
||||
function perbaruiSesi() {
|
||||
$.ajax({
|
||||
url: `${apiBase}/perbarui-sesi?token=${encodeURIComponent(token)}`,
|
||||
method: 'POST',
|
||||
success: function (res) {
|
||||
window.location.replace(res.url);
|
||||
},
|
||||
error: function (xhr) {
|
||||
tampilkanPesan('fa-exclamation-triangle', ambilPesanError(xhr), 'text-danger');
|
||||
selesaiLoading();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function muatData() {
|
||||
@@ -288,10 +362,18 @@
|
||||
url: `${apiBase}?token=${encodeURIComponent(token)}`,
|
||||
method: 'GET',
|
||||
success: function (res) {
|
||||
tampilkanDetail(res.data, res.state);
|
||||
try { sessionStorage.setItem(kunciDibuka, '1'); } catch (e) {}
|
||||
tampilkanDetail(res.data, res.state, res.sisa_detik);
|
||||
selesaiLoading();
|
||||
},
|
||||
error: function (xhr) {
|
||||
// Tab baru (klik tombol WA) dengan sesi habis: minta token baru.
|
||||
// Reload di tab yang sama (sudah pernah dibuka) tetap menampilkan expired.
|
||||
if (xhr.responseJSON?.state === 'session_expired' && !sudahDibukaDiTabIni()) {
|
||||
perbaruiSesi();
|
||||
return;
|
||||
}
|
||||
|
||||
const pesan = ambilPesanError(xhr);
|
||||
tampilkanPesan('fa-exclamation-triangle', pesan, 'text-danger');
|
||||
selesaiLoading();
|
||||
|
||||
+2
-1
@@ -116,7 +116,8 @@ 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}/setujui', [VerifikasiAbsenController::class, 'setujui'])->name('setujui');
|
||||
Route::post('/{id}/perbarui-sesi', [VerifikasiAbsenController::class, 'perbaruiSesi'])->name('perbarui-sesi');
|
||||
Route::post('/{id}/setujui',[VerifikasiAbsenController::class, 'setujui'])->name('setujui');
|
||||
Route::post('/{id}/tolak', [VerifikasiAbsenController::class, 'tolak'])->name('tolak');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user