Update Pengajuan Presensi WhatsApp
This commit is contained in:
@@ -312,6 +312,25 @@ class PengajuanAbsenController extends Controller
|
||||
], 500);
|
||||
}
|
||||
|
||||
if ($request->hasFile('file_lampiran')) {
|
||||
$namaFileLampiran = uniqid().'-'.$pegawai->nip.'-lampiran.pdf';
|
||||
$filePathLampiran = $request->file('file_lampiran')->storeAs("absen/presensi/$periode", $namaFileLampiran, 'public');
|
||||
|
||||
if (! $filePathLampiran) {
|
||||
Log::error("Gagal menyimpan file lampiran: $namaFileLampiran");
|
||||
|
||||
return response()->json([
|
||||
'code' => 500,
|
||||
'status' => 'failed',
|
||||
'message' => 'Gagal menyimpan file lampiran',
|
||||
], 500);
|
||||
}
|
||||
|
||||
$absen['file_lampiran'] = $namaFileLampiran;
|
||||
} else {
|
||||
unset($absen['file_lampiran']);
|
||||
}
|
||||
|
||||
$pengajuan = PengajuanPresensi::create($absen);
|
||||
|
||||
$this->notifikasiAtasanWhatsApp($pengajuan, $pegawai, $dawaiService);
|
||||
|
||||
@@ -0,0 +1,302 @@
|
||||
<?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);
|
||||
|
||||
$nipAtasan = $this->otorisasiAtasan($request, $pengajuan);
|
||||
|
||||
if ($nipAtasan === false) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'state' => 'unauthorized',
|
||||
'message' => 'Tautan ini tidak valid, sudah kedaluwarsa, atau Anda bukan atasan dari pegawai pemohon.',
|
||||
], 403);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
$nipAtasan = $this->otorisasiAtasan($request, $pengajuan);
|
||||
|
||||
if ($nipAtasan === false) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Anda tidak berwenang memverifikasi pengajuan ini.',
|
||||
], 403);
|
||||
}
|
||||
|
||||
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)) {
|
||||
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);
|
||||
|
||||
$nipAtasan = $this->otorisasiAtasan($request, $pengajuan);
|
||||
|
||||
if ($nipAtasan === false) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Anda tidak berwenang memverifikasi pengajuan ini.',
|
||||
], 403);
|
||||
}
|
||||
|
||||
if ($gagal = $this->cekBisaDiproses($pengajuan)) {
|
||||
return $gagal;
|
||||
}
|
||||
|
||||
if (! $this->proses($pengajuan, 'Ditolak', $request->catatan, $nipAtasan)) {
|
||||
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;
|
||||
}
|
||||
|
||||
private function otorisasiAtasan(Request $request, PengajuanPresensi $pengajuan): string|false
|
||||
{
|
||||
$nip = $request->input('nip');
|
||||
$expires = $request->input('expires');
|
||||
$signature = $request->input('signature');
|
||||
|
||||
if (! $this->linkService->verify($pengajuan->id, $nip, $expires, $signature)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->dawaiService->isAtasanDari($nip, $pengajuan->nip)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $nip;
|
||||
}
|
||||
|
||||
private function proses(PengajuanPresensi $pengajuan, string $status, ?string $catatan, string $nipAtasan): 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();
|
||||
|
||||
$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,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user