Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eaeb276774 | |||
| 77ae40b53f | |||
| 79a005d05a | |||
| ad6246d951 | |||
| c314baf68b | |||
| dbcea98ab0 |
@@ -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([
|
||||
@@ -188,6 +221,7 @@ class VerifikasiAbsenController extends Controller
|
||||
'kategori' => $pengajuan->kategori,
|
||||
'keterangan' => $pengajuan->keterangan,
|
||||
'file' => $pengajuan->file,
|
||||
'file_nama' => $pengajuan->bdr_id === null ? $pengajuan->getRawOriginal('file') : null,
|
||||
'status' => $pengajuan->status,
|
||||
'alasan' => $pengajuan->notes?->last()?->message,
|
||||
'created_at' => $pengajuan->created_at,
|
||||
@@ -231,7 +265,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,
|
||||
|
||||
@@ -2,10 +2,52 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Verifikator\PengajuanPresensi;
|
||||
use App\Services\VerifikasiAbsenLinkService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class VerifikasiAbsenHalamanController extends Controller
|
||||
{
|
||||
public function show(int $id)
|
||||
public function __construct(
|
||||
protected VerifikasiAbsenLinkService $linkService,
|
||||
) {}
|
||||
|
||||
public function show(Request $request, int $id)
|
||||
{
|
||||
if ($request->filled('img')) {
|
||||
return $this->tampilkanFile($request, $id, (string) $request->query('img'));
|
||||
}
|
||||
|
||||
return view('verifikasi-absen.halaman', ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan file bukti pengajuan lewat ?token=...&img=<nama file>.
|
||||
* Hanya file milik pengajuan ini yang bisa dibuka, dan token harus masih dalam sesi yang valid.
|
||||
*/
|
||||
private function tampilkanFile(Request $request, int $id, string $img)
|
||||
{
|
||||
$hasil = $this->linkService->verify($id, $request->query('token'));
|
||||
|
||||
abort_unless($hasil['ok'], 403, 'Tautan tidak valid atau sesi sudah berakhir.');
|
||||
|
||||
$pengajuan = PengajuanPresensi::findOrFail($id);
|
||||
|
||||
$namaFile = basename($img);
|
||||
$fileSah = array_filter([
|
||||
$pengajuan->getRawOriginal('file'),
|
||||
$pengajuan->getRawOriginal('file_lampiran'),
|
||||
]);
|
||||
|
||||
abort_unless($pengajuan->bdr_id === null && in_array($namaFile, $fileSah, true), 404);
|
||||
|
||||
$path = "{$pengajuan->tipe}/presensi/{$pengajuan->periode}/{$namaFile}";
|
||||
|
||||
abort_unless(Storage::disk('public')->exists($path), 404, 'File tidak ditemukan.');
|
||||
|
||||
return Storage::disk('public')->response($path, $namaFile, [
|
||||
'Content-Disposition' => 'inline; filename="'.$namaFile.'"',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,27 +3,24 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Verifikator\VerifikasiAbsenToken;
|
||||
use Illuminate\Contracts\Encryption\DecryptException;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
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 +73,73 @@ 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;
|
||||
}
|
||||
|
||||
// Token pengganti yang sesinya masih jalan dipakai ulang, jangan buat baru tiap tombol WA diklik.
|
||||
// Token di DB hanya berupa hash, jadi plaintext pengganti disimpan (terenkripsi) di cache selama sesinya.
|
||||
$kunciCache = "verifikasi-absen:pengganti:{$row->id}";
|
||||
|
||||
if ($tersimpan = Cache::get($kunciCache)) {
|
||||
try {
|
||||
return Crypt::decryptString($tersimpan);
|
||||
} catch (DecryptException) {
|
||||
Cache::forget($kunciCache);
|
||||
}
|
||||
}
|
||||
|
||||
$tokenBaru = $this->buatToken($pengajuanId, $row->nip, $row->expires_at);
|
||||
|
||||
Cache::put($kunciCache, Crypt::encryptString($tokenBaru), now()->addMinutes(self::SESI_MENIT));
|
||||
|
||||
return $tokenBaru;
|
||||
}
|
||||
|
||||
public function markUsed(int $pengajuanId, string $token): void
|
||||
{
|
||||
VerifikasiAbsenToken::where('pengajuan_id', $pengajuanId)
|
||||
@@ -84,6 +148,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');
|
||||
|
||||
@@ -260,7 +264,11 @@
|
||||
$('#dataTanggal').text(data.tanggal || '-');
|
||||
$('#dataKategori').text(data.kategori || '-');
|
||||
$('#dataKeterangan').text(data.keterangan || '-');
|
||||
$('#dataFile').attr('href', data.file || '#');
|
||||
// File bukti dibuka lewat halaman ini sendiri (?token=...&img=nama-file) supaya tidak bergantung pada /storage.
|
||||
const linkFile = data.file_nama
|
||||
? `${window.location.pathname}?token=${encodeURIComponent(token)}&img=${encodeURIComponent(data.file_nama)}`
|
||||
: (data.file || '#');
|
||||
$('#dataFile').attr('href', linkFile);
|
||||
|
||||
const badge = statusBadgeMap[data.status] || ['bg-secondary', data.status];
|
||||
$('#statusBadge').attr('class', `badge status-badge ${badge[0]}`).text(badge[1]);
|
||||
@@ -275,6 +283,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 +366,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