Approve Array
This commit is contained in:
@@ -14,6 +14,7 @@ use App\Services\VerifikatorWebhookService;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@@ -47,11 +48,13 @@ class VerifikasiAbsenController extends Controller
|
||||
return $this->responseTidakBerwenang($otorisasi['reason']);
|
||||
}
|
||||
|
||||
$grup = $this->grupPengajuan($pengajuan);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'state' => $this->resolveState($pengajuan),
|
||||
'state' => $this->resolveStateGrup($grup),
|
||||
'sisa_detik' => $this->linkService->sisaSesiDetik($pengajuan->id, $request->input('token')),
|
||||
'data' => $this->formatPengajuan($pengajuan),
|
||||
'data' => $this->formatPengajuan($pengajuan, $grup),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -108,8 +111,11 @@ class VerifikasiAbsenController extends Controller
|
||||
|
||||
$nipAtasan = $otorisasi;
|
||||
|
||||
if ($gagal = $this->cekBisaDiproses($pengajuan)) {
|
||||
return $gagal;
|
||||
$grup = $this->grupPengajuan($pengajuan);
|
||||
$menunggu = $this->yangBisaDiproses($grup);
|
||||
|
||||
if ($menunggu->isEmpty()) {
|
||||
return $this->cekBisaDiproses($pengajuan) ?? $this->responseSudahDiproses($pengajuan, $grup);
|
||||
}
|
||||
|
||||
// File lokal dibaca langsung dari disk (bukan lewat URL /storage) supaya tidak bergantung pada symlink & HTTP ke diri sendiri.
|
||||
@@ -145,26 +151,40 @@ class VerifikasiAbsenController extends Controller
|
||||
Log::error("Gagal mengunggah dokumen tertandatangani ke Google Drive untuk pengajuan #$id");
|
||||
}
|
||||
|
||||
if (! $this->proses(
|
||||
$pengajuan,
|
||||
'Diterima',
|
||||
null,
|
||||
$nipAtasan,
|
||||
(string) $request->input('token'),
|
||||
$linkDrive ?: null,
|
||||
fn () => $this->kirimKeDawai($pengajuan, 'Diterima', null, $nipAtasan, $linkDrive ?: null)
|
||||
)) {
|
||||
$gagal = 0;
|
||||
|
||||
foreach ($menunggu as $item) {
|
||||
if (! $this->proses(
|
||||
$item,
|
||||
'Diterima',
|
||||
null,
|
||||
$nipAtasan,
|
||||
(string) $request->input('token'),
|
||||
$linkDrive ?: null,
|
||||
fn () => $this->kirimKeDawai($item, 'Diterima', null, $nipAtasan, $linkDrive ?: null)
|
||||
)) {
|
||||
$gagal++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($gagal > 0) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal menyetujui pengajuan absen. Silakan coba lagi.',
|
||||
'message' => $gagal === $menunggu->count()
|
||||
? 'Gagal menyetujui pengajuan absen. Silakan coba lagi.'
|
||||
: "$gagal dari {$menunggu->count()} pengajuan gagal disetujui. Muat ulang halaman lalu coba lagi.",
|
||||
], 500);
|
||||
}
|
||||
|
||||
$grup = $this->grupPengajuan($pengajuan);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Pengajuan absen berhasil disetujui.',
|
||||
'state' => $this->resolveState($pengajuan),
|
||||
'data' => $this->formatPengajuan($pengajuan),
|
||||
'message' => $menunggu->count() > 1
|
||||
? "{$menunggu->count()} pengajuan absen berhasil disetujui."
|
||||
: 'Pengajuan absen berhasil disetujui.',
|
||||
'state' => $this->resolveStateGrup($grup),
|
||||
'data' => $this->formatPengajuan($pengajuan, $grup),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -188,30 +208,47 @@ class VerifikasiAbsenController extends Controller
|
||||
|
||||
$nipAtasan = $otorisasi;
|
||||
|
||||
if ($gagal = $this->cekBisaDiproses($pengajuan)) {
|
||||
return $gagal;
|
||||
$grup = $this->grupPengajuan($pengajuan);
|
||||
$menunggu = $this->yangBisaDiproses($grup);
|
||||
|
||||
if ($menunggu->isEmpty()) {
|
||||
return $this->cekBisaDiproses($pengajuan) ?? $this->responseSudahDiproses($pengajuan, $grup);
|
||||
}
|
||||
|
||||
if (! $this->proses(
|
||||
$pengajuan,
|
||||
'Ditolak',
|
||||
$request->catatan,
|
||||
$nipAtasan,
|
||||
(string) $request->input('token'),
|
||||
null,
|
||||
fn () => $this->kirimKeDawai($pengajuan, 'Ditolak', $request->catatan, $nipAtasan)
|
||||
)) {
|
||||
$gagal = 0;
|
||||
|
||||
foreach ($menunggu as $item) {
|
||||
if (! $this->proses(
|
||||
$item,
|
||||
'Ditolak',
|
||||
$request->catatan,
|
||||
$nipAtasan,
|
||||
(string) $request->input('token'),
|
||||
null,
|
||||
fn () => $this->kirimKeDawai($item, 'Ditolak', $request->catatan, $nipAtasan)
|
||||
)) {
|
||||
$gagal++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($gagal > 0) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal menolak pengajuan absen. Silakan coba lagi.',
|
||||
'message' => $gagal === $menunggu->count()
|
||||
? 'Gagal menolak pengajuan absen. Silakan coba lagi.'
|
||||
: "$gagal dari {$menunggu->count()} pengajuan gagal ditolak. Muat ulang halaman lalu coba lagi.",
|
||||
], 500);
|
||||
}
|
||||
|
||||
$grup = $this->grupPengajuan($pengajuan);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Pengajuan absen berhasil ditolak.',
|
||||
'state' => $this->resolveState($pengajuan),
|
||||
'data' => $this->formatPengajuan($pengajuan),
|
||||
'message' => $menunggu->count() > 1
|
||||
? "{$menunggu->count()} pengajuan absen berhasil ditolak."
|
||||
: 'Pengajuan absen berhasil ditolak.',
|
||||
'state' => $this->resolveStateGrup($grup),
|
||||
'data' => $this->formatPengajuan($pengajuan, $grup),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -251,13 +288,64 @@ class VerifikasiAbsenController extends Controller
|
||||
return 'pending';
|
||||
}
|
||||
|
||||
private function formatPengajuan(PengajuanPresensi $pengajuan): array
|
||||
/**
|
||||
* Pengajuan batch (banyak tanggal dalam satu request) berbagi nip, kategori, dan file yang sama,
|
||||
* sehingga diverifikasi sekaligus lewat satu tautan. Pengajuan tunggal menghasilkan grup berisi dirinya sendiri.
|
||||
*/
|
||||
private function grupPengajuan(PengajuanPresensi $pengajuan): Collection
|
||||
{
|
||||
return PengajuanPresensi::with(['pegawai', 'notes'])
|
||||
->where('nip', $pengajuan->nip)
|
||||
->where('tipe', $pengajuan->tipe)
|
||||
->where('kategori', $pengajuan->getRawOriginal('kategori'))
|
||||
->where('file', $pengajuan->getRawOriginal('file'))
|
||||
->orderBy('tanggal_mulai')
|
||||
->get();
|
||||
}
|
||||
|
||||
private function yangBisaDiproses(Collection $grup): Collection
|
||||
{
|
||||
return $grup->filter(fn (PengajuanPresensi $item) => $item->status === 'Diajukan' && ! $this->isExpired($item))->values();
|
||||
}
|
||||
|
||||
private function resolveStateGrup(Collection $grup): string
|
||||
{
|
||||
if ($this->yangBisaDiproses($grup)->isNotEmpty()) {
|
||||
return 'pending';
|
||||
}
|
||||
|
||||
if ($grup->contains(fn (PengajuanPresensi $item) => $item->status === 'Diajukan')) {
|
||||
return 'expired';
|
||||
}
|
||||
|
||||
return 'already_processed';
|
||||
}
|
||||
|
||||
private function responseSudahDiproses(PengajuanPresensi $pengajuan, Collection $grup): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Pengajuan ini sudah diproses sebelumnya.',
|
||||
'state' => 'already_processed',
|
||||
'data' => $this->formatPengajuan($pengajuan, $grup),
|
||||
], 422);
|
||||
}
|
||||
|
||||
private function formatPengajuan(PengajuanPresensi $pengajuan, ?Collection $grup = null): array
|
||||
{
|
||||
$grup ??= collect([$pengajuan]);
|
||||
$items = $grup->map(fn (PengajuanPresensi $item) => [
|
||||
'id' => $item->id,
|
||||
'tanggal' => $item->tanggal_mulai,
|
||||
'status' => $item->status,
|
||||
])->values();
|
||||
|
||||
return [
|
||||
'id' => $pengajuan->id,
|
||||
'nip' => $pengajuan->nip,
|
||||
'nama' => $pengajuan->pegawai?->nama,
|
||||
'tanggal' => $pengajuan->tanggal_mulai,
|
||||
'items' => $items,
|
||||
'kategori' => $pengajuan->kategori,
|
||||
'keterangan' => $pengajuan->keterangan,
|
||||
'file' => $pengajuan->file,
|
||||
|
||||
Reference in New Issue
Block a user