Files
verifikator/app/Services/InsentifService.php
T
2026-08-07 10:47:09 +07:00

130 lines
4.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\Verifikator\Periode;
use Error;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
use Throwable;
class InsentifService
{
private $client;
protected $urlDevelopment = 'http://172.16.40.47:9000/';
protected $urlProduction = 'https://insentif.jurnal.untan.ac.id/';
public function __construct()
{
if (env('APP_ENV') === 'DEVELOPMENT') {
$default = $this->urlDevelopment;
} else {
$default = $this->urlProduction;
}
$this->client = new Client([
'base_uri' => $default,
'connect_timeout' => 5,
'timeout' => 15,
]);
}
public function bukuAjar(string $nip, int $periode, ?int $limit = 1, ?string $status = 'Diterima')
{
try {
$url = "api/v1/buku/pengajuan/$nip?kategori=Buku Ajar&periode=$periode";
if ($status !== null) {
$url .= "&status=$status";
}
$response = $this->client->get($url);
$data = json_decode($response->getBody()->getContents(), true);
$data = collect($data['data'])->sortByDesc('poin');
if ($limit) {
$data = $data->take($limit);
}
return $data;
} catch (Throwable $th) {
Log::info("Insentif ($nip): ".$th->getMessage());
throw new Error('Server Insentif Jurnal Sedang Bermasalah', 500);
}
}
public function modulAjar(string $nip, int $periode, ?int $limit = 2, ?string $status = 'Diterima')
{
// API Insentif modul ajar sudah tidak terpakai lagi, return empty collection (poin 0)
return collect([]);
}
public function publikasi(string $type, string $nip, int $periode = 386)
{
try {
$response = $this->client->get("api/$type/$nip/$periode");
$data = json_decode($response->getBody()->getContents(), true);
return collect($data['data']);
} catch (Throwable $th) {
Log::info("Insentif ($nip): ".$th->getMessage());
throw new Error('Server Insentif Jurnal Sedang Bermasalah', 500);
}
}
public function cekData(string $nip)
{
try {
$response = $this->client->get("api/v1/perhitungan/$nip");
$data = json_decode($response->getBody()->getContents(), true);
return collect($data);
} catch (Throwable $th) {
Log::info("Insentif ($nip): ".$th->getMessage());
throw new Error('Server Insentif Jurnal Sedang Bermasalah', 500);
}
}
public function verifikasiBukuAjar(string $unique, string $action, string $nip, ?string $note = null): array
{
try {
$url = "api/v1/buku/verifikasi/$unique/$action";
$body = [
'kategori' => 'buku-ajar',
'nip' => $nip,
'apps' => 'Verifikator',
];
if ($note !== null) {
$body['note'] = $note;
}
$response = $this->client->put($url, [
'json' => $body,
'http_errors' => false,
]);
return [
'_http_status' => $response->getStatusCode(),
'data' => json_decode($response->getBody()->getContents(), true) ?? [],
];
} catch (Throwable $th) {
Log::info("Insentif verifikasi buku ajar ($unique/$action): ".$th->getMessage());
throw new Error('Server Insentif Jurnal Sedang Bermasalah', 500);
}
}
public function jurnal(string $nip, ?int $periode = null)
{
try {
if ($periode === null) {
$periode = Periode::where('status', true)->first()->id_periode;
}
$response = $this->client->get("api/v1/jurnal/pengajuan/$nip?periode=$periode");
$data = json_decode($response->getBody()->getContents(), true);
return collect($data['data']);
} catch (Throwable $th) {
Log::info("Insentif ($nip): ".$th->getMessage());
throw new Error('Server Insentif Jurnal Sedang Bermasalah', 500);
}
}
}