From 092e5cd1938facc98588b92f27392b680d3dde36 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Thu, 24 Sep 2026 11:12:54 +0530 Subject: [PATCH] fix(abdm): route health record push with FHIR bundle to /v3/records/push and unify care context reference format --- app/Controllers/AbdmGateway.php | 23 ++++++++++++-- app/Libraries/Abdm/EAtriaBridgeConnector.php | 32 +++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/app/Controllers/AbdmGateway.php b/app/Controllers/AbdmGateway.php index b141c16..c768a5f 100644 --- a/app/Controllers/AbdmGateway.php +++ b/app/Controllers/AbdmGateway.php @@ -1541,9 +1541,26 @@ class AbdmGateway extends BaseController $visitDate = $visitDateRaw !== '' ? date('Y-m-d', strtotime($visitDateRaw)) : date('Y-m-d'); $careContextRef = trim((string) ($this->request->getPost('careContextId') ?? $this->request->getPost('care_context_reference') ?? '')); if ($careContextRef === '') { - $careContextRef = $hiType === 'PrescriptionRecord' - ? 'PRESCRIPTION-' . $opdId . '-S' . ($sessionForRef > 0 ? $sessionForRef : 0) . '-' . $visitDate - : 'OPD-' . $opdId . '-S' . ($sessionForRef > 0 ? $sessionForRef : 0) . '-' . $visitDate; + $existingCcRef = ''; + if ($this->db->tableExists('health_records')) { + $existingHrRow = $this->db->table('health_records') + ->select('care_context_reference') + ->where('entity_type', 'opd') + ->where('entity_id', (string) $opdId) + ->where('care_context_reference !=', '') + ->orderBy('id', 'DESC') + ->get(1) + ->getRowArray(); + if (! empty($existingHrRow['care_context_reference'])) { + $existingCcRef = trim((string) $existingHrRow['care_context_reference']); + } + } + if ($existingCcRef !== '') { + $careContextRef = $existingCcRef; + } else { + $dateClean = str_replace('-', '', $visitDate); + $careContextRef = 'OPD-' . $patientId . '-S' . ($sessionForRef > 0 ? $sessionForRef : 0) . '-' . $dateClean; + } } $careContextDisplay = $hiType === 'PrescriptionRecord' ? 'Prescription - ' . $visitDate diff --git a/app/Libraries/Abdm/EAtriaBridgeConnector.php b/app/Libraries/Abdm/EAtriaBridgeConnector.php index 25daba6..4849cde 100644 --- a/app/Libraries/Abdm/EAtriaBridgeConnector.php +++ b/app/Libraries/Abdm/EAtriaBridgeConnector.php @@ -1393,10 +1393,34 @@ class EAtriaBridgeConnector implements AbdmConnectorInterface $effectiveAbha = $abhaAddress !== '' ? $abhaAddress : $abhaId; $visitDate = (string) ($data['visit_date'] ?? date('Y-m-d')); - // Under M3 On-Demand Fetch architecture, full FHIR records are NOT sent to the Bridge. - // Instead, only Care Context metadata is registered with the ABDM Bridge (/v3/hip/link/notify). - // The complete FHIR Document Bundle stays securely inside HMS (health_records table) - // and is served on-demand when the Bridge calls POST /records/fetch upon patient consent. + $bundle = $data['record_data'] ?? $data['fhir_bundle'] ?? null; + if (! empty($bundle) && is_array($bundle)) { + // When a FHIR bundle is attached, push to /api/v3/records/push so the bridge + // validates the FHIR resources and holds the bundle for on-demand patient fetch. + $recordPushPayload = [ + 'hfr_id' => $this->hfrId, + 'patient_id' => (string) ($data['patient_id'] ?? ''), + 'patient_name' => $patientName, + 'abha_address' => $effectiveAbha, + 'care_context_reference' => $careContextReference, + 'care_context_display' => $careContextDisplay, + 'hi_type' => $hiType, + 'visit_date' => $visitDate, + 'fhir_bundle' => $bundle, + 'record_data' => $bundle, + ]; + if ($this->bridgeHospitalId !== '') { + $recordPushPayload['hospital_id'] = $this->bridgeHospitalId; + } + if ($abhaId !== '') { + $recordPushPayload['abha_id'] = $abhaId; + } + + $res = $this->post('/v3/records/push', $recordPushPayload); + return self::normalizePushRecordResponse($res); + } + + // Care-context only metadata registration $notifyPayload = [ 'hfr_id' => $this->hfrId, 'hospital_id' => $this->bridgeHospitalId,