Files
aso/app/Console/Commands/UpdateNoSjp.php
2025-09-22 09:19:52 +07:00

62 lines
2.2 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\LivechatUpdateLog;
use App\Models\OLDLMS\Livechat;
use Carbon\Carbon;
use Illuminate\Console\Command;
use GuzzleHttp\Client;
class UpdateNoSJP extends Command {
protected $signature = 'update:no-sjp';
protected $description = 'Cek data Livechat yang sNoSpj kosong lalu generate SJP lewat API';
public function handle() {
$this->info('Checking Data ...');
$client = new Client([ 'base_uri' => 'https://api.linksehat.dev', 'headers' => ['Content-Type' => 'application/json'], 'timeout' => 10, ]);
$liveChats = Livechat::query()
->join('tx_livechat_summary', 'tx_livechat_summary.nIDLiveChat', '=', 'tx_livechat.nID')
->whereNull('tx_livechat.sNoSpj')
->whereDate('tx_livechat.dCreateOn', Carbon::today())
->select('tx_livechat.nID', 'tx_livechat_summary.nID as summary_id')
->get();
foreach ($liveChats as $row) {
try {
$prescriptionData = [
'nIDLivechats' => [$row->nID]
];
$response = $client->post('/api/rujukan-dan-sjp', [
'json' => $prescriptionData,
]);
$body = json_decode($response->getBody()->getContents(), true);
LivechatUpdateLog::create([
'nIDLivechat' => $row->nID,
'nIDSummary' => $row->summary_id,
'status' => 'success',
'response' => json_encode($body)
]);
$this->info("Nomor SJP berhasil dibuat untuk Livechat {$row->nID}");
} catch (\Exception $e) {
LivechatUpdateLog::create([
'nIDLivechat' => $row->nID,
'nIDSummary' => $row->summary_id,
'status' => 'fail',
'response' => $e->getMessage()
]);
$this->error("Gagal generate Nomor SJP Livechat {$row->nID}: " . $e->getMessage());
}
}
$this->info('Proses selesai.');
return Command::SUCCESS;
}
}