From 45f1888227eb706d4d9345866e6ecec2b8e0613c Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Fri, 23 Oct 2026 09:51:31 +0530 Subject: [PATCH] fix(abdm): preserve structured care context reference in patient referenceNumber during linking --- app/Controllers/AbdmGateway.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/Controllers/AbdmGateway.php b/app/Controllers/AbdmGateway.php index 2d0559c..944dec9 100644 --- a/app/Controllers/AbdmGateway.php +++ b/app/Controllers/AbdmGateway.php @@ -7186,11 +7186,13 @@ class AbdmGateway extends BaseController 'transaction_id' => (string) ($txn['txn_id'] ?? ''), ]); + $confirmedPatientRef = (! empty($confirmedContexts[0]['referenceNumber'])) ? (string) $confirmedContexts[0]['referenceNumber'] : $patientRef; + return $this->response->setJSON([ 'ok' => 1, 'message' => 'Care contexts linked successfully', 'patient' => [ - 'referenceNumber' => $patientRef, + 'referenceNumber' => $confirmedPatientRef, 'display' => $patientDisplay, 'careContexts' => $confirmedContexts, ], @@ -13889,8 +13891,14 @@ class AbdmGateway extends BaseController } } - if (empty($body['patient_ref']) || $body['patient_ref'] === $body['abha_address'] || preg_match('/^(OPD|PRESC|INVOICE|LAB|RAD|WELLNESS)-/i', (string) $body['patient_ref'])) { - if (! empty($patientRow['p_code'])) { + if (empty($body['patient_ref']) || $body['patient_ref'] === $body['abha_address']) { + $firstContextRef = ''; + if (! empty($body['care_contexts']) && is_array($body['care_contexts'])) { + $firstContextRef = (string) ($body['care_contexts'][0]['ref'] ?? $body['care_contexts'][0]['referenceNumber'] ?? $body['care_contexts'][0]['reference_number'] ?? ''); + } + if ($firstContextRef !== '') { + $body['patient_ref'] = $firstContextRef; + } elseif (! empty($patientRow['p_code'])) { $body['patient_ref'] = (string) $patientRow['p_code']; } elseif ($patientId > 0) { $body['patient_ref'] = 'P-' . $patientId;