From e15a2d98db251e6fca055f5b6c71ce96799ee1c8 Mon Sep 17 00:00:00 2001
From: mahdahar <89adham@gmail.com>
Date: Fri, 26 Sep 2025 15:03:11 +0700
Subject: [PATCH] fix contact creation wo detail
---
app/Controllers/Contact.php | 11 ++++-----
app/Controllers/PatVisit.php | 16 ++++++-------
.../Migrations/2025-09-12-011643_Contact.php | 4 ++--
app/Models/PatVisitModel.php | 24 +++++++------------
phpunit.xml.dist | 4 ++--
tests/feature/Patients/PatientCreateTest.php | 6 ++---
6 files changed, 26 insertions(+), 39 deletions(-)
diff --git a/app/Controllers/Contact.php b/app/Controllers/Contact.php
index 6a23034..df028f6 100644
--- a/app/Controllers/Contact.php
+++ b/app/Controllers/Contact.php
@@ -94,7 +94,6 @@ class Contact extends Controller {
$db->transStart();
try {
-
if (!empty($input['ContactID'])) {
$ContactID = $input['ContactID'];
if (!$contactModel->update($ContactID, $input)) { throw new \RuntimeException('Failed to update contact'); }
@@ -103,12 +102,12 @@ class Contact extends Controller {
if (!$ContactID) { throw new \RuntimeException('Failed to insert contact'); }
}
-
- $result = $detailModel->syncDetails($ContactID, $input['Details']);
- if ($result['status'] !== 'success') {
- throw new \RuntimeException('Failed to sync details: ' . $result['message']);
+ if(isset($input['Details'])) {
+ $result = $detailModel->syncDetails($ContactID, $input['Details']);
+ if ($result['status'] !== 'success') {
+ throw new \RuntimeException('Failed to sync details: ' . $result['message']);
+ }
}
-
$db->transComplete();
diff --git a/app/Controllers/PatVisit.php b/app/Controllers/PatVisit.php
index fc43d8c..6779bb8 100644
--- a/app/Controllers/PatVisit.php
+++ b/app/Controllers/PatVisit.php
@@ -21,8 +21,8 @@ class PatVisit extends Controller {
public function show($PVID = null) {
try {
- $row = new PatVisitModel()->show($PVID);
-
+ $model = new PatVisitModel();
+ $row = $model->show($PVID);
return $this->respond([
'status' => 'success',
'message'=> "data found",
@@ -35,11 +35,8 @@ class PatVisit extends Controller {
public function showByPatient($InternalPID = null) {
try {
- $row = $this->db->table('patvisit pv')
- ->join('patdiag pd', 'pd.InternalPVID=pv.InternalPVID', 'left')
- ->join('patvisitadt pva', 'pd.InternalPVID=pva.InternalPVID', 'left')
- ->get()->getResultArray();
-
+ $model = new PatVisitModel();
+ $row = $model->showByPatient($InternalPID);
return $this->respond([
'status' => 'success',
'message'=> "data found",
@@ -51,8 +48,9 @@ class PatVisit extends Controller {
}
public function update() {
+ $input = $this->request->getJSON(true);
try {
- $input = $this->request->getJSON(true);
+
if (!$input) { return $this->respond(['status' => 'error', 'message' => 'Invalid JSON input'], 400); }
if (!$input["InternalPVID"] || !is_numeric($input["InternalPVID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
@@ -94,7 +92,7 @@ class PatVisit extends Controller {
if($input['PVID'] =='' || !isset($input['PVID'])) {
$model = new CounterModel();
- $input['PVID'] = $this->visnum_prefix .$model->use(2);
+ $input['PVID'] = $this->visnum_prefix .$model->use(2);
//$input['PVID'] = $this->preparePVID();
}
diff --git a/app/Database/Migrations/2025-09-12-011643_Contact.php b/app/Database/Migrations/2025-09-12-011643_Contact.php
index 503cc69..6ec57cf 100644
--- a/app/Database/Migrations/2025-09-12-011643_Contact.php
+++ b/app/Database/Migrations/2025-09-12-011643_Contact.php
@@ -21,9 +21,9 @@ class CreateContactTable extends Migration {
'MobilePhone2' => [ 'type' => 'VARCHAR', 'constraint' => 50, 'null' => true ],
'Specialty' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],
'SubSpecialty' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],
+ 'CreateDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP',
'EndDate' => [ 'type' => 'DATETIME', 'null' => true ],
]);
- $this->forge->addField('CreateDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP');
$this->forge->addKey('ContactID', true);
$this->forge->createTable('contact');
@@ -38,7 +38,7 @@ class CreateContactTable extends Migration {
'JobTitle' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],
'Department' => [ 'type' => 'VARCHAR', 'constraint' => 100, 'null' => true ],
'ContactStartDate' => [ 'type' => 'DATETIME', 'null' => true ],
- 'ContactEndDate' => [ 'type' => 'DATETIME', 'null' => true ],
+ 'ContactEndDate' => [ 'type' => 'DATETIME ', 'null' => true ],
]);
$this->forge->addKey('ContactDetID', true);
$this->forge->addUniqueKey(['SiteID','ContactID']);
diff --git a/app/Models/PatVisitModel.php b/app/Models/PatVisitModel.php
index e1caf3d..6a17263 100644
--- a/app/Models/PatVisitModel.php
+++ b/app/Models/PatVisitModel.php
@@ -15,25 +15,17 @@ class PatVisitModel extends Model {
protected $deletedField = 'EndDate';
public function show($PVID) {
- $rows = $this->join('patdiag pd', 'pd.InternalPVID=pv.InternalPVID', 'left')
- ->join('patvisitadt pva', 'pd.InternalPVID=pva.InternalPVID', 'left')
- ->where('PVID',$PVID)->get()->getResultArray();
+ $rows = $this->join('patdiag pd', 'pd.InternalPVID=patvisit.InternalPVID', 'left')
+ ->join('patvisitadt pva', 'pva.InternalPVID=patvisit.InternalPVID', 'left')
+ ->where('patvisit.PVID',$PVID)->get()->getResultArray();
return $rows;
}
- public function use($CounterID) {
- $row = $this->where('CounterID',$CounterID)->get()->getResultArray();
- $cValue = $row[0]['CounterValue'];
- $cStart = $row[0]['CounterStart'];
- $cEnd = $row[0]['CounterEnd'];
- $cReset = $row[0]['CounterReset'];
- $cPad = strlen((string)$cEnd);
- // next value > end, back to start
- if($cValue > $cEnd) { $cValue = $cStart; }
- $cnum = str_pad($cValue, $cPad, "0", STR_PAD_LEFT);
- $cValue_next = $cValue+1;
- $this->set('CounterValue', $cValue_next)->where('CounterID',$CounterID)->update();
- return $cnum;
+ public function showByPatient($InternalPID) {
+ $rows = $this->join('patdiag pd', 'pd.InternalPVID=patvisit.InternalPVID', 'left')
+ ->join('patvisitadt pva', 'pd.InternalPVID=pva.InternalPVID', 'left')
+ ->where('patvisit.InternalPID',$InternalPID)->get()->getResultArray();
+ return $rows;
}
}
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index d7a6003..f7d5608 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -56,9 +56,9 @@
-
+
-
+
diff --git a/tests/feature/Patients/PatientCreateTest.php b/tests/feature/Patients/PatientCreateTest.php
index 637dced..cc415bf 100644
--- a/tests/feature/Patients/PatientCreateTest.php
+++ b/tests/feature/Patients/PatientCreateTest.php
@@ -10,8 +10,7 @@ class PatientCreateTest extends CIUnitTestCase
use FeatureTestTrait;
protected $endpoint = 'api/patient';
- public function testCreatePatientValidationFail()
- {
+ public function testCreatePatientValidationFail() {
// error 400 yg diharapkan
$payload = ['Name' => 'Ngawur'];
$result = $this->withBodyFormat('json')
@@ -45,8 +44,7 @@ class PatientCreateTest extends CIUnitTestCase
}
// Wajib Diganti ya payloadnya kalau mau dijalankan
- public function testCreatePatientSuccess()
- {
+ public function testCreatePatientSuccess() {
$payload = [
"PatientID"=> "SMAJ6", //Wajib Ganti
"AlternatePID"=> "P0234",