Harden ABHA cleanup against non-standard patient and OPD key columns

This commit is contained in:
DevSoftTech Server 2026-07-30 14:34:08 +05:30
parent 1429ffc567
commit 7d5622d958

View File

@ -92,13 +92,19 @@ class BridgeSyncService
$opdId = (int) ($payload['bundle_json']['opd_id'] ?? 0); $opdId = (int) ($payload['bundle_json']['opd_id'] ?? 0);
} }
if ($opdId > 0 && $this->db->tableExists('opd_master')) { 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') $row = $this->db->table('opd_master')
->select('p_id') ->select($opdPatientFk)
->where('id', $opdId) ->where($opdPk, $opdId)
->get(1) ->get(1)
->getRowArray(); ->getRowArray();
return (int) ($row['p_id'] ?? 0); return (int) ($row[$opdPatientFk] ?? 0);
} }
} }
@ -111,6 +117,11 @@ class BridgeSyncService
return false; return false;
} }
$patientPk = $this->resolveTableIdColumn('patient_master', ['id', 'p_id', 'patient_id', 'pid']);
if ($patientPk === null) {
return false;
}
$allFields = $this->db->getFieldNames('patient_master') ?? []; $allFields = $this->db->getFieldNames('patient_master') ?? [];
$selectFields = []; $selectFields = [];
foreach (['abha_verified_status', 'abha_kyc_verified', 'abha_mobile_verified', 'abha_id', 'abha_no', 'abha', 'abha_address'] as $field) { 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') $row = $this->db->table('patient_master')
->select(implode(',', $selectFields)) ->select(implode(',', $selectFields))
->where('id', $patientId) ->where($patientPk, $patientId)
->get(1) ->get(1)
->getRowArray(); ->getRowArray();
@ -439,6 +450,25 @@ class BridgeSyncService
return null; return null;
} }
/**
* @param list<string> $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<string, mixed> $requestPayload * @param array<string, mixed> $requestPayload
*/ */