109 lines
3.9 KiB
PHP
109 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Pengajuan\TriDharma;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\DokSiremun\Mengajar;
|
|
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 MengajarController extends Controller
|
|
{
|
|
function store(Request $request)
|
|
{
|
|
$exceptions = [196312201988101001, 196106051986022001];
|
|
$start = Carbon::parse('2024-05-20 00:00:00');
|
|
$end = Carbon::parse('2024-06-15 23:59:59');
|
|
$user = Pegawai::select('id', 'nip', 'nama', 'jenis_pegawai')->where('nip', $request->nip)->orWhere('nip_siakad', $request->nip)->first();
|
|
|
|
if (!in_array($user?->nip, $exceptions)) {
|
|
if (!now()->isBetween($start, $end)) {
|
|
return response()->json([
|
|
'code' => 400,
|
|
'status' => 'failed',
|
|
'message' => 'Pengajuan Sudah Ditutup',
|
|
], 400);
|
|
}
|
|
}
|
|
abort_if(!$user, 404, 'Pegawai tidak ditemukan');
|
|
try {
|
|
if ($user->jenis_pegawai === 'dosen') {
|
|
$token = $request->bearerToken();
|
|
$data = Crypt::decrypt($token);
|
|
if ($data['nip'] !== $request->nip) {
|
|
throw new AuthorizationException();
|
|
}
|
|
}
|
|
$countUpdated = 0;
|
|
|
|
DB::beginTransaction();
|
|
foreach ($request->id as $id) {
|
|
$mengajar = Mengajar::where('id', $id)
|
|
->where('id_pelaksana', $user->nip)
|
|
->firstOrFail();
|
|
|
|
if ($mengajar->dpna && $mengajar->rps && $mengajar->absen > 0 && $mengajar->status_verifikasi_id === 1) {
|
|
$countUpdated++;
|
|
$mengajar->logs()->create([
|
|
'user_id' => $user->id,
|
|
'method' => $request->method(),
|
|
'employee_id' => $user->nip,
|
|
'employee_name' => $user->name,
|
|
'employee_role' => 'Dosen',
|
|
'message' => 'Pengajuan baru berhasil ditambahkan',
|
|
]);
|
|
|
|
$mengajar->update([
|
|
'status_verifikasi_id' => 3,
|
|
'tanggal_pengajuan' => Carbon::today(),
|
|
]);
|
|
}
|
|
}
|
|
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("Mengajar: " . $th->getMessage());
|
|
Log::error("Mengajar: " . $request->nip);
|
|
if ($th instanceof ModelNotFoundException) {
|
|
return response()->json([
|
|
'code' => 400,
|
|
'status' => 'failed',
|
|
'message' => 'Kegiatan tidak ditemukan',
|
|
], 400);
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|