Update Patient Create, menambah transtatus, validasi, insert patitd
This commit is contained in:
parent
a8fd894168
commit
8acfac74d9
@ -90,12 +90,13 @@ class Patient extends Controller {
|
|||||||
|
|
||||||
// OK - Done
|
// OK - Done
|
||||||
public function create() {
|
public function create() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
$input = $this->request->getJSON(true);
|
$input = $this->request->getJSON(true);
|
||||||
|
|
||||||
$data = [
|
// =========================
|
||||||
|
// 1. Data untuk tabel patient
|
||||||
|
// =========================
|
||||||
|
$dataPatient = [
|
||||||
"PatientID" => $input['PatientID'] ?? null,
|
"PatientID" => $input['PatientID'] ?? null,
|
||||||
"AlternatePID" => $input['AlternatePID'] ?? null,
|
"AlternatePID" => $input['AlternatePID'] ?? null,
|
||||||
"Prefix" => $input['Prefix'] ?? null,
|
"Prefix" => $input['Prefix'] ?? null,
|
||||||
@ -104,79 +105,102 @@ class Patient extends Controller {
|
|||||||
"NameMaiden" => $input['NameMaiden'] ?? null,
|
"NameMaiden" => $input['NameMaiden'] ?? null,
|
||||||
"NameLast" => $input['NameLast'] ?? null,
|
"NameLast" => $input['NameLast'] ?? null,
|
||||||
"Suffix" => $input['Suffix'] ?? null,
|
"Suffix" => $input['Suffix'] ?? null,
|
||||||
"NameAlias" => null,
|
"NameAlias" => $input['NameAlias'] ?? null,
|
||||||
"Gender" => isset($input['Gender']) ? (int) $input['Gender'] : null,
|
"Gender" => isset($input['Gender']) ? (int) $input['Gender'] : null,
|
||||||
"PlaceOfBirth" => $input['PlaceOfBirth'] ?? null,
|
"PlaceOfBirth" => $input['PlaceOfBirth'] ?? null,
|
||||||
"Birthdate" => $input['Birthdate'] ?? null,
|
"Birthdate" => $input['Birthdate'] ?? null,
|
||||||
"Street_1" => $input['Street_1'] ?? null,
|
"Street_1" => $input['Street_1'] ?? null,
|
||||||
"Street_2" => $input['Street_2'] ?? null,
|
"Street_2" => $input['Street_2'] ?? null,
|
||||||
"Street_3" => null,
|
"Street_3" => $input['Street_3'] ?? null,
|
||||||
"City" => $input['City'] ?? null,
|
"City" => $input['City'] ?? null,
|
||||||
"Province" => $input['Province'] ?? null,
|
"Province" => $input['Province'] ?? null,
|
||||||
"ZIP" => $input['ZIP'] ?? null,
|
"ZIP" => $input['ZIP'] ?? null,
|
||||||
"CountryID" => isset($input['CountryID']) ? (int) $input['CountryID'] : null,
|
|
||||||
"EmailAddress1" => $input['EmailAddress1'] ?? null,
|
"EmailAddress1" => $input['EmailAddress1'] ?? null,
|
||||||
"EmailAddress2" => $input['EmailAddress2'] ?? null,
|
"EmailAddress2" => $input['EmailAddress2'] ?? null,
|
||||||
"Phone" => $input['Phone'] ?? null,
|
"Phone" => $input['Phone'] ?? null,
|
||||||
"MobilePhone" => $input['MobilePhone'] ?? 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,
|
"RaceID" => isset($input['RaceID']) ? (int) $input['RaceID'] : null,
|
||||||
|
"IntCountryID" => isset($input['IntCountryID']) ? (int) $input['IntCountryID'] : null,
|
||||||
"MaritalStatus" => $input['MaritalStatus'] ?? null,
|
"MaritalStatus" => $input['MaritalStatus'] ?? null,
|
||||||
"ReligionID" => isset($input['ReligionID']) ? (int) $input['ReligionID'] : null,
|
"ReligionID" => isset($input['ReligionID']) ? (int) $input['ReligionID'] : null,
|
||||||
"EthnicID" => isset($input['EthnicID']) ? (int) $input['EthnicID'] : null,
|
"EthnicID" => isset($input['EthnicID']) ? (int) $input['EthnicID'] : null,
|
||||||
"Citizenship" => $input['Citizenship'] ?? null,
|
"Citizenship" => $input['Citizenship'] ?? null,
|
||||||
"DeathIndicator" => isset($input['DeathIndicator']) ? (int) $input['DeathIndicator'] : null,
|
"DeathIndicator" => isset($input['DeathIndicator']) ? (int) $input['DeathIndicator'] : null,
|
||||||
"DeathDateTime" => $input['DeathDateTime'] ?? null,
|
"DeathDateTime" => $input['DeathDateTime'] ?? null,
|
||||||
// "LinkTo" => $input['LinkTo'] ?? null,
|
|
||||||
"CreateDate" => date('Y-m-d H:i:s'),
|
"CreateDate" => date('Y-m-d H:i:s'),
|
||||||
"DelDate" => null,
|
"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 = [
|
$rulesDataPatient = [
|
||||||
'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]',
|
'PatientID' => 'required|is_unique[patient.PatientID]|max_length[50]',
|
||||||
'NameFirst' => 'required|min_length[3]|max_length[255]',
|
'AlternatePID' => 'permit_empty|max_length[50]',
|
||||||
'NameMiddle' => 'permit_empty',
|
'NameFirst' => 'required|min_length[1]|max_length[255]',
|
||||||
'NameMaiden' => 'permit_empty',
|
'EmailAddress1' => 'required|is_unique[patient.EmailAddress1]',
|
||||||
'NameLast' => 'permit_empty',
|
'DeathIndicator' => 'required',
|
||||||
'AlternatePID' => 'permit_empty|max_length[50]',
|
'Gender' => 'required'
|
||||||
'Street_1' => 'permit_empty',
|
|
||||||
'Street_2' => 'permit_empty',
|
|
||||||
'Street_3' => 'permit_empty',
|
|
||||||
'City' => 'permit_empty',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// 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([
|
return $this->respond([
|
||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'message' => 'Validation failed',
|
'message' => 'Validation failed (patient)',
|
||||||
'errors' => $this->validator->getErrors()
|
'errors' => $this->validator->getErrors()
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->db->table('patient')->insert($data);
|
if (!$this->validateData($dataPatidt, $rulesDataPatidt)) {
|
||||||
$newPatientId = $this->db->insertID();
|
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([
|
return $this->respondCreated([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => 'Patient created successfully',
|
'message' => 'Patient created successfully',
|
||||||
'data' => $newPatientId
|
'data' => $newInternalPatientId
|
||||||
], 201);
|
], 201);
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
$this->db->transRollback();
|
||||||
// Error Server = 500
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
||||||
return $this->failServerError('Something went wrong'.$e->getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// OK - Done
|
// OK - Done
|
||||||
public function update($InternalPID = null) {
|
public function update($InternalPID = null) {
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user