Update Patient Create, menambah transtatus, validasi, insert patitd

This commit is contained in:
mikael-zakaria 2025-08-08 13:49:18 +07:00
parent a8fd894168
commit 8acfac74d9

View File

@ -90,12 +90,13 @@ class Patient extends Controller {
// OK - Done
public function create() {
try {
$input = $this->request->getJSON(true);
$data = [
// =========================
// 1. Data untuk tabel patient
// =========================
$dataPatient = [
"PatientID" => $input['PatientID'] ?? null,
"AlternatePID" => $input['AlternatePID'] ?? null,
"Prefix" => $input['Prefix'] ?? null,
@ -104,79 +105,102 @@ class Patient extends Controller {
"NameMaiden" => $input['NameMaiden'] ?? null,
"NameLast" => $input['NameLast'] ?? null,
"Suffix" => $input['Suffix'] ?? null,
"NameAlias" => null,
"NameAlias" => $input['NameAlias'] ?? null,
"Gender" => isset($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" => null,
"Street_3" => $input['Street_3'] ?? null,
"City" => $input['City'] ?? null,
"Province" => $input['Province'] ?? null,
"ZIP" => $input['ZIP'] ?? null,
"CountryID" => isset($input['CountryID']) ? (int) $input['CountryID'] : null,
"EmailAddress1" => $input['EmailAddress1'] ?? null,
"EmailAddress2" => $input['EmailAddress2'] ?? null,
"Phone" => $input['Phone'] ?? null,
"MobilePhone" => $input['MobilePhone'] ?? null,
"Mother" => $input['Mother'] ?? null,
"AccountNumber" => isset($input['AccountNumber']) ? (int) $input['AccountNumber'] : 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,
// "LinkTo" => $input['LinkTo'] ?? null,
"CreateDate" => date('Y-m-d H:i:s'),
"DelDate" => null,
// Field tambahan dari struktur sebelumnya (bisa dihapus jika tidak dipakai)
// "PatientComment" => $input['PatientComment'] ?? null,
// "IdentityIDType" => $input['IdentityIDType'] ?? null,
// "IdentityID" => $input['IdentityID'] ?? null
];
$rules = [
'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]',
'NameFirst' => 'required|min_length[3]|max_length[255]',
'NameMiddle' => 'permit_empty',
'NameMaiden' => 'permit_empty',
'NameLast' => 'permit_empty',
'AlternatePID' => 'permit_empty|max_length[50]',
'Street_1' => 'permit_empty',
'Street_2' => 'permit_empty',
'Street_3' => 'permit_empty',
'City' => 'permit_empty',
$rulesDataPatient = [
'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]',
'AlternatePID' => 'permit_empty|max_length[50]',
'NameFirst' => 'required|min_length[1]|max_length[255]',
'EmailAddress1' => 'required|is_unique[patient.EmailAddress1]',
'DeathIndicator' => 'required',
'Gender' => 'required'
];
// Request dari client tidak valid atau tidak bisa diproses oleh server - 400
if (!$this->validateData($data, $rules)) {
// =========================
// 2. Data untuk tabel patidt
// =========================
$dataPatidt = [
"IdentifierType" => $input['IdentifierType'] ?? null,
"Identifier" => $input['Identifier'] ?? null
];
$rulesDataPatidt = [
'Identifier' => 'required|is_unique[patidt.Identifier]',
];
// =========================
// Validasi semua sebelum insert
// =========================
if (!$this->validateData($dataPatient, $rulesDataPatient)) {
return $this->respond([
'status' => 'error',
'message' => 'Validation failed',
'errors' => $this->validator->getErrors()
'status' => 'error',
'message' => 'Validation failed (patient)',
'errors' => $this->validator->getErrors()
], 400);
}
$this->db->table('patient')->insert($data);
$newPatientId = $this->db->insertID();
if (!$this->validateData($dataPatidt, $rulesDataPatidt)) {
return $this->respond([
'status' => 'error',
'message' => 'Validation failed (patidt)',
'errors' => $this->validator->getErrors()
], 400);
}
// =========================
// Mulai transaksi
// =========================
$this->db->transStart();
$this->db->table('patient')->insert($dataPatient);
$newInternalPatientId = $this->db->insertID();
$dataPatidt['InternalPID'] = $newInternalPatientId;
$this->db->table('patidt')->insert($dataPatidt);
$this->db->transComplete();
if ($this->db->transStatus() === false) {
return $this->failServerError('Failed to create patient data (transaction rolled back)');
}
// Sukses & Insert = 201 - Kirim data patient ID
return $this->respondCreated([
'status' => 'success',
'status' => 'success',
'message' => 'Patient created successfully',
'data' => $newPatientId
'data' => $newInternalPatientId
], 201);
} catch (\Exception $e) {
// Error Server = 500
return $this->failServerError('Something went wrong'.$e->getMessage());
$this->db->transRollback();
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
// OK - Done
public function update($InternalPID = null) {