6672f34adc
Mengembalikan fitur verifikasi presensi via WhatsApp ke kondisi semula sebelum perubahan mekanisme regenerasi token sesi. This reverts commit1d58c66a7a. This reverts commitdcf57bcfdc. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
323 lines
11 KiB
PHP
323 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\VerifikasiAbsen;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Verifikator\PengajuanPresensi;
|
|
use App\Models\Verifikator\View\Pegawai;
|
|
use App\Services\DawaiService;
|
|
use App\Services\GoogleDriveService;
|
|
use App\Services\TteService;
|
|
use App\Services\VerifikasiAbsenLinkService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class VerifikasiAbsenController extends Controller
|
|
{
|
|
const BATAS_HARI_VERIFIKASI = 5;
|
|
|
|
private DawaiService $dawaiService;
|
|
|
|
public function __construct(
|
|
protected TteService $tteService,
|
|
protected GoogleDriveService $googleDriveService,
|
|
protected VerifikasiAbsenLinkService $linkService,
|
|
) {
|
|
$this->dawaiService = new DawaiService('http://172.16.40.128/');
|
|
}
|
|
|
|
public function show(Request $request, int $id): JsonResponse
|
|
{
|
|
$pengajuan = PengajuanPresensi::with(['pegawai', 'notes'])->findOrFail($id);
|
|
|
|
$otorisasi = $this->otorisasiAtasan($request, $pengajuan);
|
|
|
|
if (is_array($otorisasi)) {
|
|
return $this->responseTidakBerwenang($otorisasi['reason']);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'state' => $this->resolveState($pengajuan),
|
|
'data' => $this->formatPengajuan($pengajuan),
|
|
]);
|
|
}
|
|
|
|
public function setujui(Request $request, int $id): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'nik' => 'required|string',
|
|
'passphrase' => 'required|string',
|
|
]);
|
|
|
|
$pengajuan = PengajuanPresensi::with(['pegawai', 'notes'])->findOrFail($id);
|
|
|
|
$otorisasi = $this->otorisasiAtasan($request, $pengajuan);
|
|
|
|
if (is_array($otorisasi)) {
|
|
return $this->responseTidakBerwenang($otorisasi['reason']);
|
|
}
|
|
|
|
$nipAtasan = $otorisasi;
|
|
|
|
if ($gagal = $this->cekBisaDiproses($pengajuan)) {
|
|
return $gagal;
|
|
}
|
|
|
|
$dokumenTertandatangani = $this->tteService->signDokumenUser(
|
|
$request->nik,
|
|
$request->passphrase,
|
|
$pengajuan->file,
|
|
$this->linkService->url($pengajuan->id)
|
|
);
|
|
|
|
if ($dokumenTertandatangani === false) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal menandatangani dokumen. Periksa kembali NIK dan Passphrase Anda.',
|
|
], 422);
|
|
}
|
|
|
|
if (! $this->googleDriveService->uploadPdf(
|
|
$dokumenTertandatangani,
|
|
$pengajuan->nip,
|
|
$pengajuan->pegawai?->nama ?? $pengajuan->nip,
|
|
$pengajuan->tanggal_mulai,
|
|
$pengajuan->kategori
|
|
)) {
|
|
Log::error("Gagal mengunggah dokumen tertandatangani ke Google Drive untuk pengajuan #$id");
|
|
}
|
|
|
|
if (! $this->proses($pengajuan, 'Diterima', null, $nipAtasan, (string) $request->input('token'))) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal menyetujui pengajuan absen. Silakan coba lagi.',
|
|
], 500);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Pengajuan absen berhasil disetujui.',
|
|
'state' => $this->resolveState($pengajuan),
|
|
'data' => $this->formatPengajuan($pengajuan),
|
|
]);
|
|
}
|
|
|
|
public function tolak(Request $request, int $id): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'catatan' => 'required|string|max:500',
|
|
]);
|
|
|
|
$pengajuan = PengajuanPresensi::with(['pegawai', 'notes'])->findOrFail($id);
|
|
|
|
$otorisasi = $this->otorisasiAtasan($request, $pengajuan);
|
|
|
|
if (is_array($otorisasi)) {
|
|
return $this->responseTidakBerwenang($otorisasi['reason']);
|
|
}
|
|
|
|
$nipAtasan = $otorisasi;
|
|
|
|
if ($gagal = $this->cekBisaDiproses($pengajuan)) {
|
|
return $gagal;
|
|
}
|
|
|
|
if (! $this->proses($pengajuan, 'Ditolak', $request->catatan, $nipAtasan, (string) $request->input('token'))) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal menolak pengajuan absen. Silakan coba lagi.',
|
|
], 500);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Pengajuan absen berhasil ditolak.',
|
|
'state' => $this->resolveState($pengajuan),
|
|
'data' => $this->formatPengajuan($pengajuan),
|
|
]);
|
|
}
|
|
|
|
private function cekBisaDiproses(PengajuanPresensi $pengajuan): ?JsonResponse
|
|
{
|
|
if ($pengajuan->status !== 'Diajukan') {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Pengajuan ini sudah diproses sebelumnya.',
|
|
'state' => 'already_processed',
|
|
'data' => $this->formatPengajuan($pengajuan),
|
|
], 422);
|
|
}
|
|
|
|
if ($this->isExpired($pengajuan)) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Pengajuan ini sudah kadaluarsa dan tidak dapat diverifikasi lagi.',
|
|
'state' => 'expired',
|
|
'data' => $this->formatPengajuan($pengajuan),
|
|
], 422);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function resolveState(PengajuanPresensi $pengajuan): string
|
|
{
|
|
if ($pengajuan->status !== 'Diajukan') {
|
|
return 'already_processed';
|
|
}
|
|
|
|
if ($this->isExpired($pengajuan)) {
|
|
return 'expired';
|
|
}
|
|
|
|
return 'pending';
|
|
}
|
|
|
|
private function formatPengajuan(PengajuanPresensi $pengajuan): array
|
|
{
|
|
return [
|
|
'id' => $pengajuan->id,
|
|
'nip' => $pengajuan->nip,
|
|
'nama' => $pengajuan->pegawai?->nama,
|
|
'tanggal' => $pengajuan->tanggal_mulai,
|
|
'kategori' => $pengajuan->kategori,
|
|
'keterangan' => $pengajuan->keterangan,
|
|
'file' => $pengajuan->file,
|
|
'status' => $pengajuan->status,
|
|
'alasan' => $pengajuan->notes?->last()?->message,
|
|
'created_at' => $pengajuan->created_at,
|
|
'batas_hari_verifikasi' => self::BATAS_HARI_VERIFIKASI,
|
|
];
|
|
}
|
|
|
|
private function isExpired(PengajuanPresensi $pengajuan): bool
|
|
{
|
|
return $pengajuan->created_at?->copy()->addDays(self::BATAS_HARI_VERIFIKASI)->isPast() ?? false;
|
|
}
|
|
|
|
/**
|
|
* @return string|array{reason: string} Nip atasan kalau berhasil, atau array alasan gagal.
|
|
*/
|
|
private function otorisasiAtasan(Request $request, PengajuanPresensi $pengajuan): string|array
|
|
{
|
|
$token = $request->input('token');
|
|
|
|
$hasil = $this->linkService->verify($pengajuan->id, $token);
|
|
|
|
if (! $hasil['ok']) {
|
|
return ['reason' => $hasil['reason']];
|
|
}
|
|
|
|
if (! $this->dawaiService->isAtasanDari($hasil['nip'], $pengajuan->nip)) {
|
|
return ['reason' => 'invalid'];
|
|
}
|
|
|
|
return $hasil['nip'];
|
|
}
|
|
|
|
private function responseTidakBerwenang(string $reason): JsonResponse
|
|
{
|
|
return match ($reason) {
|
|
'link_expired' => response()->json([
|
|
'success' => false,
|
|
'state' => 'link_expired',
|
|
'message' => 'Tautan verifikasi ini sudah kedaluwarsa.',
|
|
], 403),
|
|
'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.',
|
|
], 403),
|
|
default => response()->json([
|
|
'success' => false,
|
|
'state' => 'unauthorized',
|
|
'message' => 'Tautan ini tidak valid, sudah kedaluwarsa, atau Anda bukan atasan dari pegawai pemohon.',
|
|
], 403),
|
|
};
|
|
}
|
|
|
|
private function proses(PengajuanPresensi $pengajuan, string $status, ?string $catatan, string $nipAtasan, string $token): bool
|
|
{
|
|
$atasan = Pegawai::withoutGlobalScopes()->where('nip', $nipAtasan)->first();
|
|
$verifikatorRole = 'Atasan (via WhatsApp)';
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$pengajuan->update(['status' => $status]);
|
|
|
|
$pengajuan->logs()->create([
|
|
'user_id' => null,
|
|
'method' => 'POST',
|
|
'employee_id' => $nipAtasan,
|
|
'employee_name' => $atasan?->nama ?? $pengajuan->pegawai?->nama,
|
|
'employee_role' => $verifikatorRole,
|
|
'message' => 'Pengajuan Presensi Berhasil '.strtolower($status).' via WhatsApp',
|
|
]);
|
|
|
|
if ($status === 'Ditolak') {
|
|
$pengajuan->notes()->create([
|
|
'type' => 'Catatan',
|
|
'user_id' => null,
|
|
'employee_id' => $nipAtasan,
|
|
'employee_name' => $atasan?->nama ?? $pengajuan->pegawai?->nama,
|
|
'employee_role' => $verifikatorRole,
|
|
'message' => $catatan,
|
|
]);
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
$this->linkService->markUsed($pengajuan->id, $token);
|
|
|
|
$pengajuan->refresh()->load('notes');
|
|
|
|
try {
|
|
$webhookResponse = Http::timeout(5)
|
|
->withHeaders(['X-API-Key' => config('services.verifikator_webhook.key')])
|
|
->post(config('services.verifikator_webhook.url'), [
|
|
'jenis' => 'izin',
|
|
'nip' => $pengajuan->nip,
|
|
'tanggal' => $pengajuan->tanggal_mulai,
|
|
'kategori' => $this->mapKategoriSlug($pengajuan->getRawOriginal('kategori')),
|
|
'status' => $status === 'Diterima' ? 'approved' : 'rejected',
|
|
'alasan' => $catatan ?? '',
|
|
]);
|
|
|
|
if ($webhookResponse->successful()) {
|
|
Log::info("Webhook Verifikator via WhatsApp berhasil: {$pengajuan->nip} ($status)");
|
|
} else {
|
|
Log::warning("Webhook Verifikator via WhatsApp gagal: {$pengajuan->nip} ($status) - HTTP {$webhookResponse->status()}");
|
|
}
|
|
} catch (Throwable $webhookException) {
|
|
Log::error("Webhook Verifikator via WhatsApp error: {$pengajuan->nip} ($status) - ".$webhookException->getMessage());
|
|
}
|
|
|
|
return true;
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
Log::error("Verifikasi Absen via WhatsApp ($status): ".$th->getMessage());
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private function mapKategoriSlug(mixed $kategoriId): ?string
|
|
{
|
|
return match ((int) $kategoriId) {
|
|
2 => 'dinas',
|
|
3 => 'terlambat',
|
|
20 => 'lupa_absen',
|
|
21 => 'cepat_pulang',
|
|
24 => 'lupa_absen_masuk',
|
|
25 => 'lupa_absen_pulang',
|
|
default => null,
|
|
};
|
|
}
|
|
}
|