refactor patient

This commit is contained in:
mahdahar 2025-10-16 13:22:28 +07:00
parent 33e7c84fc4
commit 60df79ed02
2 changed files with 41 additions and 62 deletions

View File

@ -15,7 +15,7 @@ class Patient extends Controller {
public function __construct() { public function __construct() {
$this->db = \Config\Database::connect(); $this->db = \Config\Database::connect();
$this->model = new PatientModel(); $this->model = new PatientModel();
$this->rules = [ $this->rules = [
'PatientID' => 'required|max_length[50]', 'PatientID' => 'required|max_length[50]',
'AlternatePID' => 'permit_empty|max_length[50]', 'AlternatePID' => 'permit_empty|max_length[50]',
@ -55,7 +55,7 @@ class Patient extends Controller {
public function create() { public function create() {
$input = $this->request->getJSON(true); $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 { try {
$InternalPID = $this->model->createPatient($input); $InternalPID = $this->model->createPatient($input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $InternalPID created successfully" ]); return $this->respondCreated([ 'status' => 'success', 'message' => "data $InternalPID created successfully" ]);
@ -66,7 +66,7 @@ class Patient extends Controller {
public function update() { public function update() {
$input = $this->request->getJSON(true); $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 { try {
$InternalPID = $this->model->updatePatient($input); $InternalPID = $this->model->updatePatient($input);
return $this->respondCreated([ 'status' => 'success', 'message' => "data $InternalPID update successfully" ]); 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()); 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);
}
} }

View File

@ -2,7 +2,6 @@
namespace App\Models\Patient; namespace App\Models\Patient;
use App\Models\BaseModel; use App\Models\BaseModel;
use App\Models\BaseUtcModel;
use App\Models\Patient\PatAttModel; use App\Models\Patient\PatAttModel;
use App\Models\Patient\PatComModel; use App\Models\Patient\PatComModel;
@ -23,42 +22,33 @@ class PatientModel extends BaseModel {
protected $useSoftDeletes = true; protected $useSoftDeletes = true;
protected $deletedField = 'DelDate'; protected $deletedField = 'DelDate';
protected $beforeInsert = ['normalizeDatesToUTC'];
protected $beforeUpdate = ['normalizeDatesToUTC'];
protected $afterFind = ['convertDatesToUTCISO'];
protected $afterInsert = ['convertDatesToUTCISO'];
protected $afterUpdate = ['convertDatesToUTCISO'];
public function getPatients($filters = []) { public function getPatients($filters = []) {
$qname = "LOWER(CONCAT_WS(' ', IFNULL(Prefix,''), IFNULL(NameFirst,''), IFNULL(NameMiddle,''), IFNULL(NameLast,''), IFNULL(NameMaiden,''), IFNULL(Suffix,'')))"; $qname = "LOWER(CONCAT_WS(' ', IFNULL(Prefix,''), IFNULL(NameFirst,''), IFNULL(NameMiddle,''), IFNULL(NameLast,''), IFNULL(NameMaiden,''), IFNULL(Suffix,'')))";
$builder = $this->db->table($this->table); $this->select("InternalPID, PatientID, $qname as FullName, Gender, Birthdate, EmailAddress1 as Email, MobilePhone");
$builder->select("InternalPID, PatientID, $qname as FullName, Gender, Birthdate, EmailAddress1 as Email, MobilePhone");
if (!empty($filters['Name'])) { if (!empty($filters['Name'])) {
$rawSql = new RawSql($qname); $this->like($qname, $filters['Name'], 'both');
$builder->like($rawSql, $filters['Name'], 'both');
} }
if (!empty($filters['InternalPID'])) { if (!empty($filters['InternalPID'])) {
$builder->where('InternalPID', $filters['InternalPID']); $this->where('InternalPID', $filters['InternalPID']);
} }
if (!empty($filters['PatientID'])) { if (!empty($filters['PatientID'])) {
$builder->like('PatientID', $filters['PatientID'], 'both'); $this->like('PatientID', $filters['PatientID'], 'both');
} }
if (!empty($filters['Birthdate'])) { 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) { public function getPatient($InternalPID) {
$rows = $this->db->table('patient p') $rows = $this->select("
->select(" patient.*,
p.*,
country.VDesc as Country, country.VDesc as Country,
country.VID as CountryVID, country.VID as CountryVID,
race.VDesc as Race, race.VDesc as Race,
@ -78,19 +68,18 @@ class PatientModel extends BaseModel {
patidt.Identifier, patidt.Identifier,
patatt.Address patatt.Address
") ")
->join('valueset country', 'country.VID = p.Country', 'left') ->join('valueset country', 'country.VID = patient.Country', 'left')
->join('valueset race', 'race.VID = p.Race', 'left') ->join('valueset race', 'race.VID = patient.Race', 'left')
->join('valueset religion', 'religion.VID = p.Religion', 'left') ->join('valueset religion', 'religion.VID = patient.Religion', 'left')
->join('valueset ethnic', 'ethnic.VID = p.Ethnic', 'left') ->join('valueset ethnic', 'ethnic.VID = patient.Ethnic', 'left')
->join('valueset gender', 'gender.VID = p.Gender', 'left') ->join('valueset gender', 'gender.VID = patient.Gender', 'left')
->join('valueset deathindicator', 'deathindicator.VID = p.DeathIndicator', 'left') ->join('valueset deathindicator', 'deathindicator.VID = patient.DeathIndicator', 'left')
->join('valueset maritalstatus', 'maritalstatus.VID = p.MaritalStatus', 'left') ->join('valueset maritalstatus', 'maritalstatus.VID = patient.MaritalStatus', 'left')
->join('patcom', 'patcom.InternalPID = p.InternalPID', 'left') ->join('patcom', 'patcom.InternalPID = patient.InternalPID', 'left')
->join('patidt', 'patidt.InternalPID = p.InternalPID', 'left') ->join('patidt', 'patidt.InternalPID = patient.InternalPID', 'left')
->join('patatt', 'patatt.InternalPID = p.InternalPID and patatt.DelDate is null', 'left') ->join('patatt', 'patatt.InternalPID = patient.InternalPID and patatt.DelDate is null', 'left')
->where('p.InternalPID', (int) $InternalPID) ->where('patient.InternalPID', (int) $InternalPID)
->get() ->findAll();
->getResultArray();
if (empty($rows)) { return null; } if (empty($rows)) { return null; }
@ -127,10 +116,11 @@ class PatientModel extends BaseModel {
public function createPatient($input) { public function createPatient($input) {
$db = \Config\Database::connect(); $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['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo'];
$input['Birthdate'] = $this->isValidDateTime($input['Birthdate']); $input['Birthdate'] = $this->isValidDateTime($input['Birthdate']);
$input['DeathDateTime'] = $this->isValidDateTime($input['DeathDateTime']); $input['DeathDateTime'] = $this->isValidDateTime($input['DeathDateTime']);
@ -146,19 +136,19 @@ class PatientModel extends BaseModel {
// Insert Data ke Tabel PatIdt // Insert Data ke Tabel PatIdt
if (!empty($input['PatIdt'])) { if (!empty($input['PatIdt'])) {
$this->modelPatIdt->createPatIdt($input['PatIdt'], $newInternalPID); $modelPatIdt->createPatIdt($input['PatIdt'], $newInternalPID);
$this->checkDbError($db, 'Insert PatIdt'); $this->checkDbError($db, 'Insert PatIdt');
} }
// Insert Data ke Tabel PatCom // Insert Data ke Tabel PatCom
if (!empty($input['PatCom'])) { if (!empty($input['PatCom'])) {
$this->modelPatCom->createPatCom($input['PatCom'], $newInternalPID); $modelPatCom->createPatCom($input['PatCom'], $newInternalPID);
$this->checkDbError($db, 'Insert PatCom'); $this->checkDbError($db, 'Insert PatCom');
} }
// Insert Data ke Tabel PatAtt // Insert Data ke Tabel PatAtt
if (!empty($input['PatAtt'])) { if (!empty($input['PatAtt'])) {
$this->modelPatAtt->createPatAtt($input['PatAtt'], $newInternalPID); $modelPatAtt->createPatAtt($input['PatAtt'], $newInternalPID);
$this->checkDbError($db, 'Insert PatAtt'); $this->checkDbError($db, 'Insert PatAtt');
} }
@ -174,9 +164,9 @@ class PatientModel extends BaseModel {
public function updatePatient($input) { public function updatePatient($input) {
$db = \Config\Database::connect(); $db = \Config\Database::connect();
$this->modelPatIdt = new PatIdtModel(); $modelPatIdt = new PatIdtModel();
$this->modelPatCom = new PatComModel(); $modelPatCom = new PatComModel();
$this->modelPatAtt = new PatAttModel(); $modelPatAtt = new PatAttModel();
$input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo']; $input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo'];
$input['Birthdate'] = $this->isValidDateTime($input['Birthdate']); $input['Birthdate'] = $this->isValidDateTime($input['Birthdate']);
@ -193,28 +183,28 @@ class PatientModel extends BaseModel {
// Update Patidt // Update Patidt
if (!empty($input['PatIdt'])) { if (!empty($input['PatIdt'])) {
$this->modelPatIdt->updatePatIdt($input['PatIdt'], $InternalPID); $modelPatIdt->updatePatIdt($input['PatIdt'], $InternalPID);
$this->checkDbError($db, 'Update patIdt'); $this->checkDbError($db, 'Update patIdt');
} else { } else {
$this->modelPatIdt->deletePatIdt($InternalPID); $modelPatIdt->deletePatIdt($InternalPID);
$this->checkDbError($db, 'Update patidt'); $this->checkDbError($db, 'Update patidt');
} }
// Update Patcom // Update Patcom
if (!empty($input['PatCom'])) { if (!empty($input['PatCom'])) {
$this->modelPatCom->updatePatCom($input['PatCom'], $InternalPID); $modelPatCom->updatePatCom($input['PatCom'], $InternalPID);
$this->checkDbError($db, 'Update PatCom'); $this->checkDbError($db, 'Update PatCom');
} else { } else {
$this->modelPatCom->deletePatCom($InternalPID); $modelPatCom->deletePatCom($InternalPID);
$this->checkDbError($db, 'Update patcom'); $this->checkDbError($db, 'Update patcom');
} }
// Update Patatt // Update Patatt
if (!empty($input['PatAtt'])) { if (!empty($input['PatAtt'])) {
$this->modelPatAtt->updatePatAtt($input['PatAtt'], $InternalPID); $modelPatAtt->updatePatAtt($input['PatAtt'], $InternalPID);
$this->checkDbError($db, 'Update PatAtt'); $this->checkDbError($db, 'Update PatAtt');
} else { } else {
$this->modelPatAtt->deletePatAtt($InternalPID); $modelPatAtt->deletePatAtt($InternalPID);
$this->checkDbError($db, 'Update/Delete patatt'); $this->checkDbError($db, 'Update/Delete patatt');
} }
@ -257,11 +247,9 @@ class PatientModel extends BaseModel {
return null; return null;
} }
return $this->db->table('patient') return $this->select('InternalPID, PatientID')
->select('InternalPID, PatientID')
->where('InternalPID', (int) $custodianId) ->where('InternalPID', (int) $custodianId)
->get() ->findAll() ?: null;
->getRowArray() ?: null;
} }
// Conversion to (Years Months Days) - For Age // Conversion to (Years Months Days) - For Age