From 60df79ed024ed7f91bd5146793156c56c4581e99 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Thu, 16 Oct 2025 13:22:28 +0700 Subject: [PATCH] refactor patient --- app/Controllers/Patient/Patient.php | 15 +---- app/Models/Patient/PatientModel.php | 88 +++++++++++++---------------- 2 files changed, 41 insertions(+), 62 deletions(-) diff --git a/app/Controllers/Patient/Patient.php b/app/Controllers/Patient/Patient.php index 7218b77..c0ec498 100644 --- a/app/Controllers/Patient/Patient.php +++ b/app/Controllers/Patient/Patient.php @@ -15,7 +15,7 @@ class Patient extends Controller { public function __construct() { $this->db = \Config\Database::connect(); - $this->model = new PatientModel(); + $this->model = new PatientModel(); $this->rules = [ 'PatientID' => 'required|max_length[50]', 'AlternatePID' => 'permit_empty|max_length[50]', @@ -55,7 +55,7 @@ class Patient extends Controller { public function create() { $input = $this->request->getJSON(true); - if (!$this->validateData($input, $this->rules)) { return $this->validationError('patient', $this->validator->getErrors()); } + if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); } try { $InternalPID = $this->model->createPatient($input); return $this->respondCreated([ 'status' => 'success', 'message' => "data $InternalPID created successfully" ]); @@ -66,7 +66,7 @@ class Patient extends Controller { public function update() { $input = $this->request->getJSON(true); - if (!$this->validateData($input, $this->rules)) { return $this->validationError('patient', $this->validator->getErrors()); } + if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); } try { $InternalPID = $this->model->updatePatient($input); return $this->respondCreated([ 'status' => 'success', 'message' => "data $InternalPID update successfully" ]); @@ -146,13 +146,4 @@ class Patient extends Controller { return $this->failServerError('Something went wrong.'.$e->getMessage()); } } - - private function validationError(string $context, array $errors) { - return $this->respond([ - 'status' => 'error', - 'message' => "Validation failed ({$context})", - 'errors' => $errors - ], 400); - } - } \ No newline at end of file diff --git a/app/Models/Patient/PatientModel.php b/app/Models/Patient/PatientModel.php index 57bd4f8..44a2ebc 100644 --- a/app/Models/Patient/PatientModel.php +++ b/app/Models/Patient/PatientModel.php @@ -2,7 +2,6 @@ namespace App\Models\Patient; use App\Models\BaseModel; -use App\Models\BaseUtcModel; use App\Models\Patient\PatAttModel; use App\Models\Patient\PatComModel; @@ -23,42 +22,33 @@ class PatientModel extends BaseModel { protected $useSoftDeletes = true; protected $deletedField = 'DelDate'; - protected $beforeInsert = ['normalizeDatesToUTC']; - protected $beforeUpdate = ['normalizeDatesToUTC']; - protected $afterFind = ['convertDatesToUTCISO']; - protected $afterInsert = ['convertDatesToUTCISO']; - protected $afterUpdate = ['convertDatesToUTCISO']; - public function getPatients($filters = []) { $qname = "LOWER(CONCAT_WS(' ', IFNULL(Prefix,''), IFNULL(NameFirst,''), IFNULL(NameMiddle,''), IFNULL(NameLast,''), IFNULL(NameMaiden,''), IFNULL(Suffix,'')))"; - $builder = $this->db->table($this->table); - $builder->select("InternalPID, PatientID, $qname as FullName, Gender, Birthdate, EmailAddress1 as Email, MobilePhone"); + $this->select("InternalPID, PatientID, $qname as FullName, Gender, Birthdate, EmailAddress1 as Email, MobilePhone"); if (!empty($filters['Name'])) { - $rawSql = new RawSql($qname); - $builder->like($rawSql, $filters['Name'], 'both'); + $this->like($qname, $filters['Name'], 'both'); } if (!empty($filters['InternalPID'])) { - $builder->where('InternalPID', $filters['InternalPID']); + $this->where('InternalPID', $filters['InternalPID']); } if (!empty($filters['PatientID'])) { - $builder->like('PatientID', $filters['PatientID'], 'both'); + $this->like('PatientID', $filters['PatientID'], 'both'); } if (!empty($filters['Birthdate'])) { - $builder->where('Birthdate', $filters['Birthdate']); + $this->where('Birthdate', $filters['Birthdate']); } - return $builder->get()->getResultArray(); + return $this->findAll(); } public function getPatient($InternalPID) { - $rows = $this->db->table('patient p') - ->select(" - p.*, + $rows = $this->select(" + patient.*, country.VDesc as Country, country.VID as CountryVID, race.VDesc as Race, @@ -78,19 +68,18 @@ class PatientModel extends BaseModel { patidt.Identifier, patatt.Address ") - ->join('valueset country', 'country.VID = p.Country', 'left') - ->join('valueset race', 'race.VID = p.Race', 'left') - ->join('valueset religion', 'religion.VID = p.Religion', 'left') - ->join('valueset ethnic', 'ethnic.VID = p.Ethnic', 'left') - ->join('valueset gender', 'gender.VID = p.Gender', 'left') - ->join('valueset deathindicator', 'deathindicator.VID = p.DeathIndicator', 'left') - ->join('valueset maritalstatus', 'maritalstatus.VID = p.MaritalStatus', 'left') - ->join('patcom', 'patcom.InternalPID = p.InternalPID', 'left') - ->join('patidt', 'patidt.InternalPID = p.InternalPID', 'left') - ->join('patatt', 'patatt.InternalPID = p.InternalPID and patatt.DelDate is null', 'left') - ->where('p.InternalPID', (int) $InternalPID) - ->get() - ->getResultArray(); + ->join('valueset country', 'country.VID = patient.Country', 'left') + ->join('valueset race', 'race.VID = patient.Race', 'left') + ->join('valueset religion', 'religion.VID = patient.Religion', 'left') + ->join('valueset ethnic', 'ethnic.VID = patient.Ethnic', 'left') + ->join('valueset gender', 'gender.VID = patient.Gender', 'left') + ->join('valueset deathindicator', 'deathindicator.VID = patient.DeathIndicator', 'left') + ->join('valueset maritalstatus', 'maritalstatus.VID = patient.MaritalStatus', 'left') + ->join('patcom', 'patcom.InternalPID = patient.InternalPID', 'left') + ->join('patidt', 'patidt.InternalPID = patient.InternalPID', 'left') + ->join('patatt', 'patatt.InternalPID = patient.InternalPID and patatt.DelDate is null', 'left') + ->where('patient.InternalPID', (int) $InternalPID) + ->findAll(); if (empty($rows)) { return null; } @@ -127,10 +116,11 @@ class PatientModel extends BaseModel { public function createPatient($input) { $db = \Config\Database::connect(); - $this->modelPatAtt = new PatAttModel(); - $this->modelPatCom = new PatComModel(); - $this->modelPatIdt = new PatIdtModel(); + $modelPatAtt = new PatAttModel(); + $modelPatCom = new PatComModel(); + $modelPatIdt = new PatIdtModel(); + $input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo']; $input['Birthdate'] = $this->isValidDateTime($input['Birthdate']); $input['DeathDateTime'] = $this->isValidDateTime($input['DeathDateTime']); @@ -146,19 +136,19 @@ class PatientModel extends BaseModel { // Insert Data ke Tabel PatIdt if (!empty($input['PatIdt'])) { - $this->modelPatIdt->createPatIdt($input['PatIdt'], $newInternalPID); + $modelPatIdt->createPatIdt($input['PatIdt'], $newInternalPID); $this->checkDbError($db, 'Insert PatIdt'); } // Insert Data ke Tabel PatCom if (!empty($input['PatCom'])) { - $this->modelPatCom->createPatCom($input['PatCom'], $newInternalPID); + $modelPatCom->createPatCom($input['PatCom'], $newInternalPID); $this->checkDbError($db, 'Insert PatCom'); } // Insert Data ke Tabel PatAtt if (!empty($input['PatAtt'])) { - $this->modelPatAtt->createPatAtt($input['PatAtt'], $newInternalPID); + $modelPatAtt->createPatAtt($input['PatAtt'], $newInternalPID); $this->checkDbError($db, 'Insert PatAtt'); } @@ -174,9 +164,9 @@ class PatientModel extends BaseModel { public function updatePatient($input) { $db = \Config\Database::connect(); - $this->modelPatIdt = new PatIdtModel(); - $this->modelPatCom = new PatComModel(); - $this->modelPatAtt = new PatAttModel(); + $modelPatIdt = new PatIdtModel(); + $modelPatCom = new PatComModel(); + $modelPatAtt = new PatAttModel(); $input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo']; $input['Birthdate'] = $this->isValidDateTime($input['Birthdate']); @@ -193,28 +183,28 @@ class PatientModel extends BaseModel { // Update Patidt if (!empty($input['PatIdt'])) { - $this->modelPatIdt->updatePatIdt($input['PatIdt'], $InternalPID); + $modelPatIdt->updatePatIdt($input['PatIdt'], $InternalPID); $this->checkDbError($db, 'Update patIdt'); } else { - $this->modelPatIdt->deletePatIdt($InternalPID); + $modelPatIdt->deletePatIdt($InternalPID); $this->checkDbError($db, 'Update patidt'); } // Update Patcom if (!empty($input['PatCom'])) { - $this->modelPatCom->updatePatCom($input['PatCom'], $InternalPID); + $modelPatCom->updatePatCom($input['PatCom'], $InternalPID); $this->checkDbError($db, 'Update PatCom'); } else { - $this->modelPatCom->deletePatCom($InternalPID); + $modelPatCom->deletePatCom($InternalPID); $this->checkDbError($db, 'Update patcom'); } // Update Patatt if (!empty($input['PatAtt'])) { - $this->modelPatAtt->updatePatAtt($input['PatAtt'], $InternalPID); + $modelPatAtt->updatePatAtt($input['PatAtt'], $InternalPID); $this->checkDbError($db, 'Update PatAtt'); } else { - $this->modelPatAtt->deletePatAtt($InternalPID); + $modelPatAtt->deletePatAtt($InternalPID); $this->checkDbError($db, 'Update/Delete patatt'); } @@ -257,11 +247,9 @@ class PatientModel extends BaseModel { return null; } - return $this->db->table('patient') - ->select('InternalPID, PatientID') + return $this->select('InternalPID, PatientID') ->where('InternalPID', (int) $custodianId) - ->get() - ->getRowArray() ?: null; + ->findAll() ?: null; } // Conversion to (Years Months Days) - For Age