From 67b1ffefada1fef07a3bd04def0ea85350477acf Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Wed, 1 Oct 2025 15:36:55 +0700 Subject: [PATCH] patient done, pending patatt --- app/Controllers/Patient.php | 166 ++---------------------------------- app/Models/PatientModel.php | 94 +++++++++++++++----- 2 files changed, 83 insertions(+), 177 deletions(-) diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php index ea404fc..cbcbcb3 100644 --- a/app/Controllers/Patient.php +++ b/app/Controllers/Patient.php @@ -17,13 +17,12 @@ class Patient extends Controller { $this->db = \Config\Database::connect(); $this->modelPatient = new PatientModel(); $this->rulesPatient = [ - 'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]', + 'PatientID' => 'required|max_length[50]', 'AlternatePID' => 'permit_empty|max_length[50]', 'NameFirst' => 'required|min_length[1]|max_length[255]', - 'EmailAddress1' => 'required|is_unique[patient.EmailAddress1]', + 'EmailAddress1' => 'required', 'Gender' => 'required' ]; - $this->rulesPatIdt = ['Identifier' => 'required|is_unique[patidt.Identifier]']; } public function index() { @@ -55,90 +54,14 @@ class Patient extends Controller { public function create() { $input = $this->request->getJSON(true); if (!$this->validateData($input, $this->rulesPatient)) { return $this->validationError('patient', $this->validator->getErrors()); } - if (!$this->validateData($input, $this->rulesPatIdt)) { return $this->validationError('patidt', $this->validator->getErrors()); } try { - $newInternalPID = $this->modelPatient->createPatient($input); - return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $newInternalPID]); + $InternalPID = $this->modelPatient->createPatient($input); + return $this->respondCreated([ 'status' => 'success', 'message' => "data $InternalPID created successfully" ]); } catch (\Exception $e) { return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } - // Setting up data - private function preparePatientData(array $input, string $mode = 'create'): array { - $LinkTo = null; - if (!empty($input['LinkTo'])) { - $ids = array_column($input['LinkTo'], 'InternalPID'); - $LinkTo = implode(',', $ids); - } - - $data = [ - "PatientID" => $input['PatientID'] ?? null, - "AlternatePID" => $input['AlternatePID'] ?? null, - "Prefix" => $input['Prefix'] ?? null, - "NameFirst" => $input['NameFirst'] ?? null, - "NameMiddle" => $input['NameMiddle'] ?? null, - "NameMaiden" => $input['NameMaiden'] ?? null, - "NameLast" => $input['NameLast'] ?? null, - "Suffix" => $input['Suffix'] ?? null, - "NameAlias" => $input['NameAlias'] ?? null, - "Gender" => !empty($input['Gender']) ? (int)$input['Gender'] : null, - "PlaceOfBirth" => $input['PlaceOfBirth'] ?? null, - "Birthdate" => $input['Birthdate'] ?? null, - "Street_1" => $input['Street_1'] ?? null, - "Street_2" => $input['Street_2'] ?? null, - "Street_3" => $input['Street_3'] ?? null, - "City" => $input['City'] ?? null, - "Province" => $input['Province'] ?? null, - "ZIP" => $input['ZIP'] ?? null, - "EmailAddress1" => $input['EmailAddress1'] ?? null, - "EmailAddress2" => $input['EmailAddress2'] ?? null, - "Phone" => $input['Phone'] ?? null, - "MobilePhone" => $input['MobilePhone'] ?? null, - "RaceID" => isset($input['RaceID']) ? (int)$input['RaceID'] : null, - "IntCountryID" => isset($input['IntCountryID']) ? (int)$input['IntCountryID'] : null, - "MaritalStatus" => $input['MaritalStatus'] ?? null, - "ReligionID" => isset($input['ReligionID']) ? (int)$input['ReligionID'] : null, - "EthnicID" => isset($input['EthnicID']) ? (int)$input['EthnicID'] : null, - "Citizenship" => $input['Citizenship'] ?? null, - "DeathIndicator" => isset($input['DeathIndicator']) ? (int)$input['DeathIndicator'] : null, - "DeathDateTime" => $input['DeathDateTime'] ?? null, - "Custodian" => isset($input['Custodian']) ? (int)$input['Custodian'] : null, - "DelDate" => null, - "LinkTo" => $LinkTo - ]; - - if(!empty($input['InternalPID'])) { $data["InternalPID"] = $input["InternalPID"]; } - - return $data; - } - private function preparePatidtData(array $input ): array { - $data = [ - "IdentifierType" => $input['Identity']['IdentifierType'] ?? null, - "Identifier" => $input['Identity']['Identifier'] ?? null, - ]; - - return $data; - } - private function preparePatattData(array $input): array { - if (empty($input['Attachments'])) { - return []; - } - - return array_map(function ($attachment) { - $row = [ - "Address" => $attachment['Address'] ?? null, - ]; - return $row; - }, $input['Attachments']); - } - private function preparePatcomData(array $input): array { - $data = [ - "Comment" => $input['Comment'] ?? null, - ]; - - return $data; - } private function validationError(string $context, array $errors) { return $this->respond([ 'status' => 'error', @@ -147,86 +70,13 @@ class Patient extends Controller { ], 400); } - // Unit Testing Pass : On Progress public function update() { + $input = $this->request->getJSON(true); + if (!$this->validateData($input, $this->rulesPatient)) { return $this->validationError('patient', $this->validator->getErrors()); } try { - $input = $this->request->getJSON(true); - if (!$input) { return $this->respond(['status' => 'error', 'message' => 'Invalid JSON input'], 400); } - if (!$input["InternalPID"] || !is_numeric($input["InternalPID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing InternalPID'], 400); } - - $InternalPID = $input["InternalPID"]; - $patient = $this->db->table('patient')->where('InternalPID', $InternalPID)->get()->getRowArray(); - if (!$patient) { return $this->respond(['status' => 'error', 'message' => 'Patient not found'], 404); } - - $dataPatient = $this->preparePatientData($input); - $dataPatIdt = $this->preparePatidtData($input); - $dataPatCom = $this->preparePatcomData($input); - $dataPatAtt = $this->preparePatattData($input); - - // Validasi - if (!$this->validateData($dataPatient, $this->rulesPatient)) { - return $this->respond([ - 'status' => 'error', - 'message' => 'Validation failed (patient)', - 'errors' => $this->validator->getErrors() - ], 400); - } - - if (!$this->validateData($dataPatIdt, $this->rulesPatIdt)) { - return $this->respond([ - 'status' => 'error', - 'message' => 'Validation failed (patidt)', - 'errors' => $this->validator->getErrors() - ], 400); - } - - $this->db->transStart(); - - $this->db->table('patient')->where('InternalPID', $InternalPID)->update($dataPatient); - $dbError = $this->db->error(); - if (!empty($dbError['message'])) { - $this->db->transRollback(); - return $this->failServerError('Update patient failed: ' . $dbError['message']); - } - - $this->db->table('patidt')->where('InternalPID', $InternalPID)->update($dataPatIdt); - $dbError = $this->db->error(); - if (!empty($dbError['message'])) { - $this->db->transRollback(); - return $this->failServerError('Update patidt failed: ' . $dbError['message']); - } - - if (!empty($dataPatAtt)) { - foreach ($dataPatAtt as &$row) { - $row['InternalPID'] = $InternalPID; - } - $this->db->table('patatt')->upsertBatch($dataPatAtt); - $addresses = array_column($dataPatAtt, 'Address'); - $this->db->table('patatt')->where('InternalPID', $InternalPID)->WhereNotIn('Address', $addresses)->update(['DelDate' => date('Y-m-d H:i:s')]); - } else { - $this->db->table('patatt')->where('InternalPID', $InternalPID)->update(['DelDate' => date('Y-m-d H:i:s')]); - } - - if(!empty($dataPatcom['Comment'])) { - $dataPatcom['InternalPID'] = $InternalPID; - $this->db->table('patcom')->upsert($dataPatCom); - } - - $this->db->transComplete(); - - if ($this->db->transStatus() === false) { - $dbError = $this->db->error(); - return $this->failServerError('Failed to update patient data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown error')); - } - - return $this->respond([ - 'status' => 'success', - 'message' => 'Patient updated successfully', - 'data' => $dataPatient - ], 200); - + $InternalPID = $this->modelPatient->updatePatient($input); + return $this->respondCreated([ 'status' => 'success', 'message' => "data $InternalPID update successfully" ]); } catch (\Exception $e) { - $this->db->transRollback(); return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } diff --git a/app/Models/PatientModel.php b/app/Models/PatientModel.php index c660c79..7f25bff 100644 --- a/app/Models/PatientModel.php +++ b/app/Models/PatientModel.php @@ -64,37 +64,37 @@ class PatientModel extends Model { if (empty($rows)) { return null; } $patient = $rows[0]; - + unset($patient['Address']); if (method_exists($this, 'transformPatientData')) { $patient = $this->transformPatientData($patient); } // Default nested structures - $patient['Identity'] = null; - $patient['Attachments'] = []; + $patient['PatIdt'] = null; + $patient['PatAtt'] = []; foreach ($rows as $row) { - if ($row['IdentifierType'] && $row['Identifier'] && !$patient['Identity']) { - $patient['Identity'] = [ + if ($row['IdentifierType'] && $row['Identifier'] && !$patient['PatIdt']) { + $patient['PatIdt'] = [ 'IdentifierType' => $row['IdentifierType'], 'Identifier' => $row['Identifier'], ]; } if ($row['Address']) { - $patient['Attachments'][] = ['Address' => $row['Address']]; + $patient['PatAtt'][] = ['Address' => $row['Address']]; } } - if (empty($patient['Identity'])) { $patient['Identity'] = null; } - if (empty($patient['Attachments'])) { $patient['Attachments'] = null; } + if (empty($patient['PatIdt'])) { $patient['PatIdt'] = null; } + if (empty($patient['PatAtt'])) { $patient['PatAtt'] = null; } return $patient; } public function createPatient($input) { $db = \Config\Database::connect(); - $patidt = $input['patidt'] ?? []; - $patatt = $input['patatt'] ?? []; - $patcom = $input['patcom'] ?? []; + $patidt = $input['PatIdt'] ?? []; + $patatt = $input['PatAtt'] ?? []; + $patcom['Comment'] = $input['Comment'] ?? []; try { $db->transStart(); @@ -106,29 +106,85 @@ class PatientModel extends Model { $db->table('patidt')->insert($patidt); } + if (!empty($patcom['Comment'])) { + $patcom['InternalPID'] = $newInternalPID; + $db->table('patcom')->insert($patcom); + } + /* if (!empty($patatt)) { foreach ($patatt as &$row) { $row['InternalPID'] = $newInternalPID; } $db->table('patatt')->upsertBatch($patatt); } - - if (!empty($patcom['Comment'])) { - $patcom['InternalPID'] = $newInternalPID; - $db->table('patcom')->insert($patcom); - } - + */ $db->transComplete(); if ($db->transStatus() === false) { $error = $db->error(); throw new \Exception('Transaction failed: ' . ($error['message'] ?? 'Unknown DB error')); } - return $newInternalPID; + } catch (\Exception $e) { $db->transRollback(); - throw $e; // rethrow so controller can handle response + throw $e; + } + } + + public function updatePatient($input) { + $db = \Config\Database::connect(); + $patidt = $input['PatIdt'] ?? []; + $patatt = $input['PatAtt'] ?? []; + $patcom['Comment'] = $input['Comment'] ?? []; + try { + $db->transStart(); + $InternalPID = $input['InternalPID']; + $this->where('InternalPID',$InternalPID)->set($input)->update(); + + if (!empty($patidt)) { + $exists = $db->table('patidt')->where('InternalPID', $InternalPID)->get()->getRowArray(); + if ($exists) { + $db->table('patidt')->where('InternalPID', $InternalPID)->update($patidt); + } else { + $patidt['InternalPID'] = $InternalPID; + $db->table('patidt')->insert($patidt); + } + } else { + $db->table('patidt')->where('InternalPID', $InternalPID)->delete(); + } + + if (!empty($patcom)) { + $exists = $db->table('patcom')->where('InternalPID', $InternalPID)->get()->getRowArray(); + if ($exists) { + $db->table('patcom')->where('InternalPID', $InternalPID)->update($patcom); + } else { + $patidt['InternalPID'] = $InternalPID; + $db->table('patcom')->insert($patcom); + } + } else { + $db->table('patcom')->where('InternalPID', $InternalPID)->delete(); + } + /* + if (!empty($patatt)) { + $db->table('patatt')->where('InternalPID', $InternalPID)->delete(); + foreach ($patatt as $row) { + $row['InternalPID'] = $InternalPID; + $db->table('patatt')->insert($row); + } + } else { + $db->table('patatt')->where('InternalPID', $InternalPID)->delete(); + } + */ + $db->transComplete(); + + if ($db->transStatus() === false) { + throw new \Exception('Failed to sync patient relations'); + } + return $InternalPID; + } catch (\Exception $e) { + $db->transRollback(); + throw $e; } }