From 7d5622d95809eb16b02e361454eb7d7f7fc6ce9d Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Thu, 30 Jul 2026 14:34:08 +0530 Subject: [PATCH] Harden ABHA cleanup against non-standard patient and OPD key columns --- app/Libraries/BridgeSyncService.php | 38 ++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/app/Libraries/BridgeSyncService.php b/app/Libraries/BridgeSyncService.php index b02504c..04f86cf 100644 --- a/app/Libraries/BridgeSyncService.php +++ b/app/Libraries/BridgeSyncService.php @@ -92,13 +92,19 @@ class BridgeSyncService $opdId = (int) ($payload['bundle_json']['opd_id'] ?? 0); } if ($opdId > 0 && $this->db->tableExists('opd_master')) { + $opdPk = $this->resolveTableIdColumn('opd_master', ['id', 'opd_id']); + $opdPatientFk = $this->resolveTableIdColumn('opd_master', ['p_id', 'patient_id', 'pid']); + if ($opdPk === null || $opdPatientFk === null) { + return 0; + } + $row = $this->db->table('opd_master') - ->select('p_id') - ->where('id', $opdId) + ->select($opdPatientFk) + ->where($opdPk, $opdId) ->get(1) ->getRowArray(); - return (int) ($row['p_id'] ?? 0); + return (int) ($row[$opdPatientFk] ?? 0); } } @@ -111,6 +117,11 @@ class BridgeSyncService return false; } + $patientPk = $this->resolveTableIdColumn('patient_master', ['id', 'p_id', 'patient_id', 'pid']); + if ($patientPk === null) { + return false; + } + $allFields = $this->db->getFieldNames('patient_master') ?? []; $selectFields = []; foreach (['abha_verified_status', 'abha_kyc_verified', 'abha_mobile_verified', 'abha_id', 'abha_no', 'abha', 'abha_address'] as $field) { @@ -125,7 +136,7 @@ class BridgeSyncService $row = $this->db->table('patient_master') ->select(implode(',', $selectFields)) - ->where('id', $patientId) + ->where($patientPk, $patientId) ->get(1) ->getRowArray(); @@ -439,6 +450,25 @@ class BridgeSyncService return null; } + /** + * @param list $candidates + */ + private function resolveTableIdColumn(string $table, array $candidates): ?string + { + if (! $this->db->tableExists($table)) { + return null; + } + + $fields = $this->db->getFieldNames($table) ?? []; + foreach ($candidates as $candidate) { + if (in_array($candidate, $fields, true)) { + return $candidate; + } + } + + return null; + } + /** * @param array $requestPayload */