From d801964f59325db9f90afe435e609158a9443f8a Mon Sep 17 00:00:00 2001 From: pcalengratha Date: Thu, 23 Apr 2026 10:43:13 +0700 Subject: [PATCH] api service to retreive data from nphl --- .../app/Services/DataRetrievalService.php | 78 ++++++++++++++++--- dashboard/config/services.php | 6 ++ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/dashboard/app/Services/DataRetrievalService.php b/dashboard/app/Services/DataRetrievalService.php index b696d37..733c82f 100644 --- a/dashboard/app/Services/DataRetrievalService.php +++ b/dashboard/app/Services/DataRetrievalService.php @@ -2,27 +2,80 @@ namespace App\Services; -use Carbon\Carbon; -use App\Models\Surveillance; -use App\Models\SurveillanceCase; -use App\Models\CaseLabResult; +use Illuminate\Http\Client\RequestException; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class DataRetrievalService { + protected $apiBaseUrl; + protected $apiUsername; + protected $apiPassword; + + public function __construct() + { + $this->apiBaseUrl = config('services.nphl_api.url'); + $this->apiUsername = config('services.nphl_api.username'); + $this->apiPassword = config('services.nphl_api.password'); + } + + public function get($endpoint) + { + try { + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl . $endpoint); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_ENCODING, ''); + curl_setopt($ch, CURLOPT_MAXREDIRS, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 0); + curl_setopt($ch, CURL_HTTP_VERSION_1_1, 0); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + curl_setopt($ch, CURLOPT_USERPWD, $this->apiUsername . ":" . $this->apiPassword); + $resp = curl_exec($ch); + $b=""; + if($e = curl_error($ch)){ + $b= $e; + }else{ + $b= $resp; + } + curl_close($ch); + return $b; + + } catch (RequestException $e) { + return [ + 'error' => true, + 'message' => $e->getMessage(), + 'status' => optional($e->response)->status() + ]; + } + } + + public function getSurveillanceData() { try{ - $toDate = now(); - $this->getSARICases(now()->subDays(config('app.lookback_days.SARI'))->toDateString(), $toDate); // done - $this->getILICases(now()->subDays(config('app.lookback_days.ILI'))->toDateString(), $toDate); // done - $this->getLBMCases(now()->subDays(config('app.lookback_days.LBM'))->toDateString(), $toDate); // done - $this->getAFICases(now()->subDays(config('app.lookback_days.AFI'))->toDateString(), $toDate); // done - $this->getNSDCases(now()->subDays(config('app.lookback_days.NDS'))->toDateString(), $toDate); - $this->getSEQCases(now()->subDays(config('app.lookback_days.SEQ'))->toDateString(), $toDate); // done - Log::channel('jobs')->info($toDate->toDateString(). ' Service Reload Data Successfully Ran'); + $lookbackDays = config('app.lookback_days'); + $surveillances = DB::connection('mysql')->select("select * from surveillances"); + foreach ($surveillances as $surveillance){ + $surveillance_data = $this->get('api/labsurveil.php?surveillance_id='.$surveillance->id.'&start_date='.now()->subDays($lookbackDays[$surveillance->code])->toDateString()); + $data = json_decode(preg_replace('/^\xEF\xBB\xBF/', '', $surveillance_data)); + @$this->insert_surveillance_cases((array)$data->laboratory_cases); + @$this->insert_surveillance_case_lab_results((array)$data->laboratory_results); + + } + + //$this->getSARICases(now()->subDays(config('app.lookback_days.SARI'))->toDateString(), $toDate); // done +// $this->getILICases(now()->subDays(config('app.lookback_days.ILI'))->toDateString(), $toDate); // done +// $this->getLBMCases(now()->subDays(config('app.lookback_days.LBM'))->toDateString(), $toDate); // done +// $this->getAFICases(now()->subDays(config('app.lookback_days.AFI'))->toDateString(), $toDate); // done +// $this->getNSDCases(now()->subDays(config('app.lookback_days.NDS'))->toDateString(), $toDate); +// $this->getSEQCases(now()->subDays(config('app.lookback_days.SEQ'))->toDateString(), $toDate); // done + Log::channel('jobs')->info(now()->toDateString(). ' Service Reload Data Successfully Ran'); return true; } catch (\Exception $e){ @@ -35,6 +88,7 @@ class DataRetrievalService public function getSARICases($dateFrom, $dateTo=NULL) { + $cond = ""; $cond .= " and patient.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' "; $cond .= (!empty($dateTo)) ? " and patient.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":""; diff --git a/dashboard/config/services.php b/dashboard/config/services.php index 6a90eb8..c093a4f 100644 --- a/dashboard/config/services.php +++ b/dashboard/config/services.php @@ -35,4 +35,10 @@ return [ ], ], + 'nphl_api' => [ + 'url' => env('NPHL_API_URL'), + 'username' => env('NPHL_API_USERNAME'), + 'password' => env('NPHL_API_PASSWORD'), + ], + ];