Files
verifikator/app/Http/Controllers/Api/Pengajuan/TriDharma/MengujiController.php
T
2026-08-07 10:47:09 +07:00

103 lines
3.7 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Pengajuan\TriDharma;
use App\Http\Controllers\Controller;
use App\Models\DokSiremun\Menguji;
use App\Models\Verifikator\View\Pegawai;
use Carbon\Carbon;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;
class MengujiController extends Controller
{
function store(Request $request)
{
$start = Carbon::parse('2024-05-20 00:00:00');
$end = Carbon::parse('2024-06-15 23:59:59');
if (!now()->isBetween($start, $end)) {
return response()->json([
'code' => 400,
'status' => 'failed',
'message' => 'Pengajuan Sudah Ditutup',
], 400);
}
try {
$user = Pegawai::select('id', 'nip', 'nama', 'jenis_pegawai')->where('nip', $request->nip)->orWhere('nip_siakad', $request->nip)->firstOrFail();
$countUpdated = 0;
if ($user->jenis_pegawai === 'dosen') {
$token = $request->bearerToken();
$data = Crypt::decrypt($token);
if ($data['nip'] !== $request->nip) {
throw new AuthorizationException();
}
}
DB::beginTransaction();
foreach ($request->id as $id) {
$menguji = Menguji::where('id', $id)
->where('id_pelaksana', $user->nip)
->firstOrFail();
if ($menguji->status_verifikasi_id === 1 && $menguji->status_syarat === 'COMPLETE') {
$countUpdated++;
$menguji->update([
'status_verifikasi_id' => 3,
'tanggal_pengajuan' => Carbon::today(),
]);
$menguji->logs()->create([
'user_id' => $user->id,
'method' => $request->method(),
'employee_id' => $user->nip,
'employee_name' => $user->name,
'employee_role' => ucwords($user->jenis_pegawai),
'message' => 'Pengajuan baru berhasil ditambahkan',
]);
}
}
if ($countUpdated === 0) {
$message = 'Tidak ada pengajuan baru';
} else {
$message = "Berhasil melakukan $countUpdated pengajuan";
}
DB::commit();
return response()->json([
'code' => 201,
'status' => 'success',
'message' => $message,
], 201);
} catch (Throwable $th) {
DB::rollBack();
Log::error("Menguji: " . $th->getMessage());
Log::error("Menguji: " . $request->nip);
if ($th instanceof ModelNotFoundException) {
return response()->json([
'code' => 404,
'status' => 'failed',
'message' => 'Kegiatan tidak ditemukan',
], 404);
}
if ($th instanceof AuthorizationException) {
throw new AuthorizationException();
}
if ($th instanceof DecryptException) {
throw new AuthorizationException();
}
return response()->json([
'code' => 500,
'status' => 'failed',
'message' => 'Internal Server Error',
], 500);
}
}
}